forked from panjf2000/ants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_stack_test.go
88 lines (65 loc) Β· 2.09 KB
/
worker_stack_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
// +build !windows
package ants
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestNewWorkerStack(t *testing.T) {
size := 100
q := newWorkerStack(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 TestWorkerStack(t *testing.T) {
q := newWorkerArray(arrayType(-1), 0)
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")
expired := time.Now()
err := q.insert(&goWorker{recycleTime: expired})
if err != nil {
t.Fatal("Enqueue error")
}
time.Sleep(time.Second)
for i := 0; i < 6; i++ {
err := q.insert(&goWorker{recycleTime: time.Now()})
if err != nil {
t.Fatal("Enqueue error")
}
}
assert.EqualValues(t, 12, q.len(), "Len error")
q.retrieveExpiry(time.Second)
assert.EqualValues(t, 6, q.len(), "Len error")
}
// It seems that something wrong with time.Now() on Windows, not sure whether it is a bug on Windows,
// so exclude this test from Windows platform temporarily.
func TestSearch(t *testing.T) {
q := newWorkerStack(0)
// 1
expiry1 := time.Now()
_ = q.insert(&goWorker{recycleTime: time.Now()})
assert.EqualValues(t, 0, q.binarySearch(0, q.len()-1, time.Now()), "index should be 0")
assert.EqualValues(t, -1, q.binarySearch(0, q.len()-1, expiry1), "index should be -1")
// 2
expiry2 := time.Now()
_ = q.insert(&goWorker{recycleTime: time.Now()})
assert.EqualValues(t, -1, q.binarySearch(0, q.len()-1, expiry1), "index should be -1")
assert.EqualValues(t, 0, q.binarySearch(0, q.len()-1, expiry2), "index should be 0")
assert.EqualValues(t, 1, q.binarySearch(0, q.len()-1, time.Now()), "index should be 1")
// more
for i := 0; i < 5; i++ {
_ = q.insert(&goWorker{recycleTime: time.Now()})
}
expiry3 := time.Now()
_ = q.insert(&goWorker{recycleTime: expiry3})
for i := 0; i < 10; i++ {
_ = q.insert(&goWorker{recycleTime: time.Now()})
}
assert.EqualValues(t, 7, q.binarySearch(0, q.len()-1, expiry3), "index should be 7")
}