aboutsummaryrefslogtreecommitdiff
path: root/unit_tests/scripts/lane/tasking_cancelling.lua
diff options
context:
space:
mode:
Diffstat (limited to 'unit_tests/scripts/lane/tasking_cancelling.lua')
-rw-r--r--unit_tests/scripts/lane/tasking_cancelling.lua112
1 files changed, 46 insertions, 66 deletions
diff --git a/unit_tests/scripts/lane/tasking_cancelling.lua b/unit_tests/scripts/lane/tasking_cancelling.lua
index 85600ab..d153ffa 100644
--- a/unit_tests/scripts/lane/tasking_cancelling.lua
+++ b/unit_tests/scripts/lane/tasking_cancelling.lua
@@ -1,4 +1,7 @@
1local require_lanes_result_1, require_lanes_result_2 = require "lanes".configure(config).configure() 1local require_fixture_result_1, require_fixture_result_2 = require "fixture"
2local fixture = assert(require_fixture_result_1)
3
4local require_lanes_result_1, require_lanes_result_2 = require "lanes".configure{on_state_create = fixture.on_state_create}.configure()
2print("require_lanes_result:", require_lanes_result_1, require_lanes_result_2) 5print("require_lanes_result:", require_lanes_result_1, require_lanes_result_2)
3local lanes = require_lanes_result_1 6local lanes = require_lanes_result_1
4 7
@@ -15,69 +18,34 @@ local lanes_linda = assert(lanes.linda)
15-- ################################################################################################## 18-- ##################################################################################################
16-- ################################################################################################## 19-- ##################################################################################################
17 20
18local function task(a, b, c) 21-- cancellation of cooperating lanes
19 lane_threadname("task("..a..","..b..","..c..")") 22local cooperative = function()
20 --error "111" -- testing error messages 23 local fixture = assert(require "fixture")
21 assert(hey) 24 local which_cancel
22 local v=0 25 repeat
23 for i=a,b,c do 26 fixture.block_for(0.2)
24 v= v+i 27 which_cancel = cancel_test()
25 end 28 until which_cancel
26 return v, hey 29 return which_cancel
27end
28
29local gc_cb = function(name_, status_)
30 PRINT(" ---> lane '" .. name_ .. "' collected with status '" .. status_ .. "'")
31end 30end
31-- soft and hard are behaviorally equivalent when no blocking linda operation is involved
32local cooperative_lane_soft = lanes_gen("*", { name = 'auto' }, cooperative)()
33local a, b = cooperative_lane_soft:cancel("soft", 0) -- issue request, do not wait for lane to terminate
34assert(a == false and b == "timeout", "got " .. tostring(a) .. " " .. tostring(b))
35assert(cooperative_lane_soft[1] == "soft") -- return value of the lane body is the value returned by cancel_test()
36local cooperative_lane_hard = lanes_gen("*", { name = 'auto' }, cooperative)()
37local c, d = cooperative_lane_hard:cancel("hard", 0) -- issue request, do not wait for lane to terminate
38assert(a == false and b == "timeout", "got " .. tostring(c) .. " " .. tostring(d))
39assert(cooperative_lane_hard[1] == "hard") -- return value of the lane body is the value returned by cancel_test()
32 40
33-- ################################################################################################## 41-- ##################################################################################################
34-- ##################################################################################################
35-- ##################################################################################################
36
37PRINT("\n\n", "---=== Tasking (cancelling) ===---", "\n\n")
38
39local task_launch2 = lanes_gen("", { name = 'auto', globals={hey=true}, gc_cb = gc_cb }, task)
40
41local N=999999999
42local lane9= task_launch2(1,N,1) -- huuuuuuge...
43
44-- Wait until state changes "pending"->"running"
45--
46local st
47local t0= os.time()
48while os.time()-t0 < 5 do
49 st= lane9.status
50 io.stderr:write((i==1) and st.." " or '.')
51 if st~="pending" then break end
52end
53PRINT(" "..st)
54
55if st=="error" then
56 local _= lane9[0] -- propagate the error here
57end
58if st=="done" then
59 error("Looping to "..N.." was not long enough (cannot test cancellation)")
60end
61assert(st=="running", "st == " .. st)
62
63-- when running under luajit, the function is JIT-ed, and the instruction count isn't hit, so we need a different hook
64lane9:cancel(jit and "line" or "count", 100) -- 0 timeout, hook triggers cancelslation when reaching the specified count
65
66local t0= os.time()
67while os.time()-t0 < 5 do
68 st= lane9.status
69 io.stderr:write((i==1) and st.." " or '.')
70 if st~="running" then break end
71end
72PRINT(" "..st)
73assert(st == "cancelled", "st is '" .. st .. "' instead of 'cancelled'")
74 42
75-- cancellation of lanes waiting on a linda 43-- cancellation of lanes waiting on a linda
76local limited = lanes_linda("limited") 44local limited = lanes_linda{name = "limited"}
77assert.fails(function() limited:limit("key", -1) end) 45assert.fails(function() limited:limit("key", -1) end)
78assert.failsnot(function() limited:limit("key", 1) end) 46assert.failsnot(function() limited:limit("key", 1) end)
79-- [[################################################ 47-- [[################################################
80limited:send("key", "hello") -- saturate linda 48limited:send("key", "hello") -- saturate linda, so that subsequent sends will block
81for k, v in pairs(limited:dump()) do 49for k, v in pairs(limited:dump()) do
82 PRINT("limited[" .. tostring(k) .. "] = " .. tostring(v)) 50 PRINT("limited[" .. tostring(k) .. "] = " .. tostring(v))
83end 51end
@@ -88,11 +56,15 @@ local wait_send = function()
88end 56end
89 57
90local wait_send_lane = lanes_gen("*", { name = 'auto' }, wait_send)() 58local wait_send_lane = lanes_gen("*", { name = 'auto' }, wait_send)()
91repeat until wait_send_lane.status == "waiting" 59repeat
92print "wait_send_lane is waiting" 60 io.stderr:write('!')
61 -- currently mingw64 builds can deadlock if we cancel the lane too early (before the linda blocks, at it causes the linda condvar not to be signalled)
62 lanes.sleep(0.1)
63until wait_send_lane.status == "waiting"
64PRINT "wait_send_lane is waiting"
93wait_send_lane:cancel() -- hard cancel, 0 timeout 65wait_send_lane:cancel() -- hard cancel, 0 timeout
94repeat until wait_send_lane.status == "cancelled" 66repeat until wait_send_lane.status == "cancelled"
95print "wait_send_lane is cancelled" 67PRINT "wait_send_lane is cancelled"
96--################################################]] 68--################################################]]
97local wait_receive = function() 69local wait_receive = function()
98 local k, v 70 local k, v
@@ -101,22 +73,30 @@ local wait_receive = function()
101end 73end
102 74
103local wait_receive_lane = lanes_gen("*", { name = 'auto' }, wait_receive)() 75local wait_receive_lane = lanes_gen("*", { name = 'auto' }, wait_receive)()
104repeat until wait_receive_lane.status == "waiting" 76repeat
105print "wait_receive_lane is waiting" 77 io.stderr:write('!')
78 -- currently mingw64 builds can deadlock if we cancel the lane too early (before the linda blocks, at it causes the linda condvar not to be signalled)
79 lanes.sleep(0.1)
80until wait_receive_lane.status == "waiting"
81PRINT "wait_receive_lane is waiting"
106wait_receive_lane:cancel() -- hard cancel, 0 timeout 82wait_receive_lane:cancel() -- hard cancel, 0 timeout
107repeat until wait_receive_lane.status == "cancelled" 83repeat until wait_receive_lane.status == "cancelled"
108print "wait_receive_lane is cancelled" 84PRINT "wait_receive_lane is cancelled"
109--################################################]] 85--################################################]]
110local wait_receive_batched = function() 86local wait_receive_batched = function()
111 local k, v1, v2 87 local k, v1, v2
112 set_finalizer(function() print("wait_receive_batched", k, v1, v2) end) 88 set_finalizer(function() print("wait_receive_batched", k, v1, v2) end)
113 k, v1, v2 = limited:receive(limited.batched, "dummy", 2) -- infinite timeout, returns only when lane is cancelled 89 k, v1, v2 = limited:receive_batched("dummy", 2) -- infinite timeout, returns only when lane is cancelled
114end 90end
115 91
116local wait_receive_batched_lane = lanes_gen("*", { name = 'auto' }, wait_receive_batched)() 92local wait_receive_batched_lane = lanes_gen("*", { name = 'auto' }, wait_receive_batched)()
117repeat until wait_receive_batched_lane.status == "waiting" 93repeat
118print "wait_receive_batched_lane is waiting" 94 io.stderr:write('!')
95 -- currently mingw64 builds can deadlock if we cancel the lane too early (before the linda blocks, at it causes the linda condvar not to be signalled)
96 lanes.sleep(0.1)
97until wait_receive_batched_lane.status == "waiting"
98PRINT "wait_receive_batched_lane is waiting"
119wait_receive_batched_lane:cancel() -- hard cancel, 0 timeout 99wait_receive_batched_lane:cancel() -- hard cancel, 0 timeout
120repeat until wait_receive_batched_lane.status == "cancelled" 100repeat until wait_receive_batched_lane.status == "cancelled"
121print "wait_receive_batched_lane is cancelled" 101PRINT "wait_receive_batched_lane is cancelled"
122--################################################]] 102--################################################]]