diff options
Diffstat (limited to 'unit_tests/scripts/lane/tasking_cancelling.lua')
-rw-r--r-- | unit_tests/scripts/lane/tasking_cancelling.lua | 112 |
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 @@ | |||
1 | local require_lanes_result_1, require_lanes_result_2 = require "lanes".configure(config).configure() | 1 | local require_fixture_result_1, require_fixture_result_2 = require "fixture" |
2 | local fixture = assert(require_fixture_result_1) | ||
3 | |||
4 | local require_lanes_result_1, require_lanes_result_2 = require "lanes".configure{on_state_create = fixture.on_state_create}.configure() | ||
2 | print("require_lanes_result:", require_lanes_result_1, require_lanes_result_2) | 5 | print("require_lanes_result:", require_lanes_result_1, require_lanes_result_2) |
3 | local lanes = require_lanes_result_1 | 6 | local lanes = require_lanes_result_1 |
4 | 7 | ||
@@ -15,69 +18,34 @@ local lanes_linda = assert(lanes.linda) | |||
15 | -- ################################################################################################## | 18 | -- ################################################################################################## |
16 | -- ################################################################################################## | 19 | -- ################################################################################################## |
17 | 20 | ||
18 | local function task(a, b, c) | 21 | -- cancellation of cooperating lanes |
19 | lane_threadname("task("..a..","..b..","..c..")") | 22 | local 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 |
27 | end | ||
28 | |||
29 | local gc_cb = function(name_, status_) | ||
30 | PRINT(" ---> lane '" .. name_ .. "' collected with status '" .. status_ .. "'") | ||
31 | end | 30 | end |
31 | -- soft and hard are behaviorally equivalent when no blocking linda operation is involved | ||
32 | local cooperative_lane_soft = lanes_gen("*", { name = 'auto' }, cooperative)() | ||
33 | local a, b = cooperative_lane_soft:cancel("soft", 0) -- issue request, do not wait for lane to terminate | ||
34 | assert(a == false and b == "timeout", "got " .. tostring(a) .. " " .. tostring(b)) | ||
35 | assert(cooperative_lane_soft[1] == "soft") -- return value of the lane body is the value returned by cancel_test() | ||
36 | local cooperative_lane_hard = lanes_gen("*", { name = 'auto' }, cooperative)() | ||
37 | local c, d = cooperative_lane_hard:cancel("hard", 0) -- issue request, do not wait for lane to terminate | ||
38 | assert(a == false and b == "timeout", "got " .. tostring(c) .. " " .. tostring(d)) | ||
39 | assert(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 | |||
37 | PRINT("\n\n", "---=== Tasking (cancelling) ===---", "\n\n") | ||
38 | |||
39 | local task_launch2 = lanes_gen("", { name = 'auto', globals={hey=true}, gc_cb = gc_cb }, task) | ||
40 | |||
41 | local N=999999999 | ||
42 | local lane9= task_launch2(1,N,1) -- huuuuuuge... | ||
43 | |||
44 | -- Wait until state changes "pending"->"running" | ||
45 | -- | ||
46 | local st | ||
47 | local t0= os.time() | ||
48 | while 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 | ||
52 | end | ||
53 | PRINT(" "..st) | ||
54 | |||
55 | if st=="error" then | ||
56 | local _= lane9[0] -- propagate the error here | ||
57 | end | ||
58 | if st=="done" then | ||
59 | error("Looping to "..N.." was not long enough (cannot test cancellation)") | ||
60 | end | ||
61 | assert(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 | ||
64 | lane9:cancel(jit and "line" or "count", 100) -- 0 timeout, hook triggers cancelslation when reaching the specified count | ||
65 | |||
66 | local t0= os.time() | ||
67 | while 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 | ||
71 | end | ||
72 | PRINT(" "..st) | ||
73 | assert(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 |
76 | local limited = lanes_linda("limited") | 44 | local limited = lanes_linda{name = "limited"} |
77 | assert.fails(function() limited:limit("key", -1) end) | 45 | assert.fails(function() limited:limit("key", -1) end) |
78 | assert.failsnot(function() limited:limit("key", 1) end) | 46 | assert.failsnot(function() limited:limit("key", 1) end) |
79 | -- [[################################################ | 47 | -- [[################################################ |
80 | limited:send("key", "hello") -- saturate linda | 48 | limited:send("key", "hello") -- saturate linda, so that subsequent sends will block |
81 | for k, v in pairs(limited:dump()) do | 49 | for k, v in pairs(limited:dump()) do |
82 | PRINT("limited[" .. tostring(k) .. "] = " .. tostring(v)) | 50 | PRINT("limited[" .. tostring(k) .. "] = " .. tostring(v)) |
83 | end | 51 | end |
@@ -88,11 +56,15 @@ local wait_send = function() | |||
88 | end | 56 | end |
89 | 57 | ||
90 | local wait_send_lane = lanes_gen("*", { name = 'auto' }, wait_send)() | 58 | local wait_send_lane = lanes_gen("*", { name = 'auto' }, wait_send)() |
91 | repeat until wait_send_lane.status == "waiting" | 59 | repeat |
92 | print "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) | ||
63 | until wait_send_lane.status == "waiting" | ||
64 | PRINT "wait_send_lane is waiting" | ||
93 | wait_send_lane:cancel() -- hard cancel, 0 timeout | 65 | wait_send_lane:cancel() -- hard cancel, 0 timeout |
94 | repeat until wait_send_lane.status == "cancelled" | 66 | repeat until wait_send_lane.status == "cancelled" |
95 | print "wait_send_lane is cancelled" | 67 | PRINT "wait_send_lane is cancelled" |
96 | --################################################]] | 68 | --################################################]] |
97 | local wait_receive = function() | 69 | local wait_receive = function() |
98 | local k, v | 70 | local k, v |
@@ -101,22 +73,30 @@ local wait_receive = function() | |||
101 | end | 73 | end |
102 | 74 | ||
103 | local wait_receive_lane = lanes_gen("*", { name = 'auto' }, wait_receive)() | 75 | local wait_receive_lane = lanes_gen("*", { name = 'auto' }, wait_receive)() |
104 | repeat until wait_receive_lane.status == "waiting" | 76 | repeat |
105 | print "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) | ||
80 | until wait_receive_lane.status == "waiting" | ||
81 | PRINT "wait_receive_lane is waiting" | ||
106 | wait_receive_lane:cancel() -- hard cancel, 0 timeout | 82 | wait_receive_lane:cancel() -- hard cancel, 0 timeout |
107 | repeat until wait_receive_lane.status == "cancelled" | 83 | repeat until wait_receive_lane.status == "cancelled" |
108 | print "wait_receive_lane is cancelled" | 84 | PRINT "wait_receive_lane is cancelled" |
109 | --################################################]] | 85 | --################################################]] |
110 | local wait_receive_batched = function() | 86 | local 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 |
114 | end | 90 | end |
115 | 91 | ||
116 | local wait_receive_batched_lane = lanes_gen("*", { name = 'auto' }, wait_receive_batched)() | 92 | local wait_receive_batched_lane = lanes_gen("*", { name = 'auto' }, wait_receive_batched)() |
117 | repeat until wait_receive_batched_lane.status == "waiting" | 93 | repeat |
118 | print "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) | ||
97 | until wait_receive_batched_lane.status == "waiting" | ||
98 | PRINT "wait_receive_batched_lane is waiting" | ||
119 | wait_receive_batched_lane:cancel() -- hard cancel, 0 timeout | 99 | wait_receive_batched_lane:cancel() -- hard cancel, 0 timeout |
120 | repeat until wait_receive_batched_lane.status == "cancelled" | 100 | repeat until wait_receive_batched_lane.status == "cancelled" |
121 | print "wait_receive_batched_lane is cancelled" | 101 | PRINT "wait_receive_batched_lane is cancelled" |
122 | --################################################]] | 102 | --################################################]] |