diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-06-12 15:07:18 +0200 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-06-12 15:07:18 +0200 |
commit | 3e68678e0b1dbfc2d435862b2fe62f34cded8ed2 (patch) | |
tree | c51336d244ba655a4e32b2bb013ff48e1b21fe0a /tests | |
parent | fffa17d7e28f4a3147adf1f2ae2a73c4b0f7b945 (diff) | |
download | lanes-3e68678e0b1dbfc2d435862b2fe62f34cded8ed2.tar.gz lanes-3e68678e0b1dbfc2d435862b2fe62f34cded8ed2.tar.bz2 lanes-3e68678e0b1dbfc2d435862b2fe62f34cded8ed2.zip |
Unit test for to-be-closed Lanes and Lindas
Diffstat (limited to 'tests')
-rw-r--r-- | tests/tobeclosed.lua | 123 |
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 | |||
5 | local require_lanes_result_1, require_lanes_result_2 = require "lanes" | ||
6 | print("require_lanes_result:", require_lanes_result_1, require_lanes_result_2) | ||
7 | local lanes = require_lanes_result_1 | ||
8 | |||
9 | local 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 | ||
25 | end | ||
26 | |||
27 | -- ################################################################################################# | ||
28 | -- test that table and function handlers work fine, even with upvalues | ||
29 | WR "================================================================================================" | ||
30 | WR "Basic to-be-closed" | ||
31 | do | ||
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) | ||
53 | end | ||
54 | |||
55 | |||
56 | -- ################################################################################################# | ||
57 | -- test that linda transfer still works even when they contain a close handler | ||
58 | WR "================================================================================================" | ||
59 | WR "Through Linda" | ||
60 | do | ||
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) | ||
75 | end | ||
76 | |||
77 | -- ################################################################################################# | ||
78 | -- test that lane closing works | ||
79 | WR "================================================================================================" | ||
80 | WR "Lane closing" | ||
81 | do | ||
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") | ||
92 | end | ||
93 | |||
94 | -- ################################################################################################# | ||
95 | -- test that linda with a close handler can still be transferred in a Lane | ||
96 | WR "================================================================================================" | ||
97 | WR "Linda closing through Lane" | ||
98 | do | ||
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) | ||
120 | end | ||
121 | |||
122 | WR "================================================================================================" | ||
123 | WR "TEST OK" | ||