forked from panjf2000/ants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_loop_queue_test.go
48 lines (39 loc) Β· 1010 Bytes
/
worker_loop_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
package ants
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestNewLoopQueue(t *testing.T) {
size := 100
q := newWorkerLoopQueue(size)
assert.EqualValues(t, 0, q.len(), "Len error")
assert.Equal(t, true, q.isEmpty(), "IsEmpty error")
assert.Nil(t, q.detach(), "Dequeue error")
}
func TestLoopQueue(t *testing.T) {
size := 10
q := newWorkerLoopQueue(size)
for i := 0; i < 5; i++ {
err := q.insert(&goWorker{recycleTime: time.Now()})
if err != nil {
break
}
}
assert.EqualValues(t, 5, q.len(), "Len error")
v := q.detach()
t.Log(v)
assert.EqualValues(t, 4, q.len(), "Len error")
time.Sleep(time.Second)
for i := 0; i < 6; i++ {
err := q.insert(&goWorker{recycleTime: time.Now()})
if err != nil {
break
}
}
assert.EqualValues(t, 10, q.len(), "Len error")
err := q.insert(&goWorker{recycleTime: time.Now()})
assert.Error(t, err, "Enqueue, error")
q.retrieveExpiry(time.Second)
assert.EqualValuesf(t, 6, q.len(), "Len error: %d", q.len())
}