aboutsummaryrefslogtreecommitdiff
path: root/unit_tests
diff options
context:
space:
mode:
Diffstat (limited to 'unit_tests')
-rw-r--r--unit_tests/scripts/_utils.lua2
-rw-r--r--unit_tests/scripts/_utils54.lua2
-rw-r--r--unit_tests/scripts/lane/cooperative_shutdown.lua2
-rw-r--r--unit_tests/scripts/lane/tasking_cancelling.lua16
4 files changed, 15 insertions, 7 deletions
diff --git a/unit_tests/scripts/_utils.lua b/unit_tests/scripts/_utils.lua
index 9f46237..365fd10 100644
--- a/unit_tests/scripts/_utils.lua
+++ b/unit_tests/scripts/_utils.lua
@@ -76,7 +76,7 @@ local yield_one_by_one = function(...)
76 local _val = select(_i, ...) 76 local _val = select(_i, ...)
77 PRINT("yielding #", _i, _val) 77 PRINT("yielding #", _i, _val)
78 local _ack = coroutine.yield(_val) 78 local _ack = coroutine.yield(_val)
79 if cancel_test and cancel_test() then -- cancel_test does not exist when run immediately (not in a Lane) 79 if cancel_test and cancel_test(true) then -- cancel_test does not exist when run immediately (not in a Lane)
80 return "cancelled!" 80 return "cancelled!"
81 end 81 end
82 -- of course, if we are cancelled, we were not resumed, and yield() didn't return what we expect 82 -- of course, if we are cancelled, we were not resumed, and yield() didn't return what we expect
diff --git a/unit_tests/scripts/_utils54.lua b/unit_tests/scripts/_utils54.lua
index a511563..629f5c5 100644
--- a/unit_tests/scripts/_utils54.lua
+++ b/unit_tests/scripts/_utils54.lua
@@ -20,7 +20,7 @@ utils.yielder_with_to_be_closed = function(out_linda_, wait_)
20 local n = 1 20 local n = 1
21 while true do 21 while true do
22 coroutine.yield("I yield!", n) 22 coroutine.yield("I yield!", n)
23 if cancel_test and cancel_test() then -- cancel_test does not exist when run immediately (not in a Lane) 23 if cancel_test and cancel_test(true) then -- cancel_test does not exist when run immediately (not in a Lane)
24 return "I am cancelled" 24 return "I am cancelled"
25 end 25 end
26 n = n + 1 26 n = n + 1
diff --git a/unit_tests/scripts/lane/cooperative_shutdown.lua b/unit_tests/scripts/lane/cooperative_shutdown.lua
index 0a0943e..3329e9d 100644
--- a/unit_tests/scripts/lane/cooperative_shutdown.lua
+++ b/unit_tests/scripts/lane/cooperative_shutdown.lua
@@ -7,7 +7,7 @@ local lane1 = function()
7 -- loop breaks on soft cancellation request 7 -- loop breaks on soft cancellation request
8 repeat 8 repeat
9 lanes.sleep(0) 9 lanes.sleep(0)
10 until cancel_test() 10 until cancel_test(true)
11 print "lane1 cancelled" 11 print "lane1 cancelled"
12end 12end
13 13
diff --git a/unit_tests/scripts/lane/tasking_cancelling.lua b/unit_tests/scripts/lane/tasking_cancelling.lua
index 395bee5..f7ad729 100644
--- a/unit_tests/scripts/lane/tasking_cancelling.lua
+++ b/unit_tests/scripts/lane/tasking_cancelling.lua
@@ -29,24 +29,32 @@ local no_cancel_result = no_cancel_lane[1]
29assert(no_cancel_result == false, "cancel_test() should return boolean false, got " .. type(no_cancel_result) .. ": " .. tostring(no_cancel_result)) 29assert(no_cancel_result == false, "cancel_test() should return boolean false, got " .. type(no_cancel_result) .. ": " .. tostring(no_cancel_result))
30 30
31-- cancellation of cooperating lanes 31-- cancellation of cooperating lanes
32local cooperative = function() 32-- query_only: if true, pass true to cancel_test() so that hard cancel returns "hard" instead of raising
33local cooperative = function(query_only_)
33 local fixture = assert(require "fixture") 34 local fixture = assert(require "fixture")
34 local which_cancel 35 local which_cancel
35 repeat 36 repeat
36 fixture.block_for(0.2) 37 fixture.block_for(0.2)
37 which_cancel = cancel_test() 38 which_cancel = cancel_test(query_only_)
38 until which_cancel 39 until which_cancel
39 return which_cancel 40 return which_cancel
40end 41end
41-- soft and hard are behaviorally equivalent when no blocking linda operation is involved 42-- for soft cancel, cancel_test() returns "soft" so the lane exits the loop and returns it
42local cooperative_lane_soft = lanes_gen("*", { name = 'auto' }, cooperative)() 43local cooperative_lane_soft = lanes_gen("*", { name = 'auto' }, cooperative)()
43local a, b = cooperative_lane_soft:cancel("soft", 0) -- issue request, do not wait for lane to terminate 44local a, b = cooperative_lane_soft:cancel("soft", 0) -- issue request, do not wait for lane to terminate
44assert(a == false and b == "timeout", "got " .. tostring(a) .. " " .. tostring(b)) 45assert(a == false and b == "timeout", "got " .. tostring(a) .. " " .. tostring(b))
45assert(cooperative_lane_soft[1] == "soft", "cancel_test() should return \"soft\", got " .. type(cooperative_lane_soft[1]) .. ": " .. tostring(cooperative_lane_soft[1])) -- return value of the lane body is the value returned by cancel_test() 46assert(cooperative_lane_soft[1] == "soft", "cancel_test() should return \"soft\", got " .. type(cooperative_lane_soft[1]) .. ": " .. tostring(cooperative_lane_soft[1])) -- return value of the lane body is the value returned by cancel_test()
47-- for hard cancel, cancel_test() raises cancel_error, so the lane ends up in the cancelled state
46local cooperative_lane_hard = lanes_gen("*", { name = 'auto' }, cooperative)() 48local cooperative_lane_hard = lanes_gen("*", { name = 'auto' }, cooperative)()
47local c, d = cooperative_lane_hard:cancel("hard", 0) -- issue request, do not wait for lane to terminate 49local c, d = cooperative_lane_hard:cancel("hard", 0) -- issue request, do not wait for lane to terminate
48assert(c == false and d == "timeout", "got " .. tostring(c) .. " " .. tostring(d)) 50assert(c == false and d == "timeout", "got " .. tostring(c) .. " " .. tostring(d))
49assert(cooperative_lane_hard[1] == "hard", "cancel_test() should return \"hard\", got " .. type(cooperative_lane_hard[1]) .. ": " .. tostring(cooperative_lane_hard[1])) -- return value of the lane body is the value returned by cancel_test() 51assert(cooperative_lane_hard[1] == nil, "cancelled lane first result should be nil, got " .. type(cooperative_lane_hard[1]) .. ": " .. tostring(cooperative_lane_hard[1]))
52assert(cooperative_lane_hard[2] == lanes.cancel_error, "cancelled lane second result should be cancel_error")
53-- for hard cancel with query_only=true, cancel_test(true) returns "hard" so the lane exits the loop and returns it
54local cooperative_lane_hard_query = lanes_gen("*", { name = 'auto' }, cooperative)(true)
55local e, f = cooperative_lane_hard_query:cancel("hard", 0) -- issue request, do not wait for lane to terminate
56assert(e == false and f == "timeout", "got " .. tostring(e) .. " " .. tostring(f))
57assert(cooperative_lane_hard_query[1] == "hard", "cancel_test(true) should return \"hard\", got " .. type(cooperative_lane_hard_query[1]) .. ": " .. tostring(cooperative_lane_hard_query[1]))
50 58
51-- ################################################################################################## 59-- ##################################################################################################
52 60