From bba4ecea15320dfbf8edf35f7361de374d895ace Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Wed, 26 Sep 2012 21:22:30 +0200 Subject: version 3.4.0 * new method linda:dump() that outputs the full contents of a linda as a table, also linked to __towatch for Decoda support * linda:receive() API change! * instead of [val, key], linda:receive( timeout, key) returns [key, val] * instead of [val, [...]], linda:receive( timeout, linda.batched key) returns [key, val[, ...]] this is to unify the return values of regular and batched mode, and to be able to tell when batched mode is interrupted by a lane cancellation * fixed Lua 5.2 build to take into account the "loaders"->"searchers" name change in 'package' module. * a bit of html cleanup and added some infos in the documentation regarding the Lanes internals --- tests/basic.lua | 68 +++++++++++++++++++++++++++++++++++++-------- tests/ehynes.lua | 2 +- tests/errhangtest.lua | 2 +- tests/fifo.lua | 2 +- tests/linda_perf.lua | 8 +++--- tests/nameof.lua | 4 +++ tests/objects.lua | 2 +- tests/parallel_os_calls.lua | 14 ++++++++++ tests/timer.lua | 4 +-- 9 files changed, 84 insertions(+), 22 deletions(-) create mode 100644 tests/nameof.lua create mode 100644 tests/parallel_os_calls.lua (limited to 'tests') 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 PRINT(" "..st) assert( st == "cancelled" ) +-- cancellation of lanes waiting on a linda +local limited = lanes.linda() +limited:limit( "key", 1) +-- [[################################################ +limited:send( "key", "hello") -- saturate linda +local wait_send = function() + local a,b + set_finalizer( function() print( "wait_send", a, b) end) + a,b = limited:send( "key", "bybye") -- infinite timeout, returns only when lane is cancelled +end + +local wait_send_lane = lanes.gen( "*", wait_send)() +repeat until wait_send_lane.status == "waiting" +print "wait_send_lane is waiting" +wait_send_lane:cancel() +repeat until wait_send_lane.status == "cancelled" +print "wait_send_lane is cancelled" +--################################################]] +local wait_receive = function() + local k, v + set_finalizer( function() print( "wait_receive", k, v) end) + k, v = limited:receive( "dummy") -- infinite timeout, returns only when lane is cancelled +end + +local wait_receive_lane = lanes.gen( "*", wait_receive)() +repeat until wait_receive_lane.status == "waiting" +print "wait_receive_lane is waiting" +wait_receive_lane:cancel() +repeat until wait_receive_lane.status == "cancelled" +print "wait_receive_lane is cancelled" +--################################################]] +local wait_receive_batched = function() + local k, v1, v2 + set_finalizer( function() print( "wait_receive_batched", k, v1, v2) end) + k, v1, v2 = limited:receive( limited.batched, "dummy", 2) -- infinite timeout, returns only when lane is cancelled +end + +local wait_receive_batched_lane = lanes.gen( "*", wait_receive_batched)() +repeat until wait_receive_batched_lane.status == "waiting" +print "wait_receive_batched_lane is waiting" +wait_receive_batched_lane:cancel() +repeat until wait_receive_batched_lane.status == "cancelled" +print "wait_receive_batched_lane is cancelled" +--################################################]] PRINT( "---=== Communications ===---") @@ -148,16 +192,16 @@ local chunk= function( linda ) WR( "Lane starts!\n" ) - local v - v=receive(); WR( v.." received\n" ); assert( v==1 ) - v=receive(); WR( v.." received\n" ); assert( v==2 ) - v=receive(); WR( v.." received\n" ); assert( v==3 ) + local k,v + k,v=receive(); WR( v.." received\n" ); assert( v==1 ) + k,v=receive(); WR( v.." received\n" ); assert( v==2 ) + k,v=receive(); WR( v.." received\n" ); assert( v==3 ) send( 1,2,3 ); WR( "1,2,3 sent\n" ) send 'a'; WR( "'a' sent\n" ) send { 'a', 'b', 'c', d=10 }; WR( "{'a','b','c',d=10} sent\n" ) - v=receive(); WR( v.." received\n" ); assert( v==4 ) + k,v=receive(); WR( v.." received\n" ); assert( v==4 ) WR( "Lane ends!\n" ) end @@ -170,7 +214,7 @@ assert( type(linda) == "userdata" ) local function PEEK() return linda:get("<-") end local function SEND(...) linda:send( "->", ... ) end -local function RECEIVE() return linda:receive( "<-" ) end +local function RECEIVE() local k,v = linda:receive( "<-" ) return v end local t= lanes_gen("io",chunk)(linda) -- prepare & launch @@ -226,7 +270,7 @@ local tc= lanes_gen( "io", local function STAGE(str) io.stderr:write( ch_in..": "..str.."\n" ) linda:send( nil, ch_out, str ) - local v= linda:receive( nil, ch_in ) + local k,v= linda:receive( nil, ch_in ) assert(v==str) end STAGE("Hello") @@ -264,13 +308,13 @@ local function chunk2( linda ) assert( info.linedefined > 200 ) -- start of 'chunk2' assert( info.currentline > info.linedefined ) -- line of 'debug.getinfo' assert( info.lastlinedefined > info.currentline ) -- end of 'chunk2' - local func,k= linda:receive( "down" ) + local k,func= linda:receive( "down" ) assert( type(func)=="function" ) assert( k=="down" ) func(linda) - local str= linda:receive( "down" ) + local k,str= linda:receive( "down" ) assert( str=="ok" ) linda:send( "up", function() return ":)" end, "ok2" ) @@ -282,19 +326,19 @@ linda:send( "down", function(linda) linda:send( "up", "ready!" ) end, "ok" ) -- wait to see if the tiny function gets executed -- -local s= linda:receive( "up" ) +local k,s= linda:receive( "up" ) PRINT(s) assert( s=="ready!" ) -- returns of the 'chunk2' itself -- -local f= linda:receive( "up" ) +local k,f= linda:receive( "up" ) assert( type(f)=="function" ) local s2= f() assert( s2==":)" ) -local ok2= linda:receive( "up" ) +local k,ok2= linda:receive( "up" ) assert( ok2 == "ok2" ) diff --git a/tests/ehynes.lua b/tests/ehynes.lua index fff008c..7609ddd 100644 --- a/tests/ehynes.lua +++ b/tests/ehynes.lua @@ -17,7 +17,7 @@ local receiver_gen = lanes.gen( 'base', 'os', 'string', 'io', PRINT_FMT( 'receiver for message %s entered', message_name ) local n = 1 while linda:receive(message_name) do - PRINT_FMT( '%s %d receieved', message_name, n ) + PRINT_FMT( '%s %d received', message_name, n ) n = n + 1 end end diff --git a/tests/errhangtest.lua b/tests/errhangtest.lua index 5974005..a541aad 100644 --- a/tests/errhangtest.lua +++ b/tests/errhangtest.lua @@ -9,6 +9,6 @@ local coro = coroutine.create(function() end) -- however, this should raise an error, not hang the program... print( pcall(linda.send,linda, 'test', "oh boy")) print( pcall(linda.send,linda, 'test', coro)) -res = linda:receive('test') +k,res = linda:receive('test') print( res) -- linda:send( 'test', coro) diff --git a/tests/fifo.lua b/tests/fifo.lua index 3c403fa..b68d8a4 100644 --- a/tests/fifo.lua +++ b/tests/fifo.lua @@ -23,7 +23,7 @@ local function FIFO() linda:send( nil, my_channel, ... ) end, receive = function(self, timeout) - linda:receive( timeout, my_channel ) + return linda:receive( timeout, my_channel ) end } end diff --git a/tests/linda_perf.lua b/tests/linda_perf.lua index fff2670..87e0da7 100644 --- a/tests/linda_perf.lua +++ b/tests/linda_perf.lua @@ -3,24 +3,24 @@ lanes.configure() -- this lane eats items in the linda one by one local eater = function( l, loop) - local val, key = l:receive( "go") + local key, val = l:receive( "go") for i = 1, loop do local val, key = l:receive( "key") --print( val) end -- print "loop is over" - val, key = l:receive( "done") + key, val = l:receive( "done") -- print( val) end -- this lane eats items in the linda in batches local batched = function( l, loop, batch) - local val, key = l:receive( "go") + local key, val = l:receive( "go") for i = 1, loop/batch do l:receive( l.batched, "key", batch) end print "loop is over" - val, key = l:receive( "done") + key, val = l:receive( "done") print( val) end diff --git a/tests/nameof.lua b/tests/nameof.lua new file mode 100644 index 0000000..ed20a32 --- /dev/null +++ b/tests/nameof.lua @@ -0,0 +1,4 @@ +lanes = require "lanes".configure() + +print( lanes.nameof( string.sub)) +print( lanes.nameof( print)) diff --git a/tests/objects.lua b/tests/objects.lua index 14cf9ba..eed02bf 100644 --- a/tests/objects.lua +++ b/tests/objects.lua @@ -19,7 +19,7 @@ local start_lane= lanes.gen( "io", local mt1= getmetatable(obj1) assert(mt1) - local obj2= linda:receive("") + local k, obj2= linda:receive("") assert( obj2 ) local mt2= getmetatable(obj2) assert(mt2) diff --git a/tests/parallel_os_calls.lua b/tests/parallel_os_calls.lua new file mode 100644 index 0000000..b8cffa1 --- /dev/null +++ b/tests/parallel_os_calls.lua @@ -0,0 +1,14 @@ +local lanes = require "lanes".configure(1) +print( os.date()) +local linda = lanes.linda() +lanes.gen("os,base", function() linda:receive(10, "null") print("finished_sleeping " .. os.date()) end)() +lanes.gen("os,base", function() linda:receive(10, "null") print("finished_sleeping " .. os.date()) end)() +lanes.gen("os,base", function() linda:receive(10, "null") print("finished_sleeping " .. os.date()) end)() +lanes.gen("os,base", function() linda:receive(10, "null") print("finished_sleeping " .. os.date()) end)() + +--[[ +lanes.gen("os,base", function() os.execute('sleep 10 && echo finished_sleeping') print( os.date()) end)() +lanes.gen("os,base", function() os.execute('sleep 10 && echo finished_sleeping') print( os.date()) end)() +lanes.gen("os,base", function() os.execute('sleep 10 && echo finished_sleeping') print( os.date()) end)() +lanes.gen("os,base", function() os.execute('sleep 10 && echo finished_sleeping') print( os.date()) end)() +]] diff --git a/tests/timer.lua b/tests/timer.lua index c6f510b..84e6dec 100644 --- a/tests/timer.lua +++ b/tests/timer.lua @@ -35,7 +35,7 @@ local caught= {} -- { [T1]= bool, [T2]= bool } while true do io.stderr:write("waiting...\t") - local v,channel= linda:receive( 6.0, T1,T2 ) + local channel, v = linda:receive( 6.0, T1,T2 ) assert( channel==T1 or channel==T2 ) caught[channel]= true @@ -89,6 +89,6 @@ assert( linda:get(T2) == nil ) PRINT "...making sure no ticks are coming..." -local v= linda:receive( 1.5, T1,T2 ) -- should not get any +local k,v= linda:receive( 10, T1,T2 ) -- should not get any assert(v==nil) -- cgit v1.2.3-55-g6feb