diff options
| author | Benoit Germain <bnt.germain@gmail.com> | 2019-04-19 14:58:25 +0200 |
|---|---|---|
| committer | Benoit Germain <bnt.germain@gmail.com> | 2019-04-19 14:58:25 +0200 |
| commit | be1e9d37d9809ee55f26d811208fa64ea9b3785a (patch) | |
| tree | 1b0122ed7e7f501c64814cdbbf03e5941dbab51c /tests/basic.lua | |
| parent | 52934d46eed850c23a9b21125be73e987f34e772 (diff) | |
| download | lanes-be1e9d37d9809ee55f26d811208fa64ea9b3785a.tar.gz lanes-be1e9d37d9809ee55f26d811208fa64ea9b3785a.tar.bz2 lanes-be1e9d37d9809ee55f26d811208fa64ea9b3785a.zip | |
lane:cancel internal code refactorization
Diffstat (limited to 'tests/basic.lua')
| -rw-r--r-- | tests/basic.lua | 112 |
1 files changed, 56 insertions, 56 deletions
diff --git a/tests/basic.lua b/tests/basic.lua index 7d42ad5..020fe78 100644 --- a/tests/basic.lua +++ b/tests/basic.lua | |||
| @@ -26,7 +26,7 @@ local function PRINT(...) | |||
| 26 | end | 26 | end |
| 27 | 27 | ||
| 28 | local gc_cb = function( name_, status_) | 28 | local gc_cb = function( name_, status_) |
| 29 | PRINT( " ---> lane '" .. name_ .. "' collected with status " .. status_) | 29 | PRINT( " ---> lane '" .. name_ .. "' collected with status " .. status_) |
| 30 | end | 30 | end |
| 31 | --gc_cb = nil | 31 | --gc_cb = nil |
| 32 | 32 | ||
| @@ -77,7 +77,7 @@ local function task( a, b, c ) | |||
| 77 | end | 77 | end |
| 78 | 78 | ||
| 79 | local task_launch= lanes_gen( "", { globals={hey=true}, gc_cb = gc_cb}, task ) | 79 | local task_launch= lanes_gen( "", { globals={hey=true}, gc_cb = gc_cb}, task ) |
| 80 | -- base stdlibs, normal priority | 80 | -- base stdlibs, normal priority |
| 81 | 81 | ||
| 82 | -- 'task_launch' is a factory of multithreaded tasks, we can launch several: | 82 | -- 'task_launch' is a factory of multithreaded tasks, we can launch several: |
| 83 | 83 | ||
| @@ -155,12 +155,12 @@ limited:limit( "key", 1) | |||
| 155 | -- [[################################################ | 155 | -- [[################################################ |
| 156 | limited:send( "key", "hello") -- saturate linda | 156 | limited:send( "key", "hello") -- saturate linda |
| 157 | for k, v in pairs( limited:dump()) do | 157 | for k, v in pairs( limited:dump()) do |
| 158 | PRINT("limited[" .. tostring( k) .. "] = " .. tostring( v)) | 158 | PRINT("limited[" .. tostring( k) .. "] = " .. tostring( v)) |
| 159 | end | 159 | end |
| 160 | local wait_send = function() | 160 | local wait_send = function() |
| 161 | local a,b | 161 | local a,b |
| 162 | set_finalizer( function() print( "wait_send", a, b) end) | 162 | set_finalizer( function() print( "wait_send", a, b) end) |
| 163 | a,b = limited:send( "key", "bybye") -- infinite timeout, returns only when lane is cancelled | 163 | a,b = limited:send( "key", "bybye") -- infinite timeout, returns only when lane is cancelled |
| 164 | end | 164 | end |
| 165 | 165 | ||
| 166 | local wait_send_lane = lanes.gen( "*", wait_send)() | 166 | local wait_send_lane = lanes.gen( "*", wait_send)() |
| @@ -171,9 +171,9 @@ repeat until wait_send_lane.status == "cancelled" | |||
| 171 | print "wait_send_lane is cancelled" | 171 | print "wait_send_lane is cancelled" |
| 172 | --################################################]] | 172 | --################################################]] |
| 173 | local wait_receive = function() | 173 | local wait_receive = function() |
| 174 | local k, v | 174 | local k, v |
| 175 | set_finalizer( function() print( "wait_receive", k, v) end) | 175 | set_finalizer( function() print( "wait_receive", k, v) end) |
| 176 | k, v = limited:receive( "dummy") -- infinite timeout, returns only when lane is cancelled | 176 | k, v = limited:receive( "dummy") -- infinite timeout, returns only when lane is cancelled |
| 177 | end | 177 | end |
| 178 | 178 | ||
| 179 | local wait_receive_lane = lanes.gen( "*", wait_receive)() | 179 | local wait_receive_lane = lanes.gen( "*", wait_receive)() |
| @@ -184,9 +184,9 @@ repeat until wait_receive_lane.status == "cancelled" | |||
| 184 | print "wait_receive_lane is cancelled" | 184 | print "wait_receive_lane is cancelled" |
| 185 | --################################################]] | 185 | --################################################]] |
| 186 | local wait_receive_batched = function() | 186 | local wait_receive_batched = function() |
| 187 | local k, v1, v2 | 187 | local k, v1, v2 |
| 188 | set_finalizer( function() print( "wait_receive_batched", k, v1, v2) end) | 188 | set_finalizer( function() print( "wait_receive_batched", k, v1, v2) end) |
| 189 | k, v1, v2 = limited:receive( limited.batched, "dummy", 2) -- infinite timeout, returns only when lane is cancelled | 189 | k, v1, v2 = limited:receive( limited.batched, "dummy", 2) -- infinite timeout, returns only when lane is cancelled |
| 190 | end | 190 | end |
| 191 | 191 | ||
| 192 | local wait_receive_batched_lane = lanes.gen( "*", wait_receive_batched)() | 192 | local wait_receive_batched_lane = lanes.gen( "*", wait_receive_batched)() |
| @@ -206,7 +206,7 @@ PRINT( "\n\n", "---=== Communications ===---", "\n\n") | |||
| 206 | local function WR(...) io.stderr:write(...) end | 206 | local function WR(...) io.stderr:write(...) end |
| 207 | 207 | ||
| 208 | local chunk= function( linda ) | 208 | local chunk= function( linda ) |
| 209 | set_debug_threadname "chunk" | 209 | set_debug_threadname "chunk" |
| 210 | local function receive() return linda:receive( "->" ) end | 210 | local function receive() return linda:receive( "->" ) end |
| 211 | local function send(...) linda:send( "<-", ... ) end | 211 | local function send(...) linda:send( "<-", ... ) end |
| 212 | 212 | ||
| @@ -254,9 +254,9 @@ local a,b,c= RECEIVE(), RECEIVE(), RECEIVE() | |||
| 254 | 254 | ||
| 255 | print( "lane status: " .. t.status) | 255 | print( "lane status: " .. t.status) |
| 256 | if t.status == "error" then | 256 | if t.status == "error" then |
| 257 | print( t:join()) | 257 | print( t:join()) |
| 258 | else | 258 | else |
| 259 | WR( a..", "..b..", "..c.." received\n" ) | 259 | WR( a..", "..b..", "..c.." received\n" ) |
| 260 | end | 260 | end |
| 261 | 261 | ||
| 262 | assert( a==1 and b==2 and c==3 ) | 262 | assert( a==1 and b==2 and c==3 ) |
| @@ -286,50 +286,50 @@ linda: receive( 1, "wait") | |||
| 286 | PRINT( "\n\n", "---=== Stdlib naming ===---", "\n\n") | 286 | PRINT( "\n\n", "---=== Stdlib naming ===---", "\n\n") |
| 287 | 287 | ||
| 288 | local function dump_g( _x) | 288 | local function dump_g( _x) |
| 289 | set_debug_threadname "dump_g" | 289 | set_debug_threadname "dump_g" |
| 290 | assert(print) | 290 | assert(print) |
| 291 | print( "### dumping _G for '" .. _x .. "'") | 291 | print( "### dumping _G for '" .. _x .. "'") |
| 292 | for k, v in pairs( _G) do | 292 | for k, v in pairs( _G) do |
| 293 | print( "\t" .. k .. ": " .. type( v)) | 293 | print( "\t" .. k .. ": " .. type( v)) |
| 294 | end | 294 | end |
| 295 | return true | 295 | return true |
| 296 | end | 296 | end |
| 297 | 297 | ||
| 298 | local function io_os_f( _x) | 298 | local function io_os_f( _x) |
| 299 | set_debug_threadname "io_os_f" | 299 | set_debug_threadname "io_os_f" |
| 300 | assert(print) | 300 | assert(print) |
| 301 | print( "### checking io and os libs existence for '" .. _x .. "'") | 301 | print( "### checking io and os libs existence for '" .. _x .. "'") |
| 302 | assert(io) | 302 | assert(io) |
| 303 | assert(os) | 303 | assert(os) |
| 304 | return true | 304 | return true |
| 305 | end | 305 | end |
| 306 | 306 | ||
| 307 | local function coro_f( _x) | 307 | local function coro_f( _x) |
| 308 | set_debug_threadname "coro_f" | 308 | set_debug_threadname "coro_f" |
| 309 | assert(print) | 309 | assert(print) |
| 310 | print( "### checking coroutine lib existence for '" .. _x .. "'") | 310 | print( "### checking coroutine lib existence for '" .. _x .. "'") |
| 311 | assert(coroutine) | 311 | assert(coroutine) |
| 312 | return true | 312 | return true |
| 313 | end | 313 | end |
| 314 | 314 | ||
| 315 | assert.fails( function() lanes_gen( "xxx", {gc_cb = gc_cb}, io_os_f ) end ) | 315 | assert.fails( function() lanes_gen( "xxx", {gc_cb = gc_cb}, io_os_f ) end ) |
| 316 | 316 | ||
| 317 | local stdlib_naming_tests = | 317 | local stdlib_naming_tests = |
| 318 | { | 318 | { |
| 319 | -- { "", dump_g}, | 319 | -- { "", dump_g}, |
| 320 | -- { "coroutine", dump_g}, | 320 | -- { "coroutine", dump_g}, |
| 321 | -- { "io", dump_g}, | 321 | -- { "io", dump_g}, |
| 322 | -- { "bit32", dump_g}, | 322 | -- { "bit32", dump_g}, |
| 323 | { "coroutine", coro_f}, | 323 | { "coroutine", coro_f}, |
| 324 | { "*", io_os_f}, | 324 | { "*", io_os_f}, |
| 325 | { "io,os", io_os_f}, | 325 | { "io,os", io_os_f}, |
| 326 | { "io+os", io_os_f}, | 326 | { "io+os", io_os_f}, |
| 327 | { "io,os,base", io_os_f}, | 327 | { "io,os,base", io_os_f}, |
| 328 | } | 328 | } |
| 329 | 329 | ||
| 330 | for _, t in ipairs( stdlib_naming_tests) do | 330 | for _, t in ipairs( stdlib_naming_tests) do |
| 331 | local f= lanes_gen( t[1], {gc_cb = gc_cb}, t[2]) -- any delimiter will do | 331 | local f= lanes_gen( t[1], {gc_cb = gc_cb}, t[2]) -- any delimiter will do |
| 332 | assert( f(t[1])[1] ) | 332 | assert( f(t[1])[1] ) |
| 333 | end | 333 | end |
| 334 | 334 | ||
| 335 | collectgarbage() | 335 | collectgarbage() |
| @@ -344,7 +344,7 @@ PRINT( "\n\n", "---=== Comms criss cross ===---", "\n\n") | |||
| 344 | -- | 344 | -- |
| 345 | local tc= lanes_gen( "io", {gc_cb = gc_cb}, | 345 | local tc= lanes_gen( "io", {gc_cb = gc_cb}, |
| 346 | function( linda, ch_in, ch_out ) | 346 | function( linda, ch_in, ch_out ) |
| 347 | set_debug_threadname( "criss cross " .. ch_in .. " -> " .. ch_out) | 347 | set_debug_threadname( "criss cross " .. ch_in .. " -> " .. ch_out) |
| 348 | local function STAGE(str) | 348 | local function STAGE(str) |
| 349 | io.stderr:write( ch_in..": "..str.."\n" ) | 349 | io.stderr:write( ch_in..": "..str.."\n" ) |
| 350 | linda:send( nil, ch_out, str ) | 350 | linda:send( nil, ch_out, str ) |
| @@ -412,7 +412,7 @@ linda:send( "down", function(linda) linda:send( "up", "ready!" ) end, | |||
| 412 | -- | 412 | -- |
| 413 | local k,s= linda:receive( 1, "up" ) | 413 | local k,s= linda:receive( 1, "up" ) |
| 414 | if t2.status == "error" then | 414 | if t2.status == "error" then |
| 415 | print( "t2 error: " , t2:join()) | 415 | print( "t2 error: " , t2:join()) |
| 416 | end | 416 | end |
| 417 | PRINT(s) | 417 | PRINT(s) |
| 418 | assert( s=="ready!" ) | 418 | assert( s=="ready!" ) |
| @@ -440,13 +440,13 @@ PRINT( "\n\n", "---=== :join test ===---", "\n\n") | |||
| 440 | 440 | ||
| 441 | local S= lanes_gen( "table", {gc_cb = gc_cb}, | 441 | local S= lanes_gen( "table", {gc_cb = gc_cb}, |
| 442 | function(arg) | 442 | function(arg) |
| 443 | set_debug_threadname "join test lane" | 443 | set_debug_threadname "join test lane" |
| 444 | set_finalizer( function() end) | 444 | set_finalizer( function() end) |
| 445 | aux= {} | 445 | aux= {} |
| 446 | for i, v in ipairs(arg) do | 446 | for i, v in ipairs(arg) do |
| 447 | table.insert (aux, 1, v) | 447 | table.insert (aux, 1, v) |
| 448 | end | 448 | end |
| 449 | -- unpack was renamed table.unpack in Lua 5.2: cater for both! | 449 | -- unpack was renamed table.unpack in Lua 5.2: cater for both! |
| 450 | return (unpack or table.unpack)(aux) | 450 | return (unpack or table.unpack)(aux) |
| 451 | end ) | 451 | end ) |
| 452 | 452 | ||
| @@ -456,13 +456,13 @@ linda:receive(0.5, "gloupti") | |||
| 456 | print( "joining with '" .. h:get_debug_threadname() .. "'") | 456 | print( "joining with '" .. h:get_debug_threadname() .. "'") |
| 457 | local a,b,c,d= h:join() | 457 | local a,b,c,d= h:join() |
| 458 | if h.status == "error" then | 458 | if h.status == "error" then |
| 459 | print( h:get_debug_threadname(), "error: " , a, b, c, d) | 459 | print( h:get_debug_threadname(), "error: " , a, b, c, d) |
| 460 | else | 460 | else |
| 461 | print( h:get_debug_threadname(), a,b,c,d) | 461 | print( h:get_debug_threadname(), a,b,c,d) |
| 462 | assert(a==14) | 462 | assert(a==14) |
| 463 | assert(b==13) | 463 | assert(b==13) |
| 464 | assert(c==12) | 464 | assert(c==12) |
| 465 | assert(d==nil) | 465 | assert(d==nil) |
| 466 | end | 466 | end |
| 467 | 467 | ||
| 468 | local nameof_type, nameof_name = lanes.nameof( print) | 468 | local nameof_type, nameof_name = lanes.nameof( print) |
