aboutsummaryrefslogtreecommitdiff
path: root/tests/basic.lua
diff options
context:
space:
mode:
Diffstat (limited to 'tests/basic.lua')
-rw-r--r--tests/basic.lua68
1 files changed, 56 insertions, 12 deletions
diff --git a/tests/basic.lua b/tests/basic.lua
index eb8c04b..3eee59e 100644
--- a/tests/basic.lua
+++ b/tests/basic.lua
@@ -136,6 +136,50 @@ end
136PRINT(" "..st) 136PRINT(" "..st)
137assert( st == "cancelled" ) 137assert( st == "cancelled" )
138 138
139-- cancellation of lanes waiting on a linda
140local limited = lanes.linda()
141limited:limit( "key", 1)
142-- [[################################################
143limited:send( "key", "hello") -- saturate linda
144local wait_send = function()
145 local a,b
146 set_finalizer( function() print( "wait_send", a, b) end)
147 a,b = limited:send( "key", "bybye") -- infinite timeout, returns only when lane is cancelled
148end
149
150local wait_send_lane = lanes.gen( "*", wait_send)()
151repeat until wait_send_lane.status == "waiting"
152print "wait_send_lane is waiting"
153wait_send_lane:cancel()
154repeat until wait_send_lane.status == "cancelled"
155print "wait_send_lane is cancelled"
156--################################################]]
157local wait_receive = function()
158 local k, v
159 set_finalizer( function() print( "wait_receive", k, v) end)
160 k, v = limited:receive( "dummy") -- infinite timeout, returns only when lane is cancelled
161end
162
163local wait_receive_lane = lanes.gen( "*", wait_receive)()
164repeat until wait_receive_lane.status == "waiting"
165print "wait_receive_lane is waiting"
166wait_receive_lane:cancel()
167repeat until wait_receive_lane.status == "cancelled"
168print "wait_receive_lane is cancelled"
169--################################################]]
170local wait_receive_batched = function()
171 local k, v1, v2
172 set_finalizer( function() print( "wait_receive_batched", k, v1, v2) end)
173 k, v1, v2 = limited:receive( limited.batched, "dummy", 2) -- infinite timeout, returns only when lane is cancelled
174end
175
176local wait_receive_batched_lane = lanes.gen( "*", wait_receive_batched)()
177repeat until wait_receive_batched_lane.status == "waiting"
178print "wait_receive_batched_lane is waiting"
179wait_receive_batched_lane:cancel()
180repeat until wait_receive_batched_lane.status == "cancelled"
181print "wait_receive_batched_lane is cancelled"
182--################################################]]
139 183
140PRINT( "---=== Communications ===---") 184PRINT( "---=== Communications ===---")
141 185
@@ -148,16 +192,16 @@ local chunk= function( linda )
148 192
149 WR( "Lane starts!\n" ) 193 WR( "Lane starts!\n" )
150 194
151 local v 195 local k,v
152 v=receive(); WR( v.." received\n" ); assert( v==1 ) 196 k,v=receive(); WR( v.." received\n" ); assert( v==1 )
153 v=receive(); WR( v.." received\n" ); assert( v==2 ) 197 k,v=receive(); WR( v.." received\n" ); assert( v==2 )
154 v=receive(); WR( v.." received\n" ); assert( v==3 ) 198 k,v=receive(); WR( v.." received\n" ); assert( v==3 )
155 199
156 send( 1,2,3 ); WR( "1,2,3 sent\n" ) 200 send( 1,2,3 ); WR( "1,2,3 sent\n" )
157 send 'a'; WR( "'a' sent\n" ) 201 send 'a'; WR( "'a' sent\n" )
158 send { 'a', 'b', 'c', d=10 }; WR( "{'a','b','c',d=10} sent\n" ) 202 send { 'a', 'b', 'c', d=10 }; WR( "{'a','b','c',d=10} sent\n" )
159 203
160 v=receive(); WR( v.." received\n" ); assert( v==4 ) 204 k,v=receive(); WR( v.." received\n" ); assert( v==4 )
161 205
162 WR( "Lane ends!\n" ) 206 WR( "Lane ends!\n" )
163end 207end
@@ -170,7 +214,7 @@ assert( type(linda) == "userdata" )
170 214
171local function PEEK() return linda:get("<-") end 215local function PEEK() return linda:get("<-") end
172local function SEND(...) linda:send( "->", ... ) end 216local function SEND(...) linda:send( "->", ... ) end
173local function RECEIVE() return linda:receive( "<-" ) end 217local function RECEIVE() local k,v = linda:receive( "<-" ) return v end
174 218
175local t= lanes_gen("io",chunk)(linda) -- prepare & launch 219local t= lanes_gen("io",chunk)(linda) -- prepare & launch
176 220
@@ -226,7 +270,7 @@ local tc= lanes_gen( "io",
226 local function STAGE(str) 270 local function STAGE(str)
227 io.stderr:write( ch_in..": "..str.."\n" ) 271 io.stderr:write( ch_in..": "..str.."\n" )
228 linda:send( nil, ch_out, str ) 272 linda:send( nil, ch_out, str )
229 local v= linda:receive( nil, ch_in ) 273 local k,v= linda:receive( nil, ch_in )
230 assert(v==str) 274 assert(v==str)
231 end 275 end
232 STAGE("Hello") 276 STAGE("Hello")
@@ -264,13 +308,13 @@ local function chunk2( linda )
264 assert( info.linedefined > 200 ) -- start of 'chunk2' 308 assert( info.linedefined > 200 ) -- start of 'chunk2'
265 assert( info.currentline > info.linedefined ) -- line of 'debug.getinfo' 309 assert( info.currentline > info.linedefined ) -- line of 'debug.getinfo'
266 assert( info.lastlinedefined > info.currentline ) -- end of 'chunk2' 310 assert( info.lastlinedefined > info.currentline ) -- end of 'chunk2'
267 local func,k= linda:receive( "down" ) 311 local k,func= linda:receive( "down" )
268 assert( type(func)=="function" ) 312 assert( type(func)=="function" )
269 assert( k=="down" ) 313 assert( k=="down" )
270 314
271 func(linda) 315 func(linda)
272 316
273 local str= linda:receive( "down" ) 317 local k,str= linda:receive( "down" )
274 assert( str=="ok" ) 318 assert( str=="ok" )
275 319
276 linda:send( "up", function() return ":)" end, "ok2" ) 320 linda:send( "up", function() return ":)" end, "ok2" )
@@ -282,19 +326,19 @@ linda:send( "down", function(linda) linda:send( "up", "ready!" ) end,
282 "ok" ) 326 "ok" )
283-- wait to see if the tiny function gets executed 327-- wait to see if the tiny function gets executed
284-- 328--
285local s= linda:receive( "up" ) 329local k,s= linda:receive( "up" )
286PRINT(s) 330PRINT(s)
287assert( s=="ready!" ) 331assert( s=="ready!" )
288 332
289-- returns of the 'chunk2' itself 333-- returns of the 'chunk2' itself
290-- 334--
291local f= linda:receive( "up" ) 335local k,f= linda:receive( "up" )
292assert( type(f)=="function" ) 336assert( type(f)=="function" )
293 337
294local s2= f() 338local s2= f()
295assert( s2==":)" ) 339assert( s2==":)" )
296 340
297local ok2= linda:receive( "up" ) 341local k,ok2= linda:receive( "up" )
298assert( ok2 == "ok2" ) 342assert( ok2 == "ok2" )
299 343
300 344