-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgotcpd_test.go
346 lines (281 loc) · 8.86 KB
/
gotcpd_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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package gotcpd
import (
"os"
"fmt"
"net"
"sync"
"time"
"bufio"
"strings"
"strconv"
"testing"
"encoding/binary"
)
const serverAddr = "127.0.0.1:7777"
// Global variable initialized in main() and asserted in TestPassingArbitraryUserDataPtr()
var myDataPtr string
// Helper function
func testPtr(conn net.Conn, t *testing.T) {
fmt.Fprintf(conn, "GET ptr\n")
message, _ := bufio.NewReader(conn).ReadString('\n')
if message != myDataPtr {
t.Errorf("%#v != %#v\n", message, myDataPtr)
}
}
// Helper function
func testData(conn net.Conn, t *testing.T) {
fmt.Fprintf(conn, "GET data\n")
message, _ := bufio.NewReader(conn).ReadString('\n')
if message != "my data\n" {
t.Errorf("%#v != 'my data'\n", message)
}
}
// Check if server responds correctly
func TestEcho(t *testing.T) {
conn, _ := net.Dial("tcp", serverAddr)
fmt.Fprintf(conn, "GET echo\n")
message, _ := bufio.NewReader(conn).ReadString('\n')
if message != "echo\n" {
t.Errorf("%#v != 'echo'\n", message)
}
conn.Close()
}
// Check if server handles the case if client quits without waiting for responses
func TestPrematureQuit(t *testing.T) {
conn, _ := net.Dial("tcp", serverAddr)
fmt.Fprintf(conn, "GET sleep 1\n")
conn.Close()
time.Sleep(2 * time.Second)
// panic: (send on closed channel)
}
// Check if server handles the case if client quits without sending any request
func TestQuitWithoutRequest(t *testing.T) {
conn, _ := net.Dial("tcp", serverAddr)
conn.Close()
time.Sleep(1 * time.Second)
// panic: (send on closed channel)
}
// Check if arbitrary user data is correctly passed by pointer
func TestPassingArbitraryUserDataPtr(t *testing.T) {
// Multiple requests on single connection
conn, _ := net.Dial("tcp", serverAddr)
testPtr(conn, t)
testPtr(conn, t)
testPtr(conn, t)
conn.Close()
// New connection
conn1, _ := net.Dial("tcp", serverAddr)
testPtr(conn1, t)
conn1.Close()
// New connection
conn2, _ := net.Dial("tcp", serverAddr)
testPtr(conn2, t)
conn2.Close()
}
// Check if arbitrary user data is correctly passed
func TestPassingArbitraryUserData(t *testing.T) {
// Multiple requests on single connection
conn, _ := net.Dial("tcp", serverAddr)
testData(conn, t)
testData(conn, t)
testData(conn, t)
conn.Close()
// New connection
conn1, _ := net.Dial("tcp", serverAddr)
testData(conn1, t)
conn1.Close()
// New connection
conn2, _ := net.Dial("tcp", serverAddr)
testData(conn2, t)
conn2.Close()
}
// Test long key
func TestLongKey(t *testing.T) {
// Create a string 32MB long
var key string = "x"
for i := 0; i<25; i++ {
key = key + key
}
conn, _ := net.Dial("tcp", serverAddr)
fmt.Fprintf(conn, "GET %s\n", key)
message, _ := bufio.NewReader(conn).ReadString('\n')
if len(message) != len(key) + len("\n") {
t.Errorf("Length of key %d != %d\n", len(message), len(key) + len("\n"))
}
conn.Close()
}
// Check 2x10000 request-respone pairs
func TestManyRequests(t *testing.T) {
conn1, _ := net.Dial("tcp", serverAddr)
conn2, _ := net.Dial("tcp", serverAddr)
for i := 0; i<100000; i++ {
fmt.Fprintf(conn1, "GET %d\n", i)
fmt.Fprintf(conn2, "GET %d\n", i)
message1, err1 := bufio.NewReader(conn1).ReadString('\n')
if err1 != nil || message1 != fmt.Sprintf("%d\n", i) {
t.Errorf("Error while receiving message %d\n", i)
}
message2, err2 := bufio.NewReader(conn2).ReadString('\n')
if err2 != nil || message2 != fmt.Sprintf("%d\n", i) {
t.Errorf("Error while receiving message %d\n", i)
}
}
conn1.Close()
conn2.Close()
}
// Test multiple requests sent on one connection.
// Responses should come in order and processing time must indicate concurrent processing.
func TestResponsesOrder(t *testing.T) {
conn, _ := net.Dial("tcp", serverAddr)
reader := bufio.NewReader(conn)
start := time.Now()
fmt.Fprintf(conn, "GET sleep 2\nGET sleep 0\nGET sleep 3\nGET sleep 1\n")
if message, _ := reader.ReadString('\n'); message != "sleep 2\n" {
t.Errorf("%#v != 'sleep 2'\n", message)
}
if message, _ := reader.ReadString('\n'); message != "sleep 0\n" {
t.Errorf("%#v != 'sleep 0'\n", message)
}
if message, _ := reader.ReadString('\n'); message != "sleep 3\n" {
t.Errorf("%#v != 'sleep 3'\n", message)
}
if message, _ := reader.ReadString('\n'); message != "sleep 1\n" {
t.Errorf("%#v != 'sleep 1'\n", message)
}
end := time.Now()
elapsed := end.Sub(start)
if elapsed.Milliseconds() > 3100 {
t.Errorf("Execution time %v > 3 seconds \n", elapsed)
}
}
// Run N requests: 1 request per connection
func TestConcurrency1(t *testing.T) {
const N = 100
var waitgroup sync.WaitGroup
waitgroup.Add(N)
start := time.Now()
for i := 0; i<N; i++ {
go func() {
conn, _ := net.Dial("tcp", serverAddr)
fmt.Fprintf(conn, "GET sleep 1\n")
bufio.NewReader(conn).ReadString('\n')
waitgroup.Done()
}()
}
waitgroup.Wait()
end := time.Now()
elapsed := end.Sub(start)
if elapsed.Milliseconds() > 1100 {
t.Errorf("Execution time %v > 1 second \n", elapsed)
}
}
// Run N requests on one connection
func TestConcurrency2(t *testing.T) {
const N = 100
var waitgroup sync.WaitGroup
waitgroup.Add(N)
conn, _ := net.Dial("tcp", serverAddr)
reader := bufio.NewReader(conn)
start := time.Now()
for i := 0; i<N; i++ {
go func() {
fmt.Fprintf(conn, "GET sleep 1\n")
waitgroup.Done()
}()
}
waitgroup.Wait()
for i := 0; i<N; i++ {
reader.ReadString('\n')
}
end := time.Now()
elapsed := end.Sub(start)
if elapsed.Milliseconds() > 1100 {
t.Errorf("Execution time %v > 1 second \n", elapsed)
}
}
// Test different delimiter
func TestLongDelimiter(t *testing.T) {
conn, _ := net.Dial("tcp", "127.0.0.1:7778")
fmt.Fprintf(conn, "GET echo\r\r.\r\r")
message, _ := bufio.NewReader(conn).ReadString('\n')
if message != "My proto is CFCF.CFCF\n" {
t.Errorf("%#v != 'My proto is CFCF.CFCF'\n", message)
}
conn.Close()
}
// Test binary-text protocol
func TestBinaryTextProtocol(t *testing.T) {
// Send 10-bytes long request
var size int32 = 10;
conn, _ := net.Dial("tcp", "127.0.0.1:7779")
err := binary.Write(conn, binary.LittleEndian, size)
if err != nil {
fmt.Println("err:", err)
}
fmt.Fprintf(conn, "abcde12345")
message, _ := bufio.NewReader(conn).ReadString('\n')
if message != "Received `abcde12345` from you\n" {
t.Errorf("%#v != 'Received `abcde12345` from you\n", message)
}
conn.Close()
}
// Test binary-text protocol
func TestBinaryTextProtocolVeryLongKey(t *testing.T) {
// Create a string 32MB long
var key string = "x"
for i := 0; i<25; i++ {
key = key + key
}
var size int32 = int32(len(key))
conn, _ := net.Dial("tcp", "127.0.0.1:7779")
err := binary.Write(conn, binary.LittleEndian, size)
if err != nil {
fmt.Println("err:", err)
}
fmt.Fprintf(conn, key)
message, _ := bufio.NewReader(conn).ReadString('\n')
if len(message) != len(key) + len("Received `` from you\n") {
t.Errorf("Length of key %d != %d\n", len(key), len(key) + len("Received `` from you\n"))
}
}
type UserData struct {
mydata string
}
func worker(request string, userdata interface{}) string {
data := userdata.(*UserData)
if request == "GET ptr\n" {
return fmt.Sprintf("%p\n", &data.mydata)
}
if request == "GET data\n" {
return fmt.Sprintf("%s\n", data.mydata)
}
// Split string by spaces
fields := strings.Fields(request)
// GET sleep 2
if fields[1] == "sleep" {
seconds, _ := strconv.Atoi(fields[2])
time.Sleep(time.Duration(seconds) * time.Second)
}
return fmt.Sprintf("%s\n", strings.Join(fields[1:], " "))
}
func worker2(request string, userdata interface{}) string {
return fmt.Sprintf("My proto is CFCF.CFCF\n")
}
func worker3(request string, userdata interface{}) string {
return fmt.Sprintf("Received `%v` from you\n", request)
}
func TestMain(m *testing.M) {
// Run main test server
userdata := UserData{"my data"}
myDataPtr = fmt.Sprintf("%p\n", &userdata.mydata)
go RunServer(serverAddr, "\n", worker, &userdata)
// Run second test server
go RunServer("127.0.0.1:7778", "\r\r.\r\r", worker2, nil)
// Run third test server with binary-text protocol
go RunServer("127.0.0.1:7779", "", worker3, nil)
// Wait a bit to have tcp accepting loops ready
time.Sleep(100 * time.Millisecond)
// Run tests
code := m.Run()
os.Exit(code)
}