-
Notifications
You must be signed in to change notification settings - Fork 1
/
flo_internal_test.go
161 lines (145 loc) · 5.23 KB
/
flo_internal_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package flo
import (
"context"
"fmt"
"testing"
)
func TestTypeOfStep(t *testing.T) {
tests := []struct {
name string
step Step
want stepType
}{
{"invalid int", 7, invalid},
{"invalid string", "", invalid},
{"invalid bool", false, invalid},
{"invalid chan", make(chan struct{}), invalid},
{"invalid struct", struct{}{}, invalid},
{"invalid ptr", &struct{}{}, invalid},
{"invalid nil", nil, invalid},
{"invalid float", 7.7, invalid},
{"invalid empty fn", func() {}, invalid},
{"invalid fn 1 wrong in parm", func(i int) {}, invalid},
{"invalid fn no out parm", func(ctx context.Context) {}, invalid},
{"invalid fn 1 wrong out parm", func() int { return 0 }, invalid},
{"invalid fn 1 wrong out parm", func() error { return nil }, invalid},
{"invalid fn 1 wrong out parm correct in", func(ctx context.Context, s string) int { return 0 }, invalid},
{"invalid fn 1 wrong out parm correct in", func(ctx context.Context, s string) (int, int) { return 0, 0 }, invalid},
{"invalid fn 1 wrong in parm correct out", func(s, t string) error { return nil }, invalid},
{"invalid too many in", func(ctx context.Context, i, j int) error { return nil }, invalid},
{"invalid too many out", func(ctx context.Context) (bool, bool, error) { return false, false, nil }, invalid},
{"onlyIn", func(ctx context.Context, b bool) error { return nil }, onlyIn},
{"inOut", func(ctx context.Context, b bool) (bool, error) { return false, nil }, inOut},
{"onlyOut", func(ctx context.Context) (bool, error) { return false, nil }, onlyOut},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := typeOfStep(tt.step); got != tt.want {
t.Errorf("got %d, want %d", got, tt.want)
}
})
}
}
func TestValidateInputChannelFailures(t *testing.T) {
invalidstepRunner := &stepRunner{sType: invalid, step: 7}
inOutstepRunner := &stepRunner{sType: inOut, step: inOutFn}
tests := []struct {
name string
inCh interface{}
sr *stepRunner
want error
}{
{"invalid step type", make(chan string, 1), invalidstepRunner, errInputChStepType},
{"invalid inCh type 1", 7, inOutstepRunner, errInputChType},
{"invalid inCh type 2", "", inOutstepRunner, errInputChType},
{"invalid inCh type 3", 7.7, inOutstepRunner, errInputChType},
{"invalid inCh type 4", struct{}{}, inOutstepRunner, errInputChType},
{"invalid inCh type 5", &struct{}{}, inOutstepRunner, errInputChType},
{"invalid inCh type 6", false, inOutstepRunner, errInputChType},
{"invalid chan dir", createSendChan(), inOutstepRunner, errInputChType},
{"type mismatch", make(chan int, 1), inOutstepRunner, fmt.Errorf(inputChTypeMismatchFmt, "int", "string")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := validateInputChannel(tt.inCh, tt.sr); got.Error() != tt.want.Error() {
t.Errorf("got %v, want %v", got, tt.want)
}
})
}
}
func TestValidateOutputChannelFailures(t *testing.T) {
invalidstepRunner := &stepRunner{sType: invalid, step: 7}
inOutstepRunner := &stepRunner{sType: inOut, step: inOutFn}
tests := []struct {
name string
inCh interface{}
sr *stepRunner
want error
}{
{"invalid step type", make(chan string, 1), invalidstepRunner, errOutputChStepType},
{"invalid inCh type 1", 7, inOutstepRunner, errOutputChType},
{"invalid inCh type 2", "", inOutstepRunner, errOutputChType},
{"invalid inCh type 3", 7.7, inOutstepRunner, errOutputChType},
{"invalid inCh type 4", struct{}{}, inOutstepRunner, errOutputChType},
{"invalid inCh type 5", &struct{}{}, inOutstepRunner, errOutputChType},
{"invalid inCh type 6", false, inOutstepRunner, errOutputChType},
{"invalid chan dir", createReceiveChan(), inOutstepRunner, errOutputChType},
{"type mismatch", make(chan int, 1), inOutstepRunner, fmt.Errorf(outputChTypeMismatchFmt, "int", "string")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := validateOutputChannel(tt.inCh, tt.sr); got.Error() != tt.want.Error() {
t.Errorf("got %v, want %v", got, tt.want)
}
})
}
}
func TestWithParallelism(t *testing.T) {
f := NewBuilder()
if f.parallelism != 1 {
t.Fatalf("got %d, want 1", f.parallelism)
}
f = NewBuilder(WithParallelism(-5))
if f.parallelism != 1 {
t.Fatalf("got %d, want 1", f.parallelism)
}
f = NewBuilder(WithParallelism(5))
if f.parallelism != 5 {
t.Fatalf("got %d, want 5", f.parallelism)
}
}
func TestWithStepParallelism(t *testing.T) {
f := NewBuilder()
if f.parallelism != 1 {
t.Fatalf("got %d, want 1", f.parallelism)
}
f.Add(inOutFn)
if f.steps[0].parallelism != 1 {
t.Fatalf("got %d, want 1", f.steps[0].parallelism)
}
f = NewBuilder(WithParallelism(5))
if f.parallelism != 5 {
t.Fatalf("got %d, want 5", f.parallelism)
}
f.Add(inOutFn)
if f.steps[0].parallelism != 5 {
t.Fatalf("got %d, want 5", f.steps[0].parallelism)
}
f.Add(inOutFn, WithStepParallelism(-5))
if f.steps[1].parallelism != 5 {
t.Fatalf("got %d, want 5", f.steps[1].parallelism)
}
f.Add(inOutFn, WithStepParallelism(6))
if f.steps[2].parallelism != 6 {
t.Fatalf("got %d, want 6", f.steps[2].parallelism)
}
}
func createSendChan() chan<- bool {
return make(chan bool)
}
func createReceiveChan() <-chan bool {
return make(chan bool)
}
func inOutFn(ctx context.Context, s string) (string, error) {
return "", nil
}