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