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/multiple_keepers.lua6
-rw-r--r--unit_tests/scripts/linda/send_receive_func_and_string.lua13
-rw-r--r--unit_tests/scripts/linda/send_registered_userdata.lua2
-rw-r--r--unit_tests/scripts/linda/wake_period.lua42
4 files changed, 59 insertions, 4 deletions
diff --git a/unit_tests/scripts/linda/multiple_keepers.lua b/unit_tests/scripts/linda/multiple_keepers.lua
index 8733087..267d874 100644
--- a/unit_tests/scripts/linda/multiple_keepers.lua
+++ b/unit_tests/scripts/linda/multiple_keepers.lua
@@ -2,9 +2,9 @@
2local require_lanes_result_1, require_lanes_result_2 = require "lanes".configure{nb_user_keepers = 3, keepers_gc_threshold = 500} 2local require_lanes_result_1, require_lanes_result_2 = require "lanes".configure{nb_user_keepers = 3, keepers_gc_threshold = 500}
3local lanes = require_lanes_result_1 3local lanes = require_lanes_result_1
4 4
5local a = lanes.linda("A", 1) 5local a = lanes.linda{name = "A", group = 1}
6local b = lanes.linda("B", 2) 6local b = lanes.linda{name = "B", group = 2}
7local c = lanes.linda("C", 3) 7local c = lanes.linda{name = "C", group = 3}
8 8
9-- store each linda in the other 2 9-- store each linda in the other 2
10do 10do
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/send_registered_userdata.lua b/unit_tests/scripts/linda/send_registered_userdata.lua
index 2c0195a..90c05c9 100644
--- a/unit_tests/scripts/linda/send_registered_userdata.lua
+++ b/unit_tests/scripts/linda/send_registered_userdata.lua
@@ -1,5 +1,5 @@
1local lanes = require 'lanes'.configure{with_timers = false} 1local lanes = require 'lanes'.configure{with_timers = false}
2local l = lanes.linda'gleh' 2local l = lanes.linda{name = 'gleh'}
3l:set('yo', io.stdin) 3l:set('yo', io.stdin)
4local n, stdin_out = l:get('yo') 4local n, stdin_out = l:get('yo')
5assert(n == 1 and stdin_out == io.stdin, tostring(stdin_out) .. " ~= " .. tostring(io.stdin)) 5assert(n == 1 and stdin_out == io.stdin, tostring(stdin_out) .. " ~= " .. tostring(io.stdin))
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)