-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus_test.go
236 lines (193 loc) · 6.15 KB
/
status_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package httpstatus
import (
"context"
"encoding/json"
"errors"
"net/http/httptest"
"testing"
"time"
"github.com/smarty/assertions/should"
"github.com/smarty/gunit"
)
func TestStatusFixture(t *testing.T) {
gunit.Run(new(StatusFixture), t)
}
type StatusFixture struct {
*gunit.Fixture
ctx context.Context
shutdown context.CancelFunc
handler Handler
healthyCount int
failingCount int
failingError error
stoppingCount int
statusCount int
statusContext context.Context
statusError error
versionName string
shutdownContextOnStatusCheck int
healthCheckTimeout time.Duration
healthCheckFrequency time.Duration
shutdownDelay time.Duration
closed int
}
func (this *StatusFixture) Setup() {
this.ctx, this.shutdown = context.WithCancel(context.Background())
this.healthCheckTimeout = time.Second
this.healthCheckFrequency = time.Millisecond
this.shutdownDelay = time.Millisecond
this.initialize()
}
func (this *StatusFixture) initialize() {
this.handler = New(
Options.ResourceName("resource-name"),
Options.DisplayName("display-name"),
Options.HealthCheck(this),
Options.Monitor(this),
Options.Context(this.ctx),
Options.HealthCheckTimeout(this.healthCheckTimeout),
Options.HealthCheckFrequency(this.healthCheckFrequency),
Options.ShutdownDelay(this.shutdownDelay),
Options.VersionName(this.versionName),
)
}
func (this *StatusFixture) TestHTTPResponseShouldBeWrittenCorrectly() {
this.versionName = "version"
this.initialize()
this.assertHTTP(stateStarting, 503, "Starting")
this.assertHTTP(stateHealthy, 200, "OK")
this.assertHTTP(stateFailing, 503, "Failing")
this.assertHTTP(stateStopping, 503, "Stopping")
}
func (this *StatusFixture) assertHTTP(state uint32, statusCode int, expectedStatus string) {
this.handler.(*defaultStatus).state = state
response := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/", nil)
this.handler.ServeHTTP(response, request)
this.So(response.Code, should.Equal, statusCode)
this.So(response.Header().Get("Content-Type"), should.Equal, "application/json; charset=utf-8")
var actual jsonResponse
err := json.Unmarshal(response.Body.Bytes(), &actual)
this.So(err, should.BeNil)
this.So(actual, should.Equal, jsonResponse{
Compatibility: "display-name:" + expectedStatus,
Application: "display-name",
Resource: "resource-name",
State: expectedStatus,
Version: this.versionName,
})
}
func (this *StatusFixture) TestWhenStatusHealthy_MarkAsHealthy() {
go func() {
time.Sleep(time.Millisecond)
_ = this.handler.Close()
}()
this.handler.Listen()
this.So(this.healthyCount, should.BeGreaterThan, 0)
this.So(this.failingCount, should.Equal, 0)
}
func (this *StatusFixture) TestWhenContextIsCancelled_ListenExits() {
this.shutdown()
this.handler.Listen()
this.So(this.statusContext, should.BeNil)
this.So(this.healthyCount, should.Equal, 0)
this.So(this.failingCount, should.Equal, 0)
this.So(this.stoppingCount, should.Equal, 1)
}
func (this *StatusFixture) TestWhenStatusFailing_MarkAsFailing() {
this.statusError = errors.New("")
this.shutdownContextOnStatusCheck = 2
this.handler.Listen()
this.So(this.failingCount, should.Equal, 1)
this.So(this.failingError, should.Equal, this.statusError)
}
func (this *StatusFixture) TestWhenStatusCheckContextTimesOut_MarkAsFailing() {
this.healthCheckTimeout = time.Nanosecond
this.shutdownContextOnStatusCheck = 1
this.initialize()
this.handler.Listen()
this.So(this.statusCount, should.Equal, 2)
this.So(this.failingCount, should.Equal, 1)
this.So(this.failingError, should.Resemble, context.DeadlineExceeded)
}
func (this *StatusFixture) TestSleepBetweenHealthChecks() {
this.healthCheckFrequency = time.Millisecond * 10
this.shutdownContextOnStatusCheck = 1
this.initialize()
started := time.Now().UTC()
this.handler.Listen()
this.So(time.Since(started), should.BeGreaterThan, this.healthCheckFrequency)
}
func (this *StatusFixture) TestWhenConsecutiveChecksAreHealthy_OnlyUpdateMonitorOnce() {
this.shutdownContextOnStatusCheck = 4
this.handler.Listen()
this.So(this.healthyCount, should.Equal, 1)
this.So(this.failingCount, should.Equal, 0)
}
func (this *StatusFixture) TestWhenConsecutiveChecksAreFailing_OnlyUpdateMonitorOnce() {
this.statusError = errors.New("")
this.shutdownContextOnStatusCheck = 4
this.handler.Listen()
this.So(this.failingCount, should.Equal, 1)
this.So(this.healthyCount, should.Equal, 0)
}
func (this *StatusFixture) TestWhenShuttingDown_DelayShouldBeUsed() {
this.shutdownDelay = time.Millisecond * 10
this.initialize()
go func() {
time.Sleep(time.Millisecond)
_ = this.handler.Close()
}()
started := time.Now().UTC()
this.handler.Listen()
this.So(time.Since(started), should.BeGreaterThan, this.shutdownDelay)
}
func (this *StatusFixture) TestWhenShuttingDownHard_DelayShouldBeIgnored() {
this.shutdown()
this.shutdownDelay = time.Second
started := time.Now().UTC()
this.handler.Listen()
this.So(time.Since(started), should.BeLessThan, this.shutdownDelay)
}
func (this *StatusFixture) TestWhenShuttingDownWhileNotHealthy_DelayShouldBeIgnored() {
this.shutdownDelay = time.Second
this.statusError = errors.New("")
this.initialize()
_ = this.handler.Close()
started := time.Now().UTC()
this.handler.Listen()
this.So(time.Since(started), should.BeLessThan, time.Millisecond)
}
func (this *StatusFixture) TestWhenShuttingDown_HealthCheckClosed() {
this.initialize()
_ = this.handler.Close()
this.So(this.closed, should.Equal, 1)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func (this *StatusFixture) Status(ctx context.Context) error {
this.statusCount++
this.statusContext = ctx
if this.shutdownContextOnStatusCheck > 0 && this.statusCount > this.shutdownContextOnStatusCheck {
this.shutdown()
}
select {
case <-ctx.Done():
return ctx.Err()
default:
return this.statusError
}
}
func (this *StatusFixture) Healthy() {
this.healthyCount++
}
func (this *StatusFixture) Failing(err error) {
this.failingCount++
this.failingError = err
}
func (this *StatusFixture) Stopping() {
this.stoppingCount++
}
func (this *StatusFixture) Close() error {
this.closed++
return nil
}