diff options
Diffstat (limited to 'unit_tests')
| -rw-r--r-- | unit_tests/scripts/_utils.lua | 2 | ||||
| -rw-r--r-- | unit_tests/scripts/_utils54.lua | 2 | ||||
| -rw-r--r-- | unit_tests/scripts/lane/cooperative_shutdown.lua | 2 | ||||
| -rw-r--r-- | unit_tests/scripts/lane/tasking_cancelling.lua | 16 |
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" |
| 12 | end | 12 | end |
| 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] | |||
| 29 | assert(no_cancel_result == false, "cancel_test() should return boolean false, got " .. type(no_cancel_result) .. ": " .. tostring(no_cancel_result)) | 29 | assert(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 |
| 32 | local cooperative = function() | 32 | -- query_only: if true, pass true to cancel_test() so that hard cancel returns "hard" instead of raising |
| 33 | local 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 |
| 40 | end | 41 | end |
| 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 |
| 42 | local cooperative_lane_soft = lanes_gen("*", { name = 'auto' }, cooperative)() | 43 | local cooperative_lane_soft = lanes_gen("*", { name = 'auto' }, cooperative)() |
| 43 | local a, b = cooperative_lane_soft:cancel("soft", 0) -- issue request, do not wait for lane to terminate | 44 | local a, b = cooperative_lane_soft:cancel("soft", 0) -- issue request, do not wait for lane to terminate |
| 44 | assert(a == false and b == "timeout", "got " .. tostring(a) .. " " .. tostring(b)) | 45 | assert(a == false and b == "timeout", "got " .. tostring(a) .. " " .. tostring(b)) |
| 45 | assert(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() | 46 | assert(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 | ||
| 46 | local cooperative_lane_hard = lanes_gen("*", { name = 'auto' }, cooperative)() | 48 | local cooperative_lane_hard = lanes_gen("*", { name = 'auto' }, cooperative)() |
| 47 | local c, d = cooperative_lane_hard:cancel("hard", 0) -- issue request, do not wait for lane to terminate | 49 | local c, d = cooperative_lane_hard:cancel("hard", 0) -- issue request, do not wait for lane to terminate |
| 48 | assert(c == false and d == "timeout", "got " .. tostring(c) .. " " .. tostring(d)) | 50 | assert(c == false and d == "timeout", "got " .. tostring(c) .. " " .. tostring(d)) |
| 49 | assert(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() | 51 | assert(cooperative_lane_hard[1] == nil, "cancelled lane first result should be nil, got " .. type(cooperative_lane_hard[1]) .. ": " .. tostring(cooperative_lane_hard[1])) |
| 52 | assert(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 | ||
| 54 | local cooperative_lane_hard_query = lanes_gen("*", { name = 'auto' }, cooperative)(true) | ||
| 55 | local e, f = cooperative_lane_hard_query:cancel("hard", 0) -- issue request, do not wait for lane to terminate | ||
| 56 | assert(e == false and f == "timeout", "got " .. tostring(e) .. " " .. tostring(f)) | ||
| 57 | assert(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 | ||
