/
opossum-tests.ts
125 lines (105 loc) · 3.98 KB
/
opossum-tests.ts
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
import * as fs from 'fs';
import * as CircuitBreaker from 'opossum';
import { promisify } from 'util';
let breaker: CircuitBreaker;
const callbackNoArgs = async () => console.log('foo');
CircuitBreaker.isOurError(new Error()); // $ExpectType boolean
breaker = new CircuitBreaker(async () => true, {
timeout: 1000,
maxFailures: 50,
resetTimeout: 10,
rollingCountTimeout: 500,
rollingCountBuckets: 20,
name: 'test',
group: 'group',
rollingPercentilesEnabled: true,
capacity: 1,
errorThresholdPercentage: 1,
enabled: true,
allowWarmUp: true,
volumeThreshold: 1,
cache: true,
});
breaker.name; // $ExpectType string
breaker.group; // $ExpectType string
breaker.enabled; // $ExpectType boolean
breaker.pendingClose; // $ExpectType boolean
breaker.closed; // $ExpectType boolean
breaker.opened; // $ExpectType boolean
breaker.halfOpen; // $ExpectType boolean
breaker.warmUp; // $ExpectType boolean
breaker.isShutdown; // $ExpectType boolean
breaker.volumeThreshold; // $ExpectType number
breaker.status.stats.latencyMean; // $ExpectType number
breaker.stats.latencyTimes; // $ExpectType number[]
breaker.clearCache(); // $ExpectType void
breaker.open(); // $ExpectType void
breaker.close(); // $ExpectType void
breaker.disable(); // $ExpectType void
breaker.enable(); // $ExpectType void
breaker.shutdown(); // $ExpectType void
// Check the generic types pass down correctly from constructor to `fire` and events.
const action = async (foo: string, bar: number) => {
return foo ? bar : bar * 2;
};
const typedBreaker = new CircuitBreaker(action);
typedBreaker.fire(5, 'hello'); // $ExpectError
typedBreaker.fire('hello world', 42); // $ExpectType Promise<number>
typedBreaker.on('success', (result, latencyMs) => {
result; // $ExpectType number
latencyMs; // $ExpectType number
});
typedBreaker.on('fire', ([foo, bar]) => {
foo; // $ExpectType string
bar; // $ExpectType number
});
// The following are examples are from the libs README and official documentation
// https://nodeshift.github.io/opossum/index.html.
function asyncFunctionThatCouldFail(x: any, y: any) {
return new Promise((resolve, reject) => {
// Do something, maybe on the network or a disk
resolve([x, y]);
});
}
const options: CircuitBreaker.Options = {
timeout: 3000, // If our function takes longer than 3 seconds, trigger a failure
errorThresholdPercentage: 50, // When 50% of requests fail, trip the circuit
resetTimeout: 30000, // After 30 seconds, try again.
};
breaker = new CircuitBreaker(asyncFunctionThatCouldFail, options);
breaker
.fire('foo')
.then(console.log)
.catch(console.error);
breaker = new CircuitBreaker(asyncFunctionThatCouldFail, options);
// if asyncFunctionThatCouldFail starts to fail, firing the breaker
// will trigger our fallback function
breaker.fallback(() => 'Sorry, out of service right now');
breaker.on('fallback', result => console.log(result));
breaker = new CircuitBreaker(callbackNoArgs, options);
breaker.fallback(callbackNoArgs);
breaker.on('success', result => console.log(result));
breaker.on('timeout', callbackNoArgs);
breaker.on('reject', callbackNoArgs);
breaker.on('open', callbackNoArgs);
breaker.on('halfOpen', callbackNoArgs);
breaker.on('close', callbackNoArgs);
breaker.on('fallback', data => console.log(data));
const readFile = promisify(fs.readFile);
breaker = new CircuitBreaker(readFile, options);
breaker
.fire('./package.json', 'utf-8')
.then(console.log)
.catch(console.error);
breaker = new CircuitBreaker(readFile, {});
// Creates a 1 second window consisting of ten time slices,
// each 100ms long.
const circuit = new CircuitBreaker(readFile, {
rollingCountBuckets: 10,
rollingCountTimeout: 1000,
});
// get the cumulative statistics for the last second
const theStats: CircuitBreaker.Stats = breaker.status.stats;
// get the array of 10, 1 second time slices for the last second
const window: CircuitBreaker.Window = breaker.status.window;
window[0].fires; // $ExpectType number