-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatchchan_test.go
More file actions
71 lines (60 loc) · 1.43 KB
/
batchchan_test.go
File metadata and controls
71 lines (60 loc) · 1.43 KB
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
package batchutil
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestBatchChanTimeout(t *testing.T) {
t.Run("empty input channel", func(t *testing.T) {
// Create an empty channel
in := make(chan int)
close(in)
// Iterate over the batches and ensure no values are received
for range BatchChanTimeout(in, 5, time.Hour) {
t.Error("Unexpected batch received from an empty input channel")
}
})
t.Run("max items per batch", func(t *testing.T) {
in := make(chan int)
go func() {
defer close(in)
for i := 0; i < 10; i++ {
in <- i
}
}()
var gotValues []int
for ints := range BatchChanTimeout(in, 5, time.Hour) {
require.Len(t, ints, 5)
gotValues = append(gotValues, ints...)
}
require.Len(t, gotValues, 10)
for i := 0; i < 10; i++ {
require.Contains(t, gotValues, i)
}
})
t.Run("max timeout", func(t *testing.T) {
in := make(chan int)
go func() {
defer close(in)
for i := 0; i < 10; i++ {
in <- i
}
}()
var gotValues []int
for ints := range BatchChanTimeout(in, 100, time.Millisecond) {
require.Len(t, ints, 10)
gotValues = append(gotValues, ints...)
}
require.Len(t, gotValues, 10)
for i := 0; i < 10; i++ {
require.Contains(t, gotValues, i)
}
})
}
func TestManager(t *testing.T) {
manager := NewManager[int](100, time.Second)
go manager.ProcessMessages(nil)
manager.Stop()
err := manager.SendMessage(1)
require.Error(t, err)
}