Skip to content

Commit

Permalink
Updated test
Browse files Browse the repository at this point in the history
  • Loading branch information
delvedor committed Feb 8, 2018
1 parent 7f107c6 commit 04cbea5
Showing 1 changed file with 121 additions and 9 deletions.
130 changes: 121 additions & 9 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,26 +82,138 @@ test('Should call the function (error threshold)', t => {
test('Should call the function (error timeout)', t => {
t.plan(2)

const circuitBreaker = CircuitBreaker(longHttpCall, {
const circuitBreaker = CircuitBreaker(httpCall, {
threshold: 2,
timeout: 200,
resetTimeout: 1000
})

circuitBreaker.run(true, err => {
circuitBreaker.run(true, 1000, err => {
t.is(err.message, 'Timeout')
t.is(circuitBreaker._failures, 1)
})
})

function httpCall (shouldError, callback) {
setTimeout(() => {
callback(shouldError ? new Error('kaboom') : null)
}, 0)
}
test('Should call the function (multiple error timeout - threshold)', t => {
t.plan(6)

const circuitBreaker = CircuitBreaker(httpCall, {
threshold: 2,
timeout: 200,
resetTimeout: 1000
})

circuitBreaker.run(true, 1000, err => {
t.is(err.message, 'Timeout')
t.is(circuitBreaker._failures, 1)

circuitBreaker.run(true, 1000, err => {
t.is(err.message, 'Timeout')
t.is(circuitBreaker._failures, 2)

circuitBreaker.run(true, 1000, err => {
t.is(err.message, 'Circuit open')
t.is(circuitBreaker._failures, 2)
})
})
})
})

test('Half open state', t => {
t.plan(6)

const circuitBreaker = CircuitBreaker(httpCall, {
threshold: 2,
timeout: 200,
resetTimeout: 200
})

circuitBreaker.run(true, err => {
t.is(err.message, 'kaboom')
t.is(circuitBreaker._failures, 1)

circuitBreaker.run(true, err => {
t.is(err.message, 'kaboom')
t.is(circuitBreaker._failures, 2)
t.is(circuitBreaker.state, 'open')
setTimeout(again, 300)
})
})

function again () {
t.is(circuitBreaker.state, 'half-open')
}
})

test('Half open state, set to close on good response', t => {
t.plan(9)

const circuitBreaker = CircuitBreaker(httpCall, {
threshold: 2,
timeout: 200,
resetTimeout: 200
})

circuitBreaker.run(true, err => {
t.is(err.message, 'kaboom')
t.is(circuitBreaker._failures, 1)

circuitBreaker.run(true, err => {
t.is(err.message, 'kaboom')
t.is(circuitBreaker._failures, 2)
t.is(circuitBreaker.state, 'open')
setTimeout(again, 300)
})
})

function again () {
t.is(circuitBreaker.state, 'half-open')
circuitBreaker.run(false, err => {
t.error(err)
t.is(circuitBreaker._failures, 0)
t.is(circuitBreaker.state, 'close')
})
}
})

test('Half open state, set to open on bad response', t => {
t.plan(9)

const circuitBreaker = CircuitBreaker(httpCall, {
threshold: 2,
timeout: 200,
resetTimeout: 200
})

circuitBreaker.run(true, err => {
t.is(err.message, 'kaboom')
t.is(circuitBreaker._failures, 1)

circuitBreaker.run(true, err => {
t.is(err.message, 'kaboom')
t.is(circuitBreaker._failures, 2)
t.is(circuitBreaker.state, 'open')
setTimeout(again, 300)
})
})

function again () {
t.is(circuitBreaker.state, 'half-open')
circuitBreaker.run(true, err => {
t.is(err.message, 'kaboom')
t.is(circuitBreaker._failures, 2)
t.is(circuitBreaker.state, 'open')
})
}
})

function httpCall (shouldError, delay, callback) {
if (callback == null) {
callback = delay
delay = 0
}

function longHttpCall (shouldError, callback) {
setTimeout(() => {
callback(shouldError ? new Error('kaboom') : null)
}, 1000)
}, delay)
}

0 comments on commit 04cbea5

Please sign in to comment.