aboutsummaryrefslogtreecommitdiff
path: root/unit_tests/scripts/lane/tasking_cancelling_with_hook.lua
diff options
context:
space:
mode:
Diffstat (limited to 'unit_tests/scripts/lane/tasking_cancelling_with_hook.lua')
-rw-r--r--unit_tests/scripts/lane/tasking_cancelling_with_hook.lua68
1 files changed, 68 insertions, 0 deletions
diff --git a/unit_tests/scripts/lane/tasking_cancelling_with_hook.lua b/unit_tests/scripts/lane/tasking_cancelling_with_hook.lua
new file mode 100644
index 0000000..56b934f
--- /dev/null
+++ b/unit_tests/scripts/lane/tasking_cancelling_with_hook.lua
@@ -0,0 +1,68 @@
1local require_lanes_result_1, require_lanes_result_2 = require "lanes".configure(config).configure()
2print("require_lanes_result:", require_lanes_result_1, require_lanes_result_2)
3local lanes = require_lanes_result_1
4
5local require_assert_result_1, require_assert_result_2 = require "_assert"
6print("require_assert_result:", require_assert_result_1, require_assert_result_2)
7
8local utils = lanes.require "_utils"
9local PRINT = utils.MAKE_PRINT()
10
11-- ##################################################################################################
12-- ##################################################################################################
13-- ##################################################################################################
14
15local function task(a, b, c)
16 lane_threadname("task("..a..","..b..","..c..")")
17 --error "111" -- testing error messages
18 assert(hey)
19 local v=0
20 for i=a,b,c do
21 v= v+i
22 end
23 return v, hey
24end
25
26local gc_cb = function(name_, status_)
27 PRINT(" ---> lane '" .. name_ .. "' collected with status '" .. status_ .. "'")
28end
29
30-- ##################################################################################################
31-- ##################################################################################################
32-- ##################################################################################################
33
34local generator = lanes.gen("", { name = 'auto', globals={hey=true}, gc_cb = gc_cb }, task)
35
36local N = 999999999
37local lane_h = generator(1,N,1) -- huuuuuuge...
38
39-- Wait until state changes "pending"->"running"
40--
41local st
42local t0 = os.time()
43while os.time()-t0 < 5 do
44 st = lane_h.status
45 io.stderr:write((i==1) and st.." " or '.')
46 if st~="pending" then break end
47end
48PRINT(" "..st)
49
50if st == "error" then
51 local _ = lane_h[0] -- propagate the error here
52end
53if st == "done" then
54 error("Looping to "..N.." was not long enough (cannot test cancellation)")
55end
56assert(st == "running", "st == " .. st)
57
58-- when running under luajit, the function is JIT-ed, and the instruction count isn't hit, so we need a different hook
59lane_h:cancel(jit and "line" or "count", 100) -- 0 timeout, hook triggers cancelslation when reaching the specified count
60
61local t0 = os.time()
62while os.time()-t0 < 5 do
63 st = lane_h.status
64 io.stderr:write((i==1) and st.." " or '.')
65 if st~="running" then break end
66end
67PRINT(" "..st)
68assert(st == "cancelled", "st is '" .. st .. "' instead of 'cancelled'")