diff options
Diffstat (limited to 'tests/basic.lua')
| -rw-r--r-- | tests/basic.lua | 68 |
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 | |||
| 136 | PRINT(" "..st) | 136 | PRINT(" "..st) |
| 137 | assert( st == "cancelled" ) | 137 | assert( st == "cancelled" ) |
| 138 | 138 | ||
| 139 | -- cancellation of lanes waiting on a linda | ||
| 140 | local limited = lanes.linda() | ||
| 141 | limited:limit( "key", 1) | ||
| 142 | -- [[################################################ | ||
| 143 | limited:send( "key", "hello") -- saturate linda | ||
| 144 | local 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 | ||
| 148 | end | ||
| 149 | |||
| 150 | local wait_send_lane = lanes.gen( "*", wait_send)() | ||
| 151 | repeat until wait_send_lane.status == "waiting" | ||
| 152 | print "wait_send_lane is waiting" | ||
| 153 | wait_send_lane:cancel() | ||
| 154 | repeat until wait_send_lane.status == "cancelled" | ||
| 155 | print "wait_send_lane is cancelled" | ||
| 156 | --################################################]] | ||
| 157 | local 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 | ||
| 161 | end | ||
| 162 | |||
| 163 | local wait_receive_lane = lanes.gen( "*", wait_receive)() | ||
| 164 | repeat until wait_receive_lane.status == "waiting" | ||
| 165 | print "wait_receive_lane is waiting" | ||
| 166 | wait_receive_lane:cancel() | ||
| 167 | repeat until wait_receive_lane.status == "cancelled" | ||
| 168 | print "wait_receive_lane is cancelled" | ||
| 169 | --################################################]] | ||
| 170 | local 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 | ||
| 174 | end | ||
| 175 | |||
| 176 | local wait_receive_batched_lane = lanes.gen( "*", wait_receive_batched)() | ||
| 177 | repeat until wait_receive_batched_lane.status == "waiting" | ||
| 178 | print "wait_receive_batched_lane is waiting" | ||
| 179 | wait_receive_batched_lane:cancel() | ||
| 180 | repeat until wait_receive_batched_lane.status == "cancelled" | ||
| 181 | print "wait_receive_batched_lane is cancelled" | ||
| 182 | --################################################]] | ||
| 139 | 183 | ||
| 140 | PRINT( "---=== Communications ===---") | 184 | PRINT( "---=== 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" ) |
| 163 | end | 207 | end |
| @@ -170,7 +214,7 @@ assert( type(linda) == "userdata" ) | |||
| 170 | 214 | ||
| 171 | local function PEEK() return linda:get("<-") end | 215 | local function PEEK() return linda:get("<-") end |
| 172 | local function SEND(...) linda:send( "->", ... ) end | 216 | local function SEND(...) linda:send( "->", ... ) end |
| 173 | local function RECEIVE() return linda:receive( "<-" ) end | 217 | local function RECEIVE() local k,v = linda:receive( "<-" ) return v end |
| 174 | 218 | ||
| 175 | local t= lanes_gen("io",chunk)(linda) -- prepare & launch | 219 | local 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 | -- |
| 285 | local s= linda:receive( "up" ) | 329 | local k,s= linda:receive( "up" ) |
| 286 | PRINT(s) | 330 | PRINT(s) |
| 287 | assert( s=="ready!" ) | 331 | assert( s=="ready!" ) |
| 288 | 332 | ||
| 289 | -- returns of the 'chunk2' itself | 333 | -- returns of the 'chunk2' itself |
| 290 | -- | 334 | -- |
| 291 | local f= linda:receive( "up" ) | 335 | local k,f= linda:receive( "up" ) |
| 292 | assert( type(f)=="function" ) | 336 | assert( type(f)=="function" ) |
| 293 | 337 | ||
| 294 | local s2= f() | 338 | local s2= f() |
| 295 | assert( s2==":)" ) | 339 | assert( s2==":)" ) |
| 296 | 340 | ||
| 297 | local ok2= linda:receive( "up" ) | 341 | local k,ok2= linda:receive( "up" ) |
| 298 | assert( ok2 == "ok2" ) | 342 | assert( ok2 == "ok2" ) |
| 299 | 343 | ||
| 300 | 344 | ||
