circuit-breaker-await-async
Advanced tools
Comparing version 1.2.0 to 1.2.1
@@ -233,2 +233,29 @@ import debug from 'debug' | ||
}) | ||
it('circuitBreaker.call errors preserves args between calls', async (done) => { | ||
log('Starting end to end test') | ||
// Let's start with simulating a non-working API | ||
let fn = jest.fn((one, two, three) => { | ||
// mock async call using Promise | ||
return new Promise((resolve, reject) => { | ||
setTimeout(reject, 1, new Error('Server Error', [one, two, three])) | ||
}) | ||
}) | ||
// smaller timeouts for faster tests | ||
let circuitBreaker = new CircuitBreaker(fn, { | ||
callTimeoutMs: 100, | ||
resetTimeoutMs: 1000 | ||
}) | ||
try { | ||
await circuitBreaker.call(1, 2, 3) | ||
} catch (err) { | ||
log('expected error after 10 attempts') | ||
expect(err).toEqual(new Error('CIRCUIT_IS_OPEN', [1, 2, 3])) | ||
expect(circuitBreaker.state).toEqual(states.OPEN) | ||
done() | ||
} | ||
}) | ||
}) |
@@ -53,4 +53,6 @@ 'use strict'; | ||
this.on('circuit-breaker.call', async ({ args, state } = {}) => { | ||
log('circuit-breaker.call received'); | ||
const handleCall = async () => { | ||
const { args, state } = this; | ||
log('circuit-breaker.call received', { args, state }); | ||
const isHalfOpen = state === states.HALF_OPEN; | ||
@@ -89,10 +91,10 @@ if (isHalfOpen) { | ||
} | ||
}); | ||
}; | ||
this.on('circuit-breaker.call.succeeded', () => { | ||
const handleCallSucceeded = () => { | ||
this.state = states.CLOSED; | ||
this.currentAttempt = 0; | ||
}); | ||
}; | ||
this.on('circuit-breaker.trip', async () => { | ||
const handleTrip = async () => { | ||
log('tripping circuitbreaker'); | ||
@@ -105,16 +107,21 @@ this.state = states.OPEN; | ||
}, this.resetTimeoutMs); | ||
}); | ||
}; | ||
this.on('circuit-breaker.attempt-reset', async () => { | ||
const handleAttemptedReset = async () => { | ||
log('attempting to reset circuit breaker'); | ||
this.state = states.HALF_OPEN; | ||
}); | ||
}; | ||
this.on('circuit-breaker.call', handleCall); | ||
this.on('circuit-breaker.call.succeeded', handleCallSucceeded); | ||
this.on('circuit-breaker.trip', handleTrip); | ||
this.on('circuit-breaker.attempt-reset', handleAttemptedReset); | ||
} | ||
call() { | ||
const args = arguments; | ||
this.args = arguments; | ||
const doCall = ({ args, state }) => { | ||
const doCall = ({ state }) => { | ||
return new Promise((resolve, reject) => { | ||
this.emit('circuit-breaker.call', { args, state }); | ||
this.emit('circuit-breaker.call'); | ||
@@ -143,3 +150,3 @@ this.on('circuit-breaker.call.succeeded', result => { | ||
case states.HALF_OPEN: | ||
return doCall({ args, state: this.state }); | ||
return doCall({ args: this.args, state: this.state }); | ||
@@ -146,0 +153,0 @@ case states.OPEN: |
{ | ||
"name": "circuit-breaker-await-async", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "ES6 circuit breaker built around await/async patterns", | ||
@@ -24,3 +24,3 @@ "main": "dist/main.js", | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-cli": "6.26.0", | ||
"babel-core": "6.26.3", | ||
@@ -27,0 +27,0 @@ "babel-eslint": "10.0.3", |
@@ -39,4 +39,6 @@ import debug from 'debug' | ||
this.on('circuit-breaker.call', async ({ args, state } = {}) => { | ||
log('circuit-breaker.call received') | ||
const handleCall = async () => { | ||
const { args, state } = this | ||
log('circuit-breaker.call received', { args, state }) | ||
const isHalfOpen = state === states.HALF_OPEN | ||
@@ -75,10 +77,10 @@ if (isHalfOpen) { | ||
} | ||
}) | ||
} | ||
this.on('circuit-breaker.call.succeeded', () => { | ||
const handleCallSucceeded = () => { | ||
this.state = states.CLOSED | ||
this.currentAttempt = 0 | ||
}) | ||
} | ||
this.on('circuit-breaker.trip', async () => { | ||
const handleTrip = async () => { | ||
log('tripping circuitbreaker') | ||
@@ -91,16 +93,21 @@ this.state = states.OPEN | ||
}, this.resetTimeoutMs) | ||
}) | ||
} | ||
this.on('circuit-breaker.attempt-reset', async () => { | ||
const handleAttemptedReset = async () => { | ||
log('attempting to reset circuit breaker') | ||
this.state = states.HALF_OPEN | ||
}) | ||
} | ||
this.on('circuit-breaker.call', handleCall) | ||
this.on('circuit-breaker.call.succeeded', handleCallSucceeded) | ||
this.on('circuit-breaker.trip', handleTrip) | ||
this.on('circuit-breaker.attempt-reset', handleAttemptedReset) | ||
} | ||
call() { | ||
const args = arguments | ||
this.args = arguments | ||
const doCall = ({ args, state }) => { | ||
const doCall = ({ state }) => { | ||
return new Promise((resolve, reject) => { | ||
this.emit('circuit-breaker.call', { args, state }) | ||
this.emit('circuit-breaker.call') | ||
@@ -129,3 +136,3 @@ this.on('circuit-breaker.call.succeeded', (result) => { | ||
case states.HALF_OPEN: | ||
return doCall({ args, state: this.state }) | ||
return doCall({ args: this.args, state: this.state }) | ||
@@ -132,0 +139,0 @@ case states.OPEN: |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20222
495