diff options
| author | Benoit Germain <benoit.germain@ubisoft.com> | 2026-02-26 10:17:36 +0100 |
|---|---|---|
| committer | Benoit Germain <benoit.germain@ubisoft.com> | 2026-02-26 10:17:36 +0100 |
| commit | 6b8c83b3bc44b0a1bc186f58de0fea6dfc214c4b (patch) | |
| tree | cedc4e5c7ded4393e20acdb932d82ffa1b8961f0 | |
| parent | 469bc8451db8078dca6cac4350cf874c47cef5f2 (diff) | |
| download | lanes-6b8c83b3bc44b0a1bc186f58de0fea6dfc214c4b.tar.gz lanes-6b8c83b3bc44b0a1bc186f58de0fea6dfc214c4b.tar.bz2 lanes-6b8c83b3bc44b0a1bc186f58de0fea6dfc214c4b.zip | |
Improve cancel_test() documentation and unit test
| -rw-r--r-- | docs/index.html | 8 | ||||
| -rw-r--r-- | unit_tests/scripts/lane/tasking_cancelling.lua | 16 |
2 files changed, 19 insertions, 5 deletions
diff --git a/docs/index.html b/docs/index.html index 452dcfa..3dc2b61 100644 --- a/docs/index.html +++ b/docs/index.html | |||
| @@ -1294,10 +1294,14 @@ | |||
| 1294 | This means the execution of the lane will resume although the operation has not completed, to give the lane a chance to detect cancellation (even in the case the code waits on a <a href="#lindas">linda</a> with infinite timeout). | 1294 | This means the execution of the lane will resume although the operation has not completed, to give the lane a chance to detect cancellation (even in the case the code waits on a <a href="#lindas">linda</a> with infinite timeout). |
| 1295 | <br /> | 1295 | <br /> |
| 1296 | The code should be able to handle this situation appropriately if required (in other words, it should gracefully handle the fact that it didn't receive the expected values). | 1296 | The code should be able to handle this situation appropriately if required (in other words, it should gracefully handle the fact that it didn't receive the expected values). |
| 1297 | <br /> | ||
| 1298 | It is also possible to manually test for cancel requests with <tt>cancel_test()</tt>. | ||
| 1299 | </p> | 1297 | </p> |
| 1300 | 1298 | ||
| 1299 | <table border="1" bgcolor="#E0E0FF" cellpadding="10" style="width:50%"><tr><td><pre> | ||
| 1300 | false|"soft"|"hard" = cancel_test() -- inside the lane | ||
| 1301 | </pre></td></tr></table> | ||
| 1302 | <p> | ||
| 1303 | Lanes installs the function <tt>cancel_test()</tt> in each created lane to manually test for cancel requests. | ||
| 1304 | </p> | ||
| 1301 | 1305 | ||
| 1302 | <!-- finalizers +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | 1306 | <!-- finalizers +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> |
| 1303 | <hr/> | 1307 | <hr/> |
diff --git a/unit_tests/scripts/lane/tasking_cancelling.lua b/unit_tests/scripts/lane/tasking_cancelling.lua index d153ffa..395bee5 100644 --- a/unit_tests/scripts/lane/tasking_cancelling.lua +++ b/unit_tests/scripts/lane/tasking_cancelling.lua | |||
| @@ -18,6 +18,16 @@ local lanes_linda = assert(lanes.linda) | |||
| 18 | -- ################################################################################################## | 18 | -- ################################################################################################## |
| 19 | -- ################################################################################################## | 19 | -- ################################################################################################## |
| 20 | 20 | ||
| 21 | -- test that cancel_test() returns exactly false (boolean) when no cancel is pending | ||
| 22 | local no_cancel = function() | ||
| 23 | local result = cancel_test() | ||
| 24 | assert(result == false, "cancel_test() should return boolean false when not cancelled, got " .. type(result) .. ": " .. tostring(result)) | ||
| 25 | return result | ||
| 26 | end | ||
| 27 | local no_cancel_lane = lanes_gen("*", { name = 'auto' }, no_cancel)() | ||
| 28 | 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)) | ||
| 30 | |||
| 21 | -- cancellation of cooperating lanes | 31 | -- cancellation of cooperating lanes |
| 22 | local cooperative = function() | 32 | local cooperative = function() |
| 23 | local fixture = assert(require "fixture") | 33 | local fixture = assert(require "fixture") |
| @@ -32,11 +42,11 @@ end | |||
| 32 | local cooperative_lane_soft = lanes_gen("*", { name = 'auto' }, cooperative)() | 42 | 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 | 43 | 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)) | 44 | 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() | 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() |
| 36 | local cooperative_lane_hard = lanes_gen("*", { name = 'auto' }, cooperative)() | 46 | 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 | 47 | 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)) | 48 | assert(c == false and d == "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() | 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() |
| 40 | 50 | ||
| 41 | -- ################################################################################################## | 51 | -- ################################################################################################## |
| 42 | 52 | ||
