diff options
author | Benoit Germain <bnt.germain@gmail.com> | 2012-09-26 21:22:30 +0200 |
---|---|---|
committer | Benoit Germain <bnt.germain@gmail.com> | 2012-09-26 21:22:30 +0200 |
commit | bba4ecea15320dfbf8edf35f7361de374d895ace (patch) | |
tree | 73bd0bfd29d2c4dbfe3a46681b0e3b2261352207 /tests | |
parent | 282f59ca02d1d1f304b9126b84a8b1427caf3172 (diff) | |
download | lanes-bba4ecea15320dfbf8edf35f7361de374d895ace.tar.gz lanes-bba4ecea15320dfbf8edf35f7361de374d895ace.tar.bz2 lanes-bba4ecea15320dfbf8edf35f7361de374d895ace.zip |
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
Diffstat (limited to 'tests')
-rw-r--r-- | tests/basic.lua | 68 | ||||
-rw-r--r-- | tests/ehynes.lua | 2 | ||||
-rw-r--r-- | tests/errhangtest.lua | 2 | ||||
-rw-r--r-- | tests/fifo.lua | 2 | ||||
-rw-r--r-- | tests/linda_perf.lua | 8 | ||||
-rw-r--r-- | tests/nameof.lua | 4 | ||||
-rw-r--r-- | tests/objects.lua | 2 | ||||
-rw-r--r-- | tests/parallel_os_calls.lua | 14 | ||||
-rw-r--r-- | tests/timer.lua | 4 |
9 files changed, 84 insertions, 22 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 | ||
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', | |||
17 | PRINT_FMT( 'receiver for message %s entered', message_name ) | 17 | PRINT_FMT( 'receiver for message %s entered', message_name ) |
18 | local n = 1 | 18 | local n = 1 |
19 | while linda:receive(message_name) do | 19 | while linda:receive(message_name) do |
20 | PRINT_FMT( '%s %d receieved', message_name, n ) | 20 | PRINT_FMT( '%s %d received', message_name, n ) |
21 | n = n + 1 | 21 | n = n + 1 |
22 | end | 22 | end |
23 | end | 23 | 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) | |||
9 | -- however, this should raise an error, not hang the program... | 9 | -- however, this should raise an error, not hang the program... |
10 | print( pcall(linda.send,linda, 'test', "oh boy")) | 10 | print( pcall(linda.send,linda, 'test', "oh boy")) |
11 | print( pcall(linda.send,linda, 'test', coro)) | 11 | print( pcall(linda.send,linda, 'test', coro)) |
12 | res = linda:receive('test') | 12 | k,res = linda:receive('test') |
13 | print( res) | 13 | print( res) |
14 | -- linda:send( 'test', coro) | 14 | -- 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() | |||
23 | linda:send( nil, my_channel, ... ) | 23 | linda:send( nil, my_channel, ... ) |
24 | end, | 24 | end, |
25 | receive = function(self, timeout) | 25 | receive = function(self, timeout) |
26 | linda:receive( timeout, my_channel ) | 26 | return linda:receive( timeout, my_channel ) |
27 | end | 27 | end |
28 | } | 28 | } |
29 | end | 29 | 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() | |||
3 | 3 | ||
4 | -- this lane eats items in the linda one by one | 4 | -- this lane eats items in the linda one by one |
5 | local eater = function( l, loop) | 5 | local eater = function( l, loop) |
6 | local val, key = l:receive( "go") | 6 | local key, val = l:receive( "go") |
7 | for i = 1, loop do | 7 | for i = 1, loop do |
8 | local val, key = l:receive( "key") | 8 | local val, key = l:receive( "key") |
9 | --print( val) | 9 | --print( val) |
10 | end | 10 | end |
11 | -- print "loop is over" | 11 | -- print "loop is over" |
12 | val, key = l:receive( "done") | 12 | key, val = l:receive( "done") |
13 | -- print( val) | 13 | -- print( val) |
14 | end | 14 | end |
15 | 15 | ||
16 | -- this lane eats items in the linda in batches | 16 | -- this lane eats items in the linda in batches |
17 | local batched = function( l, loop, batch) | 17 | local batched = function( l, loop, batch) |
18 | local val, key = l:receive( "go") | 18 | local key, val = l:receive( "go") |
19 | for i = 1, loop/batch do | 19 | for i = 1, loop/batch do |
20 | l:receive( l.batched, "key", batch) | 20 | l:receive( l.batched, "key", batch) |
21 | end | 21 | end |
22 | print "loop is over" | 22 | print "loop is over" |
23 | val, key = l:receive( "done") | 23 | key, val = l:receive( "done") |
24 | print( val) | 24 | print( val) |
25 | end | 25 | end |
26 | 26 | ||
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 @@ | |||
1 | lanes = require "lanes".configure() | ||
2 | |||
3 | print( lanes.nameof( string.sub)) | ||
4 | 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", | |||
19 | local mt1= getmetatable(obj1) | 19 | local mt1= getmetatable(obj1) |
20 | assert(mt1) | 20 | assert(mt1) |
21 | 21 | ||
22 | local obj2= linda:receive("") | 22 | local k, obj2= linda:receive("") |
23 | assert( obj2 ) | 23 | assert( obj2 ) |
24 | local mt2= getmetatable(obj2) | 24 | local mt2= getmetatable(obj2) |
25 | assert(mt2) | 25 | 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 @@ | |||
1 | local lanes = require "lanes".configure(1) | ||
2 | print( os.date()) | ||
3 | local linda = lanes.linda() | ||
4 | lanes.gen("os,base", function() linda:receive(10, "null") print("finished_sleeping " .. os.date()) end)() | ||
5 | lanes.gen("os,base", function() linda:receive(10, "null") print("finished_sleeping " .. os.date()) end)() | ||
6 | lanes.gen("os,base", function() linda:receive(10, "null") print("finished_sleeping " .. os.date()) end)() | ||
7 | lanes.gen("os,base", function() linda:receive(10, "null") print("finished_sleeping " .. os.date()) end)() | ||
8 | |||
9 | --[[ | ||
10 | lanes.gen("os,base", function() os.execute('sleep 10 && echo finished_sleeping') print( os.date()) end)() | ||
11 | lanes.gen("os,base", function() os.execute('sleep 10 && echo finished_sleeping') print( os.date()) end)() | ||
12 | lanes.gen("os,base", function() os.execute('sleep 10 && echo finished_sleeping') print( os.date()) end)() | ||
13 | lanes.gen("os,base", function() os.execute('sleep 10 && echo finished_sleeping') print( os.date()) end)() | ||
14 | ]] | ||
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 } | |||
35 | 35 | ||
36 | while true do | 36 | while true do |
37 | io.stderr:write("waiting...\t") | 37 | io.stderr:write("waiting...\t") |
38 | local v,channel= linda:receive( 6.0, T1,T2 ) | 38 | local channel, v = linda:receive( 6.0, T1,T2 ) |
39 | assert( channel==T1 or channel==T2 ) | 39 | assert( channel==T1 or channel==T2 ) |
40 | caught[channel]= true | 40 | caught[channel]= true |
41 | 41 | ||
@@ -89,6 +89,6 @@ assert( linda:get(T2) == nil ) | |||
89 | 89 | ||
90 | PRINT "...making sure no ticks are coming..." | 90 | PRINT "...making sure no ticks are coming..." |
91 | 91 | ||
92 | local v= linda:receive( 1.5, T1,T2 ) -- should not get any | 92 | local k,v= linda:receive( 10, T1,T2 ) -- should not get any |
93 | assert(v==nil) | 93 | assert(v==nil) |
94 | 94 | ||