-
Notifications
You must be signed in to change notification settings - Fork 0
/
election_timer_test.go
91 lines (70 loc) · 1.88 KB
/
election_timer_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
package graft
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// These tests are really finnicky since they are highly time dependent
// just a heads up :D
func TestTriggersElection(t *testing.T) {
bound := 10 * time.Millisecond
timerInvoked := false
timer := newElectionTimer(graftConfig{
electionTimeoutDuration: bound,
}, func() { timerInvoked = true })
timer.start()
time.Sleep(3 * bound)
assert := assert.New(t)
assert.True(timerInvoked)
}
func TestShouldProperlyDisableTimer(t *testing.T) {
bound := 150 * time.Millisecond
timerInvoked := false
timer := newElectionTimer(graftConfig{
electionTimeoutDuration: bound,
}, func() { timerInvoked = true })
assert := assert.New(t)
// potential transient test failure
timer.start()
timer.stop()
// sleep for "after" the timer, the timer should not be triggered
time.Sleep(3 * bound)
assert.False(timerInvoked)
// restart it and sleep (under) the duration should not be triggered
timer.start()
time.Sleep(bound)
assert.False(timerInvoked)
// sleep longer, should be triggered
time.Sleep(2 * bound)
assert.True(timerInvoked)
}
func TestShouldNotPersistDeadTimers(t *testing.T) {
// basically spin up and stop multiple different timers
bound := 150 * time.Millisecond
assert := assert.New(t)
timerInvoked := false
timer := newElectionTimer(graftConfig{
electionTimeoutDuration: bound,
}, func() { timerInvoked = true })
for i := 0; i < 5; i++ {
timer.start()
timer.stop()
time.Sleep(2 * bound)
assert.False(timerInvoked)
}
}
func TestResetShouldReset(t *testing.T) {
bound := 150 * time.Millisecond
assert := assert.New(t)
timerInvoked := false
timer := newElectionTimer(graftConfig{
electionTimeoutDuration: bound,
}, func() { timerInvoked = true })
for i := 0; i < 5; i++ {
timer.start()
time.Sleep(bound / 2)
timer.reset()
}
timer.stop()
assert.False(timerInvoked)
}