Skip to content

Commit 4b57f32

Browse files
committed
refactor(network-controller): replace magic number with calculated constant
Replace the magic number 5 with a well-documented constant that explains how the number of attempts needed to break both circuits in an RPC service chain is calculated. This makes the test intent clearer and the calculation explicit. - Add DEFAULT_REQUEST_ATTEMPTS constant - Add DEFAULT_RPC_SERVICE_ATTEMPTS_UNTIL_BREAK constant - Add DEFAULT_RPC_CHAIN_ATTEMPTS_UNTIL_BREAK constant with documentation - Replace all occurrences of magic number 5 with the constant
1 parent 067a807 commit 4b57f32

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

packages/network-controller/src/rpc-service/rpc-service-chain.test.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,37 @@ import nock from 'nock';
88
import { useFakeTimers } from 'sinon';
99
import type { SinonFakeTimers } from 'sinon';
1010

11-
import { DEFAULT_MAX_CONSECUTIVE_FAILURES } from './rpc-service';
11+
import {
12+
DEFAULT_MAX_CONSECUTIVE_FAILURES,
13+
DEFAULT_MAX_RETRIES,
14+
} from './rpc-service';
1215
import { RpcServiceChain } from './rpc-service-chain';
1316

17+
/**
18+
* The number of fetch requests made for a single request to an RPC service, using default max
19+
* retry attempts.
20+
*/
21+
const DEFAULT_REQUEST_ATTEMPTS = 1 + DEFAULT_MAX_RETRIES;
22+
23+
/**
24+
* Number of attempts required to break the circuit of an RPC service using default retry attempts
25+
* and max consecutive failures.
26+
*
27+
* Note: This calculation and later ones assume that there is no remainder.
28+
*/
29+
const DEFAULT_RPC_SERVICE_ATTEMPTS_UNTIL_BREAK =
30+
DEFAULT_MAX_CONSECUTIVE_FAILURES / DEFAULT_REQUEST_ATTEMPTS;
31+
32+
/**
33+
* Number of attempts required to break the circuit of an RPC service chain (with a single
34+
* failover) that uses default retry attempts and max consecutive failures.
35+
*
36+
* The value is one less than double the number of attempts needed to break a single circuit
37+
* because on failure of the primary, the request gets forwarded to the failover immediately.
38+
*/
39+
const DEFAULT_RPC_CHAIN_ATTEMPTS_UNTIL_BREAK =
40+
2 * DEFAULT_RPC_SERVICE_ATTEMPTS_UNTIL_BREAK - 1;
41+
1442
describe('RpcServiceChain', () => {
1543
let clock: SinonFakeTimers;
1644

@@ -754,7 +782,7 @@ describe('RpcServiceChain', () => {
754782
};
755783
// Retry the first endpoint until its circuit breaks, then retry the
756784
// second endpoint until *its* circuit breaks.
757-
for (let i = 0; i < 5; i++) {
785+
for (let i = 0; i < DEFAULT_RPC_CHAIN_ATTEMPTS_UNTIL_BREAK; i++) {
758786
await expect(rpcServiceChain.request(jsonRpcRequest)).rejects.toThrow(
759787
expectedError,
760788
);
@@ -765,7 +793,7 @@ describe('RpcServiceChain', () => {
765793
await rpcServiceChain.request(jsonRpcRequest);
766794
// Do it again: retry the first endpoint until its circuit breaks, then
767795
// retry the second endpoint until *its* circuit breaks.
768-
for (let i = 0; i < 5; i++) {
796+
for (let i = 0; i < DEFAULT_RPC_CHAIN_ATTEMPTS_UNTIL_BREAK; i++) {
769797
await expect(rpcServiceChain.request(jsonRpcRequest)).rejects.toThrow(
770798
expectedError,
771799
);
@@ -856,7 +884,7 @@ describe('RpcServiceChain', () => {
856884
};
857885
// Retry the first endpoint until its circuit breaks, then retry the
858886
// second endpoint until *its* circuit breaks.
859-
for (let i = 0; i < 5; i++) {
887+
for (let i = 0; i < DEFAULT_RPC_CHAIN_ATTEMPTS_UNTIL_BREAK; i++) {
860888
await expect(rpcServiceChain.request(jsonRpcRequest)).rejects.toThrow(
861889
expectedError,
862890
);
@@ -867,7 +895,7 @@ describe('RpcServiceChain', () => {
867895
await rpcServiceChain.request(jsonRpcRequest);
868896
// Do it again: retry the first endpoint until its circuit breaks, then
869897
// retry the second endpoint until *its* circuit breaks.
870-
for (let i = 0; i < 5; i++) {
898+
for (let i = 0; i < DEFAULT_RPC_CHAIN_ATTEMPTS_UNTIL_BREAK; i++) {
871899
await expect(rpcServiceChain.request(jsonRpcRequest)).rejects.toThrow(
872900
expectedError,
873901
);

0 commit comments

Comments
 (0)