@ohos-ports/cloudnative-health 2.1.2-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1003 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright IBM Corporation 2018
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
18
+ if (k2 === undefined) k2 = k;
19
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
20
+ }) : (function(o, m, k, k2) {
21
+ if (k2 === undefined) k2 = k;
22
+ o[k2] = m[k];
23
+ }));
24
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
25
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
26
+ }) : function(o, v) {
27
+ o["default"] = v;
28
+ });
29
+ var __importStar = (this && this.__importStar) || function (mod) {
30
+ if (mod && mod.__esModule) return mod;
31
+ var result = {};
32
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
33
+ __setModuleDefault(result, mod);
34
+ return result;
35
+ };
36
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
37
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
38
+ return new (P || (P = Promise))(function (resolve, reject) {
39
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
40
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
41
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
42
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
43
+ });
44
+ };
45
+ Object.defineProperty(exports, "__esModule", { value: true });
46
+ const chai_1 = require("chai");
47
+ const index_1 = require("../../index");
48
+ const http = __importStar(require("http"));
49
+ chai_1.should();
50
+ let mockServer;
51
+ before((done) => {
52
+ mockServer = http.createServer((req, res) => {
53
+ res.writeHead(200);
54
+ res.end();
55
+ });
56
+ mockServer.listen(3000, done);
57
+ });
58
+ after((done) => {
59
+ if (mockServer) {
60
+ mockServer.close(done);
61
+ }
62
+ else {
63
+ done();
64
+ }
65
+ });
66
+ describe('Health Checker test suite', () => {
67
+ it('Startup reports DOWN', () => __awaiter(void 0, void 0, void 0, function* () {
68
+ let healthCheck = new index_1.HealthChecker();
69
+ const promise = () => new Promise((_resolve, _reject) => {
70
+ throw new Error("Startup Failure");
71
+ });
72
+ let check = new index_1.StartupCheck("check", promise);
73
+ yield healthCheck.registerStartupCheck(check);
74
+ const status = yield healthCheck.getStatus();
75
+ const result = status.status;
76
+ chai_1.expect(result).to.equal(index_1.State.DOWN, `Should return: ${index_1.State.DOWN} , but returned: ${result}`);
77
+ }));
78
+ it('Startup reports UP', () => __awaiter(void 0, void 0, void 0, function* () {
79
+ let healthCheck = new index_1.HealthChecker();
80
+ // tslint:disable-next-line:no-shadowed-variable
81
+ const promise = () => new Promise((resolve, _reject) => {
82
+ resolve();
83
+ });
84
+ let check = new index_1.StartupCheck("check", promise);
85
+ yield healthCheck.registerStartupCheck(check);
86
+ const status = yield healthCheck.getStatus();
87
+ const result = status.status;
88
+ chai_1.expect(result).to.equal(index_1.State.UP, `Should return: ${index_1.State.UP} , but returned: ${result}`);
89
+ }));
90
+ it('Startup reports STARTING when first is starting and second is up', () => __awaiter(void 0, void 0, void 0, function* () {
91
+ let healthCheck = new index_1.HealthChecker();
92
+ const Check1 = () => new Promise((resolve, _reject) => {
93
+ setTimeout(resolve, 1000, 'foo');
94
+ });
95
+ let check1 = new index_1.StartupCheck('Check1', Check1);
96
+ healthCheck.registerStartupCheck(check1);
97
+ const Check2 = () => new Promise((resolve, _reject) => {
98
+ resolve();
99
+ });
100
+ let check2 = new index_1.StartupCheck('Check2', Check2);
101
+ //don't await as the check should not be resolved or rejected -> resembles an app starting
102
+ healthCheck.registerStartupCheck(check2);
103
+ const status = yield healthCheck.getStatus();
104
+ const result = status.status;
105
+ const expected = index_1.State.STARTING;
106
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
107
+ }));
108
+ it('Health reports STARTING with a pending startUp check', () => __awaiter(void 0, void 0, void 0, function* () {
109
+ let healthCheck = new index_1.HealthChecker();
110
+ const Check1 = () => new Promise((resolve, _reject) => {
111
+ setTimeout(resolve, 100, 'foo');
112
+ });
113
+ let startCheck = new index_1.StartupCheck('StartCheck', Check1);
114
+ healthCheck.registerStartupCheck(startCheck);
115
+ const status = yield healthCheck.getStatus();
116
+ const result = status.status;
117
+ const expected = index_1.State.STARTING;
118
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
119
+ }));
120
+ it('Liveness reports UP with a pending startUp check that will resolve', () => __awaiter(void 0, void 0, void 0, function* () {
121
+ let healthCheck = new index_1.HealthChecker();
122
+ const Check1 = () => new Promise((resolve, _reject) => {
123
+ setTimeout(resolve, 100, 'foo');
124
+ });
125
+ let startCheck = new index_1.StartupCheck('StartCheck', Check1);
126
+ healthCheck.registerStartupCheck(startCheck);
127
+ const status = yield healthCheck.getLivenessStatus();
128
+ const result = status.status;
129
+ const expected = index_1.State.UP;
130
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
131
+ }));
132
+ it('Liveness reports DOWN with a pending startUp check that will fail', () => __awaiter(void 0, void 0, void 0, function* () {
133
+ let healthCheck = new index_1.HealthChecker();
134
+ const Check1 = () => new Promise((_resolve, reject) => {
135
+ setTimeout(reject, 1000, 'foo');
136
+ });
137
+ let startCheck = new index_1.StartupCheck('StartCheck', Check1);
138
+ healthCheck.registerStartupCheck(startCheck);
139
+ const status = yield healthCheck.getLivenessStatus();
140
+ const result = status.status;
141
+ const expected = index_1.State.DOWN;
142
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
143
+ }));
144
+ it('Readiness reports UP with a pending startUp check that will resolve', () => __awaiter(void 0, void 0, void 0, function* () {
145
+ let healthCheck = new index_1.HealthChecker();
146
+ const Check1 = () => new Promise((resolve, _reject) => {
147
+ setTimeout(resolve, 100, 'foo');
148
+ });
149
+ let startCheck = new index_1.StartupCheck('StartCheck', Check1);
150
+ healthCheck.registerStartupCheck(startCheck);
151
+ const status = yield healthCheck.getReadinessStatus();
152
+ const result = status.status;
153
+ const expected = index_1.State.UP;
154
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
155
+ }));
156
+ it('Readiness reports DOWN with a pending startUp check that will fail', () => __awaiter(void 0, void 0, void 0, function* () {
157
+ let healthCheck = new index_1.HealthChecker();
158
+ const Check1 = () => new Promise((resolve, reject) => {
159
+ setTimeout(reject, 100, 'foo');
160
+ });
161
+ let startCheck = new index_1.StartupCheck('StartCheck', Check1);
162
+ healthCheck.registerStartupCheck(startCheck);
163
+ const status = yield healthCheck.getReadinessStatus();
164
+ const result = status.status;
165
+ const expected = index_1.State.DOWN;
166
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
167
+ }));
168
+ it('Startup reports STARTING when first is up and the second is starting', () => __awaiter(void 0, void 0, void 0, function* () {
169
+ let healthCheck = new index_1.HealthChecker();
170
+ const Check1 = () => new Promise((resolve, _reject) => {
171
+ resolve();
172
+ });
173
+ let check1 = new index_1.StartupCheck('Check1', Check1);
174
+ healthCheck.registerStartupCheck(check1);
175
+ const Check2 = () => new Promise((resolve, _reject) => {
176
+ setTimeout(resolve, 1000, 'foo');
177
+ });
178
+ let check2 = new index_1.StartupCheck('Check2', Check2);
179
+ healthCheck.registerStartupCheck(check2); //do not await as its starting but not complete
180
+ const status = yield healthCheck.getStatus();
181
+ const result = status.status;
182
+ const expected = index_1.State.STARTING;
183
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
184
+ }));
185
+ it('Startup reports STARTING with returned Promise', () => __awaiter(void 0, void 0, void 0, function* () {
186
+ let healthCheck = new index_1.HealthChecker();
187
+ const promise = () => new Promise((_resolve, _reject) => {
188
+ // tslint:disable-next-line:no-shadowed-variable no-unused-expression
189
+ new Promise((resolve, _reject) => {
190
+ setTimeout(resolve, 1000, 'foo');
191
+ });
192
+ });
193
+ let check = new index_1.StartupCheck("check", promise);
194
+ healthCheck.registerStartupCheck(check)
195
+ .then(() => __awaiter(void 0, void 0, void 0, function* () {
196
+ const status = yield healthCheck.getStatus();
197
+ const result = status.status;
198
+ chai_1.expect(result).to.equal(index_1.State.STARTING, `Should return: ${index_1.State.STARTING} , but returned: ${result}`);
199
+ }));
200
+ }));
201
+ it('Startup reports STARTING without returned Promise', () => __awaiter(void 0, void 0, void 0, function* () {
202
+ let healthCheck = new index_1.HealthChecker();
203
+ const promise = () => new Promise((_resolve, _reject) => {
204
+ // tslint:disable-next-line:no-unused-expression no-shadowed-variable
205
+ new Promise((resolve, _reject) => {
206
+ setTimeout(resolve, 1000, 'foo');
207
+ });
208
+ });
209
+ let check = new index_1.StartupCheck("check", promise);
210
+ healthCheck.registerStartupCheck(check)
211
+ .then(() => __awaiter(void 0, void 0, void 0, function* () {
212
+ const status = yield healthCheck.getStatus();
213
+ const result = status.status;
214
+ chai_1.expect(result).to.equal(index_1.State.STARTING, `Should return: ${index_1.State.STARTING} , but returned: ${result}`);
215
+ }));
216
+ }));
217
+ it('Startup reports STARTING when multiple checks are still starting', () => __awaiter(void 0, void 0, void 0, function* () {
218
+ let healthCheck = new index_1.HealthChecker();
219
+ const promise1 = () => new Promise((_resolve, _reject) => {
220
+ // tslint:disable-next-line:no-unused-expression no-shadowed-variable
221
+ new Promise((resolve, _reject) => {
222
+ setTimeout(resolve, 1000, 'foo');
223
+ });
224
+ });
225
+ let check1 = new index_1.StartupCheck("check", promise1);
226
+ const promise2 = () => new Promise((_resolve, _reject) => {
227
+ // tslint:disable-next-line:no-unused-expression no-shadowed-variable
228
+ new Promise((resolve, _reject) => {
229
+ setTimeout(resolve, 100, 'foo');
230
+ });
231
+ });
232
+ let check2 = new index_1.StartupCheck("check", promise2);
233
+ healthCheck.registerStartupCheck(check1);
234
+ healthCheck.registerStartupCheck(check2)
235
+ .then(() => __awaiter(void 0, void 0, void 0, function* () {
236
+ const status = yield healthCheck.getStatus();
237
+ const result = status.status;
238
+ chai_1.expect(result).to.equal(index_1.State.STARTING, `Should return: ${index_1.State.STARTING} , but returned: ${result}`);
239
+ }));
240
+ }));
241
+ it('Liveness reports DOWN if startup is DOWN', () => __awaiter(void 0, void 0, void 0, function* () {
242
+ let healthCheck = new index_1.HealthChecker();
243
+ const promiseOne = () => new Promise((_resolve, _reject) => {
244
+ throw new Error("Liveness Failure");
245
+ });
246
+ let checkOne = new index_1.LivenessCheck("checkOne", promiseOne);
247
+ healthCheck.registerLivenessCheck(checkOne);
248
+ const status = yield healthCheck.getLivenessStatus();
249
+ const result = status.status;
250
+ chai_1.expect(result).to.equal(index_1.State.DOWN, `Should return: ${index_1.State.DOWN} , but returned: ${result}`);
251
+ }));
252
+ it('Startup is UP and Liveness is DOWN, calling Liveness status should report DOWN', () => __awaiter(void 0, void 0, void 0, function* () {
253
+ let healthCheck = new index_1.HealthChecker();
254
+ const LivenessPromise = () => new Promise((_resolve, _reject) => {
255
+ throw new Error("error");
256
+ });
257
+ const StartupPromise = () => new Promise((resolve, _reject) => {
258
+ resolve();
259
+ });
260
+ let checkOne = new index_1.LivenessCheck("checkOne", LivenessPromise);
261
+ let checkTwo = new index_1.StartupCheck("checkTwo", StartupPromise);
262
+ //startup check promise is resolved so liveness should not fallback to get startupStatus
263
+ yield healthCheck.registerStartupCheck(checkTwo);
264
+ healthCheck.registerLivenessCheck(checkOne);
265
+ const status = yield healthCheck.getLivenessStatus();
266
+ const result = status.status;
267
+ chai_1.expect(result).to.equal(index_1.State.DOWN, `Should return: ${index_1.State.DOWN} , but returned: ${result}`);
268
+ }));
269
+ it('Startup is UP and Liveness is DOWN, calling getStatus should report DOWN', () => __awaiter(void 0, void 0, void 0, function* () {
270
+ let healthCheck = new index_1.HealthChecker();
271
+ const LivenessPromise = () => new Promise((_resolve, _reject) => {
272
+ throw new Error("error");
273
+ });
274
+ const StartupPromise = () => new Promise((resolve, _reject) => {
275
+ resolve();
276
+ });
277
+ let checkOne = new index_1.LivenessCheck("checkOne", LivenessPromise);
278
+ let checkTwo = new index_1.StartupCheck("checkTwo", StartupPromise);
279
+ yield healthCheck.registerStartupCheck(checkTwo);
280
+ healthCheck.registerLivenessCheck(checkOne);
281
+ const status = yield healthCheck.getStatus();
282
+ const result = status.status;
283
+ chai_1.expect(result).to.equal(index_1.State.DOWN, `Should return: ${index_1.State.DOWN} , but returned: ${result}`);
284
+ }));
285
+ it('Startup is UP and Liveness is UP, calling Liveness status should report UP', () => __awaiter(void 0, void 0, void 0, function* () {
286
+ let healthCheck = new index_1.HealthChecker();
287
+ const LivenessPromise = () => new Promise((resolve, _reject) => {
288
+ resolve();
289
+ });
290
+ const StartupPromise = () => new Promise((resolve, _reject) => {
291
+ resolve();
292
+ });
293
+ let checkOne = new index_1.LivenessCheck("checkOne", LivenessPromise);
294
+ let checkTwo = new index_1.StartupCheck("checkTwo", StartupPromise);
295
+ yield healthCheck.registerStartupCheck(checkTwo);
296
+ healthCheck.registerLivenessCheck(checkOne);
297
+ const status = yield healthCheck.getLivenessStatus();
298
+ const result = status.status;
299
+ chai_1.expect(result).to.equal(index_1.State.UP, `Should return: ${index_1.State.UP} , but returned: ${result}`);
300
+ }));
301
+ it('Startup is UP and Readiness is DOWN, calling Readiness status should report DOWN', () => __awaiter(void 0, void 0, void 0, function* () {
302
+ let healthCheck = new index_1.HealthChecker();
303
+ const ReadinessPromise = () => new Promise((_resolve, _reject) => {
304
+ throw new Error("error");
305
+ });
306
+ const StartupPromise = () => new Promise((resolve, _reject) => {
307
+ resolve();
308
+ });
309
+ let checkOne = new index_1.ReadinessCheck("checkOne", ReadinessPromise);
310
+ let checkTwo = new index_1.StartupCheck("checkTwo", StartupPromise);
311
+ yield healthCheck.registerStartupCheck(checkTwo);
312
+ healthCheck.registerReadinessCheck(checkOne);
313
+ const status = yield healthCheck.getReadinessStatus();
314
+ const result = status.status;
315
+ chai_1.expect(result).to.equal(index_1.State.DOWN, `Should return: ${index_1.State.DOWN} , but returned: ${result}`);
316
+ }));
317
+ it('Startup is UP and Readiness is DOWN, calling getSatus should report DOWN', () => __awaiter(void 0, void 0, void 0, function* () {
318
+ let healthcheck = new index_1.HealthChecker();
319
+ const ReadinessPromise = () => new Promise((_resolve, _reject) => {
320
+ throw new Error("error");
321
+ });
322
+ const StartupPromise = () => new Promise((resolve, _reject) => {
323
+ resolve();
324
+ });
325
+ let checkone = new index_1.ReadinessCheck("checkone", ReadinessPromise);
326
+ let checktwo = new index_1.StartupCheck("checktwo", StartupPromise);
327
+ yield healthcheck.registerStartupCheck(checktwo);
328
+ healthcheck.registerReadinessCheck(checkone);
329
+ const status = yield healthcheck.getStatus();
330
+ const result = status.status;
331
+ chai_1.expect(result).to.equal(index_1.State.DOWN, `Should return: ${index_1.State.DOWN} , but returned: ${result}`);
332
+ }));
333
+ it('Startup is UP, getStartupComplete should return true with a liveness check', () => __awaiter(void 0, void 0, void 0, function* () {
334
+ let healthcheck = new index_1.HealthChecker();
335
+ const StartupPromise = () => new Promise((resolve, _reject) => {
336
+ resolve();
337
+ });
338
+ let check = new index_1.LivenessCheck("check", StartupPromise);
339
+ healthcheck.registerLivenessCheck(check);
340
+ let status = yield healthcheck.getLivenessStatus();
341
+ let result = yield healthcheck.getStartUpComplete();
342
+ chai_1.expect(result).to.equal(true, `Should return that startupComplete is true, but returned ${result}`);
343
+ }));
344
+ it('Startup is DOWN, getStartupComplete should return false with a getStatus call', () => __awaiter(void 0, void 0, void 0, function* () {
345
+ let healthcheck = new index_1.HealthChecker();
346
+ const StartupPromise = () => new Promise((_resolve, _reject) => {
347
+ throw new Error("Startup failed");
348
+ });
349
+ let check = new index_1.StartupCheck("check", StartupPromise);
350
+ healthcheck.registerStartupCheck(check);
351
+ yield healthcheck.getStatus();
352
+ let result = healthcheck.getStartUpComplete();
353
+ chai_1.expect(result).to.equal(false, `Should return that startupComplete is false, but returned ${result}`);
354
+ }));
355
+ it('Startup is DOWN and should return DOWN with a liveness check', () => __awaiter(void 0, void 0, void 0, function* () {
356
+ let healthcheck = new index_1.HealthChecker();
357
+ const StartupPromise = () => new Promise((_resolve, _reject) => {
358
+ throw new Error("Startup failed");
359
+ });
360
+ const LivenessPromise = () => new Promise((resolve, _reject) => {
361
+ resolve();
362
+ });
363
+ let check = new index_1.StartupCheck("check", StartupPromise);
364
+ let check2 = new index_1.ReadinessCheck("check2", LivenessPromise);
365
+ yield healthcheck.registerStartupCheck(check);
366
+ healthcheck.registerLivenessCheck(check2);
367
+ let status = yield healthcheck.getLivenessStatus();
368
+ let result = status.status;
369
+ chai_1.expect(result).to.equal(index_1.State.DOWN, `Should return ${index_1.State.DOWN} but returned ${result}`);
370
+ }));
371
+ it('Health reports UP by default', () => __awaiter(void 0, void 0, void 0, function* () {
372
+ let healthcheck = new index_1.HealthChecker();
373
+ const status = yield healthcheck.getStatus();
374
+ let result = status.status;
375
+ chai_1.expect(result).to.equal(index_1.State.UP, `Should return: ${index_1.State.UP}, but returned: ${result}`);
376
+ }));
377
+ it('Health reports UP and empty checks array no registered liveness checks', () => __awaiter(void 0, void 0, void 0, function* () {
378
+ let healthcheck = new index_1.HealthChecker();
379
+ const status = yield healthcheck.getStatus();
380
+ const result = JSON.stringify(status);
381
+ let expected = "{\"status\":\"UP\",\"checks\":[]}";
382
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
383
+ }));
384
+ it('Health reports UP and check result with single registered liveness check', () => __awaiter(void 0, void 0, void 0, function* () {
385
+ let healthcheck = new index_1.HealthChecker();
386
+ // tslint:disable-next-line:no-shadowed-variable
387
+ const promise = () => new Promise((resolve, _reject) => {
388
+ resolve();
389
+ });
390
+ let check = new index_1.LivenessCheck("check", promise);
391
+ healthcheck.registerLivenessCheck(check);
392
+ const status = yield healthcheck.getStatus();
393
+ const result = JSON.stringify(status);
394
+ let expected = "{\"status\":\"UP\",\"checks\":[{\"name\":\"check\",\"state\":\"UP\",\"data\":{\"reason\":\"\"}}]}";
395
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
396
+ }));
397
+ it('Health reports UP and check result with two registered liveness checks', () => __awaiter(void 0, void 0, void 0, function* () {
398
+ let healthcheck = new index_1.HealthChecker();
399
+ // tslint:disable-next-line:no-shadowed-variable
400
+ const promiseone = () => new Promise((resolve, _reject) => {
401
+ resolve();
402
+ });
403
+ let checkone = new index_1.LivenessCheck("checkone", promiseone);
404
+ // tslint:disable-next-line:no-shadowed-variable
405
+ const promisetwo = () => new Promise((resolve, _reject) => {
406
+ resolve();
407
+ });
408
+ let checktwo = new index_1.LivenessCheck("checktwo", promisetwo);
409
+ healthcheck.registerLivenessCheck(checkone);
410
+ healthcheck.registerLivenessCheck(checktwo);
411
+ const status = yield healthcheck.getStatus();
412
+ const result = JSON.stringify(status);
413
+ let expected = "{\"status\":\"UP\",\"checks\":[{\"name\":\"checkone\",\"state\":\"UP\",\"data\":{\"reason\":\"\"}},{\"name\":\"checktwo\",\"state\":\"UP\",\"data\":{\"reason\":\"\"}}]}";
414
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
415
+ }));
416
+ it('Health reports DOWN and check result with single failed liveness check', () => __awaiter(void 0, void 0, void 0, function* () {
417
+ let healthcheck = new index_1.HealthChecker();
418
+ const promise = () => new Promise((_resolve, _reject) => {
419
+ throw new Error("Startup Failure");
420
+ });
421
+ let check = new index_1.LivenessCheck("check", promise);
422
+ healthcheck.registerLivenessCheck(check);
423
+ const status = yield healthcheck.getStatus();
424
+ const result = JSON.stringify(status);
425
+ let expected = "{\"status\":\"DOWN\",\"checks\":[{\"name\":\"check\",\"state\":\"DOWN\",\"data\":{\"reason\":\"Startup Failure\"}}]}";
426
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
427
+ }));
428
+ it('Health reports DOWN and check result with single rejected liveness check', () => __awaiter(void 0, void 0, void 0, function* () {
429
+ let healthcheck = new index_1.HealthChecker();
430
+ const promise = () => new Promise((_resolve, reject) => {
431
+ reject(new Error("Startup Failure"));
432
+ });
433
+ let check = new index_1.LivenessCheck("check", promise);
434
+ healthcheck.registerLivenessCheck(check);
435
+ const status = yield healthcheck.getStatus();
436
+ const result = JSON.stringify(status);
437
+ let expected = "{\"status\":\"DOWN\",\"checks\":[{\"name\":\"check\",\"state\":\"DOWN\",\"data\":{\"reason\":\"Startup Failure\"}}]}";
438
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
439
+ }));
440
+ it('Health reports UP with running Liveness PingCheck', () => __awaiter(void 0, void 0, void 0, function* () {
441
+ let healthcheck = new index_1.HealthChecker();
442
+ let check = new index_1.PingCheck("localhost", "", "3000");
443
+ healthcheck.registerLivenessCheck(check);
444
+ const status = yield healthcheck.getStatus();
445
+ const result = JSON.stringify(status);
446
+ let expected = "{\"status\":\"UP\",\"checks\":[{\"name\":\"PingCheck HEAD:localhost:3000/\",\"state\":\"UP\",\"data\":{\"reason\":\"\"}}]}";
447
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
448
+ }));
449
+ it('Health reports DOWN with failing Liveness PingCheck', () => __awaiter(void 0, void 0, void 0, function* () {
450
+ let healthcheck = new index_1.HealthChecker();
451
+ let check = new index_1.PingCheck("not-an-address.com");
452
+ healthcheck.registerLivenessCheck(check);
453
+ const status = yield healthcheck.getStatus();
454
+ let expected = 'PingCheck HEAD:not-an-address.com:80/';
455
+ const statusVal = status.status;
456
+ const checksState = status.checks[0].state;
457
+ const checkName = status.checks[0].name;
458
+ chai_1.expect(statusVal).to.equal(index_1.State.DOWN, `Should return: ${index_1.State.DOWN}, but returned: ${statusVal}`);
459
+ chai_1.expect(checksState).to.equal(index_1.State.DOWN, `Should return: ${index_1.State.DOWN}, but returned: ${checksState}`);
460
+ chai_1.expect(checkName).to.equal(expected, `Should return: ${expected}, but returned: ${checkName}`);
461
+ }));
462
+ it('Health reports DOWN on second invocation of a liveness check', () => __awaiter(void 0, void 0, void 0, function* () {
463
+ let healthcheck = new index_1.HealthChecker();
464
+ let count = 0;
465
+ const promise = () => new Promise((resolve, reject) => {
466
+ if (count > 0) {
467
+ reject(new Error("Liveness failure"));
468
+ }
469
+ else {
470
+ count = count + 1;
471
+ resolve();
472
+ }
473
+ });
474
+ let check = new index_1.LivenessCheck("check", promise);
475
+ healthcheck.registerLivenessCheck(check);
476
+ let status = yield healthcheck.getStatus();
477
+ status = yield healthcheck.getStatus();
478
+ const result = JSON.stringify(status);
479
+ let expected = "{\"status\":\"\DOWN\",\"checks\":[{\"name\":\"check\",\"state\":\"DOWN\",\"data\":{\"reason\":\"Liveness failure\"}}]}";
480
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
481
+ }));
482
+ it('Health reports UP on second invocation of a liveness check', () => __awaiter(void 0, void 0, void 0, function* () {
483
+ let healthcheck = new index_1.HealthChecker();
484
+ let count = 0;
485
+ const promise = () => new Promise((resolve, reject) => {
486
+ if (count > 0) {
487
+ resolve();
488
+ }
489
+ else {
490
+ count = count + 1;
491
+ reject(new Error("Liveness failure"));
492
+ }
493
+ });
494
+ let check = new index_1.LivenessCheck("check", promise);
495
+ healthcheck.registerLivenessCheck(check);
496
+ let status = yield healthcheck.getStatus();
497
+ status = yield healthcheck.getStatus();
498
+ const result = JSON.stringify(status);
499
+ let expected = "{\"status\":\"\UP\",\"checks\":[{\"name\":\"check\",\"state\":\"UP\",\"data\":{\"reason\":\"\"}}]}";
500
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
501
+ }));
502
+ it('Health reports DOWN and check result with one passed and one failed Liveness checks', () => __awaiter(void 0, void 0, void 0, function* () {
503
+ let healthcheck = new index_1.HealthChecker();
504
+ const promiseone = () => new Promise((_resolve, _reject) => {
505
+ throw new Error("Startup Failure");
506
+ });
507
+ let checkone = new index_1.LivenessCheck("checkone", promiseone);
508
+ const promisetwo = () => new Promise((resolve, _reject) => {
509
+ resolve();
510
+ });
511
+ let checktwo = new index_1.LivenessCheck("checktwo", promisetwo);
512
+ healthcheck.registerLivenessCheck(checkone);
513
+ healthcheck.registerLivenessCheck(checktwo);
514
+ const status = yield healthcheck.getStatus();
515
+ const result = JSON.stringify(status);
516
+ let expected = "{\"status\":\"DOWN\",\"checks\":[{\"name\":\"checkone\",\"state\":\"DOWN\",\"data\":{\"reason\":\"Startup Failure\"}},{\"name\":\"checktwo\",\"state\":\"UP\",\"data\":{\"reason\":\"\"}}]}";
517
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
518
+ }));
519
+ it('Health reports DOWN and check result with one passed Liveness and one failed Readiness checks', () => __awaiter(void 0, void 0, void 0, function* () {
520
+ let healthcheck = new index_1.HealthChecker();
521
+ const promiseone = () => new Promise((_resolve, _reject) => {
522
+ throw new Error("Readiness Failure");
523
+ });
524
+ let checkone = new index_1.ReadinessCheck("checkone", promiseone);
525
+ const promisetwo = () => new Promise((resolve, _reject) => {
526
+ resolve();
527
+ });
528
+ let checktwo = new index_1.LivenessCheck("checktwo", promisetwo);
529
+ healthcheck.registerReadinessCheck(checkone);
530
+ healthcheck.registerLivenessCheck(checktwo);
531
+ const status = yield healthcheck.getStatus();
532
+ const result = JSON.stringify(status);
533
+ let expected = "{\"status\":\"DOWN\",\"checks\":[{\"name\":\"checkone\",\"state\":\"DOWN\",\"data\":{\"reason\":\"Readiness Failure\"}},{\"name\":\"checktwo\",\"state\":\"UP\",\"data\":{\"reason\":\"\"}}]}";
534
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
535
+ }));
536
+ it('Readiness reports UP by default', () => __awaiter(void 0, void 0, void 0, function* () {
537
+ let healthcheck = new index_1.HealthChecker();
538
+ const status = yield healthcheck.getReadinessStatus();
539
+ let result = status.status;
540
+ chai_1.expect(result).to.equal(index_1.State.UP, `Should return: ${index_1.State.UP}, but returned: ${result}`);
541
+ }));
542
+ it('Readiness reports UP and empty checks array no registered checks', () => __awaiter(void 0, void 0, void 0, function* () {
543
+ let healthcheck = new index_1.HealthChecker();
544
+ const status = yield healthcheck.getReadinessStatus();
545
+ const result = JSON.stringify(status);
546
+ let expected = "{\"status\":\"UP\",\"checks\":[]}";
547
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
548
+ }));
549
+ it('Readiness reports UP and check result with single registered check', () => __awaiter(void 0, void 0, void 0, function* () {
550
+ let healthcheck = new index_1.HealthChecker();
551
+ // tslint:disable-next-line:no-shadowed-variable
552
+ const promise = () => new Promise((resolve, _reject) => {
553
+ resolve();
554
+ });
555
+ let check = new index_1.ReadinessCheck("check", promise);
556
+ healthcheck.registerReadinessCheck(check);
557
+ const status = yield healthcheck.getReadinessStatus();
558
+ const result = JSON.stringify(status);
559
+ let expected = "{\"status\":\"UP\",\"checks\":[{\"name\":\"check\",\"state\":\"UP\",\"data\":{\"reason\":\"\"}}]}";
560
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
561
+ }));
562
+ it('Readiness reports UP and check result with two registered checks', () => __awaiter(void 0, void 0, void 0, function* () {
563
+ let healthcheck = new index_1.HealthChecker();
564
+ // tslint:disable-next-line:no-shadowed-variable
565
+ const promiseone = () => new Promise((resolve, _reject) => {
566
+ resolve();
567
+ });
568
+ let checkone = new index_1.ReadinessCheck("checkone", promiseone);
569
+ // tslint:disable-next-line:no-shadowed-variable
570
+ const promisetwo = () => new Promise((resolve, _reject) => {
571
+ resolve();
572
+ });
573
+ let checktwo = new index_1.ReadinessCheck("checktwo", promisetwo);
574
+ healthcheck.registerReadinessCheck(checkone);
575
+ healthcheck.registerReadinessCheck(checktwo);
576
+ const status = yield healthcheck.getReadinessStatus();
577
+ const result = JSON.stringify(status);
578
+ let expected = "{\"status\":\"UP\",\"checks\":[{\"name\":\"checkone\",\"state\":\"UP\",\"data\":{\"reason\":\"\"}},{\"name\":\"checktwo\",\"state\":\"UP\",\"data\":{\"reason\":\"\"}}]}";
579
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
580
+ }));
581
+ it('Readiness reports DOWN and check result with single failed check', () => __awaiter(void 0, void 0, void 0, function* () {
582
+ let healthcheck = new index_1.HealthChecker();
583
+ const promise = () => new Promise((_resolve, _reject) => {
584
+ throw new Error("Readiness Failure");
585
+ });
586
+ let check = new index_1.ReadinessCheck("check", promise);
587
+ healthcheck.registerReadinessCheck(check);
588
+ const status = yield healthcheck.getReadinessStatus();
589
+ const result = JSON.stringify(status);
590
+ let expected = "{\"status\":\"DOWN\",\"checks\":[{\"name\":\"check\",\"state\":\"DOWN\",\"data\":{\"reason\":\"Readiness Failure\"}}]}";
591
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
592
+ }));
593
+ it('Readiness reports DOWN and check result with single rejected check', () => __awaiter(void 0, void 0, void 0, function* () {
594
+ let healthcheck = new index_1.HealthChecker();
595
+ const promise = () => new Promise((_resolve, reject) => {
596
+ reject(new Error("Readiness Failure"));
597
+ });
598
+ let check = new index_1.ReadinessCheck("check", promise);
599
+ healthcheck.registerReadinessCheck(check);
600
+ const status = yield healthcheck.getReadinessStatus();
601
+ const result = JSON.stringify(status);
602
+ let expected = "{\"status\":\"DOWN\",\"checks\":[{\"name\":\"check\",\"state\":\"DOWN\",\"data\":{\"reason\":\"Readiness Failure\"}}]}";
603
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
604
+ }));
605
+ it('Readiness reports UP with running PingCheck', () => __awaiter(void 0, void 0, void 0, function* () {
606
+ let healthcheck = new index_1.HealthChecker();
607
+ let check = new index_1.PingCheck("localhost", "", "3000");
608
+ healthcheck.registerReadinessCheck(check);
609
+ const status = yield healthcheck.getReadinessStatus();
610
+ const result = JSON.stringify(status);
611
+ let expected = "{\"status\":\"UP\",\"checks\":[{\"name\":\"PingCheck HEAD:localhost:3000/\",\"state\":\"UP\",\"data\":{\"reason\":\"\"}}]}";
612
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
613
+ }));
614
+ it('Readiness reports DOWN with failing PingCheck', () => __awaiter(void 0, void 0, void 0, function* () {
615
+ let healthcheck = new index_1.HealthChecker();
616
+ let check = new index_1.PingCheck("not-an-address.com");
617
+ healthcheck.registerReadinessCheck(check);
618
+ const status = yield healthcheck.getReadinessStatus();
619
+ let expected = 'PingCheck HEAD:not-an-address.com:80/';
620
+ const statusVal = status.status;
621
+ const checksState = status.checks[0].state;
622
+ const checkName = status.checks[0].name;
623
+ chai_1.expect(statusVal).to.equal(index_1.State.DOWN, `Should return: ${index_1.State.DOWN}, but returned: ${statusVal}`);
624
+ chai_1.expect(checksState).to.equal(index_1.State.DOWN, `Should return: ${index_1.State.DOWN}, but returned: ${checksState}`);
625
+ chai_1.expect(checkName).to.equal(expected, `Should return: ${expected}, but returned: ${checkName}`);
626
+ }));
627
+ it('Readiness reports DOWN and check result with one passed and one failed registered checks', () => __awaiter(void 0, void 0, void 0, function* () {
628
+ let healthcheck = new index_1.HealthChecker();
629
+ const promiseone = () => new Promise((_resolve, _reject) => {
630
+ throw new Error("Readiness Failure");
631
+ });
632
+ let checkone = new index_1.ReadinessCheck("checkone", promiseone);
633
+ const promisetwo = () => new Promise((resolve, _reject) => {
634
+ resolve();
635
+ });
636
+ let checktwo = new index_1.ReadinessCheck("checktwo", promisetwo);
637
+ healthcheck.registerReadinessCheck(checkone);
638
+ healthcheck.registerReadinessCheck(checktwo);
639
+ const status = yield healthcheck.getReadinessStatus();
640
+ const result = JSON.stringify(status);
641
+ let expected = "{\"status\":\"DOWN\",\"checks\":[{\"name\":\"checkone\",\"state\":\"DOWN\",\"data\":{\"reason\":\"Readiness Failure\"}},{\"name\":\"checktwo\",\"state\":\"UP\",\"data\":{\"reason\":\"\"}}]}";
642
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
643
+ }));
644
+ it('Readiness reports DOWN on second invocation of a readiness check', () => __awaiter(void 0, void 0, void 0, function* () {
645
+ let healthcheck = new index_1.HealthChecker();
646
+ let count = 0;
647
+ const promise = () => new Promise((resolve, reject) => {
648
+ if (count > 0) {
649
+ reject(new Error("Readiness failure"));
650
+ }
651
+ else {
652
+ count = count + 1;
653
+ resolve();
654
+ }
655
+ });
656
+ let check = new index_1.ReadinessCheck("check", promise);
657
+ healthcheck.registerReadinessCheck(check);
658
+ let status = yield healthcheck.getReadinessStatus();
659
+ status = yield healthcheck.getReadinessStatus();
660
+ const result = JSON.stringify(status);
661
+ let expected = "{\"status\":\"\DOWN\",\"checks\":[{\"name\":\"check\",\"state\":\"DOWN\",\"data\":{\"reason\":\"Readiness failure\"}}]}";
662
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
663
+ }));
664
+ it('Readiness reports UP on second invocation of a readiness check', () => __awaiter(void 0, void 0, void 0, function* () {
665
+ let healthcheck = new index_1.HealthChecker();
666
+ let count = 0;
667
+ const promise = () => new Promise((resolve, reject) => {
668
+ if (count > 0) {
669
+ resolve();
670
+ }
671
+ else {
672
+ count = count + 1;
673
+ reject(new Error("Readiness failure"));
674
+ }
675
+ });
676
+ let check = new index_1.ReadinessCheck("check", promise);
677
+ healthcheck.registerReadinessCheck(check);
678
+ let status = yield healthcheck.getReadinessStatus();
679
+ status = yield healthcheck.getReadinessStatus();
680
+ const result = JSON.stringify(status);
681
+ let expected = "{\"status\":\"\UP\",\"checks\":[{\"name\":\"check\",\"state\":\"UP\",\"data\":{\"reason\":\"\"}}]}";
682
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
683
+ }));
684
+ it('Liveness reports UP by default', () => __awaiter(void 0, void 0, void 0, function* () {
685
+ let healthcheck = new index_1.HealthChecker();
686
+ const status = yield healthcheck.getLivenessStatus();
687
+ let result = status.status;
688
+ chai_1.expect(result).to.equal(index_1.State.UP, `Should return: ${index_1.State.UP}, but returned: ${result}`);
689
+ }));
690
+ it('Liveness reports UP and empty checks array no registered checks', () => __awaiter(void 0, void 0, void 0, function* () {
691
+ let healthcheck = new index_1.HealthChecker();
692
+ const status = yield healthcheck.getLivenessStatus();
693
+ const result = JSON.stringify(status);
694
+ let expected = "{\"status\":\"UP\",\"checks\":[]}";
695
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
696
+ }));
697
+ it('Liveness reports UP and check result with single registered check', () => __awaiter(void 0, void 0, void 0, function* () {
698
+ let healthcheck = new index_1.HealthChecker();
699
+ // tslint:disable-next-line:no-shadowed-variable
700
+ const promise = () => new Promise((resolve, _reject) => {
701
+ resolve();
702
+ });
703
+ let check = new index_1.LivenessCheck("check", promise);
704
+ healthcheck.registerLivenessCheck(check);
705
+ const status = yield healthcheck.getLivenessStatus();
706
+ const result = JSON.stringify(status);
707
+ let expected = "{\"status\":\"UP\",\"checks\":[{\"name\":\"check\",\"state\":\"UP\",\"data\":{\"reason\":\"\"}}]}";
708
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
709
+ }));
710
+ it('Liveness reports UP and check result with two registered checks', () => __awaiter(void 0, void 0, void 0, function* () {
711
+ let healthcheck = new index_1.HealthChecker();
712
+ // tslint:disable-next-line:no-shadowed-variable
713
+ const promiseone = () => new Promise((resolve, _reject) => {
714
+ resolve();
715
+ });
716
+ let checkone = new index_1.LivenessCheck("checkone", promiseone);
717
+ // tslint:disable-next-line:no-shadowed-variable
718
+ const promisetwo = () => new Promise((resolve, _reject) => {
719
+ resolve();
720
+ });
721
+ let checktwo = new index_1.LivenessCheck("checktwo", promisetwo);
722
+ healthcheck.registerLivenessCheck(checkone);
723
+ healthcheck.registerLivenessCheck(checktwo);
724
+ const status = yield healthcheck.getLivenessStatus();
725
+ const result = JSON.stringify(status);
726
+ let expected = "{\"status\":\"UP\",\"checks\":[{\"name\":\"checkone\",\"state\":\"UP\",\"data\":{\"reason\":\"\"}},{\"name\":\"checktwo\",\"state\":\"UP\",\"data\":{\"reason\":\"\"}}]}";
727
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
728
+ }));
729
+ it('Liveness reports DOWN and check result with single failed check', () => __awaiter(void 0, void 0, void 0, function* () {
730
+ let healthcheck = new index_1.HealthChecker();
731
+ const promise = () => new Promise((_resolve, _reject) => {
732
+ throw new Error("Liveness Failure");
733
+ });
734
+ let check = new index_1.LivenessCheck("check", promise);
735
+ healthcheck.registerLivenessCheck(check);
736
+ const status = yield healthcheck.getLivenessStatus();
737
+ const result = JSON.stringify(status);
738
+ let expected = "{\"status\":\"DOWN\",\"checks\":[{\"name\":\"check\",\"state\":\"DOWN\",\"data\":{\"reason\":\"Liveness Failure\"}}]}";
739
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
740
+ }));
741
+ it('Liveness reports DOWN and check result with single rejected check', () => __awaiter(void 0, void 0, void 0, function* () {
742
+ let healthcheck = new index_1.HealthChecker();
743
+ const promise = () => new Promise((_resolve, reject) => {
744
+ reject(new Error("Liveness Failure"));
745
+ });
746
+ let check = new index_1.LivenessCheck("check", promise);
747
+ healthcheck.registerLivenessCheck(check);
748
+ const status = yield healthcheck.getLivenessStatus();
749
+ const result = JSON.stringify(status);
750
+ let expected = "{\"status\":\"DOWN\",\"checks\":[{\"name\":\"check\",\"state\":\"DOWN\",\"data\":{\"reason\":\"Liveness Failure\"}}]}";
751
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
752
+ }));
753
+ it('Liveness reports UP with running PingCheck', () => __awaiter(void 0, void 0, void 0, function* () {
754
+ let healthcheck = new index_1.HealthChecker();
755
+ let check = new index_1.PingCheck("localhost", "", "3000");
756
+ healthcheck.registerLivenessCheck(check);
757
+ const status = yield healthcheck.getLivenessStatus();
758
+ const result = JSON.stringify(status);
759
+ let expected = "{\"status\":\"UP\",\"checks\":[{\"name\":\"PingCheck HEAD:localhost:3000/\",\"state\":\"UP\",\"data\":{\"reason\":\"\"}}]}";
760
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
761
+ }));
762
+ it('Liveness reports DOWN with failing PingCheck', () => __awaiter(void 0, void 0, void 0, function* () {
763
+ let healthcheck = new index_1.HealthChecker();
764
+ let check = new index_1.PingCheck("not-an-address.com");
765
+ healthcheck.registerLivenessCheck(check);
766
+ const status = yield healthcheck.getLivenessStatus();
767
+ let expected = 'PingCheck HEAD:not-an-address.com:80/';
768
+ const statusVal = status.status;
769
+ const checksState = status.checks[0].state;
770
+ const checkName = status.checks[0].name;
771
+ chai_1.expect(statusVal).to.equal(index_1.State.DOWN, `Should return: ${index_1.State.DOWN}, but returned: ${statusVal}`);
772
+ chai_1.expect(checksState).to.equal(index_1.State.DOWN, `Should return: ${index_1.State.DOWN}, but returned: ${checksState}`);
773
+ chai_1.expect(checkName).to.equal(expected, `Should return: ${expected}, but returned: ${checkName}`);
774
+ }));
775
+ it('Liveness reports DOWN and check result with one passed and one failed registered checks', () => __awaiter(void 0, void 0, void 0, function* () {
776
+ let healthcheck = new index_1.HealthChecker();
777
+ const promiseone = () => new Promise((_resolve, _reject) => {
778
+ throw new Error("Liveness Failure");
779
+ });
780
+ let checkone = new index_1.LivenessCheck("checkone", promiseone);
781
+ const promisetwo = () => new Promise((resolve, _reject) => {
782
+ resolve();
783
+ });
784
+ let checktwo = new index_1.LivenessCheck("checktwo", promisetwo);
785
+ healthcheck.registerLivenessCheck(checkone);
786
+ healthcheck.registerLivenessCheck(checktwo);
787
+ const status = yield healthcheck.getLivenessStatus();
788
+ const result = JSON.stringify(status);
789
+ let expected = "{\"status\":\"DOWN\",\"checks\":[{\"name\":\"checkone\",\"state\":\"DOWN\",\"data\":{\"reason\":\"Liveness Failure\"}},{\"name\":\"checktwo\",\"state\":\"UP\",\"data\":{\"reason\":\"\"}}]}";
790
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
791
+ }));
792
+ it('Liveness reports DOWN on second invocation of a liveness check', () => __awaiter(void 0, void 0, void 0, function* () {
793
+ let healthcheck = new index_1.HealthChecker();
794
+ let count = 0;
795
+ const promise = () => new Promise((resolve, reject) => {
796
+ if (count > 0) {
797
+ reject(new Error("Liveness failure"));
798
+ }
799
+ else {
800
+ count = count + 1;
801
+ resolve();
802
+ }
803
+ });
804
+ let check = new index_1.LivenessCheck("check", promise);
805
+ healthcheck.registerLivenessCheck(check);
806
+ let status = yield healthcheck.getLivenessStatus();
807
+ status = yield healthcheck.getLivenessStatus();
808
+ const result = JSON.stringify(status);
809
+ let expected = "{\"status\":\"\DOWN\",\"checks\":[{\"name\":\"check\",\"state\":\"DOWN\",\"data\":{\"reason\":\"Liveness failure\"}}]}";
810
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
811
+ }));
812
+ it('Liveness reports UP on second invocation of a liveness check', () => __awaiter(void 0, void 0, void 0, function* () {
813
+ let healthcheck = new index_1.HealthChecker();
814
+ let count = 0;
815
+ const promise = () => new Promise((resolve, reject) => {
816
+ if (count > 0) {
817
+ resolve();
818
+ }
819
+ else {
820
+ count = count + 1;
821
+ reject(new Error("Liveness failure"));
822
+ }
823
+ });
824
+ let check = new index_1.LivenessCheck("check", promise);
825
+ healthcheck.registerLivenessCheck(check);
826
+ let status = yield healthcheck.getLivenessStatus();
827
+ status = yield healthcheck.getLivenessStatus();
828
+ const result = JSON.stringify(status);
829
+ let expected = "{\"status\":\"\UP\",\"checks\":[{\"name\":\"check\",\"state\":\"UP\",\"data\":{\"reason\":\"\"}}]}";
830
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
831
+ }));
832
+ it('Shutdown reports STOPPED once stopped', () => __awaiter(void 0, void 0, void 0, function* () {
833
+ process.removeAllListeners('SIGTERM');
834
+ let healthcheck = new index_1.HealthChecker();
835
+ // tslint:disable-next-line:no-shadowed-variable
836
+ const promiseone = () => new Promise((resolve, _reject) => {
837
+ setTimeout(resolve, 50);
838
+ });
839
+ let checkone = new index_1.ShutdownCheck("checkone", promiseone);
840
+ healthcheck.registerShutdownCheck(checkone);
841
+ let result;
842
+ yield new Promise((resolve) => {
843
+ process.once('SIGTERM', () => __awaiter(void 0, void 0, void 0, function* () {
844
+ // Give shutdown a chance to process
845
+ yield setTimeout(() => __awaiter(void 0, void 0, void 0, function* () {
846
+ const status = yield healthcheck.getStatus();
847
+ result = status.status;
848
+ resolve();
849
+ }), 100);
850
+ }));
851
+ process.kill(process.pid, 'SIGTERM');
852
+ });
853
+ const expected = index_1.State.STOPPED;
854
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
855
+ }));
856
+ it('Shutdown reports STOPPING whilst stopping', () => __awaiter(void 0, void 0, void 0, function* () {
857
+ process.removeAllListeners('SIGTERM');
858
+ let healthcheck = new index_1.HealthChecker();
859
+ const promiseone = () => new Promise((_resolve, _reject) => {
860
+ // tslint:disable-next-line:no-shadowed-variable no-unused-expression
861
+ new Promise((resolve, _reject) => {
862
+ setTimeout(resolve, 1000, 'foo');
863
+ });
864
+ });
865
+ let checkone = new index_1.ShutdownCheck("checkone", promiseone);
866
+ healthcheck.registerShutdownCheck(checkone);
867
+ let result;
868
+ yield new Promise(resolve => {
869
+ process.once('SIGTERM', () => __awaiter(void 0, void 0, void 0, function* () {
870
+ const status = yield healthcheck.getStatus();
871
+ result = status.status;
872
+ resolve();
873
+ }));
874
+ process.kill(process.pid, 'SIGTERM');
875
+ });
876
+ const expected = index_1.State.STOPPING;
877
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
878
+ }));
879
+ it('Shutdown reports STOPPED and DOWN for check for error during shutdown', () => __awaiter(void 0, void 0, void 0, function* () {
880
+ process.removeAllListeners('SIGTERM');
881
+ let healthcheck = new index_1.HealthChecker();
882
+ const promise = () => new Promise((_resolve, _reject) => {
883
+ throw new Error("Shutdown Failure");
884
+ });
885
+ let checkone = new index_1.ShutdownCheck("checkone", promise);
886
+ healthcheck.registerShutdownCheck(checkone);
887
+ let result;
888
+ yield new Promise(resolve => {
889
+ process.once('SIGTERM', () => __awaiter(void 0, void 0, void 0, function* () {
890
+ // must be wrapped in timeout to simulate a node tick to ensure "process.on('SIGTERM', this.onShutdownRequest)" have been executed
891
+ setTimeout(() => __awaiter(void 0, void 0, void 0, function* () {
892
+ const status = yield healthcheck.getStatus();
893
+ result = JSON.stringify(status);
894
+ resolve();
895
+ }));
896
+ }));
897
+ process.kill(process.pid, 'SIGTERM');
898
+ });
899
+ const expected = "{\"status\":\"STOPPED\",\"checks\":[{\"name\":\"checkone\",\"state\":\"DOWN\",\"data\":{\"reason\":\"Shutdown Failure\"}}]}";
900
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
901
+ }));
902
+ it('Shutdown reports STOPPED and DOWN/DOWN for check for error during shutdown', () => __awaiter(void 0, void 0, void 0, function* () {
903
+ process.removeAllListeners('SIGTERM');
904
+ let healthcheck = new index_1.HealthChecker();
905
+ const promiseone = () => new Promise((_resolve, _reject) => {
906
+ throw new Error("Shutdown Failure");
907
+ });
908
+ let checkone = new index_1.ShutdownCheck("checkone", promiseone);
909
+ healthcheck.registerShutdownCheck(checkone);
910
+ const promisetwo = () => new Promise((_resolve, _reject) => {
911
+ throw new Error("Shutdown Failure");
912
+ });
913
+ let checktwo = new index_1.ShutdownCheck("checktwo", promisetwo);
914
+ healthcheck.registerShutdownCheck(checktwo);
915
+ let result;
916
+ yield new Promise(resolve => {
917
+ process.once('SIGTERM', () => {
918
+ // must be wrapped in timeout to simulate a node tick to ensure "process.on('SIGTERM', this.onShutdownRequest)" have been executed
919
+ setTimeout(() => __awaiter(void 0, void 0, void 0, function* () {
920
+ const status = yield healthcheck.getStatus();
921
+ result = JSON.stringify(status);
922
+ resolve();
923
+ }));
924
+ });
925
+ process.kill(process.pid, 'SIGTERM');
926
+ });
927
+ let expected = "{\"status\":\"STOPPED\",\"checks\":[{\"name\":\"checkone\",\"state\":\"DOWN\",\"data\":{\"reason\":\"Shutdown Failure\"}},{\"name\":\"checktwo\",\"state\":\"DOWN\",\"data\":{\"reason\":\"Shutdown Failure\"}}]}";
928
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
929
+ }));
930
+ it('Shutdown reports STOPPING and STOPPED/STOPPING for checks for one complete and one running shutdown', () => __awaiter(void 0, void 0, void 0, function* () {
931
+ process.removeAllListeners('SIGTERM');
932
+ let healthcheck = new index_1.HealthChecker();
933
+ // tslint:disable-next-line:no-shadowed-variable
934
+ const promiseone = () => new Promise((resolve, _reject) => {
935
+ resolve();
936
+ });
937
+ let checkone = new index_1.ShutdownCheck("checkone", promiseone);
938
+ healthcheck.registerShutdownCheck(checkone);
939
+ const promisetwo = () => new Promise((_resolve, _reject) => {
940
+ // tslint:disable-next-line:no-shadowed-variable no-unused-expression
941
+ new Promise((resolve, _reject) => {
942
+ setTimeout(resolve, 1000, 'foo');
943
+ });
944
+ });
945
+ let checktwo = new index_1.ShutdownCheck("checktwo", promisetwo);
946
+ healthcheck.registerShutdownCheck(checktwo);
947
+ let result;
948
+ yield new Promise(resolve => {
949
+ process.once('SIGTERM', () => {
950
+ setTimeout(() => __awaiter(void 0, void 0, void 0, function* () {
951
+ const status = yield healthcheck.getStatus();
952
+ result = JSON.stringify(status);
953
+ resolve();
954
+ }));
955
+ });
956
+ process.kill(process.pid, 'SIGTERM');
957
+ });
958
+ const expected = "{\"status\":\"STOPPING\",\"checks\":[{\"name\":\"checkone\",\"state\":\"STOPPED\",\"data\":{\"reason\":\"\"}},{\"name\":\"checktwo\",\"state\":\"STOPPING\",\"data\":{\"reason\":\"\"}}]}";
959
+ chai_1.expect(result).to.equal(expected, `Should return: ${expected}, but returned: ${result}`);
960
+ }));
961
+ });
962
+ describe('Should convert any promise reject to return an error message as a string', () => {
963
+ {
964
+ class foo {
965
+ get message() {
966
+ return "bar";
967
+ }
968
+ }
969
+ // array of errors and their expected return values when passed to promise reject
970
+ [
971
+ [new Error("Readiness Failure"), "Readiness Failure"],
972
+ [null, ""],
973
+ [undefined, ""],
974
+ [1, "1"],
975
+ [new Error(), "Error"],
976
+ [new foo(), "bar"]
977
+ ].forEach(([err, str]) => {
978
+ it(`When err is ${err} should return ${str}`, () => __awaiter(void 0, void 0, void 0, function* () {
979
+ let healthcheck = new index_1.HealthChecker();
980
+ const readinessPromise = () => new Promise((resolve, reject) => {
981
+ reject(err);
982
+ });
983
+ let check = new index_1.ReadinessCheck("readinessCheck", readinessPromise);
984
+ healthcheck.registerReadinessCheck(check);
985
+ let status = yield healthcheck.getReadinessStatus();
986
+ const result = JSON.stringify(status);
987
+ let expected = {
988
+ "status": "DOWN",
989
+ "checks": [{
990
+ "name": "readinessCheck",
991
+ "state": "DOWN",
992
+ "data": {
993
+ "reason": str
994
+ }
995
+ }]
996
+ };
997
+ chai_1.expect(result).to.equal(JSON.stringify(expected), `Should return: ${expected}, but returned: ${result}`);
998
+ }));
999
+ });
1000
+ }
1001
+ ;
1002
+ });
1003
+ //# sourceMappingURL=HealthChecker.test.js.map