1 | const { makeCounters } = require('./httpRequestCounters');
|
2 | const { runWithRetries } = require('../utils');
|
3 | const { rejects, doesNotReject, strictEqual } = require('assert');
|
4 |
|
5 | describe('the http request counters', () => {
|
6 | let wrapWithMonitoring;
|
7 | beforeEach(() => {
|
8 | wrapWithMonitoring = makeCounters();
|
9 | });
|
10 |
|
11 | it('Marks an always failing network as unhealthy', async () => {
|
12 | const fn = runWithRetries(wrapWithMonitoring(() => { throw new Error('bad network'); }), 20, 1);
|
13 | await rejects(fn);
|
14 | strictEqual(wrapWithMonitoring.isNetworkHealthy(), false);
|
15 | });
|
16 |
|
17 | it('Marks an unstable network as unhealthy', async () => {
|
18 | const fn = runWithRetries(wrapWithMonitoring(() => { throw new Error('bad network'); }), 20, 1);
|
19 | const fn2 = runWithRetries(wrapWithMonitoring(() => 'hello'), 20, 1);
|
20 | await rejects(fn);
|
21 | await doesNotReject(fn2);
|
22 | strictEqual(wrapWithMonitoring.isNetworkHealthy(), false);
|
23 | });
|
24 |
|
25 | it('Marks a recovering network as healthy', async () => {
|
26 | const fn = runWithRetries(wrapWithMonitoring(() => { throw new Error('bad network'); }), 20, 1);
|
27 | await rejects(fn);
|
28 | const fn2 = wrapWithMonitoring(() => 'hello');
|
29 | await Promise.all(Array(200).fill().map(fn2));
|
30 | strictEqual(wrapWithMonitoring.isNetworkHealthy(), true);
|
31 | });
|
32 |
|
33 | it('Marks a healthy network as healthy', async () => {
|
34 | const fn2 = wrapWithMonitoring(() => 'hello');
|
35 | await Promise.all(Array(200).fill().map(fn2));
|
36 | strictEqual(wrapWithMonitoring.isNetworkHealthy(), true);
|
37 | });
|
38 | });
|