11package timer
22
33import (
4+ "context"
45 "math"
6+ "sync/atomic"
57 "testing"
68 "time"
79
@@ -24,6 +26,7 @@ func Test_GenVersion(t *testing.T) {
2426 assert .Equal (t , genVersionHeight (1 , 64 ), uint64 (0x0001004000000000 ))
2527}
2628
29+ // 测试1小时
2730func Test_hour (t * testing.T ) {
2831 tw := newTimeWheel ()
2932
@@ -48,3 +51,109 @@ func Test_hour(t *testing.T) {
4851 }
4952 assert .True (t , * testHour )
5053}
54+
55+ // 测试周期性定时器, 5s
56+ func Test_ScheduleFunc_5s (t * testing.T ) {
57+ tw := newTimeWheel ()
58+
59+ var first5 int32
60+ ctx , cancel := context .WithCancel (context .Background ())
61+
62+ const total = int32 (1000 )
63+
64+ testTime := time .Second * 5
65+
66+ tw .ScheduleFunc (testTime , func () {
67+ atomic .AddInt32 (& first5 , 1 )
68+ if atomic .LoadInt32 (& first5 ) == total {
69+ cancel ()
70+ }
71+
72+ })
73+
74+ expire := getExpire (testTime * time .Duration (total ), 0 )
75+ for i := 0 ; i <= int (expire )+ 10 ; i ++ {
76+ get10Ms := func () time.Duration {
77+ return tw .curTimePoint + 1
78+ }
79+ tw .run (get10Ms )
80+ }
81+
82+ select {
83+ case <- ctx .Done ():
84+ case <- time .After (time .Second / 100 ):
85+ }
86+
87+ assert .Equal (t , total , first5 )
88+
89+ }
90+
91+ // 测试周期性定时器, 1hour
92+ func Test_ScheduleFunc_hour (t * testing.T ) {
93+ tw := newTimeWheel ()
94+
95+ var first5 int32
96+ ctx , cancel := context .WithCancel (context .Background ())
97+
98+ const total = int32 (100 )
99+ testTime := time .Hour
100+
101+ tw .ScheduleFunc (testTime , func () {
102+ atomic .AddInt32 (& first5 , 1 )
103+ if atomic .LoadInt32 (& first5 ) == total {
104+ cancel ()
105+ }
106+
107+ })
108+
109+ expire := getExpire (testTime * time .Duration (total ), 0 )
110+ for i := 0 ; i <= int (expire )+ 10 ; i ++ {
111+ get10Ms := func () time.Duration {
112+ return tw .curTimePoint + 1
113+ }
114+ tw .run (get10Ms )
115+ }
116+
117+ select {
118+ case <- ctx .Done ():
119+ case <- time .After (time .Second / 100 ):
120+ }
121+
122+ assert .Equal (t , total , first5 )
123+
124+ }
125+
126+ // 测试周期性定时器, 1day
127+ func Test_ScheduleFunc_day (t * testing.T ) {
128+ tw := newTimeWheel ()
129+
130+ var first5 int32
131+ ctx , cancel := context .WithCancel (context .Background ())
132+
133+ const total = int32 (10 )
134+ testTime := time .Hour * 24
135+
136+ tw .ScheduleFunc (testTime , func () {
137+ atomic .AddInt32 (& first5 , 1 )
138+ if atomic .LoadInt32 (& first5 ) == total {
139+ cancel ()
140+ }
141+
142+ })
143+
144+ expire := getExpire (testTime * time .Duration (total ), 0 )
145+ for i := 0 ; i <= int (expire )+ 10 ; i ++ {
146+ get10Ms := func () time.Duration {
147+ return tw .curTimePoint + 1
148+ }
149+ tw .run (get10Ms )
150+ }
151+
152+ select {
153+ case <- ctx .Done ():
154+ case <- time .After (time .Second / 100 ):
155+ }
156+
157+ assert .Equal (t , total , first5 )
158+
159+ }
0 commit comments