-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor_test.go
165 lines (136 loc) · 3.55 KB
/
monitor_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package nested
import (
"errors"
"fmt"
"strings"
"testing"
)
func assertPanic(t *testing.T, f func(), msg string) {
t.Helper()
defer func() {
r := recover()
if r == nil {
t.Errorf("want panic %q, got no panic", msg)
return
}
got := fmt.Sprint(r)
if !strings.Contains(got, msg) {
t.Errorf("want panic containing %q, got panic %q", msg, got)
return
}
}()
f()
}
func assertReceived[X any](t *testing.T, ch <-chan X) (x X) {
select {
case x = <-ch:
default:
t.Errorf("no message recevied")
}
return
}
func TestMonitor(t *testing.T) {
// A new Monitor's state is Initializing.
mon := Monitor{}
assertEqual(t, Initializing, mon.GetState())
assertEqual(t, nil, mon.Err())
assertEqual(t, 0, mon.ErrCount())
// Set to Ready.
mon.SetReady()
assertEqual(t, Ready, mon.GetState())
assertEqual(t, nil, mon.Err())
assertEqual(t, 0, mon.ErrCount())
// Set to Error.
reason := errors.New("some reason")
mon.SetError(reason)
assertEqual(t, Error, mon.GetState())
assertEqual(t, reason, mon.Err())
assertEqual(t, 1, mon.ErrCount())
// Two consecutive errors.
mon.SetError(reason)
assertEqual(t, Error, mon.GetState())
assertEqual(t, reason, mon.Err())
assertEqual(t, 2, mon.ErrCount())
// Set Ready again. Previous error can still be retrieved.
mon.SetReady()
assertEqual(t, Ready, mon.GetState())
assertEqual(t, reason, mon.Err())
assertEqual(t, 0, mon.ErrCount())
// Stop.
mon.Stop()
assertEqual(t, Stopped, mon.GetState())
// Can't restart.
assertPanic(t, func() { mon.SetReady() }, "cannot transition from stopped state")
assertPanic(t, func() { mon.SetError(reason) }, "cannot transition from stopped state")
}
func TestMonitorNotifications(t *testing.T) {
mon := Monitor{}
ch := make(chan Event, 1)
mon.RegisterCallback(func(ev Event) { ch <- ev })
// Set to Ready.
mon.SetReady()
n := assertReceived(t, ch)
assertEqual(t, Initializing, n.OldState)
assertEqual(t, Ready, n.NewState)
assertEqual(t, nil, n.Error)
assertEqual(t, 0, n.ErrCount)
// Set to Ready again, and there's not an additional notification.
mon.SetReady()
if len(ch) > 0 {
t.Error("unexpected notification")
}
// Set to Error.
reason := errors.New("some reason")
mon.SetError(reason)
n = assertReceived(t, ch)
assertEqual(t, Ready, n.OldState)
assertEqual(t, Error, n.NewState)
assertEqual(t, reason, n.Error)
assertEqual(t, 1, n.ErrCount)
// Two consecutive errors.
mon.SetError(reason)
n = assertReceived(t, ch)
assertEqual(t, Error, mon.GetState())
assertEqual(t, reason, mon.Err())
assertEqual(t, 2, n.ErrCount)
// Set ready again.
mon.SetReady()
n = assertReceived(t, ch)
assertEqual(t, Error, n.OldState)
assertEqual(t, Ready, n.NewState)
assertEqual(t, nil, n.Error)
assertEqual(t, 0, n.ErrCount)
// Stop.
mon.Stop()
n = assertReceived(t, ch)
assertEqual(t, Ready, n.OldState)
assertEqual(t, Stopped, n.NewState)
assertEqual(t, nil, n.Error)
assertEqual(t, 0, n.ErrCount)
// Stop again, and there's not an additional notification.
mon.Stop()
if len(ch) > 0 {
t.Error("unexpected notification")
}
close(ch)
}
func TestDeregister(t *testing.T) {
mon := Monitor{}
ch := make(chan Event, 1)
foo := mon.RegisterCallback(func(ev Event) { ch <- ev })
// Set to ready.
mon.SetReady()
n := assertReceived(t, ch)
assertEqual(t, n.OldState, Initializing)
assertEqual(t, n.NewState, Ready)
assertEqual(t, n.Error, nil)
mon.DeregisterCallback(foo)
// No more notifications.
mon.Stop()
if len(ch) > 0 {
t.Error("unexpected notification")
}
// Deregistering again is not an error
mon.DeregisterCallback(foo)
close(ch)
}