aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2024-06-12 15:07:18 +0200
committerBenoit Germain <benoit.germain@ubisoft.com>2024-06-12 15:07:18 +0200
commit3e68678e0b1dbfc2d435862b2fe62f34cded8ed2 (patch)
treec51336d244ba655a4e32b2bb013ff48e1b21fe0a
parentfffa17d7e28f4a3147adf1f2ae2a73c4b0f7b945 (diff)
downloadlanes-3e68678e0b1dbfc2d435862b2fe62f34cded8ed2.tar.gz
lanes-3e68678e0b1dbfc2d435862b2fe62f34cded8ed2.tar.bz2
lanes-3e68678e0b1dbfc2d435862b2fe62f34cded8ed2.zip
Unit test for to-be-closed Lanes and Lindas
-rw-r--r--tests/tobeclosed.lua123
1 files changed, 123 insertions, 0 deletions
diff --git a/tests/tobeclosed.lua b/tests/tobeclosed.lua
new file mode 100644
index 0000000..6e3de4c
--- /dev/null
+++ b/tests/tobeclosed.lua
@@ -0,0 +1,123 @@
1--
2-- TOBECLOSED.LUA Copyright (C) 2024 benoit Germain <bnt.germain@gmail.com>
3--
4
5local require_lanes_result_1, require_lanes_result_2 = require "lanes"
6print("require_lanes_result:", require_lanes_result_1, require_lanes_result_2)
7local lanes = require_lanes_result_1
8
9local WR = function(...)
10 local str=""
11 for i=1,select('#',...) do
12 local v = select(i,...)
13 if type(v) == "function" then
14 local infos = debug.getinfo(v)
15 --[[for k,v in pairs(infos) do
16 print(k,v)
17 end]]
18 v = infos.source..":"..infos.linedefined
19 end
20 str= str..tostring(v).."\t"
21 end
22 if io then
23 io.stderr:write(str.."\n")
24 end
25end
26
27-- #################################################################################################
28-- test that table and function handlers work fine, even with upvalues
29WR "================================================================================================"
30WR "Basic to-be-closed"
31do
32 local closed_by_f = false
33 local closed_by_t = false
34 do
35 local close_handler_f = function(linda_, err_)
36 WR("f closing ", linda_)
37 closed_by_f = true
38 end
39 local lf <close> = lanes.linda("closed by f", close_handler_f)
40
41 local close_handler_t = setmetatable({},
42 {
43 __call = function(self_, linda_)
44 WR("t closing ", linda_)
45 closed_by_t = true
46 end
47 }
48 )
49 local lt <close> = lanes.linda("closed by t", close_handler_t)
50 end
51 assert(closed_by_f == true)
52 assert(closed_by_t == true)
53end
54
55
56-- #################################################################################################
57-- test that linda transfer still works even when they contain a close handler
58WR "================================================================================================"
59WR "Through Linda"
60do
61 local l = lanes.linda("channel")
62
63 local close_handler_f = function(linda_, err_)
64 WR("f closing ", linda_)
65 linda_:set("closed", true)
66 end
67 local l_in = lanes.linda("voyager", close_handler_f)
68 l:set("trip", l_in)
69
70 do
71
72 local l_out <close> = l:get("trip")
73 end
74 assert(l_in:get("closed") == true)
75end
76
77-- #################################################################################################
78-- test that lane closing works
79WR "================================================================================================"
80WR "Lane closing"
81do
82 local lane_body = function()
83 WR "In lane body"
84 lanes.sleep(1)
85 end
86
87 local h = lanes.gen("*", lane_body)()
88 do
89 local tobeclosed <close> = h
90 end
91 assert(h.status == "done")
92end
93
94-- #################################################################################################
95-- test that linda with a close handler can still be transferred in a Lane
96WR "================================================================================================"
97WR "Linda closing through Lane"
98do
99 local l = lanes.linda("channel")
100 local lane_body = function(l_arg_)
101 WR "In lane body"
102 -- linda obtained through a linda
103 local l_out <close> = l:get("trip")
104 -- linda from arguments
105 local l_arg <close> = l_arg_
106 return true
107 end
108
109 local close_handler_f = function(linda_, err_)
110 WR("f closing ", linda_)
111 linda_:set("closed", (linda_:get("closed") or 0) + 1)
112 end
113 local l_in = lanes.linda("voyager", close_handler_f)
114 l:set("trip", l_in)
115
116 do
117 lanes.gen("*", lane_body)(l_in):join()
118 end
119 assert(l_in:get("closed") == 2)
120end
121
122WR "================================================================================================"
123WR "TEST OK"