forked from valyala/fasthttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workerpool_test.go
169 lines (151 loc) · 3.46 KB
/
workerpool_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
package fasthttp
import (
"io/ioutil"
"net"
"testing"
"time"
"github.com/valyala/fasthttp/fasthttputil"
)
func TestWorkerPoolStartStopSerial(t *testing.T) {
testWorkerPoolStartStop(t)
}
func TestWorkerPoolStartStopConcurrent(t *testing.T) {
concurrency := 10
ch := make(chan struct{}, concurrency)
for i := 0; i < concurrency; i++ {
go func() {
testWorkerPoolStartStop(t)
ch <- struct{}{}
}()
}
for i := 0; i < concurrency; i++ {
select {
case <-ch:
case <-time.After(time.Second):
t.Fatalf("timeout")
}
}
}
func testWorkerPoolStartStop(t *testing.T) {
wp := &workerPool{
WorkerFunc: func(conn net.Conn) error { return nil },
MaxWorkersCount: 10,
Logger: defaultLogger,
}
for i := 0; i < 10; i++ {
wp.Start()
wp.Stop()
}
}
func TestWorkerPoolMaxWorkersCountSerial(t *testing.T) {
testWorkerPoolMaxWorkersCountMulti(t)
}
func TestWorkerPoolMaxWorkersCountConcurrent(t *testing.T) {
concurrency := 4
ch := make(chan struct{}, concurrency)
for i := 0; i < concurrency; i++ {
go func() {
testWorkerPoolMaxWorkersCountMulti(t)
ch <- struct{}{}
}()
}
for i := 0; i < concurrency; i++ {
select {
case <-ch:
case <-time.After(time.Second):
t.Fatalf("timeout")
}
}
}
func testWorkerPoolMaxWorkersCountMulti(t *testing.T) {
for i := 0; i < 5; i++ {
testWorkerPoolMaxWorkersCount(t)
}
}
func testWorkerPoolMaxWorkersCount(t *testing.T) {
ready := make(chan struct{})
wp := &workerPool{
WorkerFunc: func(conn net.Conn) error {
buf := make([]byte, 100)
n, err := conn.Read(buf)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
buf = buf[:n]
if string(buf) != "foobar" {
t.Fatalf("unexpected data read: %q. Expecting %q", buf, "foobar")
}
if _, err = conn.Write([]byte("baz")); err != nil {
t.Fatalf("unexpected error: %s", err)
}
<-ready
return nil
},
MaxWorkersCount: 10,
Logger: defaultLogger,
connState: func(net.Conn, ConnState) {},
}
wp.Start()
ln := fasthttputil.NewInmemoryListener()
clientCh := make(chan struct{}, wp.MaxWorkersCount)
for i := 0; i < wp.MaxWorkersCount; i++ {
go func() {
conn, err := ln.Dial()
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if _, err = conn.Write([]byte("foobar")); err != nil {
t.Fatalf("unexpected error: %s", err)
}
data, err := ioutil.ReadAll(conn)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if string(data) != "baz" {
t.Fatalf("unexpected value read: %q. Expecting %q", data, "baz")
}
if err = conn.Close(); err != nil {
t.Fatalf("unexpected error: %s", err)
}
clientCh <- struct{}{}
}()
}
for i := 0; i < wp.MaxWorkersCount; i++ {
conn, err := ln.Accept()
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !wp.Serve(conn) {
t.Fatalf("worker pool must have enough workers to serve the conn")
}
}
go func() {
if _, err := ln.Dial(); err != nil {
t.Fatalf("unexpected error: %s", err)
}
}()
conn, err := ln.Accept()
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
for i := 0; i < 5; i++ {
if wp.Serve(conn) {
t.Fatalf("worker pool must be full")
}
}
if err = conn.Close(); err != nil {
t.Fatalf("unexpected error: %s", err)
}
close(ready)
for i := 0; i < wp.MaxWorkersCount; i++ {
select {
case <-clientCh:
case <-time.After(time.Second):
t.Fatalf("timeout")
}
}
if err := ln.Close(); err != nil {
t.Fatalf("unexpected error: %s", err)
}
wp.Stop()
}