diff options
Diffstat (limited to 'unit_tests/scripts/lane/tasking_cancelling_with_hook.lua')
-rw-r--r-- | unit_tests/scripts/lane/tasking_cancelling_with_hook.lua | 68 |
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 @@ | |||
1 | local require_lanes_result_1, require_lanes_result_2 = require "lanes".configure(config).configure() | ||
2 | print("require_lanes_result:", require_lanes_result_1, require_lanes_result_2) | ||
3 | local lanes = require_lanes_result_1 | ||
4 | |||
5 | local require_assert_result_1, require_assert_result_2 = require "_assert" | ||
6 | print("require_assert_result:", require_assert_result_1, require_assert_result_2) | ||
7 | |||
8 | local utils = lanes.require "_utils" | ||
9 | local PRINT = utils.MAKE_PRINT() | ||
10 | |||
11 | -- ################################################################################################## | ||
12 | -- ################################################################################################## | ||
13 | -- ################################################################################################## | ||
14 | |||
15 | local 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 | ||
24 | end | ||
25 | |||
26 | local gc_cb = function(name_, status_) | ||
27 | PRINT(" ---> lane '" .. name_ .. "' collected with status '" .. status_ .. "'") | ||
28 | end | ||
29 | |||
30 | -- ################################################################################################## | ||
31 | -- ################################################################################################## | ||
32 | -- ################################################################################################## | ||
33 | |||
34 | local generator = lanes.gen("", { name = 'auto', globals={hey=true}, gc_cb = gc_cb }, task) | ||
35 | |||
36 | local N = 999999999 | ||
37 | local lane_h = generator(1,N,1) -- huuuuuuge... | ||
38 | |||
39 | -- Wait until state changes "pending"->"running" | ||
40 | -- | ||
41 | local st | ||
42 | local t0 = os.time() | ||
43 | while 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 | ||
47 | end | ||
48 | PRINT(" "..st) | ||
49 | |||
50 | if st == "error" then | ||
51 | local _ = lane_h[0] -- propagate the error here | ||
52 | end | ||
53 | if st == "done" then | ||
54 | error("Looping to "..N.." was not long enough (cannot test cancellation)") | ||
55 | end | ||
56 | assert(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 | ||
59 | lane_h:cancel(jit and "line" or "count", 100) -- 0 timeout, hook triggers cancelslation when reaching the specified count | ||
60 | |||
61 | local t0 = os.time() | ||
62 | while 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 | ||
66 | end | ||
67 | PRINT(" "..st) | ||
68 | assert(st == "cancelled", "st is '" .. st .. "' instead of 'cancelled'") | ||