forked from trustmaster/goflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
component_test.go
93 lines (75 loc) · 1.32 KB
/
component_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
package goflow
import (
"testing"
)
// Test a simple component that runs only once
func TestSimpleComponent(t *testing.T) {
in := make(chan int)
out := make(chan int)
c := &doubleOnce{
in,
out,
}
wait := Run(c)
in <- 12
res := <-out
if res != 24 {
t.Errorf("%d != %d", res, 24)
}
<-wait
}
// Test a simple long running component with one input
func TestSimpleLongRunningComponent(t *testing.T) {
data := map[int]int{
12: 24,
7: 14,
400: 800,
}
in := make(chan int)
out := make(chan int)
c := &doubler{
in,
out,
}
wait := Run(c)
for src, expected := range data {
in <- src
actual := <-out
if actual != expected {
t.Errorf("%d != %d", actual, expected)
}
}
// We have to close input for the process to finish
close(in)
<-wait
}
func TestComponentWithTwoInputs(t *testing.T) {
op1 := []int{3, 5, 92, 28}
op2 := []int{38, 94, 4, 9}
sums := []int{41, 99, 96, 37}
in1 := make(chan int)
in2 := make(chan int)
out := make(chan int)
c := &adder{in1, in2, out}
wait := Run(c)
go func() {
for _, n := range op1 {
in1 <- n
}
close(in1)
}()
go func() {
for _, n := range op2 {
in2 <- n
}
close(in2)
}()
for i := 0; i < len(sums); i++ {
actual := <-out
expected := sums[i]
if actual != expected {
t.Errorf("%d != %d", actual, expected)
}
}
<-wait
}