From fe51e51bb07b1cb662b0d4f972604f56e63f8432 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Thu, 24 Apr 2025 18:21:15 +0200 Subject: Unit test fixes * fix cooperative_shutdown * add a test to tasking_cancelling * fix bad fixture function name give_me_back --- unit_tests/scripts/lane/cooperative_shutdown.lua | 15 ++++++++---- unit_tests/scripts/lane/tasking_cancelling.lua | 27 +++++++++++++++++++++- unit_tests/scripts/lane/uncooperative_shutdown.lua | 2 +- 3 files changed, 38 insertions(+), 6 deletions(-) (limited to 'unit_tests/scripts') diff --git a/unit_tests/scripts/lane/cooperative_shutdown.lua b/unit_tests/scripts/lane/cooperative_shutdown.lua index 0e30f91..0a0943e 100644 --- a/unit_tests/scripts/lane/cooperative_shutdown.lua +++ b/unit_tests/scripts/lane/cooperative_shutdown.lua @@ -1,10 +1,10 @@ -local lanes = require "lanes" +local lanes = require "lanes".configure{on_state_create = require "fixture".on_state_create} -- launch lanes that cooperate properly with cancellation request local lane1 = function() lane_threadname("lane1") - -- loop breaks on cancellation request + -- loop breaks on soft cancellation request repeat lanes.sleep(0) until cancel_test() @@ -42,7 +42,14 @@ local h2 = g2(linda) local h3 = g3() --- wait until they are both started -repeat until h1.status == "running" and h2.status == "waiting" and h3.status == "running" +lanes.sleep(0.1) + +local is_running = function(lane_h) + local status = lane_h.status + return status == "running" or status == "waiting" +end + +-- wait until they are all started +repeat until is_running(h1) and is_running(h2) and is_running(h3) -- let the script terminate, Lanes should not crash at shutdown diff --git a/unit_tests/scripts/lane/tasking_cancelling.lua b/unit_tests/scripts/lane/tasking_cancelling.lua index 873140e..d153ffa 100644 --- a/unit_tests/scripts/lane/tasking_cancelling.lua +++ b/unit_tests/scripts/lane/tasking_cancelling.lua @@ -1,4 +1,7 @@ -local require_lanes_result_1, require_lanes_result_2 = require "lanes".configure(config).configure() +local require_fixture_result_1, require_fixture_result_2 = require "fixture" +local fixture = assert(require_fixture_result_1) + +local require_lanes_result_1, require_lanes_result_2 = require "lanes".configure{on_state_create = fixture.on_state_create}.configure() print("require_lanes_result:", require_lanes_result_1, require_lanes_result_2) local lanes = require_lanes_result_1 @@ -15,6 +18,28 @@ local lanes_linda = assert(lanes.linda) -- ################################################################################################## -- ################################################################################################## +-- cancellation of cooperating lanes +local cooperative = function() + local fixture = assert(require "fixture") + local which_cancel + repeat + fixture.block_for(0.2) + which_cancel = cancel_test() + until which_cancel + return which_cancel +end +-- soft and hard are behaviorally equivalent when no blocking linda operation is involved +local cooperative_lane_soft = lanes_gen("*", { name = 'auto' }, cooperative)() +local a, b = cooperative_lane_soft:cancel("soft", 0) -- issue request, do not wait for lane to terminate +assert(a == false and b == "timeout", "got " .. tostring(a) .. " " .. tostring(b)) +assert(cooperative_lane_soft[1] == "soft") -- return value of the lane body is the value returned by cancel_test() +local cooperative_lane_hard = lanes_gen("*", { name = 'auto' }, cooperative)() +local c, d = cooperative_lane_hard:cancel("hard", 0) -- issue request, do not wait for lane to terminate +assert(a == false and b == "timeout", "got " .. tostring(c) .. " " .. tostring(d)) +assert(cooperative_lane_hard[1] == "hard") -- return value of the lane body is the value returned by cancel_test() + +-- ################################################################################################## + -- cancellation of lanes waiting on a linda local limited = lanes_linda{name = "limited"} assert.fails(function() limited:limit("key", -1) end) diff --git a/unit_tests/scripts/lane/uncooperative_shutdown.lua b/unit_tests/scripts/lane/uncooperative_shutdown.lua index 89e1ff8..eb89ed3 100644 --- a/unit_tests/scripts/lane/uncooperative_shutdown.lua +++ b/unit_tests/scripts/lane/uncooperative_shutdown.lua @@ -8,7 +8,7 @@ local lanes = require "lanes".configure{shutdown_timeout = 0.001, on_state_creat -- launch lanes that blocks forever local lane = function() local fixture = require "fixture" - fixture.sleep_for() + fixture.block_for() end -- the generator -- cgit v1.2.3-55-g6feb