-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy path091-coroutine.t
1751 lines (1493 loc) · 37.5 KB
/
091-coroutine.t
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# vim:set ft= ts=4 sw=4 et fdm=marker:
use Test::Nginx::Socket::Lua;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 3 + 14);
$ENV{TEST_NGINX_RESOLVER} ||= '8.8.8.8';
our $StapScript = <<'_EOC_';
global ids, cur
function gen_id(k) {
if (ids[k]) return ids[k]
ids[k] = ++cur
return cur
}
F(ngx_http_handler) {
delete ids
cur = 0
}
/*
F(ngx_http_lua_run_thread) {
id = gen_id($ctx->cur_co)
printf("run thread %d\n", id)
}
probe process("/usr/local/openresty-debug/luajit/lib/libluajit-5.1.so.2").function("lua_resume") {
id = gen_id($L)
printf("lua resume %d\n", id)
}
*/
M(http-lua-user-coroutine-resume) {
p = gen_id($arg2)
c = gen_id($arg3)
printf("resume %x in %x\n", c, p)
}
M(http-lua-thread-yield) {
println("thread yield")
}
/*
F(ngx_http_lua_coroutine_yield) {
printf("yield %x\n", gen_id($L))
}
*/
M(http-lua-user-coroutine-yield) {
p = gen_id($arg2)
c = gen_id($arg3)
printf("yield %x in %x\n", c, p)
}
F(ngx_http_lua_atpanic) {
printf("lua atpanic(%d):", gen_id($L))
print_ubacktrace();
}
M(http-lua-user-coroutine-create) {
p = gen_id($arg2)
c = gen_id($arg3)
printf("create %x in %x\n", c, p)
}
F(ngx_http_lua_ngx_exec) { println("exec") }
F(ngx_http_lua_ngx_exit) { println("exit") }
F(ngx_http_lua_ffi_exit) { println("exit") }
_EOC_
no_shuffle();
no_long_string();
run_tests();
__DATA__
=== TEST 1: basic coroutine print
--- config
location /lua {
content_by_lua '
local cc, cr, cy = coroutine.create, coroutine.resume, coroutine.yield
local function f()
local cnt = 0
for i = 1, 20 do
ngx.say("Hello, ", cnt)
cy()
cnt = cnt + 1
end
end
local c = cc(f)
for i=1,3 do
cr(c)
ngx.say("***")
end
';
}
--- request
GET /lua
--- stap2 eval: $::StapScript
--- response_body
Hello, 0
***
Hello, 1
***
Hello, 2
***
--- no_error_log
[error]
=== TEST 2: basic coroutine2
--- config
location /lua {
content_by_lua '
local function f(fid)
local cnt = 0
while true do
ngx.say("cc", fid, ": ", cnt)
coroutine.yield()
cnt = cnt + 1
end
end
local ccs = {}
for i=1,3 do
ccs[#ccs+1] = coroutine.create(function() f(i) end)
end
for i=1,9 do
local cc = table.remove(ccs, 1)
coroutine.resume(cc)
ccs[#ccs+1] = cc
end
';
}
--- request
GET /lua
--- response_body
cc1: 0
cc2: 0
cc3: 0
cc1: 1
cc2: 1
cc3: 1
cc1: 2
cc2: 2
cc3: 2
--- no_error_log
[error]
=== TEST 3: basic coroutine and cosocket
access the public network is unstable, need a bigger timeout
--- quic_max_idle_timeout: 4
--- config
resolver $TEST_NGINX_RESOLVER ipv6=off;
location /lua {
content_by_lua '
local function worker(url)
local sock = ngx.socket.tcp()
local ok, err = sock:connect(url, 80)
coroutine.yield()
if not ok then
ngx.say("failed to connect to: ", url, " error: ", err)
return
end
coroutine.yield()
ngx.say("successfully connected to: ", url)
sock:close()
end
local urls = {
"agentzh.org",
"openresty.com",
"openresty.org"
}
local ccs = {}
for i, url in ipairs(urls) do
local cc = coroutine.create(function() worker(url) end)
ccs[#ccs+1] = cc
end
while true do
if #ccs == 0 then break end
local cc = table.remove(ccs, 1)
local ok = coroutine.resume(cc)
if ok then
ccs[#ccs+1] = cc
end
end
ngx.say("*** All Done ***")
';
}
--- request
GET /lua
--- response_body
successfully connected to: agentzh.org
successfully connected to: openresty.com
successfully connected to: openresty.org
*** All Done ***
--- no_error_log
[error]
--- timeout: 10
=== TEST 4: coroutine.wrap(generate prime numbers)
--- config
location /lua {
content_by_lua '
-- generate all the numbers from 2 to n
local function gen (n)
return coroutine.wrap(function ()
for i=2,n do coroutine.yield(i) end
end)
end
-- filter the numbers generated by g, removing multiples of p
local function filter (p, g)
return coroutine.wrap(function ()
while 1 do
local n = g()
if n == nil then return end
if math.fmod(n, p) ~= 0 then coroutine.yield(n) end
end
end)
end
local N = 10
local x = gen(N) -- generate primes up to N
while 1 do
local n = x() -- pick a number until done
if n == nil then break end
ngx.say(n) -- must be a prime number
x = filter(n, x) -- now remove its multiples
end
';
}
--- request
GET /lua
--- response_body
2
3
5
7
--- no_error_log
[error]
=== TEST 5: coroutine.wrap(generate prime numbers,reset create and resume)
--- config
location /lua {
content_by_lua '
coroutine.create = nil
coroutine.resume = nil
-- generate all the numbers from 2 to n
local function gen (n)
return coroutine.wrap(function ()
for i=2,n do coroutine.yield(i) end
end)
end
-- filter the numbers generated by g, removing multiples of p
local function filter (p, g)
return coroutine.wrap(function ()
while 1 do
local n = g()
if n == nil then return end
if math.fmod(n, p) ~= 0 then coroutine.yield(n) end
end
end)
end
local N = 10
local x = gen(N) -- generate primes up to N
while 1 do
local n = x() -- pick a number until done
if n == nil then break end
ngx.say(n) -- must be a prime number
x = filter(n, x) -- now remove its multiples
end
';
}
--- request
GET /lua
--- response_body
2
3
5
7
--- no_error_log
[error]
=== TEST 6: coroutine.wrap(generate fib)
--- config
location /lua {
content_by_lua '
local function generatefib (n)
return coroutine.wrap(function ()
local a,b = 1, 1
while a <= n do
coroutine.yield(a)
a, b = b, a+b
end
end)
end
-- In lua, because OP_TFORLOOP uses luaD_call to execute the iterator function,
-- and luaD_call is a C function, so we can not yield in the iterator function.
-- So the following case(using for loop) will be failed.
-- Luajit is OK.
if package.loaded["jit"] then
for i in generatefib(1000) do ngx.say(i) end
else
local gen = generatefib(1000)
while true do
local i = gen()
if not i then break end
ngx.say(i)
end
end
';
}
--- request
GET /lua
--- response_body
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
--- no_error_log
[error]
=== TEST 7: coroutine wrap and cosocket
--- config
resolver $TEST_NGINX_RESOLVER ipv6=off;
location /lua {
content_by_lua '
local function worker(url)
local sock = ngx.socket.tcp()
local ok, err = sock:connect(url, 80)
coroutine.yield()
if not ok then
ngx.say("failed to connect to: ", url, " error: ", err)
return
end
coroutine.yield()
ngx.say("successfully connected to: ", url)
sock:close()
end
local urls = {
"agentzh.org",
"openresty.com",
"openresty.org"
}
local cfs = {}
for i, url in ipairs(urls) do
local cf = coroutine.wrap(function() worker(url) end)
cfs[#cfs+1] = cf
end
for i=1,3 do cfs[i]() end
for i=1,3 do cfs[i]() end
for i=1,3 do cfs[i]() end
ngx.say("*** All Done ***")
';
}
--- request
GET /lua
--- response_body
successfully connected to: agentzh.org
successfully connected to: openresty.com
successfully connected to: openresty.org
*** All Done ***
--- no_error_log
[error]
--- timeout: 10
=== TEST 8: coroutine status, running
--- config
location /lua {
content_by_lua '
local cc, cr, cy = coroutine.create, coroutine.resume, coroutine.yield
local st, rn = coroutine.status, coroutine.running
local function f(self)
local cnt = 0
if rn() ~= self then ngx.say("error"); return end
ngx.say("running: ", st(self)) --running
cy()
local c = cc(function(father)
ngx.say("normal: ", st(father))
end) -- normal
cr(c, self)
end
local c = cc(f)
ngx.say("suspended: ", st(c)) -- suspended
cr(c, c)
ngx.say("suspended: ", st(c)) -- suspended
cr(c, c)
ngx.say("dead: ", st(c)) -- dead
';
}
--- request
GET /lua
--- response_body
suspended: suspended
running: running
suspended: suspended
normal: normal
dead: dead
--- no_error_log
[error]
=== TEST 9: entry coroutine yielded will be resumed immediately
--- config
location /lua {
content_by_lua '
ngx.say("[", {coroutine.yield()}, "]")
ngx.say("[", {coroutine.yield(1, "a")}, "]")
ngx.say("done")
';
}
--- request
GET /lua
--- response_body
[]
[]
done
--- no_error_log
[error]
=== TEST 10: thread traceback (multi-thread)
Note: only coroutine.wrap propagates errors to the parent coroutine
(and thus produces a traceback)
--- config
location /lua {
content_by_lua_block {
local f = function(cr) coroutine.resume(cr) end
-- emit a error
local g = function() unknown.unknown = 1 end
local l1 = coroutine.wrap(f)
local l2 = coroutine.wrap(g)
local l3 = coroutine.wrap(function() l1(l2) end)
l3()
ngx.say("hello")
}
}
--- request
GET /lua
--- error_code: 500
--- response_body_unlike
hello
--- error_log eval
["stack traceback:", "coroutine 0:", "coroutine 1:", "coroutine 2:"]
=== TEST 11: thread traceback (only the entry thread)
--- config
location /lua {
content_by_lua '
-- emit a error
unknown.unknown = 1
ngx.say("hello")
';
}
--- request
GET /lua
--- error_code: 500
--- error_log eval
["stack traceback:", "coroutine 0:"]
=== TEST 12: bug: resume dead coroutine with args
--- config
location /lua {
content_by_lua '
local function print(...)
local args = {...}
local is_first = true
for i,v in ipairs(args) do
if is_first then
is_first = false
else
ngx.print(" ")
end
ngx.print(v)
end
ngx.print("\\\n")
end
local function foo (a)
print("foo", a)
return coroutine.yield(2*a)
end
local co = coroutine.create(function (a,b)
print("co-body", a, b)
local r = foo(a+1)
print("co-body", r)
local r, s = coroutine.yield(a+b, a-b)
print("co-body", r, s)
return b, "end"
end)
print("main", coroutine.resume(co, 1, 10))
print("main", coroutine.resume(co, "r"))
print("main", coroutine.resume(co, "x", "y"))
print("main", coroutine.resume(co, "x", "y"))
';
}
--- request
GET /lua
--- response_body
co-body 1 10
foo 2
main true 4
co-body r
main true 11 -9
co-body x y
main true 10 end
main false cannot resume dead coroutine
--- no_error_log
[error]
=== TEST 13: deeply nested coroutines
--- config
location /lua {
content_by_lua '
local create = coroutine.create
local resume = coroutine.resume
local yield = coroutine.yield
local g
local function f()
ngx.say("f begin")
yield()
local c2 = create(g)
ngx.say("1: resuming c2")
resume(c2)
ngx.say("2: resuming c2")
resume(c2)
yield()
ngx.say("3: resuming c2")
resume(c2)
ngx.say("f done")
end
function g()
ngx.say("g begin")
yield()
ngx.say("g going")
yield()
ngx.say("g done")
end
local c1 = create(f)
ngx.say("1: resuming c1")
resume(c1)
ngx.say("2: resuming c1")
resume(c1)
ngx.say("3: resuming c1")
resume(c1)
ngx.say("main done")
';
}
--- request
GET /lua
--- response_body
1: resuming c1
f begin
2: resuming c1
1: resuming c2
g begin
2: resuming c2
g going
3: resuming c1
3: resuming c2
g done
f done
main done
--- no_error_log
[error]
=== TEST 14: using ngx.exit in user coroutines
--- config
location /lua {
content_by_lua '
local create = coroutine.create
local resume = coroutine.resume
local yield = coroutine.yield
local code = 400
local g
local function f()
local c2 = create(g)
yield()
code = code + 1
resume(c2)
yield()
resume(c2)
end
function g()
code = code + 1
yield()
code = code + 1
ngx.exit(code)
end
local c1 = create(f)
resume(c1)
resume(c1)
resume(c1)
ngx.say("done")
';
}
--- request
GET /lua
--- stap eval: $::StapScript
--- stap_out
create 2 in 1
resume 2 in 1
create 3 in 2
yield 2 in 1
resume 2 in 1
resume 3 in 2
yield 3 in 2
yield 2 in 1
resume 2 in 1
resume 3 in 2
exit
--- response_body_like: 403 Forbidden
--- error_code: 403
--- no_error_log
[error]
=== TEST 15: using ngx.exec in user coroutines
--- config
location /lua {
content_by_lua '
local create = coroutine.create
local resume = coroutine.resume
local yield = coroutine.yield
local code = 0
local g
local function f()
local c2 = create(g)
yield()
code = code + 1
resume(c2)
yield()
resume(c2)
end
function g()
code = code + 1
yield()
code = code + 1
ngx.exec("/n/" .. code)
end
local c1 = create(f)
resume(c1)
resume(c1)
resume(c1)
ngx.say("done")
';
}
location ~ '^/n/(\d+)' {
echo "num: $1";
}
--- stap eval: $::StapScript
--- stap_out
create 2 in 1
resume 2 in 1
create 3 in 2
yield 2 in 1
resume 2 in 1
resume 3 in 2
yield 3 in 2
yield 2 in 1
resume 2 in 1
resume 3 in 2
exec
--- request
GET /lua
--- response_body
num: 3
--- no_error_log
[error]
=== TEST 16: coroutine.create in header_filter_by_lua
--- config
location /lua {
echo hello;
header_filter_by_lua '
local function f()
yield()
end
local c1 = coroutine.create(f)
ngx.say("done")
';
}
--- request
GET /lua
--- ignore_response
--- error_log
API disabled in the context of header_filter_by_lua*
--- curl_error eval
qr/curl: \(52\) Empty reply from server|curl: \(92\) HTTP\/2 stream 1 was not closed cleanly|curl: \(95\) HTTP\/3 stream 0 reset by server/
=== TEST 17: resume coroutines from within another one that is not its parent
--- config
location /t {
content_by_lua '
local print = ngx.say
local c1, c2
local function f()
print("f 1")
print(coroutine.resume(c2))
print("f 2")
end
local function g()
print("g 1")
-- print(coroutine.resume(c1))
print("g 2")
end
c1 = coroutine.create(f)
c2 = coroutine.create(g)
coroutine.resume(c1)
';
}
--- request
GET /t
--- response_body
f 1
g 1
g 2
true
f 2
--- no_error_log
[error]
=== TEST 18: infinite recursive calls of coroutine.resume
--- config
location /t {
content_by_lua '
local print = ngx.say
local c1, c2
local function f()
print("f 1")
print(coroutine.resume(c2))
print("f 2")
end
local function g()
print("g 1")
print(coroutine.resume(c1))
print("g 2")
end
c1 = coroutine.create(f)
c2 = coroutine.create(g)
coroutine.resume(c1)
';
}
--- request
GET /t
--- stap2 eval: $::StapScript
--- response_body
f 1
g 1
falsecannot resume normal coroutine
g 2
true
f 2
--- no_error_log
[error]
=== TEST 19: resume running (entry) coroutines
--- config
location /t {
content_by_lua '
ngx.say(coroutine.status(coroutine.running()))
ngx.say(coroutine.resume(coroutine.running()))
';
}
--- request
GET /t
--- response_body
running
falsecannot resume running coroutine
--- no_error_log
[error]
=== TEST 20: resume running (user) coroutines
--- config
location /t {
content_by_lua '
local co
local function f()
ngx.say("f: ", coroutine.status(co))
ngx.say("f: ", coroutine.resume(co))
end
co = coroutine.create(f)
ngx.say("chunk: ", coroutine.status(co))
ngx.say("chunk: ", coroutine.resume(co))
';
}
--- request
GET /t
--- response_body
chunk: suspended
f: running
f: falsecannot resume running coroutine
chunk: true
--- no_error_log
[error]
=== TEST 21: user coroutine end with errors, and the parent coroutine gets the right status
--- config
location /t {
content_by_lua '
local co
local function f()
error("bad")
end
co = coroutine.create(f)
ngx.say("child: resume: ", coroutine.resume(co))
ngx.say("child: status: ", coroutine.status(co))
ngx.say("parent: status: ", coroutine.status(coroutine.running()))
';
}
--- request
GET /t
--- response_body eval
qr/^child: resume: falsecontent_by_lua\(nginx\.conf:\d+\):4: bad
child: status: dead
parent: status: running
$/s
--- no_error_log
[error]
=== TEST 22: entry coroutine is yielded by hand and still gets the right status
--- config
location /t {
content_by_lua '
local co = coroutine.running()
ngx.say("status: ", coroutine.status(co))
coroutine.yield(co)
ngx.say("status: ", coroutine.status(co))
';
}
--- request
GET /t
--- response_body
status: running
status: running
--- no_error_log
[error]
=== TEST 23: github issue #208: coroutine as iterator doesn't work
--- config
location = /t {
content_by_lua '
local say = ngx.say
local wrap, yield = coroutine.wrap, coroutine.yield
local function it(it_state)
for i = 1, it_state.i do
yield(it_state.path, tostring(i))
end
return nil
end
local function it_factory(path)
local it_state = { i = 10, path = path }
return wrap(it), it_state
end
--[[
for path, value in it_factory("test") do
say(path, value)
end
]]
do
local f, s, var = it_factory("test")
while true do
local path, value = f(s, var)
var = path
if var == nil then break end
say(path, value)
end
end
';
}
--- request
GET /t
--- more_headers
Cookie: abc=32
--- stap2 eval: $::StapScript
--- response_body
test1
test2
test3
test4
test5
test6
test7
test8
test9
test10
--- no_error_log
[error]
=== TEST 24: init_by_lua + our own coroutines in content_by_lua
--- http_config
init_by_lua 'return';
--- config