diff options
Diffstat (limited to 'unit_tests/scripts/linda')
-rw-r--r-- | unit_tests/scripts/linda/multiple_keepers.lua | 6 | ||||
-rw-r--r-- | unit_tests/scripts/linda/send_receive_func_and_string.lua | 13 | ||||
-rw-r--r-- | unit_tests/scripts/linda/send_registered_userdata.lua | 2 | ||||
-rw-r--r-- | unit_tests/scripts/linda/wake_period.lua | 42 |
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 @@ | |||
2 | local require_lanes_result_1, require_lanes_result_2 = require "lanes".configure{nb_user_keepers = 3, keepers_gc_threshold = 500} | 2 | local require_lanes_result_1, require_lanes_result_2 = require "lanes".configure{nb_user_keepers = 3, keepers_gc_threshold = 500} |
3 | local lanes = require_lanes_result_1 | 3 | local lanes = require_lanes_result_1 |
4 | 4 | ||
5 | local a = lanes.linda("A", 1) | 5 | local a = lanes.linda{name = "A", group = 1} |
6 | local b = lanes.linda("B", 2) | 6 | local b = lanes.linda{name = "B", group = 2} |
7 | local c = lanes.linda("C", 3) | 7 | local 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 |
10 | do | 10 | do |
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 @@ | |||
1 | local lanes = require "lanes" | ||
2 | |||
3 | -- a newly created linda doesn't contain anything | ||
4 | local l = lanes.linda() | ||
5 | |||
6 | -- send a function and a string, make sure that's what we read back | ||
7 | l:send("k", function() end, "str") | ||
8 | local c = l:count("k") | ||
9 | assert(c == 2, "got " .. c) | ||
10 | local k, v1, v2 = l:receive_batched("k", 2) | ||
11 | local tv1, tv2 = type(v1), type(v2) | ||
12 | assert(k == "k" and tv1 == "function" and tv2 == "string", "got " .. tv1 .. " " .. tv2) | ||
13 | assert(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 @@ | |||
1 | local lanes = require 'lanes'.configure{with_timers = false} | 1 | local lanes = require 'lanes'.configure{with_timers = false} |
2 | local l = lanes.linda'gleh' | 2 | local l = lanes.linda{name = 'gleh'} |
3 | l:set('yo', io.stdin) | 3 | l:set('yo', io.stdin) |
4 | local n, stdin_out = l:get('yo') | 4 | local n, stdin_out = l:get('yo') |
5 | assert(n == 1 and stdin_out == io.stdin, tostring(stdin_out) .. " ~= " .. tostring(io.stdin)) | 5 | assert(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 | ||
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 "done" | ||
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 | 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) | ||
33 | end | ||
34 | |||
35 | -- legacy behavior: linda waits until operation timeout | ||
36 | check_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 | ||
38 | check_wake_duration(lanes.linda{name = "B", wake_period = 0.25}, 0.25, true) | ||
39 | check_wake_duration(lanes.linda{name = "C"}, 0.5, true) -- wake_period defaults to 0.5 (see above) | ||
40 | check_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 | ||
42 | check_wake_duration(lanes.linda{name = "E", wake_period = 0.1}, 2, false) | ||