aboutsummaryrefslogtreecommitdiff
path: root/unit_tests/scripts/linda
diff options
context:
space:
mode:
Diffstat (limited to 'unit_tests/scripts/linda')
-rw-r--r--unit_tests/scripts/linda/send_receive_func_and_string.lua13
-rw-r--r--unit_tests/scripts/linda/wake_period.lua42
2 files changed, 55 insertions, 0 deletions
diff --git a/unit_tests/scripts/linda/send_receive_func_and_string.lua b/unit_tests/scripts/linda/send_receive_func_and_string.lua
new file mode 100644
index 0000000..188cfcd
--- /dev/null
+++ b/unit_tests/scripts/linda/send_receive_func_and_string.lua
@@ -0,0 +1,13 @@
1local lanes = require "lanes"
2
3-- a newly created linda doesn't contain anything
4local l = lanes.linda()
5
6-- send a function and a string, make sure that's what we read back
7l:send("k", function() end, "str")
8local c = l:count("k")
9assert(c == 2, "got " .. c)
10local k, v1, v2 = l:receive_batched("k", 2)
11local tv1, tv2 = type(v1), type(v2)
12assert(k == "k" and tv1 == "function" and tv2 == "string", "got " .. tv1 .. " " .. tv2)
13assert(l:count("k") == 0)
diff --git a/unit_tests/scripts/linda/wake_period.lua b/unit_tests/scripts/linda/wake_period.lua
new file mode 100644
index 0000000..d2dccc3
--- /dev/null
+++ b/unit_tests/scripts/linda/wake_period.lua
@@ -0,0 +1,42 @@
1-- default wake period is 0.5 seconds
2local require_lanes_result_1, require_lanes_result_2 = require "lanes".configure{linda_wake_period = 0.5}
3local lanes = require_lanes_result_1
4
5-- a lane that performs a blocking operation for 2 seconds
6local body = function(linda_)
7 -- a blocking read that lasts longer than the tested wake_period values
8 linda_:receive(2, "empty_slot")
9 return "done"
10end
11
12-- if we don't cancel the lane, we should wait the whole duration
13local 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 local r, ret = h:join()
26 assert(r == true and ret == "done")
27 local t1 = lanes.now_secs()
28 local delta = t1 - t0
29 -- the linda should check for cancellation at about the expected period, not earlier
30 assert(delta >= expected_, tostring(linda_) .. " woke too early:" .. delta)
31 -- the lane shouldn't terminate too long after cancellation was processed
32 assert(delta <= expected_ * 1.1, tostring(linda_) .. " woke too late: " .. delta)
33end
34
35-- legacy behavior: linda waits until operation timeout
36check_wake_duration(lanes.linda{name = "A", wake_period = 'never'}, 2, true)
37-- early wake behavior: linda wakes after the expected time, sees a cancellation requests, and aborts the operation early
38check_wake_duration(lanes.linda{name = "B", wake_period = 0.25}, 0.25, true)
39check_wake_duration(lanes.linda{name = "C"}, 0.5, true) -- wake_period defaults to 0.5 (see above)
40check_wake_duration(lanes.linda{name = "D", wake_period = 1}, 1, true)
41-- even when there is a wake_period, the operation should reach full timeout if not cancelled early
42check_wake_duration(lanes.linda{name = "E", wake_period = 0.1}, 2, false)