diff options
Diffstat (limited to '')
-rw-r--r-- | unit_tests/scripts/linda/send_receive.lua | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/unit_tests/scripts/linda/send_receive.lua b/unit_tests/scripts/linda/send_receive.lua new file mode 100644 index 0000000..9a6a7be --- /dev/null +++ b/unit_tests/scripts/linda/send_receive.lua | |||
@@ -0,0 +1,46 @@ | |||
1 | local lanes = require "lanes" | ||
2 | |||
3 | -- a newly created linda doesn't contain anything | ||
4 | local l = lanes.linda() | ||
5 | assert(l.null == lanes.null) | ||
6 | assert(l:dump() == nil) | ||
7 | |||
8 | -- read something with 0 timeout, should fail | ||
9 | local k,v = l:receive(0, "k") | ||
10 | assert(k == nil and v == 'timeout') | ||
11 | |||
12 | -- send some value | ||
13 | assert(l:send("k1", 99) == true) | ||
14 | -- make sure the contents look as expected | ||
15 | local t = l:dump() | ||
16 | assert(type(t) == 'table') | ||
17 | local tk1 = t.k1 | ||
18 | assert(type(tk1) == 'table') | ||
19 | assert(tk1.first == 1 and tk1.count == 1 and tk1.limit == 'unlimited' and tk1.restrict == 'none') | ||
20 | assert(#tk1.fifo == tk1.count, #tk1.fifo .. " ~= " .. tk1.count) | ||
21 | assert(tk1.fifo[1] == 99, tk1.fifo[1] .. " ~= " .. 99) | ||
22 | |||
23 | -- read the value, should succeed | ||
24 | local k,v = l:receive("k1") | ||
25 | assert(k == "k1" and v == 99) | ||
26 | -- after reading, the data is no longer available (even if the key itself still exists within the linda) | ||
27 | local t = l:dump() | ||
28 | assert(type(t) == 'table') | ||
29 | local tk1 = t.k1 | ||
30 | assert(type(tk1) == 'table') | ||
31 | assert(tk1.first == 1 and tk1.count == 0 and tk1.limit == 'unlimited' and tk1.restrict == 'none') | ||
32 | assert(#tk1.fifo == tk1.count and tk1.fifo[1] == nil) | ||
33 | -- read again, should fail | ||
34 | local k,v = l:receive(0, "k1") | ||
35 | assert(k == nil and v == 'timeout') | ||
36 | |||
37 | -- send null, read nil | ||
38 | l:send("k", l.null) | ||
39 | local k,v = l:receive(0, "k") | ||
40 | assert(k == "k" and v == nil) | ||
41 | |||
42 | -- using a deep userdata (such as another linda) as key, should work | ||
43 | local l2 = lanes.linda() | ||
44 | l:send(nil, l2, 99) | ||
45 | local k,v = l:receive(0, l2) | ||
46 | assert(k == l2 and v == 99) | ||