MCPcopy
hub / github.com/aceld/zinx / TestTimerWheel

Function TestTimerWheel

ztimer/timewheel_test.go:17–84  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

15)
16
17func TestTimerWheel(t *testing.T) {
18 //创建秒级时间轮
19 secondTw := NewTimeWheel(SecondName, SecondInterval, SecondScales, TimersMaxCap)
20 //创建分钟级时间轮
21 minuteTw := NewTimeWheel(MinuteName, MinuteInterval, MinuteScales, TimersMaxCap)
22 //创建小时级时间轮
23 hourTw := NewTimeWheel(HourName, HourInterval, HourScales, TimersMaxCap)
24
25 // 将分层时间轮做关联
26 hourTw.AddTimeWheel(minuteTw)
27 minuteTw.AddTimeWheel(secondTw)
28
29 fmt.Println("init timewheels done!")
30
31 //===== > 以上为初始化分层时间轮 <====
32
33 //给时间轮添加定时器
34 timer1 := NewTimerAfter(NewDelayFunc(myFunc, []interface{}{1, 10}), 10*time.Second)
35 _ = hourTw.AddTimer(1, timer1)
36 fmt.Println("add timer 1 done!")
37
38 //给时间轮添加定时器
39 timer2 := NewTimerAfter(NewDelayFunc(myFunc, []interface{}{2, 20}), 20*time.Second)
40 _ = hourTw.AddTimer(2, timer2)
41 fmt.Println("add timer 2 done!")
42
43 //给时间轮添加定时器
44 timer3 := NewTimerAfter(NewDelayFunc(myFunc, []interface{}{3, 30}), 30*time.Second)
45 _ = hourTw.AddTimer(3, timer3)
46 fmt.Println("add timer 3 done!")
47
48 //给时间轮添加定时器
49 timer4 := NewTimerAfter(NewDelayFunc(myFunc, []interface{}{4, 40}), 40*time.Second)
50 _ = hourTw.AddTimer(4, timer4)
51 fmt.Println("add timer 4 done!")
52
53 //给时间轮添加定时器
54 timer5 := NewTimerAfter(NewDelayFunc(myFunc, []interface{}{5, 50}), 50*time.Second)
55 _ = hourTw.AddTimer(5, timer5)
56 fmt.Println("add timer 5 done!")
57
58 //时间轮运行
59 secondTw.Run()
60 minuteTw.Run()
61 hourTw.Run()
62
63 fmt.Println("timewheels are run!")
64
65 go func() {
66 n := 0.0
67 for {
68 fmt.Println("tick...", n)
69
70 //取出近1ms的超时定时器有哪些
71 timers := hourTw.GetTimerWithIn(1000 * time.Millisecond)
72 for _, timer := range timers {
73 //调用定时器方法
74 timer.delayFunc.Call()

Callers

nothing calls this directly

Calls 8

AddTimeWheelMethod · 0.95
AddTimerMethod · 0.95
RunMethod · 0.95
GetTimerWithInMethod · 0.95
NewTimeWheelFunction · 0.85
NewTimerAfterFunction · 0.85
NewDelayFuncFunction · 0.85
CallMethod · 0.65

Tested by

no test coverage detected