aboutsummaryrefslogtreecommitdiff
path: root/tests/track_lanes.lua
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2024-05-07 09:23:12 +0200
committerBenoit Germain <benoit.germain@ubisoft.com>2024-05-13 18:15:46 +0200
commitebb0837588336e32fc1258a3838673afdd31ee71 (patch)
treef7590e7d8d22fe62f9022bf6cb1ae442dccc5861 /tests/track_lanes.lua
parent2c69c99da47b1d708606da713433608c73ecdb88 (diff)
downloadlanes-ebb0837588336e32fc1258a3838673afdd31ee71.tar.gz
lanes-ebb0837588336e32fc1258a3838673afdd31ee71.tar.bz2
lanes-ebb0837588336e32fc1258a3838673afdd31ee71.zip
API changes
* lanes.sleep accepts 'indefinitely'. * settings.with_timers is false by default
Diffstat (limited to 'tests/track_lanes.lua')
-rw-r--r--tests/track_lanes.lua50
1 files changed, 29 insertions, 21 deletions
diff --git a/tests/track_lanes.lua b/tests/track_lanes.lua
index 46cfdad..daaa94c 100644
--- a/tests/track_lanes.lua
+++ b/tests/track_lanes.lua
@@ -1,22 +1,20 @@
1local lanes = require "lanes" .configure{ with_timers = false, track_lanes = true} 1local lanes = require "lanes" .configure{ with_timers = false, track_lanes = true}
2 2local wait = lanes.sleep
3local wait
4do
5 local linda = lanes.linda()
6 wait = function( seconds_)
7 linda:receive( seconds_, "dummy_key")
8 end
9end
10 3
11print "hello" 4print "hello"
12 5
13local track = function( title_) 6local track = function( title_, expected_count_)
14 print( title_) 7 print( title_)
15 for k, v in pairs( lanes.threads()) 8 local count = 0
9 local threads = lanes.threads()
10 for k, v in pairs(threads)
16 do 11 do
17 print( k, v.name, v.status) 12 print( k, v.name, v.status)
13 count = count + 1
18 end 14 end
15 assert(count == expected_count_, "unexpected active lane count")
19 print( "\n") 16 print( "\n")
17 return threads
20end 18end
21 19
22local sleeper = function( name_, seconds_) 20local sleeper = function( name_, seconds_)
@@ -29,7 +27,7 @@ local sleeper = function( name_, seconds_)
29 set_debug_threadname( name_) 27 set_debug_threadname( name_)
30 end 28 end
31 -- suspend the lane for the specified duration with a failed linda read 29 -- suspend the lane for the specified duration with a failed linda read
32 wait( seconds_) 30 lanes.sleep(seconds_)
33 -- print( "exiting '" .. name_ .. "'") 31 -- print( "exiting '" .. name_ .. "'")
34end 32end
35 33
@@ -39,7 +37,7 @@ end
39local g = lanes.gen( "*", sleeper) 37local g = lanes.gen( "*", sleeper)
40 38
41-- start a forever-waiting lane (nil timeout) 39-- start a forever-waiting lane (nil timeout)
42g( "forever") 40g( "forever", 'indefinitely')
43 41
44-- start a lane that will last 2 seconds 42-- start a lane that will last 2 seconds
45g( "two_seconds", 2) 43g( "two_seconds", 2)
@@ -47,18 +45,17 @@ g( "two_seconds", 2)
47-- give a bit of time to reach the linda waiting call 45-- give a bit of time to reach the linda waiting call
48wait( 0.1) 46wait( 0.1)
49 47
50-- list the known lanes 48-- list the known lanes (should be living lanes)
51track( "============= START") 49local threads = track( "============= START", 2)
50-- two_seconds forever
51assert(threads[1].status == 'waiting' and threads[2].status == 'waiting')
52 52
53-- wait until "two_seconds has completed" 53-- wait until "two_seconds has completed"
54wait(2.1) 54wait(2.1)
55 55
56track( "============= two_seconds dead") 56local threads = track( "============= two_seconds dead", 2)
57 57-- two_seconds forever
58-- this will collect the completed lane (and remove it from the tracking queue) 58assert(threads[1].status == 'done' and threads[2].status == 'waiting')
59-- collectgarbage()
60
61-- track( "============= two_seconds dead (after collectgarbage)")
62 59
63-- start another lane that will last 2 seconds, with the same name 60-- start another lane that will last 2 seconds, with the same name
64g( "two_seconds", 2) 61g( "two_seconds", 2)
@@ -67,6 +64,17 @@ g( "two_seconds", 2)
67wait( 0.1) 64wait( 0.1)
68 65
69-- list the known lanes 66-- list the known lanes
70track( "============= ANOTHER") 67-- unless garbage collector cleaned it, we should have 3 lanes
68local threads = track( "============= ANOTHER", 3)
69-- two_seconds #2 two_seconds #1 forever
70assert(threads[1].status == 'waiting' and threads[2].status == 'done' and threads[3].status == 'waiting')
71
72-- this will collect the completed lane (and remove it from the tracking queue)
73collectgarbage()
74
75-- list the known lanes
76local threads = track( "============= AFTER COLLECTGARBAGE", 2)
77-- two_seconds #2 forever
78assert(threads[1].status == 'waiting' and threads[2].status == 'waiting')
71 79
72print "done" \ No newline at end of file 80print "done" \ No newline at end of file