aboutsummaryrefslogtreecommitdiff
path: root/tests/timer.lua
diff options
context:
space:
mode:
Diffstat (limited to 'tests/timer.lua')
-rw-r--r--tests/timer.lua93
1 files changed, 93 insertions, 0 deletions
diff --git a/tests/timer.lua b/tests/timer.lua
new file mode 100644
index 0000000..e95f326
--- /dev/null
+++ b/tests/timer.lua
@@ -0,0 +1,93 @@
1--
2-- TIMER.LUA
3--
4-- Sample program for Lua Lanes
5--
6
7-- On MSYS, stderr is buffered. In this test it matters.
8io.stderr:setvbuf "no"
9
10
11require "lanes"
12
13local linda= lanes.linda()
14
15local function PRINT(str)
16 io.stderr:write(str.."\n")
17end
18
19local T1= "1s" -- these keys can be anything...
20local T2= "5s"
21
22local step= {}
23
24lanes.timer( linda, T1, 1.0, 1.0 )
25step[T1]= 1.0
26
27PRINT( "\n*** Timers every second (not synced to wall clock) ***\n" )
28
29local v_first
30local v_last= {} -- { [channel]= num }
31local T2_first_round= true
32
33local caught= {} -- { [T1]= bool, [T2]= bool }
34
35while true do
36 io.stderr:write("waiting...\t")
37 local v,channel= linda:receive( 6.0, T1,T2 )
38 assert( channel==T1 or channel==T2 )
39 caught[channel]= true
40
41 io.stderr:write( ((channel==T1) and "" or "\t\t").. string.format("%.3f",v),"\n" )
42 assert( type(v)=="number" )
43
44 if v_last[channel] then
45 if channel==T2 and T2_first_round then
46 -- do not make measurements, first round is not 5secs due to wall clock adjustment
47 T2_first_round= false
48 else
49 assert( math.abs(v-v_last[channel]- step[channel]) < 0.02 )
50 end
51 end
52
53 if not v_first then
54 v_first= v
55 elseif v-v_first > 3.0 and (not step[T2]) then
56 PRINT( "\n*** Adding timers every 5 second (synced to wall clock) ***\n" )
57
58 -- The first event can be in the past (just cut seconds down to 5s)
59 --
60 local date= os.date("*t")
61 date.sec = date.sec - date.sec%5
62
63 lanes.timer( linda, T2, date, 5.0 )
64 step[T2]= 5.0
65
66 elseif v-v_first > 10 then -- exit condition
67 break
68 end
69 v_last[channel]= v
70end
71
72-- Windows version had a bug where T2 timers were not coming through, at all.
73-- AKa 24-Jan-2009
74--
75assert( caught[T1] )
76assert( caught[T2] )
77
78PRINT( "\n*** Clearing timers ***\n" )
79
80lanes.timer( linda, T1, 0 ) -- reset; no reoccuring ticks
81lanes.timer( linda, T2, 0 )
82
83linda:receive( 0, T1 ) -- clear out; there could be one tick left
84linda:receive( 0, T2 )
85
86assert( linda:get(T1) == nil )
87assert( linda:get(T2) == nil )
88
89PRINT "...making sure no ticks are coming..."
90
91local v= linda:receive( 1.5, T1,T2 ) -- should not get any
92assert(v==nil)
93