forked from trustmaster/goflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_test.go
101 lines (88 loc) · 1.72 KB
/
graph_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package goflow
import (
"testing"
)
func newDoubleEcho() (*Graph, error) {
n := NewGraph()
// Components
e1 := new(echo)
e2 := new(echo)
// Structure
if err := n.Add("e1", e1); err != nil {
return nil, err
}
if err := n.Add("e2", e2); err != nil {
return nil, err
}
if err := n.Connect("e1", "Out", "e2", "In"); err != nil {
return nil, err
}
// Ports
n.MapInPort("In", "e1", "In")
n.MapOutPort("Out", "e2", "Out")
return n, nil
}
func TestSimpleGraph(t *testing.T) {
n, err := newDoubleEcho()
if err != nil {
t.Error(err)
return
}
testGraphWithNumberSequence(n, t)
}
func testGraphWithNumberSequence(n *Graph, t *testing.T) {
data := []int{7, 97, 16, 356, 81}
in := make(chan int)
out := make(chan int)
n.SetInPort("In", in)
n.SetOutPort("Out", out)
wait := Run(n)
go func() {
for _, n := range data {
in <- n
}
close(in)
}()
i := 0
for actual := range out {
expected := data[i]
if actual != expected {
t.Errorf("%d != %d", actual, expected)
}
i++
}
<-wait
}
func TestAddInvalidProcess(t *testing.T) {
s := struct{ Name string }{"This is not a Component"}
n := NewGraph()
err := n.Add("wrong", s)
if err == nil {
t.Errorf("Expected an error")
}
}
func TestRemove(t *testing.T) {
n := NewGraph()
e1 := new(echo)
if err := n.Add("e1", e1); err != nil {
t.Error(err)
return
}
if err := n.Remove("e1"); err != nil {
t.Error(err)
return
}
if err := n.Remove("e2"); err == nil {
t.Errorf("Expected an error")
return
}
}
func RegisterTestGraph(f *Factory) error {
f.Register("doubleEcho", func() (interface{}, error) {
return newDoubleEcho()
})
f.Annotate("doubleEcho", Annotation{
Description: "Contains a chain of two echo components",
})
return nil
}