...
1
16
17 package pool
18
19 import (
20 "sync/atomic"
21 "testing"
22 "time"
23 )
24
25 const (
26 half = time.Duration(500e5)
27 quarter = time.Duration(250e5)
28 tenth = time.Duration(100e5)
29 )
30
31 var numcalls int32
32
33 func f() {
34 atomic.AddInt32(&numcalls, 1)
35 }
36
37 func TestWait(t *testing.T) {
38 atomic.StoreInt32(&numcalls, 0)
39 timer := NewTimer(quarter)
40 timer.Start(f)
41 defer timer.Stop()
42 time.Sleep(tenth)
43 if atomic.LoadInt32(&numcalls) != 0 {
44 t.Errorf("want 0, received %v", numcalls)
45 }
46 time.Sleep(quarter)
47 if atomic.LoadInt32(&numcalls) != 1 {
48 t.Errorf("want 1, received %v", numcalls)
49 }
50 time.Sleep(quarter)
51 if atomic.LoadInt32(&numcalls) != 2 {
52 t.Errorf("want 1, received %v", numcalls)
53 }
54 }
55
56 func TestReset(t *testing.T) {
57 atomic.StoreInt32(&numcalls, 0)
58 timer := NewTimer(half)
59 timer.Start(f)
60 defer timer.Stop()
61 timer.SetInterval(quarter)
62 time.Sleep(tenth)
63 if atomic.LoadInt32(&numcalls) != 0 {
64 t.Errorf("want 0, received %v", numcalls)
65 }
66 time.Sleep(quarter)
67 if atomic.LoadInt32(&numcalls) != 1 {
68 t.Errorf("want 1, received %v", numcalls)
69 }
70 }
71
72 func TestIndefinite(t *testing.T) {
73 atomic.StoreInt32(&numcalls, 0)
74 timer := NewTimer(0)
75 timer.Start(f)
76 defer timer.Stop()
77 timer.TriggerAfter(quarter)
78 time.Sleep(tenth)
79 if atomic.LoadInt32(&numcalls) != 0 {
80 t.Errorf("want 0, received %v", numcalls)
81 }
82 time.Sleep(quarter)
83 if atomic.LoadInt32(&numcalls) != 1 {
84 t.Errorf("want 1, received %v", numcalls)
85 }
86 }
87
View as plain text