aboutsummaryrefslogtreecommitdiff
path: root/tests/basic.lua
diff options
context:
space:
mode:
Diffstat (limited to 'tests/basic.lua')
-rw-r--r--tests/basic.lua62
1 files changed, 36 insertions, 26 deletions
diff --git a/tests/basic.lua b/tests/basic.lua
index 068dc25..f393175 100644
--- a/tests/basic.lua
+++ b/tests/basic.lua
@@ -163,7 +163,7 @@ PRINT(" "..st)
163assert(st == "cancelled", "st is '" .. st .. "' instead of 'cancelled'") 163assert(st == "cancelled", "st is '" .. st .. "' instead of 'cancelled'")
164 164
165-- cancellation of lanes waiting on a linda 165-- cancellation of lanes waiting on a linda
166local limited = lanes_linda("limited") 166local limited = lanes_linda{name = "limited"}
167assert.fails(function() limited:limit("key", -1) end) 167assert.fails(function() limited:limit("key", -1) end)
168assert.failsnot(function() limited:limit("key", 1) end) 168assert.failsnot(function() limited:limit("key", 1) end)
169-- [[################################################ 169-- [[################################################
@@ -173,42 +173,51 @@ for k, v in pairs(limited:dump()) do
173end 173end
174local wait_send = function() 174local wait_send = function()
175 local a,b 175 local a,b
176 set_finalizer(function() print("wait_send", a, b) end) 176 set_finalizer(function(err, stack_tbl) print("wait_send", a, b, " -> ", tostring(err)) end)
177 print("in wait_send")
177 a,b = limited:send("key", "bybye") -- infinite timeout, returns only when lane is cancelled 178 a,b = limited:send("key", "bybye") -- infinite timeout, returns only when lane is cancelled
178end 179end
179 180
180local wait_send_lane = lanes.gen("*", { name = 'auto' }, wait_send)() 181local wait_send_lane = lanes.gen("*", { name = 'auto' }, wait_send)()
181repeat until wait_send_lane.status == "waiting" 182repeat
182print "wait_send_lane is waiting" 183 io.stderr:write('!')
184 -- currently mingw64 builds can deadlock if we cancel the lane too early (before the linda blocks, at it causes the linda condvar not to be signalled)
185 lanes.sleep(0.1)
186until wait_send_lane.status == "waiting"
187PRINT "wait_send_lane is waiting"
183wait_send_lane:cancel() -- hard cancel, 0 timeout 188wait_send_lane:cancel() -- hard cancel, 0 timeout
184repeat until wait_send_lane.status == "cancelled" 189repeat until wait_send_lane.status == "cancelled"
185print "wait_send_lane is cancelled" 190PRINT "wait_send_lane is cancelled"
186--################################################]] 191--################################################]]
187local wait_receive = function() 192local wait_receive = function()
188 local k, v 193 local k, v
189 set_finalizer(function() print("wait_receive", k, v) end) 194 set_finalizer(function(err, stack_tbl) print("wait_receive", k, v, " -> ", tostring(err)) end)
190 k, v = limited:receive("dummy") -- infinite timeout, returns only when lane is cancelled 195 k, v = limited:receive("dummy") -- infinite timeout, returns only when lane is cancelled
191end 196end
192 197
193local wait_receive_lane = lanes.gen("*", { name = 'auto' }, wait_receive)() 198local wait_receive_lane = lanes.gen("*", { name = 'auto' }, wait_receive)()
194repeat until wait_receive_lane.status == "waiting" 199repeat
195print "wait_receive_lane is waiting" 200 io.stderr:write('!')
201 -- currently mingw64 builds can deadlock if we cancel the lane too early (before the linda blocks, at it causes the linda condvar not to be signalled)
202 lanes.sleep(0.1)
203until wait_receive_lane.status == "waiting"
204PRINT "wait_receive_lane is waiting"
196wait_receive_lane:cancel() -- hard cancel, 0 timeout 205wait_receive_lane:cancel() -- hard cancel, 0 timeout
197repeat until wait_receive_lane.status == "cancelled" 206repeat until wait_receive_lane.status == "cancelled"
198print "wait_receive_lane is cancelled" 207PRINT "wait_receive_lane is cancelled"
199--################################################]] 208--################################################]]
200local wait_receive_batched = function() 209local wait_receive_batched = function()
201 local k, v1, v2 210 local k, v1, v2
202 set_finalizer(function() print("wait_receive_batched", k, v1, v2) end) 211 set_finalizer(function() print("wait_receive_batched", k, v1, v2) end)
203 k, v1, v2 = limited:receive(limited.batched, "dummy", 2) -- infinite timeout, returns only when lane is cancelled 212 k, v1, v2 = limited:receive_batched("dummy", 2) -- infinite timeout, returns only when lane is cancelled
204end 213end
205 214
206local wait_receive_batched_lane = lanes.gen("*", { name = 'auto' }, wait_receive_batched)() 215local wait_receive_batched_lane = lanes.gen("*", { name = 'auto' }, wait_receive_batched)()
207repeat until wait_receive_batched_lane.status == "waiting" 216repeat until wait_receive_batched_lane.status == "waiting"
208print "wait_receive_batched_lane is waiting" 217PRINT "wait_receive_batched_lane is waiting"
209wait_receive_batched_lane:cancel() -- hard cancel, 0 timeout 218wait_receive_batched_lane:cancel() -- hard cancel, 0 timeout
210repeat until wait_receive_batched_lane.status == "cancelled" 219repeat until wait_receive_batched_lane.status == "cancelled"
211print "wait_receive_batched_lane is cancelled" 220PRINT "wait_receive_batched_lane is cancelled"
212--################################################]] 221--################################################]]
213 222
214-- ################################################################################################## 223-- ##################################################################################################
@@ -246,7 +255,7 @@ local chunk= function(linda)
246 WR("chunk ", "Lane ends!\n") 255 WR("chunk ", "Lane ends!\n")
247end 256end
248 257
249local linda = lanes_linda("communications") 258local linda = lanes_linda{name = "communications"}
250assert(type(linda) == "userdata" and tostring(linda) == "Linda: communications") 259assert(type(linda) == "userdata" and tostring(linda) == "Linda: communications")
251 -- 260 --
252 -- ["->"] master -> slave 261 -- ["->"] master -> slave
@@ -264,7 +273,7 @@ local b,x,y,z,w = linda:get("<->", 4)
264assert(b == 3 and x == "x" and y == "y" and z == "z" and w == nil) 273assert(b == 3 and x == "x" and y == "y" and z == "z" and w == nil)
265local k, x = linda:receive("<->") 274local k, x = linda:receive("<->")
266assert(k == "<->" and x == "x") 275assert(k == "<->" and x == "x")
267local k,y,z = linda:receive(linda.batched, "<->", 2) 276local k,y,z = linda:receive_batched("<->", 2)
268assert(k == "<->" and y == "y" and z == "z") 277assert(k == "<->" and y == "y" and z == "z")
269linda:set("<->") 278linda:set("<->")
270local b,x,y,z,w = linda:get("<->", 4) 279local b,x,y,z,w = linda:get("<->", 4)
@@ -401,7 +410,7 @@ local tc = lanes.gen("io", { name = 'auto', gc_cb = gc_cb },
401 end 410 end
402) 411)
403 412
404local linda= lanes_linda("criss cross") 413local linda= lanes_linda{name = "criss cross"}
405 414
406local a,b= tc(linda, "A","B"), tc(linda, "B","A") -- launching two lanes, twisted comms 415local a,b= tc(linda, "A","B"), tc(linda, "B","A") -- launching two lanes, twisted comms
407 416
@@ -437,7 +446,7 @@ local function chunk2(linda)
437 assert(config.strip_functions and info.short_src=="?" or string.match(info.short_src, "^.*basic.lua$"), "bad info.short_src") 446 assert(config.strip_functions and info.short_src=="?" or string.match(info.short_src, "^.*basic.lua$"), "bad info.short_src")
438 -- These vary so let's not be picky (they're there..) 447 -- These vary so let's not be picky (they're there..)
439 -- 448 --
440 assert(info.linedefined == 422, "bad linedefined") -- start of 'chunk2' 449 assert(info.linedefined == 431, "bad linedefined") -- start of 'chunk2'
441 assert(config.strip_functions and info.currentline==-1 or info.currentline > info.linedefined, "bad currentline") -- line of 'debug.getinfo' 450 assert(config.strip_functions and info.currentline==-1 or info.currentline > info.linedefined, "bad currentline") -- line of 'debug.getinfo'
442 assert(info.lastlinedefined > info.currentline, "bad lastlinedefined") -- end of 'chunk2' 451 assert(info.lastlinedefined > info.currentline, "bad lastlinedefined") -- end of 'chunk2'
443 local k,func= linda:receive("down") 452 local k,func= linda:receive("down")
@@ -452,7 +461,7 @@ local function chunk2(linda)
452 linda:send("up", function() return ":)" end, "ok2") 461 linda:send("up", function() return ":)" end, "ok2")
453end 462end
454 463
455local linda = lanes_linda("auto") 464local linda = lanes_linda{name = "auto"}
456local t2 = lanes.gen("debug,string,io", { name = 'auto', gc_cb = gc_cb }, chunk2)(linda) -- prepare & launch 465local t2 = lanes.gen("debug,string,io", { name = 'auto', gc_cb = gc_cb }, chunk2)(linda) -- prepare & launch
457linda:send("down", function(linda) linda:send("up", "ready!") end, 466linda:send("down", function(linda) linda:send("up", "ready!") end,
458 "ok") 467 "ok")
@@ -460,7 +469,7 @@ linda:send("down", function(linda) linda:send("up", "ready!") end,
460-- 469--
461local k,s= linda:receive(1, "up") 470local k,s= linda:receive(1, "up")
462if t2.status == "error" then 471if t2.status == "error" then
463 print("t2 error: " , t2:join()) 472 PRINT("t2 error: " , t2:join())
464end 473end
465PRINT(s) 474PRINT(s)
466assert(s=="ready!") 475assert(s=="ready!")
@@ -498,19 +507,20 @@ local S = lanes.gen("table", { name = 'auto', gc_cb = gc_cb },
498 return (unpack or table.unpack)(aux) 507 return (unpack or table.unpack)(aux)
499end) 508end)
500 509
501h= S { 12, 13, 14 } -- execution starts, h[1..3] will get the return values 510h = S { 12, 13, 14 } -- execution starts, h[1..3] will get the return values
502-- wait a bit so that the lane has a chance to set its debug name 511-- wait a bit so that the lane has a chance to set its debug name
503SLEEP(0.5) 512SLEEP(0.5)
504print("joining with '" .. h:get_threadname() .. "'") 513print("joining with '" .. h:get_threadname() .. "'")
505local a,b,c,d= h:join() 514local r,a,b,c,d= h:join()
506if h.status == "error" then 515if h.status == "error" then
507 print(h:get_threadname(), "error: " , a, b, c, d) 516 print(h:get_threadname(), "error: " , r, a, b, c, d)
508else 517else
509 print(h:get_threadname(), a,b,c,d) 518 print(h:get_threadname(), r,a,b,c,d)
510 assert(a==14) 519 assert(r == true)
511 assert(b==13) 520 assert(a == 14)
512 assert(c==12) 521 assert(b == 13)
513 assert(d==nil) 522 assert(c == 12)
523 assert(d == nil)
514end 524end
515 525
516local nameof_type, nameof_name = lanes.nameof(print) 526local nameof_type, nameof_name = lanes.nameof(print)