-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue_test.go
81 lines (65 loc) · 1.2 KB
/
queue_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
package gowork
import (
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type testWorker struct {
id int
c *counter
sleep time.Duration
}
type counter struct {
mtx *sync.Mutex
count int
}
func (c *counter) Add(a int) {
c.mtx.Lock()
c.count += a
c.mtx.Unlock()
}
func (c *counter) GetCount() int {
c.mtx.Lock()
i := c.count
c.mtx.Unlock()
return i
}
func (t *testWorker) Do() interface{} {
time.Sleep(t.sleep)
t.c.Add(1)
return nil
}
func TestQueue(t *testing.T) {
assert := assert.New(t)
c := &counter{mtx: &sync.Mutex{}}
q := NewQueue(100, 4)
q.Start(4)
for i := 0; i < 40; i++ {
q.AddWork(&testWorker{sleep: 100 * time.Millisecond, id: i, c: c})
}
qAddTime := time.Now().Unix()
q.Finish()
for r := range q.Results() {
assert.Nil(r)
}
qFinishTime := time.Now().Unix()
assert.NotEqual(qAddTime, qFinishTime)
assert.Equal(40, c.GetCount())
c = &counter{mtx: &sync.Mutex{}}
q = NewQueue(100, 4)
q.Start(4)
for i := 0; i < 40; i++ {
q.AddWork(&testWorker{sleep: 100 * time.Millisecond, id: i, c: c})
}
q.Finish()
i := 0
for r := range q.Results() {
assert.Nil(r)
if i == 15 {
q.Abort()
}
i++
}
assert.NotEqual(40, c.GetCount())
}