-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimerwheel_test.go
125 lines (110 loc) · 2.95 KB
/
timerwheel_test.go
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
###########################################################################
#
# Filename: timerwheel_test.go
#
# Author: Aniket Bhat
# Created: 03/10/2017
#
# Description: Test code for timer wheel
#
###########################################################################
#
# Copyright (c) 2017 Aniket Bhat
#
###########################################################################
*/
package timerwheel
import (
"fmt"
"strconv"
"testing"
"time"
)
type MockTimer struct {
periodic bool
interval int64
startTime int64
contextData string
index int
nextExpiration int64
}
func NewMockTimer(periodic bool, startTime, interval int64, index int, contextData string) *MockTimer {
return &MockTimer{
periodic: periodic,
startTime: startTime,
interval: interval,
contextData: contextData,
index: index,
}
}
func (mt *MockTimer) Periodic() bool {
return mt.periodic
}
func (mt *MockTimer) Oneshot() bool {
return !mt.periodic
}
func (mt *MockTimer) Interval() int64 {
return mt.interval
}
func (mt *MockTimer) Name() string {
return "MockTimer-" + strconv.Itoa(mt.index)
}
func (mt *MockTimer) NextExpiration() int64 {
return mt.nextExpiration
}
func (mt *MockTimer) SetNextExpiration(exp int64) error {
mt.nextExpiration = exp
return nil
}
func (mt *MockTimer) StartTime() int64 {
return mt.startTime
}
func (mt *MockTimer) Expired() {
fmt.Printf("Timer %v expired - context data is %v\n", mt.Name(), mt.contextData)
}
func TestTimerWheelMaxTimers(t *testing.T) {
tw := NewTimerWheel(5*time.Millisecond, 3)
if tw != nil {
t1 := NewMockTimer(false, int64(1*time.Second), int64(10*time.Millisecond), 1, "I am number one")
err := tw.Addtimer(t1)
t2 := NewMockTimer(true, int64(2*time.Second), int64(20*time.Millisecond), 2, "I am number two")
err = tw.Addtimer(t2)
t3 := NewMockTimer(true, int64(3*time.Second), int64(30*time.Millisecond), 3, "I am number three")
err = tw.Addtimer(t3)
err = tw.Addtimer(NewMockTimer(false, int64(4*time.Second), int64(25*time.Millisecond), 4, "I am number four"))
if err == nil {
t.Fail()
}
runDuration := time.NewTicker(10 * time.Second)
select {
case <-runDuration.C:
tw.Deletetimer(t2)
tw.Deletetimer(t3)
}
if tw.Running() {
t.Fail()
}
}
}
func TestTimerWheel(t *testing.T) {
tw := NewTimerWheel(5*time.Millisecond, 3)
if tw != nil {
go tw.Run()
t1 := NewMockTimer(false, int64(1*time.Second), int64(10*time.Millisecond), 1, "I am number one")
err := tw.Addtimer(t1)
t2 := NewMockTimer(true, int64(2*time.Second), int64(20*time.Millisecond), 2, "I am number two")
err = tw.Addtimer(t2)
t3 := NewMockTimer(true, int64(3*time.Second), int64(30*time.Millisecond), 3, "I am number three")
err = tw.Addtimer(t3)
if err != nil {
t.Fail()
}
runDuration := time.NewTicker(10 * time.Second)
select {
case <-runDuration.C:
tw.Deletetimer(t2)
tw.Deletetimer(t3)
}
}
}