Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

fix(typo): circuit breaker “function” input #372

Merged
merged 1 commit into from
Nov 6, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ jest.mock('holocron', () => ({

jest.mock('../../../src/server/utils/createCircuitBreaker', () => {
const breaker = jest.fn();
const mockCreateCircuitBreaker = (asyncFuntionThatMightFail) => {
const mockCreateCircuitBreaker = (asyncFunctionThatMightFail) => {
breaker.fire = jest.fn((...args) => {
asyncFuntionThatMightFail(...args);
asyncFunctionThatMightFail(...args);
return false;
});
return breaker;
Expand Down
14 changes: 7 additions & 7 deletions __tests__/server/utils/createCircuitBreaker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import createCircuitBreaker, {

jest.useFakeTimers();

const asyncFuntionThatMightFail = jest.fn(async () => false);
const mockCircuitBreaker = createCircuitBreaker(asyncFuntionThatMightFail);
const asyncFunctionThatMightFail = jest.fn(async () => false);
const mockCircuitBreaker = createCircuitBreaker(asyncFunctionThatMightFail);

jest.mock('holocron', () => ({
getModule: jest.fn(() => true),
Expand All @@ -49,7 +49,7 @@ describe('Circuit breaker', () => {
expect.assertions(2);
const input = 'hello, world';
const value = await mockCircuitBreaker.fire(input);
expect(asyncFuntionThatMightFail).toHaveBeenCalledWith(input);
expect(asyncFunctionThatMightFail).toHaveBeenCalledWith(input);
expect(value).toBe(false);
});

Expand All @@ -61,7 +61,7 @@ describe('Circuit breaker', () => {
await mockCircuitBreaker.fire('hola, mundo');
jest.clearAllMocks();
const value = await mockCircuitBreaker.fire('hola, mundo');
expect(asyncFuntionThatMightFail).not.toHaveBeenCalled();
expect(asyncFunctionThatMightFail).not.toHaveBeenCalled();
expect(value).toBe(true);
});

Expand All @@ -74,7 +74,7 @@ describe('Circuit breaker', () => {
await mockCircuitBreaker.fire('hola, mundo');
jest.clearAllMocks();
const value = await mockCircuitBreaker.fire('hola, mundo');
expect(asyncFuntionThatMightFail).toHaveBeenCalled();
expect(asyncFunctionThatMightFail).toHaveBeenCalled();
expect(value).toBe(false);
});

Expand All @@ -86,7 +86,7 @@ describe('Circuit breaker', () => {
await mockCircuitBreaker.fire('hola, mundo');
jest.clearAllMocks();
const value = await mockCircuitBreaker.fire('hola, mundo');
expect(asyncFuntionThatMightFail).toHaveBeenCalled();
expect(asyncFunctionThatMightFail).toHaveBeenCalled();
expect(value).toBe(false);
});

Expand All @@ -99,7 +99,7 @@ describe('Circuit breaker', () => {
await mockCircuitBreaker.fire('hola, mundo');
jest.clearAllMocks();
const value = await mockCircuitBreaker.fire('hola, mundo');
expect(asyncFuntionThatMightFail).toHaveBeenCalled();
expect(asyncFunctionThatMightFail).toHaveBeenCalled();
expect(value).toBe(false);
});

Expand Down
10 changes: 5 additions & 5 deletions src/server/utils/createCircuitBreaker.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ const checkMaxEventLoopDelay = async () => {
}
};

const createCircuitBreaker = (asyncFuntionThatMightFail) => {
// asyncFuntionThatMightFail should return false to indicate fallback is not needed
const breaker = new CircuitBreaker(asyncFuntionThatMightFail, options);
const createCircuitBreaker = (asyncFunctionThatMightFail) => {
// asyncFunctionThatMightFail should return false to indicate fallback is not needed
const breaker = new CircuitBreaker(asyncFunctionThatMightFail, options);
// Fallback returns true to indicate fallback behavior is needed
breaker.fallback(() => true);
// Check the max event loop delay every 500ms
breaker.healthCheck(checkMaxEventLoopDelay, 500);
// Log when circuit breaker opens and closes
breaker.on('open', () => console.log(`Circuit breaker [${asyncFuntionThatMightFail.name}] opened`));
breaker.on('close', () => console.log(`Circuit breaker [${asyncFuntionThatMightFail.name}] closed`));
breaker.on('open', () => console.log(`Circuit breaker [${asyncFunctionThatMightFail.name}] opened`));
breaker.on('close', () => console.log(`Circuit breaker [${asyncFunctionThatMightFail.name}] closed`));
breaker.on('healthCheckFailed', (error) => console.error(error));
// Track circuit breaker metrics
registerCircuitBreaker(breaker);
Expand Down