forked from panjf2000/ants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_stack_test.go
118 lines (91 loc) 路 1.92 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package ants
import (
"testing"
"time"
)
func TestNewWorkerStack(t *testing.T) {
size := 100
q := newWorkerStack(size)
if q.len() != 0 {
t.Fatalf("Len error")
}
if !q.isEmpty() {
t.Fatalf("IsEmpty error")
}
if q.detach() != nil {
t.Fatalf("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
}
}
if q.len() != 5 {
t.Fatalf("Len error")
}
expired := time.Now()
err := q.insert(&goWorker{recycleTime: expired})
if err != nil {
t.Fatalf("Enqueue error")
}
time.Sleep(time.Second)
for i := 0; i < 6; i++ {
err := q.insert(&goWorker{recycleTime: time.Now()})
if err != nil {
t.Fatalf("Enqueue error")
}
}
if q.len() != 12 {
t.Fatalf("Len error")
}
q.retrieveExpiry(time.Second)
if q.len() != 6 {
t.Fatalf("Len error")
}
}
func TestSearch(t *testing.T) {
q := newWorkerStack(0)
// 1
expiry1 := time.Now()
_ = q.insert(&goWorker{recycleTime: time.Now()})
index := q.binarySearch(0, q.len()-1, time.Now())
if index != 0 {
t.Fatalf("should is 0")
}
index = q.binarySearch(0, q.len()-1, expiry1)
if index != -1 {
t.Fatalf("should is -1")
}
// 2
expiry2 := time.Now()
_ = q.insert(&goWorker{recycleTime: time.Now()})
index = q.binarySearch(0, q.len()-1, expiry1)
if index != -1 {
t.Fatalf("should is -1")
}
index = q.binarySearch(0, q.len()-1, expiry2)
if index != 0 {
t.Fatalf("should is 0")
}
index = q.binarySearch(0, q.len()-1, time.Now())
if index != 1 {
t.Fatalf("should is 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()})
}
index = q.binarySearch(0, q.len()-1, expiry3)
if index != 7 {
t.Fatalf("should is 7")
}
}