diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-05-07 09:23:12 +0200 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-05-13 18:15:46 +0200 |
commit | ebb0837588336e32fc1258a3838673afdd31ee71 (patch) | |
tree | f7590e7d8d22fe62f9022bf6cb1ae442dccc5861 /tests/track_lanes.lua | |
parent | 2c69c99da47b1d708606da713433608c73ecdb88 (diff) | |
download | lanes-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.lua | 50 |
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 @@ | |||
1 | local lanes = require "lanes" .configure{ with_timers = false, track_lanes = true} | 1 | local lanes = require "lanes" .configure{ with_timers = false, track_lanes = true} |
2 | 2 | local wait = lanes.sleep | |
3 | local wait | ||
4 | do | ||
5 | local linda = lanes.linda() | ||
6 | wait = function( seconds_) | ||
7 | linda:receive( seconds_, "dummy_key") | ||
8 | end | ||
9 | end | ||
10 | 3 | ||
11 | print "hello" | 4 | print "hello" |
12 | 5 | ||
13 | local track = function( title_) | 6 | local 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 | ||
20 | end | 18 | end |
21 | 19 | ||
22 | local sleeper = function( name_, seconds_) | 20 | local 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_ .. "'") |
34 | end | 32 | end |
35 | 33 | ||
@@ -39,7 +37,7 @@ end | |||
39 | local g = lanes.gen( "*", sleeper) | 37 | local g = lanes.gen( "*", sleeper) |
40 | 38 | ||
41 | -- start a forever-waiting lane (nil timeout) | 39 | -- start a forever-waiting lane (nil timeout) |
42 | g( "forever") | 40 | g( "forever", 'indefinitely') |
43 | 41 | ||
44 | -- start a lane that will last 2 seconds | 42 | -- start a lane that will last 2 seconds |
45 | g( "two_seconds", 2) | 43 | g( "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 |
48 | wait( 0.1) | 46 | wait( 0.1) |
49 | 47 | ||
50 | -- list the known lanes | 48 | -- list the known lanes (should be living lanes) |
51 | track( "============= START") | 49 | local threads = track( "============= START", 2) |
50 | -- two_seconds forever | ||
51 | assert(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" |
54 | wait(2.1) | 54 | wait(2.1) |
55 | 55 | ||
56 | track( "============= two_seconds dead") | 56 | local 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) | 58 | assert(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 |
64 | g( "two_seconds", 2) | 61 | g( "two_seconds", 2) |
@@ -67,6 +64,17 @@ g( "two_seconds", 2) | |||
67 | wait( 0.1) | 64 | wait( 0.1) |
68 | 65 | ||
69 | -- list the known lanes | 66 | -- list the known lanes |
70 | track( "============= ANOTHER") | 67 | -- unless garbage collector cleaned it, we should have 3 lanes |
68 | local threads = track( "============= ANOTHER", 3) | ||
69 | -- two_seconds #2 two_seconds #1 forever | ||
70 | assert(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) | ||
73 | collectgarbage() | ||
74 | |||
75 | -- list the known lanes | ||
76 | local threads = track( "============= AFTER COLLECTGARBAGE", 2) | ||
77 | -- two_seconds #2 forever | ||
78 | assert(threads[1].status == 'waiting' and threads[2].status == 'waiting') | ||
71 | 79 | ||
72 | print "done" \ No newline at end of file | 80 | print "done" \ No newline at end of file |