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

Commit

Permalink
fix(typo): circuit breaker “function” input (#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
hongbo-miao authored Nov 6, 2020
1 parent 0faa0bf commit e1e74b9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions __tests__/server/middleware/createRequestHtmlFragment.spec.js
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

0 comments on commit e1e74b9

Please sign in to comment.