-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpool_test.go
171 lines (143 loc) · 3.28 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package wasps
import (
"context"
"fmt"
"github.com/stretchr/testify/assert"
"sync"
"testing"
"time"
)
func TestSubmitWithContextWithRecover(t *testing.T) {
pool := NewCallback(1)
defer func() {
pool.Release()
t.Log("pool released")
}()
var num = 10
ctx, _ := context.WithTimeout(context.TODO(), 1*time.Second)
var wait = make(chan int)
pool.SubmitWithContext(ctx, func(args ...interface{}) {
defer func() { wait <- 0 }()
num := args[0].(int)
assert.Equal(t, 20, num*2)
panic("custom panic")
}, WithArgs(num), WithRecoverFn(func(r interface{}) {
assert.Equal(t, "custom panic", r.(string))
t.Logf("catch panic: %+v", r)
}))
<-wait
}
func TestParallelJobs(t *testing.T) {
pool := NewCallback(2)
defer func() {
pool.Release()
t.Log("pool released")
}()
var num = 10
ctx, _ := context.WithTimeout(context.TODO(), 1*time.Second)
var wg sync.WaitGroup
wg.Add(3)
pool.SubmitWithContext(ctx, func(args ...interface{}) {
defer wg.Done()
num := args[0].(int)
t.Log("first submit", num*2)
assert.Equal(t, 20, num*2)
}, WithArgs(num))
pool.Submit(func(args ...interface{}) {
defer wg.Done()
num := args[0].(int)
assert.Equal(t, 10, num)
}, WithArgs(num), WithRecoverFn(func(r interface{}) { fmt.Printf("catch panic: %+v\n", r) }))
num = 200
pool.Submit(func(args ...interface{}) {
defer wg.Done()
assert.Equal(t, 200, num)
})
wg.Wait()
}
func TestPool_SetSize(t *testing.T) {
pool := NewCallback(10)
defer pool.Release()
time.Sleep(time.Second * 5)
stats := pool.Stats()
t.Logf("now pool stat: %+v", stats)
pool.SetCapacity(5)
stats = pool.Stats()
t.Logf("now pool stat: %+v", stats)
pool.SetCapacity(15)
time.Sleep(time.Second * 3)
stats = pool.Stats()
t.Logf("now pool stat: %+v", stats)
}
func TestPool_Stats(t *testing.T) {
pool := NewCallback(10)
defer pool.Release()
time.Sleep(time.Second * 1)
stats := pool.Stats()
t.Logf("now pool stat: %+v", stats)
stat := &Stats{
Cap: 10,
IdleWorker: 10,
WaitingTask: 0,
}
assert.Equal(t, stat, stats)
for i := 0; i < 15; i++ {
pool.Submit(func(args ...interface{}) {
time.Sleep(time.Minute)
})
}
time.Sleep(time.Second * 3)
stats = pool.Stats()
t.Logf("now pool stat: %+v", stats)
assert.Equal(t, &Stats{
Cap: 10,
IdleWorker: 0,
WaitingTask: 5,
}, stats)
}
func TestPool_Release(t *testing.T) {
pool := NewCallback(10)
defer pool.Release()
defer pool.Release()
}
//------------------------------------------------------------------------------
func BenchmarkNewCallback(b *testing.B) {
pool := NewCallback(10)
defer pool.Release()
b.ResetTimer()
var wg sync.WaitGroup
wg.Add(b.N)
for i := 0; i < b.N; i++ {
pool.Submit(func(args ...interface{}) {
defer wg.Done()
_ = args[0].(int)
})
}
wg.Wait()
}
func BenchmarkCallbackPool(b *testing.B) {
pool := NewCallback(BenchSize)
defer pool.Release()
var wg sync.WaitGroup
b.StartTimer()
for i := 0; i < b.N; i++ {
wg.Add(RunTimes)
for j := 0; j < RunTimes; j++ {
pool.Submit(func(args ...interface{}) {
demoFunc()
wg.Done()
})
}
wg.Wait()
}
b.StopTimer()
}
const (
RunTimes = 1000000
BenchParam = 10
BenchSize = 200000
DefaultExpiredTime = 10 * time.Second
)
func demoFunc() {
time.Sleep(time.Duration(BenchParam) * time.Millisecond)
}