aboutsummaryrefslogtreecommitdiff
path: root/tests/track_lanes.lua
blob: 803fae526ae26b2892ca3433d018f91293994e6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
local lanes = require "lanes" .configure{ with_timers = false, track_lanes = true}

local SLEEP = function(...)
    local k, v = lanes.sleep(...)
    assert(k == nil and v == "timeout")
end

print "hello"

local track = function( title_, expected_count_)
    print( title_)
    local count = 0
    local threads = lanes.threads()
    for k, v in pairs(threads)
    do
        print( k, v.name, v.status)
        count = count + 1
    end
    assert(count == expected_count_, "unexpected active lane count " .. count .. " ~= " .. expected_count_)
    print( "\n")
    return threads
end

local sleeper = function( name_, seconds_)
    -- no set_finalizer in main thread
    if set_finalizer
    then
        set_finalizer(function() print("Finalizing '" .. name_ .. "'") end)
    end
    -- print( "entering '" .. name_ .. "'")
    local lanes = require "lanes"
    -- no lane_threadname in main thread
    if lane_threadname
    then
        -- print( "lane_threadname('" .. name_ .. "')")
        lane_threadname( name_)
    end
    -- suspend the lane for the specified duration
    lanes.sleep(seconds_)
    -- print( "exiting '" .. name_ .. "'")
end

-- sleeper( "main", 1)

-- the generator
local g = lanes.gen( "*", sleeper)

-- start a forever-waiting lane (nil timeout)
local forever = g( "forever", 'indefinitely')

-- start a lane that will last 2 seconds
local ephemeral1 = g( "two_seconds", 2)
 
-- give a bit of time to reach the linda waiting call
SLEEP(0.1)

-- list the known lanes (both should be living lanes)
local threads = track( "============= START", 2)
--     two_seconds                        forever
assert(threads[1].status == 'waiting' and threads[2].status == 'waiting')

-- wait until ephemeral1 has completed
SLEEP(2.1)

local threads = track( "============= two_seconds dead", 2)
--     two_seconds                     forever
assert(threads[1].status == 'done' and threads[2].status == 'waiting')

-- start another lane that will last 2 seconds, with the same name
local ephemeral2 = g( "two_seconds", 2)
 
-- give a bit of time to reach the linda waiting call
SLEEP( 0.1)

-- list the known lanes
-- we should have 3 lanes
local threads = track( "============= ANOTHER", 3)
--     two_seconds #2                     two_seconds #1                  forever
assert(threads[1].status == 'waiting' and threads[2].status == 'done' and threads[3].status == 'waiting')

-- this will collect all lane handles.
-- since ephemeral1 has completed, it is no longer tracked, but the other 2 should still be
forever = nil
ephemeral1 = nil
ephemeral2 = nil
collectgarbage()

-- list the known lanes
local threads = track( "============= AFTER COLLECTGARBAGE", 2)
--     two_seconds #2                     forever
assert(threads[1].status == 'waiting' and threads[2].status == 'waiting')

-- wait a bit more for ephemeral2 to exit, so that we get to have a free-running lane terminate itself before shutdown
SLEEP(3)

print "============= AFTER WAITING"
-- this is printed after Universe shutdown terminates the only remaining lane, 'forever'
lanes.finally(function() print "TEST OK" end)