-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflow_test.go
68 lines (60 loc) · 1.15 KB
/
flow_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package flow
import (
"fmt"
"strconv"
"testing"
"time"
)
// Start => (A, B) => C => Done
func TestFlowNet(t *testing.T) {
flow := NewFlow(1 * time.Millisecond)
start := flow.InitStart("Start")
start.Tk = func() error {
start.To("A") <- 1
close(start.To("A"))
start.To("B") <- "2"
close(start.To("B"))
fmt.Printf("Start sends : 1 and \"2\"\n")
return nil
}
A := flow.InitNode("A")
A.Tk = func() error {
a := <-A.From("Start")
fmt.Printf("A got : %v\n", a)
A.To("C") <- a
return nil
}
B := flow.InitNode("B")
B.Tk = func() error {
bStr := <-B.From("Start")
switch bStr := bStr.(type) {
case string:
fmt.Printf("B got : \"%s\"\n", bStr)
b, _ := strconv.Atoi(bStr)
B.To("C") <- b
}
return nil
}
C := flow.InitSink("C")
C.Tk = func() error {
a := <-C.From("A")
b := <-C.From("B")
switch a := a.(type) {
case int:
switch b := b.(type) {
case int:
fmt.Printf("Sink got sum : %v\n", a+b)
}
}
C.ToSink() <- true
return nil
}
flow.Connect(start, A)
flow.Connect(start, B)
flow.Connect(A, C)
flow.Connect(B, C)
if err := flow.Run(); err != nil {
t.Errorf("%s", err)
}
flow.Cleanup()
}