-
Notifications
You must be signed in to change notification settings - Fork 11
/
wrap_test.go
199 lines (157 loc) · 4.42 KB
/
wrap_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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package rill
import (
"fmt"
"testing"
"time"
"github.com/destel/rill/internal/th"
)
func TestWrap(t *testing.T) {
_ = Wrap(10, nil)
}
func TestFromSlice(t *testing.T) {
t.Run("empty", func(t *testing.T) {
in := FromSlice[int](nil, nil)
outSlice, err := ToSlice(in)
th.ExpectSlice(t, outSlice, nil)
th.ExpectNoError(t, err)
})
t.Run("error in second arg", func(t *testing.T) {
in := FromSlice([]int{1, 2, 3, 4}, fmt.Errorf("err0"))
outSlice, errs := toSliceAndErrors(in)
th.ExpectSlice(t, outSlice, nil)
th.ExpectSlice(t, errs, []string{"err0"})
})
t.Run("no errors", func(t *testing.T) {
inSlice := make([]int, 20)
for i := range inSlice {
inSlice[i] = i
}
in := FromSlice(inSlice, nil)
outSlice, err := ToSlice(in)
th.ExpectSlice(t, outSlice, inSlice)
th.ExpectNoError(t, err)
})
t.Run("no errors large", func(t *testing.T) {
inSlice := make([]int, 4000)
for i := range inSlice {
inSlice[i] = i
}
in := FromSlice(inSlice, nil)
outSlice, err := ToSlice(in)
th.ExpectSlice(t, outSlice, inSlice)
th.ExpectNoError(t, err)
})
t.Run("errors", func(t *testing.T) {
inSlice := make([]int, 20)
for i := 0; i < 20; i++ {
inSlice[i] = i
}
in := FromSlice(inSlice, nil)
in = replaceWithError(in, 15, fmt.Errorf("err15"))
in = replaceWithError(in, 18, fmt.Errorf("err18"))
outSlice, err := ToSlice(in)
th.ExpectSlice(t, outSlice, inSlice[:15])
th.ExpectError(t, err, "err15")
time.Sleep(1 * time.Second)
th.ExpectDrainedChan(t, in)
})
}
func TestFromChan(t *testing.T) {
t.Run("nil", func(t *testing.T) {
res := FromChan[int](nil, nil)
th.ExpectValue(t, res, nil)
})
t.Run("no error", func(t *testing.T) {
var inSlice []int
var expectedOutSlice []Try[int]
for i := 0; i < 20000; i++ {
inSlice = append(inSlice, i)
expectedOutSlice = append(expectedOutSlice, Try[int]{Value: i})
}
wrapped := FromChan(th.FromSlice(inSlice), nil)
outSlice := th.ToSlice(wrapped)
th.ExpectSlice(t, outSlice, expectedOutSlice)
})
t.Run("with error", func(t *testing.T) {
var inSlice []int
var expectedOutSlice []Try[int]
err := fmt.Errorf("err")
expectedOutSlice = append(expectedOutSlice, Try[int]{Error: err})
for i := 0; i < 20000; i++ {
inSlice = append(inSlice, i)
expectedOutSlice = append(expectedOutSlice, Try[int]{Value: i})
}
wrapped := FromChan(th.FromSlice(inSlice), err)
outSlice := th.ToSlice(wrapped)
th.ExpectSlice(t, outSlice, expectedOutSlice)
})
}
func TestFromChans(t *testing.T) {
// slices -> FromSlice -> FromChans -> ToChans -> ToSlice -> compare
runTest := func(name string, valsIn []int, errsIn []error) {
t.Run(name, func(t *testing.T) {
var valsInChan <-chan int
if len(valsIn) > 0 {
valsInChan = th.FromSlice(valsIn)
}
var errsInChan <-chan error
if len(errsIn) > 0 {
errsInChan = th.FromSlice(errsIn)
}
valsOutChan, errsOutChan := ToChans(FromChans(valsInChan, errsInChan))
if valsInChan == nil && errsInChan == nil {
th.ExpectValue(t, valsOutChan, nil)
th.ExpectValue(t, errsOutChan, nil)
return
}
var valsOut []int
var errsOut []error
th.DoConcurrently(
func() { valsOut = th.ToSlice(valsOutChan) },
func() { errsOut = th.ToSlice(errsOutChan) },
)
// nil errors are not expected in the output
var expectedErrors []error
for _, err := range errsIn {
if err != nil {
expectedErrors = append(expectedErrors, err)
}
}
th.ExpectSlice(t, valsOut, valsIn)
th.ExpectSlice(t, errsOut, expectedErrors)
})
}
makeSlice := func(n int) []int {
out := make([]int, n)
for i := 0; i < n; i++ {
out[i] = i
}
return out
}
makeErrSlice := func(n int) []error {
out := make([]error, n)
for i := 0; i < n; i++ {
out[i] = fmt.Errorf("err%06d", i)
}
return out
}
runTest("nil", nil, nil)
runTest("no errors", makeSlice(10000), nil)
runTest("only errors", nil, makeErrSlice(10000))
runTest("values and errors", makeSlice(10000), makeErrSlice(10000))
runTest("values and nil errors", makeSlice(10), []error{nil, nil, fmt.Errorf("err"), nil})
}
func TestGenerate(t *testing.T) {
in := Generate(func(send func(int), sendErr func(error)) {
for i := 0; i < 10; i++ {
if i%2 == 0 {
send(i)
} else {
sendErr(fmt.Errorf("err%d", i))
}
}
})
outSlice, errSlice := toSliceAndErrors(in)
th.ExpectSlice(t, outSlice, []int{0, 2, 4, 6, 8})
th.ExpectSlice(t, errSlice, []string{"err1", "err3", "err5", "err7", "err9"})
}