diff options
| author | Benoit Germain <benoit.germain@ubisoft.com> | 2025-04-24 18:21:15 +0200 |
|---|---|---|
| committer | Benoit Germain <benoit.germain@ubisoft.com> | 2025-04-24 18:21:15 +0200 |
| commit | fe51e51bb07b1cb662b0d4f972604f56e63f8432 (patch) | |
| tree | 15684515325a5092433a68f65920b941a645e2aa /unit_tests/scripts | |
| parent | 0085c2f6d6b5fbc6dd9244ae9b9af5a378ac4126 (diff) | |
| download | lanes-fe51e51bb07b1cb662b0d4f972604f56e63f8432.tar.gz lanes-fe51e51bb07b1cb662b0d4f972604f56e63f8432.tar.bz2 lanes-fe51e51bb07b1cb662b0d4f972604f56e63f8432.zip | |
Unit test fixes
* fix cooperative_shutdown
* add a test to tasking_cancelling
* fix bad fixture function name give_me_back
Diffstat (limited to 'unit_tests/scripts')
| -rw-r--r-- | unit_tests/scripts/lane/cooperative_shutdown.lua | 15 | ||||
| -rw-r--r-- | unit_tests/scripts/lane/tasking_cancelling.lua | 27 | ||||
| -rw-r--r-- | unit_tests/scripts/lane/uncooperative_shutdown.lua | 2 |
3 files changed, 38 insertions, 6 deletions
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 @@ | |||
| 1 | local lanes = require "lanes" | 1 | local lanes = require "lanes".configure{on_state_create = require "fixture".on_state_create} |
| 2 | 2 | ||
| 3 | -- launch lanes that cooperate properly with cancellation request | 3 | -- launch lanes that cooperate properly with cancellation request |
| 4 | 4 | ||
| 5 | local lane1 = function() | 5 | local lane1 = function() |
| 6 | lane_threadname("lane1") | 6 | lane_threadname("lane1") |
| 7 | -- loop breaks on 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() |
| @@ -42,7 +42,14 @@ local h2 = g2(linda) | |||
| 42 | 42 | ||
| 43 | local h3 = g3() | 43 | local h3 = g3() |
| 44 | 44 | ||
| 45 | -- wait until they are both started | 45 | lanes.sleep(0.1) |
| 46 | repeat until h1.status == "running" and h2.status == "waiting" and h3.status == "running" | 46 | |
| 47 | local is_running = function(lane_h) | ||
| 48 | local status = lane_h.status | ||
| 49 | return status == "running" or status == "waiting" | ||
| 50 | end | ||
| 51 | |||
| 52 | -- wait until they are all started | ||
| 53 | repeat until is_running(h1) and is_running(h2) and is_running(h3) | ||
| 47 | 54 | ||
| 48 | -- let the script terminate, Lanes should not crash at shutdown | 55 | -- 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 @@ | |||
| 1 | local require_lanes_result_1, require_lanes_result_2 = require "lanes".configure(config).configure() | 1 | local require_fixture_result_1, require_fixture_result_2 = require "fixture" |
| 2 | local fixture = assert(require_fixture_result_1) | ||
| 3 | |||
| 4 | local require_lanes_result_1, require_lanes_result_2 = require "lanes".configure{on_state_create = fixture.on_state_create}.configure() | ||
| 2 | print("require_lanes_result:", require_lanes_result_1, require_lanes_result_2) | 5 | print("require_lanes_result:", require_lanes_result_1, require_lanes_result_2) |
| 3 | local lanes = require_lanes_result_1 | 6 | local lanes = require_lanes_result_1 |
| 4 | 7 | ||
| @@ -15,6 +18,28 @@ local lanes_linda = assert(lanes.linda) | |||
| 15 | -- ################################################################################################## | 18 | -- ################################################################################################## |
| 16 | -- ################################################################################################## | 19 | -- ################################################################################################## |
| 17 | 20 | ||
| 21 | -- cancellation of cooperating lanes | ||
| 22 | local cooperative = function() | ||
| 23 | local fixture = assert(require "fixture") | ||
| 24 | local which_cancel | ||
| 25 | repeat | ||
| 26 | fixture.block_for(0.2) | ||
| 27 | which_cancel = cancel_test() | ||
| 28 | until which_cancel | ||
| 29 | return which_cancel | ||
| 30 | end | ||
| 31 | -- soft and hard are behaviorally equivalent when no blocking linda operation is involved | ||
| 32 | local cooperative_lane_soft = lanes_gen("*", { name = 'auto' }, cooperative)() | ||
| 33 | local a, b = cooperative_lane_soft:cancel("soft", 0) -- issue request, do not wait for lane to terminate | ||
| 34 | assert(a == false and b == "timeout", "got " .. tostring(a) .. " " .. tostring(b)) | ||
| 35 | assert(cooperative_lane_soft[1] == "soft") -- return value of the lane body is the value returned by cancel_test() | ||
| 36 | local cooperative_lane_hard = lanes_gen("*", { name = 'auto' }, cooperative)() | ||
| 37 | local c, d = cooperative_lane_hard:cancel("hard", 0) -- issue request, do not wait for lane to terminate | ||
| 38 | assert(a == false and b == "timeout", "got " .. tostring(c) .. " " .. tostring(d)) | ||
| 39 | assert(cooperative_lane_hard[1] == "hard") -- return value of the lane body is the value returned by cancel_test() | ||
| 40 | |||
| 41 | -- ################################################################################################## | ||
| 42 | |||
| 18 | -- cancellation of lanes waiting on a linda | 43 | -- cancellation of lanes waiting on a linda |
| 19 | local limited = lanes_linda{name = "limited"} | 44 | local limited = lanes_linda{name = "limited"} |
| 20 | assert.fails(function() limited:limit("key", -1) end) | 45 | 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 | |||
| 8 | -- launch lanes that blocks forever | 8 | -- launch lanes that blocks forever |
| 9 | local lane = function() | 9 | local lane = function() |
| 10 | local fixture = require "fixture" | 10 | local fixture = require "fixture" |
| 11 | fixture.sleep_for() | 11 | fixture.block_for() |
| 12 | end | 12 | end |
| 13 | 13 | ||
| 14 | -- the generator | 14 | -- the generator |
