-
Notifications
You must be signed in to change notification settings - Fork 0
/
distinctWorkerQueue_test.go
66 lines (56 loc) · 1.23 KB
/
distinctWorkerQueue_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
package worker
import (
"context"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
type testJob2 struct {
poolId uint64
controlCh chan uint64
}
func (j *testJob2) Equals(obj interface{}) bool {
cast, ok := obj.(*testJob2)
if !ok {
return false
}
return j.poolId == cast.poolId
}
func (j *testJob2) HashCode() int {
return int(j.poolId)
}
func (j *testJob2) Run(ctx context.Context) {
time.Sleep(1 * time.Millisecond)
j.controlCh <- j.poolId
}
func TestDistinctWorkerQueue_Basic(t *testing.T) {
controlCh := make(chan uint64, 11)
newJob := func(id uint64) *testJob2 {
return &testJob2{id, controlCh}
}
queue := NewDistinctWorkerQueue(1, 100)
time.Sleep(1 * time.Millisecond)
queue.Do(newJob(1))
queue.Do(newJob(2))
time.Sleep(1 * time.Millisecond)
queue.Do(newJob(3))
queue.Do(newJob(4))
queue.Do(newJob(5))
queue.Do(newJob(4))
queue.Do(newJob(5))
queue.Do(newJob(6))
queue.Do(newJob(7))
time.Sleep(1 * time.Millisecond)
queue.Do(newJob(8))
queue.Do(newJob(9))
queue.Do(newJob(10))
time.Sleep(1 * time.Millisecond)
queue.Do(newJob(11))
count := uint64(0)
for i := 0; i < 11; i++ {
count += <-controlCh
}
assert.Equal(t, 66, int(count))
assert.True(t, queue.index.IsEmpty())
queue.Close()
}