-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathstate_test.go
141 lines (126 loc) · 4.44 KB
/
state_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package j8a
import (
"testing"
"time"
)
func TestStateLesser(t *testing.T) {
tests := []struct {
n string
a State
b State
v bool
}{
{n: "Bootstrap not lesser Bootstrap", a: Bootstrap, b: Bootstrap, v: false},
{n: "Bootstrap lesser Daemon", a: Bootstrap, b: Daemon, v: true},
{n: "Bootstrap lesser Shutdown", a: Bootstrap, b: Shutdown, v: true},
{n: "Daemon not lesser Bootstrap", a: Daemon, b: Bootstrap, v: false},
{n: "Daemon not lesser Daemon", a: Daemon, b: Daemon, v: false},
{n: "Daemon lesser Shutdown", a: Daemon, b: Shutdown, v: true},
{n: "Shutdown not lesser Daemon", a: Shutdown, b: Daemon, v: false},
{n: "Shutdown not lesser Bootstrap", a: Shutdown, b: Bootstrap, v: false},
{n: "Shutdown not lesser Shutdown", a: Shutdown, b: Shutdown, v: false},
}
for _, tt := range tests {
t.Run(tt.n, func(t *testing.T) {
want := tt.v
got := tt.a.Lesser(tt.b)
if want != got {
t.Errorf("%v failed, want %v got %v", tt.n, want, got)
}
})
}
}
// Tests pass under the timeout threshold if a status is lesser or equal the current status.
// results greater the treshold means the StateHandler would otherwise wait indefinitely for the status.
func TestStateHandlerWaitForStatus(t *testing.T) {
tests := []struct {
n string
current State
waitingFor State
delaySeconds int
greater bool
}{
{"Bootstrap waiting for Bootstrap", Bootstrap, Bootstrap, 1, false},
{"Bootstrap waiting for Daemon", Bootstrap, Daemon, 1, true},
{"Bootstrap waiting for Shutdown", Bootstrap, Shutdown, 1, true},
{"Daemon waiting for Bootstrap", Daemon, Bootstrap, 1, false},
{"Daemon waiting for Daemon", Daemon, Daemon, 1, false},
{"Daemon waiting for Shutdown", Daemon, Shutdown, 1, true},
{"Shutdown waiting for Bootstrap", Shutdown, Bootstrap, 1, false},
{"Shutdown waiting for Daemon", Shutdown, Daemon, 1, false},
{"Shutdown waiting for Shutdown", Shutdown, Shutdown, 1, false},
}
for _, tt := range tests {
t.Run(tt.n, func(t *testing.T) {
sh := NewStateHandler()
sh.setState(tt.current)
before := time.Now()
sh.waitState(tt.waitingFor, tt.delaySeconds)
after := time.Now().Sub(before)
if after > time.Second*time.Duration(tt.delaySeconds) == tt.greater {
t.Logf("normal. current status %v waiting for %v delay %v", tt.current, tt.waitingFor, after)
} else {
t.Errorf("current status %v waiting for %v delay %v", tt.current, tt.waitingFor, after)
}
})
}
}
func TestStateHandlerSetStateAndWaitForStatus(t *testing.T) {
tests := []struct {
n string
current State
setState1S State
waitingForState State
greaterTimeout bool
}{
{"BBB", Bootstrap, Bootstrap, Bootstrap, false},
{"BBD", Bootstrap, Bootstrap, Daemon, true},
{"BBS", Bootstrap, Bootstrap, Shutdown, true},
{"BDB", Bootstrap, Daemon, Bootstrap, false},
{"BDD", Bootstrap, Daemon, Daemon, false},
{"BDS", Bootstrap, Daemon, Shutdown, true},
{"BSB", Bootstrap, Shutdown, Bootstrap, false},
{"BSD", Bootstrap, Shutdown, Daemon, false},
{"BSS", Bootstrap, Shutdown, Shutdown, false},
{"DDB", Daemon, Daemon, Bootstrap, false},
{"DDD", Daemon, Daemon, Daemon, false},
{"DDS", Daemon, Daemon, Shutdown, true},
{"DSB", Daemon, Shutdown, Bootstrap, false},
{"DSD", Daemon, Shutdown, Daemon, false},
{"DSS", Daemon, Shutdown, Shutdown, false},
{"SSB", Shutdown, Shutdown, Bootstrap, false},
{"SSD", Shutdown, Shutdown, Daemon, false},
{"SSS", Shutdown, Shutdown, Shutdown, false},
}
for _, tt := range tests {
t.Run(tt.n, func(t *testing.T) {
timeoutSeconds := 2
sh := NewStateHandler()
sh.setState(tt.current)
before := time.Now()
time.AfterFunc(time.Second*1, func() {
sh.setState(tt.setState1S)
})
sh.waitState(tt.waitingForState, timeoutSeconds)
elapsed := time.Now().Sub(before)
if (elapsed > time.Second*time.Duration(timeoutSeconds)) != tt.greaterTimeout {
t.Errorf("current status %v set to %v after 1s, waiting for %v timed out after %v", tt.current, tt.setState1S, tt.waitingForState, elapsed)
}
})
}
}
func TestStateHandlerSetStateToShutdownWaitingForDaemonWithMultiplePreviousSetStateOperations(t *testing.T) {
sh := NewStateHandler()
before := time.Now()
for i := 0; i < 5; i++ {
sh.setState(Daemon)
}
time.AfterFunc(time.Second*1, func() {
sh.setState(Shutdown)
})
sh.waitState(Daemon, 10)
elapsed := time.Now().Sub(before)
if elapsed > time.Second*10 {
t.Errorf("state handler did not set to Shutdown")
}
}