-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequentialPerIdWorkerQueue_test.go
89 lines (72 loc) · 1.81 KB
/
sequentialPerIdWorkerQueue_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
package worker
import (
"context"
"github.com/stretchr/testify/assert"
"strconv"
"testing"
"time"
)
type testJob struct {
id uint64
controlCh chan uint64
job func()
}
func (j *testJob) Id() string {
return strconv.Itoa(int(j.id))
}
func (j *testJob) Run(ctx context.Context) {
j.job()
j.controlCh <- 1
}
func TestSequentialPerIdWorkerQueue_ProcessAllJobs(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
controlCh := make(chan uint64, 100)
newJob := func(id uint64) *testJob {
return &testJob{id, controlCh, func() {}}
}
queue := NewSequentialPerIdWorkerQueue(ctx, 2, 100, 100)
for x := 0; x < 100; x++ {
queue.Do(newJob(uint64((x % 2) + 1)))
}
count := uint64(0)
for i := 0; i < 100; i++ {
count += <-controlCh
}
assert.Equal(t, 100, int(count))
assert.Len(t, queue.(*sequentialPerIdWorkerQueue).queue, 0)
assert.Len(t, queue.(*sequentialPerIdWorkerQueue).sequentialQueue, 0)
queue.Close()
}
func TestSequentialPerIdWorkerQueue_ProcessNotParallel(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
controlCh := make(chan uint64, 200)
resMap := make(map[string]struct{})
count := 0
newJob := func(id uint64) *testJob {
return &testJob{id, controlCh, func() {
time.Sleep(10 * time.Millisecond)
count++
resMap["12313123"] = struct{}{}
}}
}
queue := NewSequentialPerIdWorkerQueue(ctx, 10, 200, 200)
go func() {
for x := 0; x < 100; x++ {
queue.Do(newJob(1))
}
}()
go func() {
for x := 0; x < 100; x++ {
queue.Do(newJob(1))
}
}()
for i := 0; i < 200; i++ {
<-controlCh
}
assert.Equal(t, 200, count)
assert.Len(t, queue.(*sequentialPerIdWorkerQueue).queue, 0)
assert.Len(t, queue.(*sequentialPerIdWorkerQueue).sequentialQueue, 0)
queue.Close()
}