-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatcher_test.go
54 lines (49 loc) · 908 Bytes
/
batcher_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
package gopool
import (
"context"
"fmt"
"testing"
"time"
)
type task struct {
ctx context.Context
i int
}
func TestBatcher(t *testing.T) {
executor := func(tasks []interface{}) error {
for i := range tasks {
t, ok := tasks[i].(*task)
if !ok {
fmt.Println("not expect task struct")
continue
}
fmt.Println("do task:", t.i)
}
fmt.Println("tasks:", tasks)
return nil
}
bWorker := &Batcher{
config: &PoolConfig{
batchSize: 5,
chanSize: 5,
linger: 10 * time.Millisecond,
batchErrCb: nil,
},
taskQueue: make(chan interface{}, 5),
executor: executor,
}
go bWorker.start()
for i := 0; i < 20; i++ {
bWorker.addTask(&task{
ctx: context.Background(),
i: i,
})
if i%7 == 0 {
fmt.Println("time sleep...")
time.Sleep(15 * time.Millisecond)
}
}
bWorker.quit()
time.Sleep(11 * time.Millisecond)
t.Log("batcher finished")
}