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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
|
# vim:set ft= ts=4 sw=4 et fdm=marker:
use Test::Nginx::Socket::Lua::Stream;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 3 + 2);
our $HtmlDir = html_dir;
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;
$ENV{TEST_NGINX_RESOLVER} ||= '8.8.8.8';
#log_level 'warn';
log_level 'debug';
no_long_string();
#no_diff();
run_tests();
__DATA__
=== TEST 1: pipelined memcached requests (sent one byte at a time)
--- stream_server_config
content_by_lua_block {
local sock = ngx.socket.tcp()
local port = $TEST_NGINX_MEMCACHED_PORT
local ok, err = sock:connect("127.0.0.1", port)
if not ok then
ngx.say("failed to connect: ", err)
return
end
ngx.say("connected: ", ok)
local req = "flush_all\r\nget foo\r\nget bar\r\n"
-- req = "OK"
local send_idx = 1
local function writer()
local sub = string.sub
while send_idx <= #req do
local bytes, err = sock:send(sub(req, send_idx, send_idx))
if not bytes then
ngx.say("failed to send request: ", err)
return
end
-- if send_idx % 2 == 0 then
ngx.sleep(0.001)
-- end
send_idx = send_idx + 1
end
-- ngx.say("request sent.")
end
local ok, err = ngx.thread.spawn(writer)
if not ok then
ngx.say("failed to spawn writer thread: ", err)
return
end
for i = 1, 3 do
local line, err, part = sock:receive()
if line then
ngx.say("received: ", line)
else
ngx.say("failed to receive a line: ", err, " [", part, "]")
break
end
end
ok, err = sock:setkeepalive()
ngx.say("setkeepalive: ", ok, " ", err)
}
--- config
server_tokens off;
--- stream_response
connected: 1
received: OK
received: END
received: END
setkeepalive: 1 nil
--- no_error_log
[error]
=== TEST 2: read timeout errors won't affect writing
--- stream_server_config
lua_socket_log_errors off;
content_by_lua_block {
local sock = ngx.socket.tcp()
local port = $TEST_NGINX_MEMCACHED_PORT
local ok, err = sock:connect("127.0.0.1", port)
if not ok then
ngx.say("failed to connect: ", err)
return
end
ngx.say("connected: ", ok)
local req = "flush_all\r\n"
-- req = "OK"
local send_idx = 1
sock:settimeout(1)
local function writer()
local sub = string.sub
while send_idx <= #req do
local bytes, err = sock:send(sub(req, send_idx, send_idx))
if not bytes then
ngx.say("failed to send request: ", err)
return
end
ngx.sleep(0.001)
send_idx = send_idx + 1
end
-- ngx.say("request sent.")
end
local ok, err = ngx.thread.spawn(writer)
if not ok then
ngx.say("failed to spawn writer thread: ", err)
return
end
local data = ""
local ntm = 0
local done = false
for i = 1, 300 do
local line, err, part = sock:receive()
if not line then
if part then
data = data .. part
end
if err ~= "timeout" then
ngx.say("failed to receive: ", err)
return
end
ntm = ntm + 1
else
data = data .. line
ngx.say("received: ", data)
done = true
break
end
end
if not done then
ngx.say("partial read: ", data)
end
ngx.say("read timed out: ", ntm)
ok, err = sock:close()
ngx.say("close: ", ok, " ", err)
}
--- config
server_tokens off;
--- stream_response_like chop
^connected: 1
(?:received: OK|failed to send request: timeout
partial read: )
read timed out: [1-9]\d*
close: 1 nil$
--- no_error_log
[error]
=== TEST 3: writes are rejected while reads are not
--- stream_server_config
lua_socket_log_errors off;
content_by_lua_block {
local sock = ngx.socket.tcp()
local port = $TEST_NGINX_RAND_PORT_1
local ok, err = sock:connect("127.0.0.1", port)
if not ok then
ngx.say("failed to connect: ", err)
return
end
ngx.say("connected: ", ok)
local req = "flush_all\r\n"
-- req = "OK"
local send_idx = 1
local function writer()
local sub = string.sub
while send_idx <= #req do
local bytes, err = sock:send(sub(req, send_idx, send_idx))
if not bytes then
ngx.say("failed to send request: ", err)
return
end
ngx.sleep(0.001)
send_idx = send_idx + 1
end
-- ngx.say("request sent.")
end
local ok, err = ngx.thread.spawn(writer)
if not ok then
ngx.say("failed to spawn writer thread: ", err)
return
end
local data = ""
local ntm = 0
local done = false
for i = 1, 3 do
local res, err, part = sock:receive(1)
if not res then
ngx.say("failed to receive: ", err)
return
else
data = data .. res
end
ngx.sleep(0.001)
end
ngx.say("received: ", data)
ok, err = sock:close()
ngx.say("close: ", ok, " ", err)
}
--- config
server_tokens off;
--- stream_response_like chop
^connected: 1
received: OK!
close: (?:nil socket busy writing|1 nil
failed to send request: closed)$
--- tcp_listen: $TEST_NGINX_RAND_PORT_1
--- tcp_shutdown: 0
--- tcp_reply: OK!
--- tcp_no_close: 1
--- no_error_log
[error]
=== TEST 4: reads are rejected while writes are not
--- stream_server_config
lua_socket_log_errors off;
content_by_lua_block {
local sock = ngx.socket.tcp()
local port = $TEST_NGINX_RAND_PORT_2
local ok, err = sock:connect("127.0.0.1", port)
if not ok then
ngx.say("failed to connect: ", err)
return
end
ngx.say("connected: ", ok)
local req = "flush_all\r\n"
-- req = "OK"
local send_idx = 1
local function writer()
local sub = string.sub
while send_idx <= #req do
local bytes, err = sock:send(sub(req, send_idx, send_idx))
if not bytes then
ngx.say("failed to send request: ", err)
return
end
-- ngx.say("sent: ", bytes)
ngx.sleep(0.001)
send_idx = send_idx + 1
end
ngx.say("request sent.")
local ok, err = sock:close()
ngx.say("close: ", ok, " ", err)
end
local ok, err = ngx.thread.spawn(writer)
if not ok then
ngx.say("failed to spawn writer thread: ", err)
return
end
local data = ""
local ntm = 0
local done = false
for i = 1, 3 do
local res, err, part = sock:receive(1)
if not res then
ngx.say("failed to receive: ", err)
return
else
data = data .. res
end
ngx.sleep(0.001)
end
ngx.say("received: ", data)
}
--- config
server_tokens off;
--- stream_response
connected: 1
failed to receive: closed
request sent.
close: 1 nil
--- stap2
F(ngx_http_lua_socket_tcp_finalize_write_part) {
print_ubacktrace()
}
--- stap_out2
--- tcp_listen: $TEST_NGINX_RAND_PORT_2
--- tcp_shutdown: 1
--- tcp_query eval: "flush_all\r\n"
--- tcp_query_len: 11
--- no_error_log
[error]
=== TEST 5: concurrent socket operations while connecting
--- stream_server_config
lua_socket_log_errors off;
resolver $TEST_NGINX_RESOLVER ipv6=off;
content_by_lua_block {
local sock = ngx.socket.tcp()
local function f()
ngx.sleep(0.001)
local res, err = sock:receive(1)
ngx.say("receive: ", res, " ", err)
local bytes, err = sock:send("hello")
ngx.say("send: ", bytes, " ", err)
local ok, err = sock:close()
ngx.say("close: ", ok, " ", err)
local ok, err = sock:getreusedtimes()
ngx.say("getreusedtimes: ", ok, " ", err)
local ok, err = sock:setkeepalive()
ngx.say("setkeepalive: ", ok, " ", err)
local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT)
ngx.say("connect: ", ok, " ", err)
end
local ok, err = ngx.thread.spawn(f)
if not ok then
ngx.say("failed to spawn writer thread: ", err)
return
end
sock:settimeout(300)
local ok, err = sock:connect("127.0.0.2", 12345)
ngx.say("connect: ", ok, " ", err)
local ok, err = sock:close()
ngx.say("close: ", ok, " ", err)
}
--- config
server_tokens off;
--- stream_response
receive: nil socket busy connecting
send: nil socket busy connecting
close: nil socket busy connecting
getreusedtimes: 0 nil
setkeepalive: nil socket busy connecting
connect: nil socket busy connecting
connect: nil timeout
close: nil closed
--- no_error_log
[error]
=== TEST 6: concurrent operations while resolving
--- stream_server_config
lua_socket_log_errors off;
resolver 127.0.0.2:12345;
resolver_timeout 300ms;
content_by_lua_block {
local sock = ngx.socket.tcp()
local function f()
ngx.sleep(0.001)
local res, err = sock:receive(1)
ngx.say("receive: ", res, " ", err)
local bytes, err = sock:send("hello")
ngx.say("send: ", bytes, " ", err)
local ok, err = sock:close()
ngx.say("close: ", ok, " ", err)
local ok, err = sock:getreusedtimes()
ngx.say("getreusedtimes: ", ok, " ", err)
local ok, err = sock:setkeepalive()
ngx.say("setkeepalive: ", ok, " ", err)
local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT)
ngx.say("connect: ", ok, " ", err)
end
local ok, err = ngx.thread.spawn(f)
if not ok then
ngx.say("failed to spawn writer thread: ", err)
return
end
sock:settimeout(300)
local ok, err = sock:connect("some2.agentzh.org", 80)
ngx.say("connect: ", ok, " ", err)
local ok, err = sock:close()
ngx.say("close: ", ok, " ", err)
}
--- config
server_tokens off;
--- stream_response
receive: nil closed
send: nil closed
close: nil closed
getreusedtimes: nil closed
setkeepalive: nil closed
connect: nil socket busy connecting
connect: nil some2.agentzh.org could not be resolved (110: Operation timed out)
close: nil closed
--- no_error_log
[error]
=== TEST 7: concurrent operations while reading (receive)
--- stream_server_config
lua_socket_log_errors off;
content_by_lua_block {
local sock = ngx.socket.tcp()
local ready = false
local function f()
while not ready do
ngx.sleep(0.001)
end
local res, err = sock:receive(1)
ngx.say("receive: ", res, " ", err)
local bytes, err = sock:send("flush_all")
ngx.say("send: ", bytes, " ", err)
local ok, err = sock:close()
ngx.say("close: ", ok, " ", err)
local ok, err = sock:getreusedtimes()
ngx.say("getreusedtimes: ", ok, " ", err)
local ok, err = sock:setkeepalive()
ngx.say("setkeepalive: ", ok, " ", err)
local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT)
ngx.say("connect: ", ok, " ", err)
end
local ok, err = ngx.thread.spawn(f)
if not ok then
ngx.say("failed to spawn writer thread: ", err)
return
end
sock:settimeout(300)
local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT)
ngx.say("connect: ", ok, " ", err)
ready = true
local res, err = sock:receive(1)
ngx.say("receive: ", res, " ", err)
local ok, err = sock:close()
ngx.say("close: ", ok, " ", err)
}
--- config
server_tokens off;
--- stream_response
connect: 1 nil
receive: nil socket busy reading
send: 9 nil
close: nil socket busy reading
getreusedtimes: 0 nil
setkeepalive: nil socket busy reading
connect: nil socket busy reading
receive: nil timeout
close: 1 nil
--- no_error_log
[error]
=== TEST 8: concurrent operations while reading (receiveuntil)
--- stream_server_config
lua_socket_log_errors off;
content_by_lua_block {
local ready = false
local sock = ngx.socket.tcp()
local function f()
while not ready do
ngx.sleep(0.001)
end
local res, err = sock:receive(1)
ngx.say("receive: ", res, " ", err)
local bytes, err = sock:send("flush_all")
ngx.say("send: ", bytes, " ", err)
local ok, err = sock:close()
ngx.say("close: ", ok, " ", err)
local ok, err = sock:getreusedtimes()
ngx.say("getreusedtimes: ", ok, " ", err)
local ok, err = sock:setkeepalive()
ngx.say("setkeepalive: ", ok, " ", err)
local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT)
ngx.say("connect: ", ok, " ", err)
end
local ok, err = ngx.thread.spawn(f)
if not ok then
ngx.say("failed to spawn writer thread: ", err)
return
end
sock:settimeout(300)
local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT)
ngx.say("connect: ", ok, " ", err)
ready = true
local it, err = sock:receiveuntil("\r\n")
if not it then
ngx.say("receiveuntil() failed: ", err)
return
end
local res, err = it()
ngx.say("receiveuntil() iterator: ", res, " ", err)
local ok, err = sock:close()
ngx.say("close: ", ok, " ", err)
}
--- config
server_tokens off;
--- stream_response
connect: 1 nil
receive: nil socket busy reading
send: 9 nil
close: nil socket busy reading
getreusedtimes: 0 nil
setkeepalive: nil socket busy reading
connect: nil socket busy reading
receiveuntil() iterator: nil timeout
close: 1 nil
--- no_error_log
[error]
|