summaryrefslogtreecommitdiff
path: root/tests/timer.lua
blob: e95f3266d70deab030acc09f412e2353ed653a38 (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
--
-- TIMER.LUA
--
-- Sample program for Lua Lanes
--

-- On MSYS, stderr is buffered. In this test it matters.
io.stderr:setvbuf "no"


require "lanes"

local linda= lanes.linda()

local function PRINT(str)
    io.stderr:write(str.."\n")
end

local T1= "1s"  -- these keys can be anything...
local T2= "5s"

local step= {}

lanes.timer( linda, T1, 1.0, 1.0 )
step[T1]= 1.0

PRINT( "\n*** Timers every second (not synced to wall clock) ***\n" )

local v_first
local v_last= {}     -- { [channel]= num }
local T2_first_round= true

local caught= {}     -- { [T1]= bool, [T2]= bool }

while true do
    io.stderr:write("waiting...\t")
    local v,channel= linda:receive( 6.0, T1,T2 )
    assert( channel==T1 or channel==T2 )
    caught[channel]= true

    io.stderr:write( ((channel==T1) and "" or "\t\t").. string.format("%.3f",v),"\n" )
    assert( type(v)=="number" )

    if v_last[channel] then
        if channel==T2 and T2_first_round then
            -- do not make measurements, first round is not 5secs due to wall clock adjustment
            T2_first_round= false
        else
            assert( math.abs(v-v_last[channel]- step[channel]) < 0.02 )
        end
    end
    
    if not v_first then
        v_first= v
    elseif v-v_first > 3.0 and (not step[T2]) then
        PRINT( "\n*** Adding timers every 5 second (synced to wall clock) ***\n" )

        -- The first event can be in the past (just cut seconds down to 5s)
        --
        local date= os.date("*t")
        date.sec = date.sec - date.sec%5

        lanes.timer( linda, T2, date, 5.0 )
        step[T2]= 5.0

    elseif v-v_first > 10 then    -- exit condition
        break
    end
    v_last[channel]= v
end  

-- Windows version had a bug where T2 timers were not coming through, at all.
-- AKa 24-Jan-2009
--
assert( caught[T1] )
assert( caught[T2] )

PRINT( "\n*** Clearing timers ***\n" )

lanes.timer( linda, T1, 0 )    -- reset; no reoccuring ticks
lanes.timer( linda, T2, 0 )

linda:receive( 0, T1 )    -- clear out; there could be one tick left
linda:receive( 0, T2 )

assert( linda:get(T1) == nil )
assert( linda:get(T2) == nil )

PRINT "...making sure no ticks are coming..."

local v= linda:receive( 1.5, T1,T2 )    -- should not get any
assert(v==nil)