-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparallel_test.go
110 lines (88 loc) · 2.95 KB
/
parallel_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
102
103
104
105
106
107
108
109
110
package process
import (
"context"
"testing"
)
func TestToStreamErrorFuncNil(t *testing.T) {
trace := make(chan string, 4)
fn := toStreamErrorFunc(newTracedSingleErrorFunc(trace, traceValue("a"), nil))
errs := fn(context.Background())
assertChannelContents(t, readErrorChannel(errs), nil)
close(trace)
assertChannelContents(t, readStringChannel(trace), seq("a"))
}
func TestToStreamErrorFuncNonNil(t *testing.T) {
trace := make(chan string, 4)
fn := toStreamErrorFunc(newTracedSingleErrorFunc(trace, traceValue("a"), testErr1))
errs := fn(context.Background())
assertChannelContents(t, readErrorChannel(errs), seq(testErr1))
close(trace)
assertChannelContents(t, readStringChannel(trace), seq("a"))
}
func TestChain(t *testing.T) {
trace := make(chan string, 4)
fn := chain(
toStreamErrorFunc(newTracedSingleErrorFunc(trace, traceValue("a"), nil)),
toStreamErrorFunc(newTracedSingleErrorFunc(trace, traceValue("b"), testErr1)),
toStreamErrorFunc(newTracedSingleErrorFunc(trace, traceValue("c"), testErr2)),
toStreamErrorFunc(newTracedSingleErrorFunc(trace, traceValue("d"), nil)),
)
errs := fn(context.Background())
assertChannelContents(t, readErrorChannel(errs), seq(testErr1))
close(trace)
assertChannelContents(t, readStringChannel(trace), seq("a", "b"))
}
func TestSequence(t *testing.T) {
trace := make(chan string, 4)
fn := sequence(
toStreamErrorFunc(newTracedSingleErrorFunc(trace, traceValue("a"), nil)),
toStreamErrorFunc(newTracedSingleErrorFunc(trace, traceValue("b"), testErr1)),
toStreamErrorFunc(newTracedSingleErrorFunc(trace, traceValue("c"), testErr2)),
toStreamErrorFunc(newTracedSingleErrorFunc(trace, traceValue("d"), nil)),
)
errs := fn(context.Background())
assertChannelContents(t, readErrorChannel(errs), seq(testErr1, testErr2))
close(trace)
assertChannelContents(t, readStringChannel(trace), seq("a", "b", "c", "d"))
}
func TestParallel(t *testing.T) {
t1 := make(chan string)
t2 := make(chan string)
t3 := make(chan string)
t4 := make(chan string)
trace := make(chan string)
traceBuffered := make(chan string, 8)
fn := parallel(
toStreamErrorFunc(newTracedSingleErrorFunc(trace, t1, nil)),
toStreamErrorFunc(newTracedSingleErrorFunc(trace, t2, testErr1)),
toStreamErrorFunc(newTracedSingleErrorFunc(trace, t3, testErr2)),
toStreamErrorFunc(newTracedSingleErrorFunc(trace, t4, nil)),
)
go func() {
type pair struct {
ch chan<- string
value string
}
for _, pair := range []pair{
{t4, "d"},
{t3, "c"},
{t1, "a"},
{t2, "b"},
{t4, "D"},
{t3, "C"},
{t1, "A"},
{t2, "B"},
} {
pair.ch <- pair.value
traceBuffered <- <-trace
}
for _, ch := range []chan string{t1, t2, t3, t4} {
close(ch)
}
}()
errs := fn(context.Background())
assertChannelContents(t, readErrorChannel(errs), seq(unordered(testErr1, testErr2)))
close(trace)
close(traceBuffered)
assertChannelContents(t, readStringChannel(traceBuffered), seq("d", "c", "a", "b", "D", "C", "A", "B"))
}