Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions packages/controller-utils/src/create-service-policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3574,6 +3574,118 @@ describe('createServicePolicy', () => {
await expect(policy.execute(mockService)).rejects.toThrow('failure');
});
});

describe('using a custom isServiceFailure predicate', () => {
it('opens the circuit when the predicate treats the error as a service failure', async () => {
const maxConsecutiveFailures = DEFAULT_MAX_RETRIES + 1;
const error = new Error('failure');
const mockService = jest.fn(() => {
throw error;
});
const onBreakListener = jest.fn();
const policy = createServicePolicy({
maxConsecutiveFailures,
isServiceFailure: () => true,
});
policy.onBreak(onBreakListener);

const promise = policy.execute(mockService);
// It's safe not to await this promise; adding it to the promise
// queue is enough to prevent this test from running indefinitely.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
jest.runAllTimersAsync();
await ignoreRejection(promise);

expect(onBreakListener).toHaveBeenCalledTimes(1);
expect(onBreakListener).toHaveBeenCalledWith({ error });
});

it('never opens the circuit when the predicate does not treat the error as a service failure', async () => {
const maxConsecutiveFailures = DEFAULT_MAX_RETRIES + 1;
const error = new Error('failure');
const mockService = jest.fn(() => {
throw error;
});
const onBreakListener = jest.fn();
const policy = createServicePolicy({
maxConsecutiveFailures,
isServiceFailure: () => false,
});
policy.onBreak(onBreakListener);

// Execute more times than the max consecutive failures so that the
// circuit would open if these errors were counted as failures.
for (let i = 0; i < maxConsecutiveFailures + 1; i++) {
const promise = policy.execute(mockService);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
jest.runAllTimersAsync();
await ignoreRejection(promise);
}

expect(onBreakListener).not.toHaveBeenCalled();
});

it('calls the predicate with the error thrown by the service', async () => {
const error = new Error('failure');
const mockService = jest.fn(() => {
throw error;
});
const isServiceFailure = jest.fn(() => true);
const policy = createServicePolicy({
maxConsecutiveFailures: DEFAULT_MAX_RETRIES + 1,
isServiceFailure,
});

const promise = policy.execute(mockService);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
jest.runAllTimersAsync();
await ignoreRejection(promise);

expect(isServiceFailure).toHaveBeenCalledWith(error);
});
});

describe('using the default isServiceFailure predicate', () => {
it('opens the circuit for an error with an HTTP status >= 500', async () => {
const maxConsecutiveFailures = DEFAULT_MAX_RETRIES + 1;
const error = Object.assign(new Error('failure'), { httpStatus: 500 });
const mockService = jest.fn(() => {
throw error;
});
const onBreakListener = jest.fn();
const policy = createServicePolicy({ maxConsecutiveFailures });
policy.onBreak(onBreakListener);

const promise = policy.execute(mockService);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
jest.runAllTimersAsync();
await ignoreRejection(promise);

expect(onBreakListener).toHaveBeenCalledTimes(1);
});

it('never opens the circuit for an error with an HTTP status < 500', async () => {
const maxConsecutiveFailures = DEFAULT_MAX_RETRIES + 1;
const error = Object.assign(new Error('failure'), { httpStatus: 400 });
const mockService = jest.fn(() => {
throw error;
});
const onBreakListener = jest.fn();
const policy = createServicePolicy({ maxConsecutiveFailures });
policy.onBreak(onBreakListener);

// Execute more times than the max consecutive failures so that the
// circuit would open if these errors were counted as failures.
for (let i = 0; i < maxConsecutiveFailures + 1; i++) {
const promise = policy.execute(mockService);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
jest.runAllTimersAsync();
await ignoreRejection(promise);
}

expect(onBreakListener).not.toHaveBeenCalled();
});
});
});

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/controller-utils/src/create-service-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,9 @@ const defaultIsServiceFailure = (error: unknown): boolean => {
return error.httpStatus >= 500;
}

// If the error is not an object, or doesn't have a numeric code property,
// consider it a service failure (e.g., network errors, timeouts, etc.)
// If the error is not an object, or doesn't have a numeric httpStatus
// property, consider it a service failure (e.g., network errors, timeouts,
// etc.)
return true;
};

Expand Down
83 changes: 83 additions & 0 deletions packages/network-controller/src/rpc-service/rpc-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,89 @@ describe('RpcService', () => {
});
});

describe('treating errors as service failures', () => {
const jsonRpcRequest = {
id: 1,
jsonrpc: '2.0' as const,
method: 'eth_chainId',
params: [],
};

describe('when the endpoint is an Infura URL', () => {
const endpointUrl = 'https://mainnet.infura.io';

it.each([400, 429])(
'does not break the circuit when the endpoint responds with %d',
async (httpStatus) => {
nock(endpointUrl)
.post('/', jsonRpcRequest)
.times(3)
.reply(httpStatus);
const service = new RpcService({
fetch,
btoa,
endpointUrl,
isOffline: (): boolean => false,
policyOptions: { maxConsecutiveFailures: 2 },
});

// Make more requests than the max consecutive failures so that the
// circuit would open if these errors were treated as failures.
await ignoreRejection(service.request(jsonRpcRequest));
await ignoreRejection(service.request(jsonRpcRequest));
await ignoreRejection(service.request(jsonRpcRequest));

expect(service.getCircuitState()).toBe(CircuitState.Closed);
},
);

it.each([401, 500])(
'breaks the circuit when the endpoint responds with %d',
async (httpStatus) => {
nock(endpointUrl)
.post('/', jsonRpcRequest)
.times(2)
.reply(httpStatus);
const service = new RpcService({
fetch,
btoa,
endpointUrl,
isOffline: (): boolean => false,
policyOptions: { maxConsecutiveFailures: 2 },
});

await ignoreRejection(service.request(jsonRpcRequest));
await ignoreRejection(service.request(jsonRpcRequest));

expect(service.getCircuitState()).toBe(CircuitState.Open);
},
);
});

describe('when the endpoint is not an Infura URL', () => {
const endpointUrl = 'https://rpc.example.chain';

it('does not break the circuit for a 4xx response that is not a server error', async () => {
nock(endpointUrl).post('/', jsonRpcRequest).times(3).reply(401);
const service = new RpcService({
fetch,
btoa,
endpointUrl,
isOffline: (): boolean => false,
policyOptions: { maxConsecutiveFailures: 2 },
});

// Make more requests than the max consecutive failures so that the
// circuit would open if these errors were treated as failures.
await ignoreRejection(service.request(jsonRpcRequest));
await ignoreRejection(service.request(jsonRpcRequest));
await ignoreRejection(service.request(jsonRpcRequest));

expect(service.getCircuitState()).toBe(CircuitState.Closed);
});
});
});

describe('request', () => {
// NOTE: Keep this list synced with CONNECTION_ERRORS
describe.each([
Expand Down
5 changes: 3 additions & 2 deletions packages/network-controller/src/rpc-service/rpc-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,9 @@ function isServiceFailureInfura(error: unknown): boolean {
return !INFURA_NON_FAILURE_HTTP_STATUS_CODES.includes(error.httpStatus);
}

// If the error is not an object, or doesn't have a numeric code property,
// consider it a service failure (e.g., network errors, timeouts, etc.)
// If the error is not an object, or doesn't have a numeric httpStatus
// property, consider it a service failure (e.g., network errors, timeouts,
// etc.)
return true;
}

Expand Down
Loading