diff options
| author | Benoit Germain <benoit.germain@ubisoft.com> | 2025-04-18 17:53:27 +0200 |
|---|---|---|
| committer | Benoit Germain <benoit.germain@ubisoft.com> | 2025-04-18 17:53:27 +0200 |
| commit | 4c5d11823802175cfaf083a6fcd20a3006b27d51 (patch) | |
| tree | 95ecb36d71620d5f61f5c06f3f022a3667181606 /unit_tests/scripts | |
| parent | 022e40cc71beda874d0bad2cec227e472d5dd4af (diff) | |
| download | lanes-4c5d11823802175cfaf083a6fcd20a3006b27d51.tar.gz lanes-4c5d11823802175cfaf083a6fcd20a3006b27d51.tar.bz2 lanes-4c5d11823802175cfaf083a6fcd20a3006b27d51.zip | |
A unit test for linda wake_period
Diffstat (limited to 'unit_tests/scripts')
| -rw-r--r-- | unit_tests/scripts/linda/wake_period.lua | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/unit_tests/scripts/linda/wake_period.lua b/unit_tests/scripts/linda/wake_period.lua new file mode 100644 index 0000000..e4a900d --- /dev/null +++ b/unit_tests/scripts/linda/wake_period.lua | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | -- default wake period is 0.5 seconds | ||
| 2 | local require_lanes_result_1, require_lanes_result_2 = require "lanes".configure{linda_wake_period = 0.5} | ||
| 3 | local lanes = require_lanes_result_1 | ||
| 4 | |||
| 5 | -- a lane that performs a blocking operation for 2 seconds | ||
| 6 | local body = function(linda_) | ||
| 7 | -- a blocking read that lasts longer than the tested wake_period values | ||
| 8 | linda_:receive(2, "empty_slot") | ||
| 9 | return true | ||
| 10 | end | ||
| 11 | |||
| 12 | -- if we don't cancel the lane, we should wait the whole duration | ||
| 13 | local function check_wake_duration(linda_, expected_, do_cancel_) | ||
| 14 | local h = lanes.gen(body)(linda_) | ||
| 15 | -- wait until the linda is blocked | ||
| 16 | repeat until h.status == "waiting" | ||
| 17 | local t0 = lanes.now_secs() | ||
| 18 | -- soft cancel, no timeout, no waking | ||
| 19 | if do_cancel_ then | ||
| 20 | local result, reason = h:cancel('soft', 0, false) | ||
| 21 | -- should say there was a timeout, since the lane didn't actually cancel (normal since we did not wake it) | ||
| 22 | assert(result == false and reason == 'timeout', "unexpected cancel result") | ||
| 23 | end | ||
| 24 | -- this should wait until the linda wakes by itself before the actual receive timeout and sees the cancel request | ||
| 25 | h:join() | ||
| 26 | local t1 = lanes.now_secs() | ||
| 27 | local delta = t1 - t0 | ||
| 28 | -- the linda should check for cancellation at about the expected period, not earlier | ||
| 29 | assert(delta >= expected_, tostring(linda_) .. " woke too early:" .. delta) | ||
| 30 | -- the lane shouldn't terminate too long after cancellation was processed | ||
| 31 | assert(delta <= expected_ * 1.1, tostring(linda_) .. " woke too late: " .. delta) | ||
| 32 | end | ||
| 33 | |||
| 34 | -- legacy behavior: linda waits until operation timeout | ||
| 35 | check_wake_duration(lanes.linda{name = "A", wake_period = 'never'}, 2, true) | ||
| 36 | -- early wake behavior: linda wakes after the expected time, sees a cancellation requests, and aborts the operation early | ||
| 37 | check_wake_duration(lanes.linda{name = "B", wake_period = 0.25}, 0.25, true) | ||
| 38 | check_wake_duration(lanes.linda{name = "C"}, 0.5, true) -- wake_period defaults to 0.5 (see above) | ||
| 39 | check_wake_duration(lanes.linda{name = "D", wake_period = 1}, 1, true) | ||
| 40 | -- even when there is a wake_period, the operation should reach full timeout if not cancelled early | ||
| 41 | check_wake_duration(lanes.linda{name = "E", wake_period = 0.1}, 2, false) | ||
