aboutsummaryrefslogtreecommitdiff
path: root/unit_tests/scripts/coro/collect_yielded_lane.lua
diff options
context:
space:
mode:
Diffstat (limited to 'unit_tests/scripts/coro/collect_yielded_lane.lua')
-rw-r--r--unit_tests/scripts/coro/collect_yielded_lane.lua64
1 files changed, 64 insertions, 0 deletions
diff --git a/unit_tests/scripts/coro/collect_yielded_lane.lua b/unit_tests/scripts/coro/collect_yielded_lane.lua
new file mode 100644
index 0000000..2ee58f8
--- /dev/null
+++ b/unit_tests/scripts/coro/collect_yielded_lane.lua
@@ -0,0 +1,64 @@
1local fixture = require "fixture"
2local lanes = require "lanes".configure{on_state_create = fixture.on_state_create}
3
4local fixture = require "fixture"
5lanes.finally(fixture.throwing_finalizer)
6
7-- this test is only for Lua 5.4+
8local utils = lanes.require "_utils54"
9local PRINT = utils.MAKE_PRINT()
10
11local out_linda = lanes.linda()
12
13------------------------------------------------------------------------------
14-- TEST: to-be-closed variables are properly closed when the lane is collected
15------------------------------------------------------------------------------
16if true then
17 -- the generator
18 local coro_g = lanes.coro("*", utils.yielder_with_to_be_closed)
19
20 -- start the lane
21 local h = coro_g(out_linda)
22
23 -- join the lane. it should be done and give back the values resulting of the first yield point
24 local r, v1, v2 = h:join()
25 assert(r == true and v1 == "I yield!" and v2 == 1, "got " .. tostring(r) .. " " .. tostring(v1) .. " " .. tostring(v2))
26 assert(h.status == "done", "got " .. h.status)
27
28 -- force collection of the lane
29 h = nil
30 collectgarbage()
31
32 -- I want the to-be-closed variable of the coroutine linda to be properly closed
33 local s, r = out_linda:receive(0, "out")
34 assert(s == "out" and r == "Closed!", "coro got " .. tostring(s) .. " " .. tostring(r)) -- THIS TEST FAILS
35end
36
37---------------------------------------------------------------------------------------------------
38-- TEST: if a to-be-closed handler takes longer than the join timeout, everything works as expected
39---------------------------------------------------------------------------------------------------
40if true then
41 -- the generator
42 local coro_g = lanes.coro("*", utils.yielder_with_to_be_closed)
43
44 -- start the lane. The to-be-closed handler will sleep for 1 second
45 local h = coro_g(out_linda, 1)
46
47 -- first join attempt should timeout
48 local r, v = h:join(0.6)
49 assert(r == nil and v == "timeout", "got " .. tostring(r) .. " " .. tostring(v))
50 assert(h.status == "running", "got " .. h.status)
51
52 -- join the lane again. it should be done and give back the values resulting of the first yield point
53 local r, v1, v2 = h:join(0.6)
54 assert(r == true and v1 == "I yield!" and v2 == 1, "got " .. tostring(r) .. " " .. tostring(v1) .. " " .. tostring(v2))
55 assert(h.status == "done", "got " .. h.status)
56
57 -- force collection of the lane
58 h = nil
59 collectgarbage()
60
61 -- I want the to-be-closed variable of the coroutine linda to be properly closed
62 local s, r = out_linda:receive(0, "out")
63 assert(s == "out" and r == "Closed!", "coro got " .. tostring(s) .. " " .. tostring(r)) -- THIS TEST FAILS
64end