-
Notifications
You must be signed in to change notification settings - Fork 3
/
pool_test.go
94 lines (87 loc) · 1.91 KB
/
pool_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
package tspool
import (
"testing"
)
func TestDefaultWorkerPool(t *testing.T) {
wp, err := DefaultWorkerPool()
if err != nil {
t.Error(err)
}
wp1 := wp.(*defaultWorkerPool)
if wp1.initCap != defaultWorkerPoolCap {
t.Errorf("DefaultWorkerPool falied. Got %d, expected %d\n", wp1.initCap, defaultWorkerPoolCap)
}
if wp1.maxCap != defaultWorkerPoolCap {
t.Errorf("DefaultWorkerPool falied. Got %d, expected %d\n", wp1.maxCap, defaultWorkerPoolCap)
}
wp, err = DefaultWorkerPool(10)
if err != nil {
t.Error(err)
}
wp2 := wp.(*defaultWorkerPool)
if wp2.initCap != 10 {
t.Errorf("DefaultWorkerPool falied. Got %d, expected 10\n", wp2.initCap)
}
if wp2.maxCap != 10 {
t.Errorf("DefaultWorkerPool falied. Got %d, expected 10\n", wp2.maxCap)
}
wp, err = DefaultWorkerPool(10, 20)
if err != nil {
t.Error(err)
}
wp3 := wp.(*defaultWorkerPool)
if wp3.initCap != 10 {
t.Errorf("DefaultWorkerPool falied. Got %d, expected 10\n", wp3.initCap)
}
if wp3.maxCap != 20 {
t.Errorf("DefaultWorkerPool falied. Got %d, expected 20\n", wp3.maxCap)
}
_, err = DefaultWorkerPool(10, 9)
if err == nil {
t.Error(errCap)
}
_, err = DefaultWorkerPool(10, 20, 30)
if err == nil {
t.Error(errArgs)
}
}
func TestGet(t *testing.T) {
wp, err := DefaultWorkerPool(10)
if err != nil {
t.Error(err)
}
for i := 0; i < 10; i++ {
w, err := wp.Get()
if err != nil {
t.Error(err)
}
dw := w.(*defaultWorker)
if dw.pos != uint(i) {
t.Errorf("Get falied. Got %d, expected %d\n", i, dw.pos)
}
}
_, err = wp.Get()
if err == nil {
t.Errorf("DefaultWorkerPool falied. Got nil, expected %s\n", errWorker)
}
}
func TestPut(t *testing.T) {
wp, err := DefaultWorkerPool(10)
var ws []Worker
if err != nil {
t.Error(err)
}
for i := 0; i < 10; i++ {
w, err := wp.Get()
if err != nil {
t.Error(err)
}
ws = append(ws, w)
}
for _, w := range ws {
err := wp.Put(w)
if err != nil {
t.Error(err)
}
}
}