-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpool_test.go
165 lines (136 loc) · 2.88 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
package gopool
import (
"context"
"fmt"
"math/rand"
"sync"
"sync/atomic"
"testing"
"time"
)
func TestPool_Do(t *testing.T) {
workers := 100
tasks := workers * 100
p := New(100, 10*time.Second)
var n int32
for i := 0; i < tasks; i++ {
err := p.Do(context.Background(), func() {
time.Sleep(100 * time.Microsecond)
atomic.AddInt32(&n, 1)
})
if err != nil {
t.Fatal("should not error")
}
}
p.Close()
if n != int32(tasks) {
t.Fatalf("worker execute, except: %d, actual: %d", workers*10, n)
}
}
func TestPool_DoWait(t *testing.T) {
p := New(2, 10*time.Second)
p.Do(context.Background(), func() {
time.Sleep(200 * time.Microsecond)
})
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Microsecond)
defer cancel()
err := p.Do(ctx, func() {
time.Sleep(200 * time.Microsecond)
})
if err != nil {
t.Fatal("should not error")
}
err = p.Do(ctx, func() {})
if err != context.DeadlineExceeded {
t.Fatalf("should return context.DeadlineExceeded error")
}
}
func TestPool_DoError(t *testing.T) {
t.Run("panic", func(t *testing.T) {
t.Parallel()
ok := false
p := New(2, 1*time.Second)
p.PanicHandler = func(v interface{}) {
ok = true
}
p.Do(context.Background(), func() {
panic("error")
})
p.Close()
if !ok {
t.Fatal("panic handler not work")
}
})
t.Run("closed", func(t *testing.T) {
t.Parallel()
p := New(1, 1*time.Second)
p.Close()
err := p.Do(context.Background(), func() {})
if err != ErrorPoolClosed {
t.Fatalf("should return pool closed error")
}
})
}
func BenchmarkMain(b *testing.B) {
idle := 5 * time.Second
testTask := func() {
n := rand.Int63n(50)
time.Sleep(time.Duration(n) * time.Microsecond)
}
for _, workers := range []int{10, 50, 100, 500, 1000} {
b.Run(fmt.Sprintf("worker %d", workers), func(b *testing.B) {
b.Run("pool", func(b *testing.B) {
p := New(workers, idle)
b.StartTimer()
for i := 0; i < b.N; i++ {
p.Do(context.Background(), testTask)
}
p.Close()
b.StopTimer()
})
b.Run("fan out", func(b *testing.B) {
var wg sync.WaitGroup
tasks := make(chan func())
for i := 0; i < workers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for task := range tasks {
task()
}
}()
}
b.StartTimer()
for i := 0; i < b.N; i++ {
tasks <- testTask
}
close(tasks)
wg.Wait()
b.StopTimer()
})
b.Run("token bucket", func(b *testing.B) {
tokens := make(chan struct{}, workers)
tasks := make(chan task)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for task := range tasks {
tokens <- struct{}{}
go func(task func()) {
task()
<-tokens
}(task)
}
}()
b.StartTimer()
for i := 0; i < b.N; i++ {
tasks <- testTask
}
close(tasks)
wg.Wait()
b.StopTimer()
})
})
}
}