aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/basic.lua11
-rw-r--r--tests/cancel.lua23
-rw-r--r--tests/deadlock.lua7
-rw-r--r--tests/ehynes.lua7
-rw-r--r--tests/errhangtest.lua4
-rw-r--r--tests/nameof.lua9
-rw-r--r--tests/objects.lua34
-rw-r--r--tests/protect_allocator.lua9
-rw-r--r--tests/timer.lua2
-rw-r--r--tests/track_lanes.lua12
10 files changed, 77 insertions, 41 deletions
diff --git a/tests/basic.lua b/tests/basic.lua
index ab8a080..da7d756 100644
--- a/tests/basic.lua
+++ b/tests/basic.lua
@@ -29,6 +29,11 @@ local function PRINT(...)
29 end 29 end
30end 30end
31 31
32local SLEEP = function(...)
33 local k, v = lanes.sleep(...)
34 assert(k == nil and v == "timeout")
35end
36
32local gc_cb = function(name_, status_) 37local gc_cb = function(name_, status_)
33 PRINT(" ---> lane '" .. name_ .. "' collected with status '" .. status_ .. "'") 38 PRINT(" ---> lane '" .. name_ .. "' collected with status '" .. status_ .. "'")
34end 39end
@@ -256,7 +261,7 @@ SEND(3); WR("main ", "3 sent\n")
256SEND(setmetatable({"should be ignored"},{__lanesignore=true})); WR("main ", "__lanesignore table sent\n") 261SEND(setmetatable({"should be ignored"},{__lanesignore=true})); WR("main ", "__lanesignore table sent\n")
257for i=1,100 do 262for i=1,100 do
258 WR "." 263 WR "."
259 lanes.sleep(0.0001) 264 SLEEP(0.0001)
260 assert(PEEK() == nil) -- nothing coming in, yet 265 assert(PEEK() == nil) -- nothing coming in, yet
261end 266end
262SEND(nil); WR("\nmain ", "nil sent\n") 267SEND(nil); WR("\nmain ", "nil sent\n")
@@ -293,7 +298,7 @@ comms_lane = nil
293collectgarbage() 298collectgarbage()
294-- wait 299-- wait
295WR("waiting 1s") 300WR("waiting 1s")
296lanes.sleep(1) 301SLEEP(1)
297 302
298-- ################################################################################################## 303-- ##################################################################################################
299-- ################################################################################################## 304-- ##################################################################################################
@@ -470,7 +475,7 @@ end)
470 475
471h= S { 12, 13, 14 } -- execution starts, h[1..3] will get the return values 476h= S { 12, 13, 14 } -- execution starts, h[1..3] will get the return values
472-- wait a bit so that the lane has a chance to set its debug name 477-- wait a bit so that the lane has a chance to set its debug name
473lanes.sleep(0.5) 478SLEEP(0.5)
474print("joining with '" .. h:get_debug_threadname() .. "'") 479print("joining with '" .. h:get_debug_threadname() .. "'")
475local a,b,c,d= h:join() 480local a,b,c,d= h:join()
476if h.status == "error" then 481if h.status == "error" then
diff --git a/tests/cancel.lua b/tests/cancel.lua
index e39ad98..b75d085 100644
--- a/tests/cancel.lua
+++ b/tests/cancel.lua
@@ -9,6 +9,11 @@ end
9 9
10local lanes = require "lanes" .configure{ with_timers = false} 10local lanes = require "lanes" .configure{ with_timers = false}
11 11
12local SLEEP = function(...)
13 local k, v = lanes.sleep(...)
14 assert(k == nil and v == "timeout")
15end
16
12local linda = lanes.linda() 17local linda = lanes.linda()
13-- a numeric value to read 18-- a numeric value to read
14linda:set( "val", 33.0) 19linda:set( "val", 33.0)
@@ -51,7 +56,7 @@ local waitCancellation = function( h, expected_status)
51 if expected_status ~= "running" then 56 if expected_status ~= "running" then
52 repeat 57 repeat
53 -- print( "lane status:", h.status) 58 -- print( "lane status:", h.status)
54 l:receive( 0.1, "yeah") -- wait a bit 59 SLEEP(0.1) -- wait a bit
55 until h.status ~= "running" 60 until h.status ~= "running"
56 end 61 end
57 print( "lane status:", h.status) 62 print( "lane status:", h.status)
@@ -80,8 +85,8 @@ local laneBody = function( mode_, payload_)
80 -- linda mode 85 -- linda mode
81 io.stdout:write( " lane calling receive() ... ") 86 io.stdout:write( " lane calling receive() ... ")
82 local key, val = linda:receive( payload_, "boob") 87 local key, val = linda:receive( payload_, "boob")
83 print( lanes.cancel_error == key and "cancel_error" or tostring( key), tostring( val)) 88 print(tostring(key), val == lanes.cancel_error and "cancel_error" or tostring(val))
84 if key == lanes.cancel_error then 89 if val == lanes.cancel_error then
85 break -- gracefully abort loop 90 break -- gracefully abort loop
86 end 91 end
87 elseif mode_ == "get" then 92 elseif mode_ == "get" then
@@ -138,9 +143,9 @@ if not next(which_tests) or which_tests.linda then
138 h = lanes.gen( "*", laneBody)( "receive", nil) -- start an infinite wait on the linda 143 h = lanes.gen( "*", laneBody)( "receive", nil) -- start an infinite wait on the linda
139 144
140 print "wait 1s" 145 print "wait 1s"
141 linda:receive( 1, "yeah") 146 SLEEP(1)
142 147
143 -- linda cancel: linda:receive() returns cancel_error immediately 148 -- linda cancel: linda:receive() returns nil,cancel_error immediately
144 print "cancelling" 149 print "cancelling"
145 linda:cancel( "both") 150 linda:cancel( "both")
146 151
@@ -159,7 +164,7 @@ if not next(which_tests) or which_tests.soft then
159 h = lanes.gen( "*", protectedBody)( "receive") -- start an infinite wait on the linda 164 h = lanes.gen( "*", protectedBody)( "receive") -- start an infinite wait on the linda
160 165
161 print "wait 1s" 166 print "wait 1s"
162 linda:receive( 1, "yeah") 167 SLEEP(1)
163 168
164 -- soft cancel, no awakening of waiting linda operations, should timeout 169 -- soft cancel, no awakening of waiting linda operations, should timeout
165 local a, b = h:cancel( "soft", 1, false) 170 local a, b = h:cancel( "soft", 1, false)
@@ -182,7 +187,7 @@ if not next(which_tests) or which_tests.hook then
182 print "\n\n####################################################################\nbegin hook cancel test\n" 187 print "\n\n####################################################################\nbegin hook cancel test\n"
183 h = lanes.gen( "*", protectedBody)( "get", 300000) 188 h = lanes.gen( "*", protectedBody)( "get", 300000)
184 print "wait 2s" 189 print "wait 2s"
185 linda:receive( 2, "yeah") 190 SLEEP(2)
186 191
187 -- count hook cancel after some instruction instructions 192 -- count hook cancel after some instruction instructions
188 print "cancelling" 193 print "cancelling"
@@ -201,7 +206,7 @@ if not next(which_tests) or which_tests.hard then
201 206
202 -- wait 2s before cancelling the lane 207 -- wait 2s before cancelling the lane
203 print "wait 2s" 208 print "wait 2s"
204 linda:receive( 2, "yeah") 209 SLEEP(2)
205 210
206 -- hard cancel: the lane will be interrupted from inside its current linda:receive() and won't return from it 211 -- hard cancel: the lane will be interrupted from inside its current linda:receive() and won't return from it
207 print "cancelling" 212 print "cancelling"
@@ -220,7 +225,7 @@ if not next(which_tests) or which_tests.hard_unprotected then
220 225
221 -- wait 2s before cancelling the lane 226 -- wait 2s before cancelling the lane
222 print "wait 2s" 227 print "wait 2s"
223 linda:receive( 2, "yeah") 228 SLEEP(2)
224 229
225 -- hard cancel: the lane will be interrupted from inside its current linda:receive() and won't return from it 230 -- hard cancel: the lane will be interrupted from inside its current linda:receive() and won't return from it
226 print "cancelling" 231 print "cancelling"
diff --git a/tests/deadlock.lua b/tests/deadlock.lua
index c85d99f..c38ca13 100644
--- a/tests/deadlock.lua
+++ b/tests/deadlock.lua
@@ -4,6 +4,11 @@
4local lanes = require('lanes').configure{with_timers=false} 4local lanes = require('lanes').configure{with_timers=false}
5local linda = lanes.linda "deadlock_linda" 5local linda = lanes.linda "deadlock_linda"
6 6
7local SLEEP = function(...)
8 local k, v = lanes.sleep(...)
9 assert(k == nil and v == "timeout")
10end
11
7print "let's begin" 12print "let's begin"
8 13
9local do_extra_stuff = true 14local do_extra_stuff = true
@@ -35,5 +40,5 @@ end
35-- With the bug not fixed, the linda keeper's mutex is still acquired, 40-- With the bug not fixed, the linda keeper's mutex is still acquired,
36-- and the program hangs when the internal linda used for timers attempts to acquire the same keeper (there is only one by default) 41-- and the program hangs when the internal linda used for timers attempts to acquire the same keeper (there is only one by default)
37print('waiting a bit') 42print('waiting a bit')
38lanes.sleep(2) 43SLEEP(2)
39print('we should reach here') \ No newline at end of file 44print('we should reach here') \ No newline at end of file
diff --git a/tests/ehynes.lua b/tests/ehynes.lua
index e203a12..9436c7d 100644
--- a/tests/ehynes.lua
+++ b/tests/ehynes.lua
@@ -4,6 +4,11 @@
4local lanes = require "lanes" 4local lanes = require "lanes"
5lanes.configure() 5lanes.configure()
6 6
7local SLEEP = function(...)
8 local k, v = lanes.sleep(...)
9 assert(k == nil and v == "timeout")
10end
11
7local function PRINT_FMT( fmt, ... ) 12local function PRINT_FMT( fmt, ... )
8 io.stderr:write( string.format(fmt,...).."\n" ) 13 io.stderr:write( string.format(fmt,...).."\n" )
9end 14end
@@ -36,7 +41,7 @@ local receiver2 = receiver_gen('another message')
36 41
37-- a function to pause and log the execution for debugging 42-- a function to pause and log the execution for debugging
38local function logf(s, f, ...) 43local function logf(s, f, ...)
39 linda:receive(1, "dummy") -- wait 1s 44 SLEEP(1)
40 PRINT_FMT( "*** %s", s ) 45 PRINT_FMT( "*** %s", s )
41 f(...) 46 f(...)
42end 47end
diff --git a/tests/errhangtest.lua b/tests/errhangtest.lua
index 84de8c6..99f44b2 100644
--- a/tests/errhangtest.lua
+++ b/tests/errhangtest.lua
@@ -24,8 +24,10 @@ if true then
24 print "\n#### set 3 -> receive batched" 24 print "\n#### set 3 -> receive batched"
25 local fun = function() print "function test ok" end 25 local fun = function() print "function test ok" end
26 print(pcall(linda.set, linda, 'test', true, nil, fun)) 26 print(pcall(linda.set, linda, 'test', true, nil, fun))
27 local k,b,n,f = linda:receive(linda.batched, 'test', 3) -- read back the contents 27 -- read back the contents
28 local k,b,n,f = linda:receive(linda.batched, 'test', 3)
28 assert(linda:get("test") == nil) 29 assert(linda:get("test") == nil)
30 -- check they are ok
29 print(k, b, n) 31 print(k, b, n)
30 f() 32 f()
31 print "OK" 33 print "OK"
diff --git a/tests/nameof.lua b/tests/nameof.lua
index 58df3e2..f48a971 100644
--- a/tests/nameof.lua
+++ b/tests/nameof.lua
@@ -1,5 +1,10 @@
1local lanes = require "lanes".configure{nb_user_keepers = 100, on_state_create = function() end} 1local lanes = require "lanes".configure{nb_user_keepers = 100, on_state_create = function() end}
2 2
3local SLEEP = function(...)
4 local k, v = lanes.sleep(...)
5 assert(k == nil and v == "timeout")
6end
7
3print("Name of table: ", lanes.nameof({})) 8print("Name of table: ", lanes.nameof({}))
4print("Name of string.sub: ", lanes.nameof(string.sub)) 9print("Name of string.sub: ", lanes.nameof(string.sub))
5print("Name of print: ", lanes.nameof(print)) 10print("Name of print: ", lanes.nameof(print))
@@ -14,12 +19,12 @@ end
14 19
15-- start the lane without any library 20-- start the lane without any library
16local h = lanes.gen(nil, body)() 21local h = lanes.gen(nil, body)()
17lanes.sleep(0.1) 22SLEEP(0.1)
18print("Name of lane: ", lanes.nameof(h), "("..h.status..")") 23print("Name of lane: ", lanes.nameof(h), "("..h.status..")")
19assert(h.status == "running") 24assert(h.status == "running")
20-- cancel the lane 25-- cancel the lane
21h:cancel("line", 1) 26h:cancel("line", 1)
22lanes.sleep(0.1) 27SLEEP(0.1)
23print("Name of lane: ", lanes.nameof(h), "("..h.status..")") 28print("Name of lane: ", lanes.nameof(h), "("..h.status..")")
24assert(h.status == "cancelled") 29assert(h.status == "cancelled")
25print "TEST OK" 30print "TEST OK"
diff --git a/tests/objects.lua b/tests/objects.lua
index eed02bf..7668e45 100644
--- a/tests/objects.lua
+++ b/tests/objects.lua
@@ -9,27 +9,27 @@ lanes.configure()
9 9
10local linda= lanes.linda() 10local linda= lanes.linda()
11 11
12local start_lane= lanes.gen( "io", 12local start_lane= lanes.gen("io",
13 function( obj1 ) 13 function(obj1 )
14 14
15 assert( obj1.v ) 15 assert(obj1.v )
16 assert( obj1.print ) 16 assert(obj1.print )
17 17
18 assert( obj1 ) 18 assert(obj1 )
19 local mt1= getmetatable(obj1) 19 local mt1= getmetatable(obj1)
20 assert(mt1) 20 assert(mt1)
21 21
22 local k, obj2= linda:receive("") 22 local k, obj2= linda:receive("")
23 assert( obj2 ) 23 assert(k == "" and obj2 )
24 local mt2= getmetatable(obj2) 24 local mt2= getmetatable(obj2)
25 assert(mt2) 25 assert(mt2)
26 assert( mt1==mt2 ) 26 assert(mt1==mt2 )
27 27
28 local v= obj1:print() 28 local v= obj1:print()
29 assert( v=="aaa" ) 29 assert(v=="aaa" )
30 30
31 v= obj2:print() 31 v = obj2:print()
32 assert( v=="bbb" ) 32 assert(v=="bbb" )
33 33
34 return true 34 return true
35 end 35 end
@@ -37,7 +37,7 @@ local start_lane= lanes.gen( "io",
37 37
38 38
39local WR= function(str) 39local WR= function(str)
40 io.stderr:write( tostring(str).."\n") 40 io.stderr:write(tostring(str).."\n")
41end 41end
42 42
43 43
@@ -47,7 +47,7 @@ end
47-- having the methods 'fixed' in the object tables themselves. 47-- having the methods 'fixed' in the object tables themselves.
48-- 48--
49local o_mt= { 49local o_mt= {
50 __index= function( me, key ) 50 __index= function(me, key )
51 if key=="print" then 51 if key=="print" then
52 return function() WR(me.v); return me.v end 52 return function() WR(me.v); return me.v end
53 end 53 end
@@ -56,22 +56,22 @@ local o_mt= {
56 56
57local function obj_gen(v) 57local function obj_gen(v)
58 local o= { v=v } 58 local o= { v=v }
59 setmetatable( o, o_mt ) 59 setmetatable(o, o_mt )
60 return o 60 return o
61end 61end
62 62
63local a= obj_gen("aaa") 63local a= obj_gen("aaa")
64local b= obj_gen("bbb") 64local b= obj_gen("bbb")
65 65
66assert( a and b ) 66assert(a and b )
67 67
68local mt_a= getmetatable(a) 68local mt_a= getmetatable(a)
69local mt_b= getmetatable(b) 69local mt_b= getmetatable(b)
70assert( mt_a and mt_a==mt_b ) 70assert(mt_a and mt_a==mt_b )
71 71
72local h= start_lane(a) -- 1st object as parameter 72local h= start_lane(a) -- 1st object as parameter
73 73
74linda:send( "", b ) -- 2nd object via Linda 74linda:send("", b ) -- 2nd object via Linda
75 75
76assert( h[1]==true ) -- wait for return 76assert(h[1]==true ) -- wait for return
77 77
diff --git a/tests/protect_allocator.lua b/tests/protect_allocator.lua
index 5cbb1d8..994cb39 100644
--- a/tests/protect_allocator.lua
+++ b/tests/protect_allocator.lua
@@ -2,6 +2,11 @@ local print = print
2 2
3local lanes = require "lanes".configure{ with_timers = false, allocator="protected"} 3local lanes = require "lanes".configure{ with_timers = false, allocator="protected"}
4 4
5local SLEEP = function(...)
6 local k, v = lanes.sleep(...)
7 assert(k == nil and v == "timeout")
8end
9
5local linda = lanes.linda() 10local linda = lanes.linda()
6 11
7local body = function( id_) 12local body = function( id_)
@@ -38,7 +43,7 @@ end
38 43
39-- wait a bit 44-- wait a bit
40print "waiting a bit ..." 45print "waiting a bit ..."
41linda:receive( 2, "foo") 46SLEEP(2)
42-- tell lanes to start running 47-- tell lanes to start running
43print "GO!" 48print "GO!"
44for i = 1, COUNT do 49for i = 1, COUNT do
@@ -49,5 +54,5 @@ end
49print "wait for completion" 54print "wait for completion"
50linda:receive( linda.batched, "key", COUNT) 55linda:receive( linda.batched, "key", COUNT)
51print "waiting a bit more ..." 56print "waiting a bit more ..."
52linda:receive( 1, "foo") 57SLEEP(1)
53print "SUCCESS" 58print "SUCCESS"
diff --git a/tests/timer.lua b/tests/timer.lua
index a633286..4da1a50 100644
--- a/tests/timer.lua
+++ b/tests/timer.lua
@@ -100,7 +100,7 @@ assert( linda:get(T2) == nil )
100PRINT "...making sure no ticks are coming..." 100PRINT "...making sure no ticks are coming..."
101 101
102local k,v= linda:receive( 10, T1,T2 ) -- should not get any 102local k,v= linda:receive( 10, T1,T2 ) -- should not get any
103assert(v==nil) 103assert(k==nil and v == "timeout")
104 104
105lanes.timer_lane:cancel() -- hard cancel, 0 timeout 105lanes.timer_lane:cancel() -- hard cancel, 0 timeout
106print (lanes.timer_lane[1], lanes.timer_lane[2]) \ No newline at end of file 106print (lanes.timer_lane[1], lanes.timer_lane[2]) \ No newline at end of file
diff --git a/tests/track_lanes.lua b/tests/track_lanes.lua
index daaa94c..5ea9a4e 100644
--- a/tests/track_lanes.lua
+++ b/tests/track_lanes.lua
@@ -1,5 +1,9 @@
1local lanes = require "lanes" .configure{ with_timers = false, track_lanes = true} 1local lanes = require "lanes" .configure{ with_timers = false, track_lanes = true}
2local wait = lanes.sleep 2
3local SLEEP = function(...)
4 local k, v = lanes.sleep(...)
5 assert(k == nil and v == "timeout")
6end
3 7
4print "hello" 8print "hello"
5 9
@@ -43,7 +47,7 @@ g( "forever", 'indefinitely')
43g( "two_seconds", 2) 47g( "two_seconds", 2)
44 48
45-- give a bit of time to reach the linda waiting call 49-- give a bit of time to reach the linda waiting call
46wait( 0.1) 50SLEEP(0.1)
47 51
48-- list the known lanes (should be living lanes) 52-- list the known lanes (should be living lanes)
49local threads = track( "============= START", 2) 53local threads = track( "============= START", 2)
@@ -51,7 +55,7 @@ local threads = track( "============= START", 2)
51assert(threads[1].status == 'waiting' and threads[2].status == 'waiting') 55assert(threads[1].status == 'waiting' and threads[2].status == 'waiting')
52 56
53-- wait until "two_seconds has completed" 57-- wait until "two_seconds has completed"
54wait(2.1) 58SLEEP(2.1)
55 59
56local threads = track( "============= two_seconds dead", 2) 60local threads = track( "============= two_seconds dead", 2)
57-- two_seconds forever 61-- two_seconds forever
@@ -61,7 +65,7 @@ assert(threads[1].status == 'done' and threads[2].status == 'waiting')
61g( "two_seconds", 2) 65g( "two_seconds", 2)
62 66
63-- give a bit of time to reach the linda waiting call 67-- give a bit of time to reach the linda waiting call
64wait( 0.1) 68SLEEP( 0.1)
65 69
66-- list the known lanes 70-- list the known lanes
67-- unless garbage collector cleaned it, we should have 3 lanes 71-- unless garbage collector cleaned it, we should have 3 lanes