Skip to content

Commit 93f4d7e

Browse files
authored
Merge pull request #36 from Icinga/test-Bulk
Test com.Bulk()
2 parents eed56d2 + 7eb4b15 commit 93f4d7e

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

com/bulker_test.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package com
2+
3+
import (
4+
"context"
5+
"github.com/stretchr/testify/require"
6+
"testing"
7+
"time"
8+
)
9+
10+
func TestBulk(t *testing.T) {
11+
noSp := NeverSplit[string]
12+
var closeContext []string = nil
13+
14+
subtests := []struct {
15+
name string
16+
input [][]string
17+
count int
18+
spf BulkChunkSplitPolicyFactory[string]
19+
output [][]string
20+
}{
21+
{"empty", nil, 1, noSp, nil},
22+
{"negative", [][]string{{"a"}}, -1, noSp, [][]string{{"a"}}},
23+
{"a0", [][]string{{"a"}}, 0, noSp, [][]string{{"a"}}},
24+
{"a1", [][]string{{"a"}}, 1, noSp, [][]string{{"a"}}},
25+
{"a2", [][]string{{"a"}}, 2, noSp, [][]string{{"a"}}},
26+
{"ab1", [][]string{{"a", "b"}}, 1, noSp, [][]string{{"a"}, {"b"}}},
27+
{"ab2", [][]string{{"a", "b"}}, 2, noSp, [][]string{{"a", "b"}}},
28+
{"ab3", [][]string{{"a", "b"}}, 3, noSp, [][]string{{"a", "b"}}},
29+
{"abc1", [][]string{{"a", "b", "c"}}, 1, noSp, [][]string{{"a"}, {"b"}, {"c"}}},
30+
{"abc2", [][]string{{"a", "b", "c"}}, 2, noSp, [][]string{{"a", "b"}, {"c"}}},
31+
{"abc3", [][]string{{"a", "b", "c"}}, 3, noSp, [][]string{{"a", "b", "c"}}},
32+
{"abc4", [][]string{{"a", "b", "c"}}, 4, noSp, [][]string{{"a", "b", "c"}}},
33+
{
34+
"chunks_by_timeout", [][]string{{"a", "b", "c", "d"}, {"e", "f", "g"}, {"h", "i"}, {"j"}}, 5,
35+
noSp, [][]string{{"a", "b", "c", "d"}, {"e", "f", "g"}, {"h", "i"}, {"j"}},
36+
},
37+
{"chunks_by_spf", [][]string{{"a", "b", "c", "d", "e", "f", "g"}}, 2, func() BulkChunkSplitPolicy[string] {
38+
return func(string) bool { return true }
39+
}, [][]string{{"a"}, {"b"}, {"c"}, {"d"}, {"e"}, {"f"}, {"g"}}},
40+
{"close-ctx_a1", [][]string{closeContext, {"a"}}, 1, noSp, nil},
41+
{"close-ctx_a4", [][]string{closeContext, {"a"}}, 4, noSp, nil},
42+
{"a_close-ctx_b1", [][]string{{"a"}, closeContext, {"b"}}, 1, noSp, [][]string{{"a"}}},
43+
{"a_close-ctx_b4", [][]string{{"a"}, closeContext, {"b"}}, 4, noSp, [][]string{{"a"}}},
44+
{"ab_close-ctx_c1", [][]string{{"a", "b"}, closeContext, {"c"}}, 1, noSp, [][]string{{"a"}, {"b"}}},
45+
{"ab_close-ctx_c4", [][]string{{"a", "b"}, closeContext, {"c"}}, 4, noSp, [][]string{{"a", "b"}}},
46+
}
47+
48+
latencies := []struct {
49+
name string
50+
latency time.Duration
51+
}{
52+
{"instantly", 0},
53+
{"1us", time.Microsecond},
54+
{"20ms", 20 * time.Millisecond},
55+
}
56+
57+
for _, st := range subtests {
58+
t.Run(st.name, func(t *testing.T) {
59+
for _, l := range latencies {
60+
t.Run(l.name, func(t *testing.T) {
61+
ctx, cancel := context.WithCancel(context.Background())
62+
defer cancel()
63+
64+
bulkCtx, cancelBulk := context.WithCancel(ctx)
65+
66+
ch := make(chan string, 1)
67+
go func() {
68+
defer close(ch)
69+
70+
for i, chunk := range st.input {
71+
if i > 0 {
72+
select {
73+
case <-time.After(time.Second / 2):
74+
case <-ctx.Done():
75+
return
76+
}
77+
}
78+
79+
if chunk == nil {
80+
cancelBulk()
81+
}
82+
83+
for _, v := range chunk {
84+
if l.latency > 0 {
85+
select {
86+
case <-time.After(l.latency):
87+
case <-ctx.Done():
88+
return
89+
}
90+
}
91+
92+
select {
93+
case ch <- v:
94+
case <-ctx.Done():
95+
return
96+
}
97+
}
98+
}
99+
}()
100+
101+
output := Bulk(bulkCtx, ch, st.count, st.spf)
102+
require.NotNil(t, output)
103+
104+
for _, expected := range st.output {
105+
select {
106+
case actual, ok := <-output:
107+
if !ok {
108+
require.Fail(t, "channel should not be closed, yet")
109+
}
110+
111+
require.Equal(t, expected, actual)
112+
case <-time.After(time.Second):
113+
require.Fail(t, "channel should not block")
114+
}
115+
}
116+
117+
select {
118+
case _, ok := <-output:
119+
if ok {
120+
require.Fail(t, "channel should be closed")
121+
}
122+
case <-time.After(time.Second):
123+
require.Fail(t, "channel should not block")
124+
}
125+
})
126+
}
127+
})
128+
}
129+
}

0 commit comments

Comments
 (0)