@fastify/circuit-breaker
Advanced tools
Comparing version 4.0.0 to 4.0.1
{ | ||
"name": "@fastify/circuit-breaker", | ||
"version": "4.0.0", | ||
"version": "4.0.1", | ||
"description": "A low overhead circuit breaker for your routes", | ||
@@ -11,3 +11,3 @@ "main": "index.js", | ||
"test": "npm run test:unit && npm run test:typescript", | ||
"test:unit": "tap", | ||
"test:unit": "c8 -100 node --test", | ||
"test:typescript": "tsd" | ||
@@ -32,5 +32,5 @@ }, | ||
"@types/node": "^22.0.0", | ||
"fastify": "^5.0.0-alpha.4", | ||
"c8": "^10.1.2", | ||
"fastify": "^5.0.0", | ||
"standard": "^17.1.0", | ||
"tap": "^20.0.1", | ||
"tsd": "^0.31.1" | ||
@@ -37,0 +37,0 @@ }, |
'use strict' | ||
const t = require('tap') | ||
const test = t.test | ||
const { test } = require('node:test') | ||
const Fastify = require('fastify') | ||
const circuitBreaker = require('..') | ||
const { setTimeout: sleep } = require('timers/promises') | ||
@@ -20,7 +20,7 @@ const opts = { | ||
test('Should respond with a 503 once the threshold has been reached', t => { | ||
test('Should respond with a 503 once the threshold has been reached', async t => { | ||
t.plan(12) | ||
const fastify = Fastify() | ||
fastify.register(circuitBreaker, { | ||
await fastify.register(circuitBreaker, { | ||
threshold: 3, | ||
@@ -34,3 +34,3 @@ timeout: 1000, | ||
fastify.get('/', opts, (req, reply) => { | ||
t.equal(typeof req._cbTime, 'number') | ||
t.assert.strictEqual(typeof req._cbTime, 'number') | ||
setTimeout(() => { | ||
@@ -44,35 +44,32 @@ reply.send( | ||
fastify.inject('/?error=true', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 500) | ||
t.same({ | ||
error: 'Internal Server Error', | ||
message: 'kaboom', | ||
statusCode: 500 | ||
}, JSON.parse(res.payload)) | ||
}) | ||
let res = await fastify.inject('/?error=true') | ||
t.assert.ok(res) | ||
t.assert.strictEqual(res.statusCode, 500) | ||
t.assert.deepStrictEqual({ | ||
error: 'Internal Server Error', | ||
message: 'kaboom', | ||
statusCode: 500 | ||
}, JSON.parse(res.payload)) | ||
fastify.inject('/?error=true', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 500) | ||
t.same({ | ||
error: 'Internal Server Error', | ||
message: 'kaboom', | ||
statusCode: 500 | ||
}, JSON.parse(res.payload)) | ||
}) | ||
res = await fastify.inject('/?error=true') | ||
t.assert.ok(res) | ||
t.assert.strictEqual(res.statusCode, 500) | ||
t.assert.deepStrictEqual({ | ||
error: 'Internal Server Error', | ||
message: 'kaboom', | ||
statusCode: 500 | ||
}, JSON.parse(res.payload)) | ||
fastify.inject('/?error=true', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 503) | ||
t.same({ | ||
error: 'Service Unavailable', | ||
message: 'Circuit open', | ||
statusCode: 503, | ||
code: 'FST_ERR_CIRCUIT_BREAKER_OPEN' | ||
}, JSON.parse(res.payload)) | ||
}) | ||
res = await fastify.inject('/?error=true') | ||
t.assert.ok(res) | ||
t.assert.strictEqual(res.statusCode, 503) | ||
t.assert.deepStrictEqual({ | ||
error: 'Service Unavailable', | ||
message: 'Circuit open', | ||
statusCode: 503, | ||
code: 'FST_ERR_CIRCUIT_BREAKER_OPEN' | ||
}, JSON.parse(res.payload)) | ||
}) | ||
test('Should respond with a 503 once the threshold has been reached (timeout)', t => { | ||
test('Should respond with a 503 once the threshold has been reached (timeout)', async t => { | ||
t.plan(15) | ||
@@ -90,3 +87,3 @@ | ||
fastify.get('/', opts, (req, reply) => { | ||
t.equal(typeof req._cbTime, 'number') | ||
t.assert.strictEqual(typeof req._cbTime, 'number') | ||
setTimeout(() => { | ||
@@ -101,5 +98,5 @@ reply.send( | ||
fastify.inject('/?error=false&delay=100', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 503) | ||
t.same({ | ||
t.assert.ifError(err) | ||
t.assert.strictEqual(res.statusCode, 503) | ||
t.assert.deepStrictEqual({ | ||
error: 'Service Unavailable', | ||
@@ -113,5 +110,5 @@ message: 'Timeout', | ||
fastify.inject('/?error=false&delay=100', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 503) | ||
t.same({ | ||
t.assert.ifError(err) | ||
t.assert.strictEqual(res.statusCode, 503) | ||
t.assert.deepStrictEqual({ | ||
error: 'Service Unavailable', | ||
@@ -125,5 +122,5 @@ message: 'Timeout', | ||
fastify.inject('/?error=false&delay=100', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 503) | ||
t.same({ | ||
t.assert.ifError(err) | ||
t.assert.strictEqual(res.statusCode, 503) | ||
t.assert.deepStrictEqual({ | ||
error: 'Service Unavailable', | ||
@@ -138,5 +135,5 @@ message: 'Timeout', | ||
fastify.inject('/?error=false&delay=100', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 503) | ||
t.same({ | ||
t.assert.ifError(err) | ||
t.assert.strictEqual(res.statusCode, 503) | ||
t.assert.deepStrictEqual({ | ||
error: 'Service Unavailable', | ||
@@ -149,9 +146,11 @@ message: 'Circuit open', | ||
}, 200) | ||
await sleep(200) | ||
}) | ||
test('Should return 503 until the circuit is open', t => { | ||
test('Should return 503 until the circuit is open', async t => { | ||
t.plan(12) | ||
const fastify = Fastify() | ||
fastify.register(circuitBreaker, { | ||
await fastify.register(circuitBreaker, { | ||
threshold: 2, | ||
@@ -165,3 +164,3 @@ timeout: 1000, | ||
fastify.get('/', opts, (req, reply) => { | ||
t.equal(typeof req._cbTime, 'number') | ||
t.assert.strictEqual(typeof req._cbTime, 'number') | ||
setTimeout(() => { | ||
@@ -175,33 +174,29 @@ reply.send( | ||
fastify.inject('/?error=true', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 500) | ||
t.same({ | ||
error: 'Internal Server Error', | ||
message: 'kaboom', | ||
statusCode: 500 | ||
}, JSON.parse(res.payload)) | ||
}) | ||
let res = await fastify.inject('/?error=true') | ||
t.assert.ok(res) | ||
t.assert.strictEqual(res.statusCode, 500) | ||
t.assert.deepStrictEqual({ | ||
error: 'Internal Server Error', | ||
message: 'kaboom', | ||
statusCode: 500 | ||
}, JSON.parse(res.payload)) | ||
fastify.inject('/?error=true', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 503) | ||
t.same({ | ||
error: 'Service Unavailable', | ||
message: 'Circuit open', | ||
statusCode: 503, | ||
code: 'FST_ERR_CIRCUIT_BREAKER_OPEN' | ||
}, JSON.parse(res.payload)) | ||
}) | ||
res = await fastify.inject('/?error=true') | ||
t.assert.ok(res) | ||
t.assert.strictEqual(res.statusCode, 503) | ||
t.assert.deepStrictEqual({ | ||
error: 'Service Unavailable', | ||
message: 'Circuit open', | ||
statusCode: 503, | ||
code: 'FST_ERR_CIRCUIT_BREAKER_OPEN' | ||
}, JSON.parse(res.payload)) | ||
setTimeout(() => { | ||
fastify.inject('/?error=false', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.same({ hello: 'world' }, JSON.parse(res.payload)) | ||
}) | ||
}, 1000) | ||
await sleep(1000) | ||
res = await fastify.inject('/?error=false') | ||
t.assert.ok(res) | ||
t.assert.strictEqual(res.statusCode, 200) | ||
t.assert.deepStrictEqual({ hello: 'world' }, JSON.parse(res.payload)) | ||
}) | ||
test('If the staus is half-open and there is an error the state should be open again', t => { | ||
test('If the staus is half-open and there is an error the state should be open again', async t => { | ||
t.plan(15) | ||
@@ -219,3 +214,3 @@ | ||
fastify.get('/', opts, (req, reply) => { | ||
t.equal(typeof req._cbTime, 'number') | ||
t.assert.strictEqual(typeof req._cbTime, 'number') | ||
setTimeout(() => { | ||
@@ -230,5 +225,5 @@ reply.send( | ||
fastify.inject('/?error=true', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 500) | ||
t.same({ | ||
t.assert.ifError(err) | ||
t.assert.strictEqual(res.statusCode, 500) | ||
t.assert.deepStrictEqual({ | ||
error: 'Internal Server Error', | ||
@@ -241,5 +236,5 @@ message: 'kaboom', | ||
fastify.inject('/?error=true', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 503) | ||
t.same({ | ||
t.assert.ifError(err) | ||
t.assert.strictEqual(res.statusCode, 503) | ||
t.assert.deepStrictEqual({ | ||
error: 'Service Unavailable', | ||
@@ -254,5 +249,5 @@ message: 'Circuit open', | ||
fastify.inject('/?error=true', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 500) | ||
t.same({ | ||
t.assert.ifError(err) | ||
t.assert.strictEqual(res.statusCode, 500) | ||
t.assert.deepStrictEqual({ | ||
error: 'Internal Server Error', | ||
@@ -265,5 +260,5 @@ message: 'kaboom', | ||
fastify.inject('/?error=true', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 503) | ||
t.same({ | ||
t.assert.ifError(err) | ||
t.assert.strictEqual(res.statusCode, 503) | ||
t.assert.deepStrictEqual({ | ||
error: 'Service Unavailable', | ||
@@ -276,5 +271,7 @@ message: 'Circuit open', | ||
}, 1000) | ||
await sleep(1200) | ||
}) | ||
test('Should customize circuit open error message', t => { | ||
test('Should customize circuit open error message', async t => { | ||
t.plan(4) | ||
@@ -291,3 +288,3 @@ | ||
fastify.get('/', opts, (req, reply) => { | ||
t.equal(typeof req._cbTime, 'number') | ||
t.assert.strictEqual(typeof req._cbTime, 'number') | ||
setTimeout(() => { | ||
@@ -301,15 +298,14 @@ reply.send( | ||
fastify.inject('/?error=true', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 503) | ||
t.same({ | ||
error: 'Service Unavailable', | ||
message: 'Oh gosh!', | ||
statusCode: 503, | ||
code: 'FST_ERR_CIRCUIT_BREAKER_OPEN' | ||
}, JSON.parse(res.payload)) | ||
}) | ||
const res = await fastify.inject('/?error=true') | ||
t.assert.ok(res) | ||
t.assert.strictEqual(res.statusCode, 503) | ||
t.assert.deepStrictEqual({ | ||
error: 'Service Unavailable', | ||
message: 'Oh gosh!', | ||
statusCode: 503, | ||
code: 'FST_ERR_CIRCUIT_BREAKER_OPEN' | ||
}, JSON.parse(res.payload)) | ||
}) | ||
test('Should customize timeout error message', t => { | ||
test('Should customize timeout error message', async t => { | ||
t.plan(4) | ||
@@ -327,3 +323,3 @@ | ||
fastify.get('/', opts, (req, reply) => { | ||
t.equal(typeof req._cbTime, 'number') | ||
t.assert.strictEqual(typeof req._cbTime, 'number') | ||
setTimeout(() => { | ||
@@ -337,15 +333,14 @@ reply.send( | ||
fastify.inject('/?error=true&delay=200', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 503) | ||
t.same({ | ||
error: 'Service Unavailable', | ||
message: 'Oh gosh!', | ||
statusCode: 503, | ||
code: 'FST_ERR_CIRCUIT_BREAKER_TIMEOUT' | ||
}, JSON.parse(res.payload)) | ||
}) | ||
const res = await fastify.inject('/?error=true&delay=200') | ||
t.assert.ok(res) | ||
t.assert.strictEqual(res.statusCode, 503) | ||
t.assert.deepStrictEqual({ | ||
error: 'Service Unavailable', | ||
message: 'Oh gosh!', | ||
statusCode: 503, | ||
code: 'FST_ERR_CIRCUIT_BREAKER_TIMEOUT' | ||
}, JSON.parse(res.payload)) | ||
}) | ||
test('One route should not interfere with others', t => { | ||
test('One route should not interfere with others', async t => { | ||
t.plan(7) | ||
@@ -361,3 +356,3 @@ | ||
fastify.get('/', opts, (req, reply) => { | ||
t.equal(typeof req._cbTime, 'number') | ||
t.assert.strictEqual(typeof req._cbTime, 'number') | ||
setTimeout(() => { | ||
@@ -376,25 +371,23 @@ reply.send( | ||
fastify.inject('/?error=true', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 503) | ||
t.same({ | ||
error: 'Service Unavailable', | ||
message: 'Circuit open', | ||
statusCode: 503, | ||
code: 'FST_ERR_CIRCUIT_BREAKER_OPEN' | ||
}, JSON.parse(res.payload)) | ||
}) | ||
let res = await fastify.inject('/?error=true') | ||
t.assert.ok(res) | ||
t.assert.strictEqual(res.statusCode, 503) | ||
t.assert.deepStrictEqual({ | ||
error: 'Service Unavailable', | ||
message: 'Circuit open', | ||
statusCode: 503, | ||
code: 'FST_ERR_CIRCUIT_BREAKER_OPEN' | ||
}, JSON.parse(res.payload)) | ||
fastify.inject('/other', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.same({ hello: 'world' }, JSON.parse(res.payload)) | ||
}) | ||
res = await fastify.inject('/other') | ||
t.assert.ok(res) | ||
t.assert.strictEqual(res.statusCode, 200) | ||
t.assert.deepStrictEqual({ hello: 'world' }, JSON.parse(res.payload)) | ||
}) | ||
test('Custom options should overwrite the globals', t => { | ||
test('Custom options should overwrite the globals', async t => { | ||
t.plan(4) | ||
const fastify = Fastify() | ||
fastify.register(circuitBreaker, { | ||
await fastify.register(circuitBreaker, { | ||
threshold: 1 | ||
@@ -406,3 +399,3 @@ }) | ||
fastify.get('/', opts, (req, reply) => { | ||
t.equal(typeof req._cbTime, 'number') | ||
t.assert.strictEqual(typeof req._cbTime, 'number') | ||
setTimeout(() => { | ||
@@ -416,14 +409,13 @@ reply.send( | ||
fastify.inject('/?error=true', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 500) | ||
t.same({ | ||
error: 'Internal Server Error', | ||
message: 'kaboom', | ||
statusCode: 500 | ||
}, JSON.parse(res.payload)) | ||
}) | ||
const res = await fastify.inject('/?error=true') | ||
t.assert.ok(res) | ||
t.assert.strictEqual(res.statusCode, 500) | ||
t.assert.deepStrictEqual({ | ||
error: 'Internal Server Error', | ||
message: 'kaboom', | ||
statusCode: 500 | ||
}, JSON.parse(res.payload)) | ||
}) | ||
test('Should handle also errors with statusCode property', t => { | ||
test('Should handle also errors with statusCode property', async t => { | ||
t.plan(6) | ||
@@ -445,25 +437,23 @@ | ||
fastify.inject('/', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 501) | ||
t.same({ | ||
error: 'Not Implemented', | ||
message: 'kaboom', | ||
statusCode: 501 | ||
}, JSON.parse(res.payload)) | ||
}) | ||
let res = await fastify.inject('/') | ||
t.assert.ok(res) | ||
t.assert.strictEqual(res.statusCode, 501) | ||
t.assert.deepStrictEqual({ | ||
error: 'Not Implemented', | ||
message: 'kaboom', | ||
statusCode: 501 | ||
}, JSON.parse(res.payload)) | ||
fastify.inject('/', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 503) | ||
t.same({ | ||
error: 'Service Unavailable', | ||
message: 'Circuit open', | ||
statusCode: 503, | ||
code: 'FST_ERR_CIRCUIT_BREAKER_OPEN' | ||
}, JSON.parse(res.payload)) | ||
}) | ||
res = await fastify.inject('/') | ||
t.assert.ok(res) | ||
t.assert.strictEqual(res.statusCode, 503) | ||
t.assert.deepStrictEqual({ | ||
error: 'Service Unavailable', | ||
message: 'Circuit open', | ||
statusCode: 503, | ||
code: 'FST_ERR_CIRCUIT_BREAKER_OPEN' | ||
}, JSON.parse(res.payload)) | ||
}) | ||
test('If a route is not under the circuit breaker, _cbRouteId should always be equal to 0', t => { | ||
test('If a route is not under the circuit breaker, _cbRouteId should always be equal to 0', async t => { | ||
t.plan(8) | ||
@@ -475,3 +465,3 @@ | ||
fastify.get('/first', (req, reply) => { | ||
t.ok(req._cbRouteId === 0) | ||
t.assert.strictEqual(req._cbRouteId, 0) | ||
reply.send({ hello: 'world' }) | ||
@@ -481,20 +471,18 @@ }) | ||
fastify.get('/second', (req, reply) => { | ||
t.ok(req._cbRouteId === 0) | ||
t.assert.strictEqual(req._cbRouteId, 0) | ||
reply.send({ hello: 'world' }) | ||
}) | ||
fastify.inject('/first', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.same({ hello: 'world' }, JSON.parse(res.payload)) | ||
}) | ||
let res = await fastify.inject('/first') | ||
t.assert.ok(res) | ||
t.assert.strictEqual(res.statusCode, 200) | ||
t.assert.deepStrictEqual({ hello: 'world' }, JSON.parse(res.payload)) | ||
fastify.inject('/second', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.same({ hello: 'world' }, JSON.parse(res.payload)) | ||
}) | ||
res = await fastify.inject('/second') | ||
t.assert.ok(res) | ||
t.assert.strictEqual(res.statusCode, 200) | ||
t.assert.deepStrictEqual({ hello: 'world' }, JSON.parse(res.payload)) | ||
}) | ||
test('Should work only if the status code is >= 500', t => { | ||
test('Should work only if the status code is >= 500', async t => { | ||
t.plan(6) | ||
@@ -520,24 +508,22 @@ | ||
fastify.inject('/first', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 400) | ||
t.same({ | ||
error: 'Bad Request', | ||
message: 'kaboom', | ||
statusCode: 400 | ||
}, JSON.parse(res.payload)) | ||
}) | ||
let res = await fastify.inject('/first') | ||
t.assert.ok(res) | ||
t.assert.strictEqual(res.statusCode, 400) | ||
t.assert.deepStrictEqual({ | ||
error: 'Bad Request', | ||
message: 'kaboom', | ||
statusCode: 400 | ||
}, JSON.parse(res.payload)) | ||
fastify.inject('/second', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 400) | ||
t.same({ | ||
error: 'Bad Request', | ||
message: 'kaboom', | ||
statusCode: 400 | ||
}, JSON.parse(res.payload)) | ||
}) | ||
res = await fastify.inject('/second') | ||
t.assert.ok(res) | ||
t.assert.strictEqual(res.statusCode, 400) | ||
t.assert.deepStrictEqual({ | ||
error: 'Bad Request', | ||
message: 'kaboom', | ||
statusCode: 400 | ||
}, JSON.parse(res.payload)) | ||
}) | ||
test('Should call onCircuitOpen when the threshold has been reached', t => { | ||
test('Should call onCircuitOpen when the threshold has been reached', async t => { | ||
t.plan(6) | ||
@@ -560,22 +546,20 @@ | ||
fastify.inject('/', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 500) | ||
t.same({ | ||
error: 'Internal Server Error', | ||
message: 'kaboom', | ||
statusCode: 500 | ||
}, JSON.parse(res.payload)) | ||
}) | ||
let res = await fastify.inject('/') | ||
t.assert.ok(res) | ||
t.assert.strictEqual(res.statusCode, 500) | ||
t.assert.deepStrictEqual({ | ||
error: 'Internal Server Error', | ||
message: 'kaboom', | ||
statusCode: 500 | ||
}, JSON.parse(res.payload)) | ||
fastify.inject('/', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 503) | ||
t.same({ | ||
message: 'hi' | ||
}, JSON.parse(res.payload)) | ||
}) | ||
res = await fastify.inject('/') | ||
t.assert.ok(res) | ||
t.assert.strictEqual(res.statusCode, 503) | ||
t.assert.deepStrictEqual({ | ||
message: 'hi' | ||
}, JSON.parse(res.payload)) | ||
}) | ||
test('Should call onTimeout when the timeout has been reached', t => { | ||
test('Should call onTimeout when the timeout has been reached', async t => { | ||
t.plan(3) | ||
@@ -600,10 +584,9 @@ | ||
fastify.inject('/', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 504) | ||
t.equal(res.payload, 'timed out') | ||
}) | ||
const res = await fastify.inject('/') | ||
t.assert.ok(res) | ||
t.assert.strictEqual(res.statusCode, 504) | ||
t.assert.strictEqual(res.payload, 'timed out') | ||
}) | ||
test('onCircuitOpen will handle a thrown error', t => { | ||
test('onCircuitOpen will handle a thrown error', async t => { | ||
t.plan(6) | ||
@@ -626,24 +609,22 @@ | ||
fastify.inject('/', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 500) | ||
t.same({ | ||
error: 'Internal Server Error', | ||
message: 'kaboom', | ||
statusCode: 500 | ||
}, JSON.parse(res.payload)) | ||
}) | ||
let res = await fastify.inject('/') | ||
t.assert.ok(res) | ||
t.assert.strictEqual(res.statusCode, 500) | ||
t.assert.deepStrictEqual({ | ||
error: 'Internal Server Error', | ||
message: 'kaboom', | ||
statusCode: 500 | ||
}, JSON.parse(res.payload)) | ||
fastify.inject('/', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 503) | ||
t.same({ | ||
error: 'Service Unavailable', | ||
message: 'circuit open', | ||
statusCode: 503 | ||
}, JSON.parse(res.payload)) | ||
}) | ||
res = await fastify.inject('/') | ||
t.assert.ok(res) | ||
t.assert.strictEqual(res.statusCode, 503) | ||
t.assert.deepStrictEqual({ | ||
error: 'Service Unavailable', | ||
message: 'circuit open', | ||
statusCode: 503 | ||
}, JSON.parse(res.payload)) | ||
}) | ||
test('onTimeout will handle a thrown error', t => { | ||
test('onTimeout will handle a thrown error', async t => { | ||
t.plan(2) | ||
@@ -668,13 +649,12 @@ | ||
fastify.inject('/', (err, res) => { | ||
t.error(err) | ||
t.same({ | ||
error: 'Gateway Timeout', | ||
message: 'timed out', | ||
statusCode: 504 | ||
}, JSON.parse(res.payload)) | ||
}) | ||
const res = await fastify.inject('/') | ||
t.assert.ok(res) | ||
t.assert.deepStrictEqual({ | ||
error: 'Gateway Timeout', | ||
message: 'timed out', | ||
statusCode: 504 | ||
}, JSON.parse(res.payload)) | ||
}) | ||
test('onCircuitOpen can be an async function', t => { | ||
test('onCircuitOpen can be an async function', async t => { | ||
t.plan(6) | ||
@@ -698,24 +678,22 @@ | ||
fastify.inject('/', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 500) | ||
t.same({ | ||
error: 'Internal Server Error', | ||
message: 'kaboom', | ||
statusCode: 500 | ||
}, JSON.parse(res.payload)) | ||
}) | ||
let res = await fastify.inject('/') | ||
t.assert.ok(res) | ||
t.assert.strictEqual(res.statusCode, 500) | ||
t.assert.deepStrictEqual({ | ||
error: 'Internal Server Error', | ||
message: 'kaboom', | ||
statusCode: 500 | ||
}, JSON.parse(res.payload)) | ||
fastify.inject('/', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 503) | ||
t.same({ | ||
error: 'Service Unavailable', | ||
message: 'circuit open', | ||
statusCode: 503 | ||
}, JSON.parse(res.payload)) | ||
}) | ||
res = await fastify.inject('/') | ||
t.assert.ok(res) | ||
t.assert.strictEqual(res.statusCode, 503) | ||
t.assert.deepStrictEqual({ | ||
error: 'Service Unavailable', | ||
message: 'circuit open', | ||
statusCode: 503 | ||
}, JSON.parse(res.payload)) | ||
}) | ||
test('onTimeout can be an async function', t => { | ||
test('onTimeout can be an async function', async t => { | ||
t.plan(2) | ||
@@ -741,13 +719,12 @@ | ||
fastify.inject('/', (err, res) => { | ||
t.error(err) | ||
t.same({ | ||
error: 'Gateway Timeout', | ||
message: 'timed out', | ||
statusCode: 504 | ||
}, JSON.parse(res.payload)) | ||
}) | ||
const res = await fastify.inject('/') | ||
t.assert.ok(res) | ||
t.assert.deepStrictEqual({ | ||
error: 'Gateway Timeout', | ||
message: 'timed out', | ||
statusCode: 504 | ||
}, JSON.parse(res.payload)) | ||
}) | ||
test('Should not throw error if no options is passed', t => { | ||
test('Should not throw error if no options is passed', async t => { | ||
t.plan(3) | ||
@@ -757,12 +734,12 @@ const fastify = Fastify() | ||
const fastify3 = Fastify() | ||
t.equal(circuitBreaker(fastify, undefined, () => {}), undefined) | ||
t.equal(circuitBreaker(fastify2, null, () => {}), undefined) | ||
t.equal(circuitBreaker(fastify3, {}, () => {}), undefined) | ||
t.assert.strictEqual(circuitBreaker(fastify, undefined, () => {}), undefined) | ||
t.assert.strictEqual(circuitBreaker(fastify2, null, () => {}), undefined) | ||
t.assert.strictEqual(circuitBreaker(fastify3, {}, () => {}), undefined) | ||
}) | ||
test('Should throw error on route status open and circuit open', t => { | ||
test('Should throw error on route status open and circuit open', async t => { | ||
t.plan(5) | ||
const fastify = Fastify() | ||
fastify.register(circuitBreaker, { | ||
await fastify.register(circuitBreaker, { | ||
threshold: 1, | ||
@@ -779,3 +756,3 @@ timeout: 1000, | ||
fastify.get('/', { preHandler: fastify.circuitBreaker() }, (req, reply) => { | ||
t.equal(typeof req._cbTime, 'number') | ||
t.assert.strictEqual(typeof req._cbTime, 'number') | ||
setTimeout(() => { | ||
@@ -787,16 +764,13 @@ reply.send(new Error('kaboom')) | ||
fastify.inject('/?error=true', (err, res) => { | ||
t.error(err) | ||
}) | ||
let res = await fastify.inject('/?error=true') | ||
t.assert.ok(res) | ||
setTimeout(() => { | ||
fastify.inject('/?error=false', (err, res) => { | ||
t.equal(null, err) | ||
t.equal(res.statusCode, 500) | ||
t.same(res.json(), { err: 'custom error' }) | ||
}) | ||
}, 1000) | ||
await sleep(1000) | ||
res = await fastify.inject('/?error=false') | ||
t.assert.ok(res) | ||
t.assert.strictEqual(res.statusCode, 500) | ||
t.assert.deepStrictEqual(res.json(), { err: 'custom error' }) | ||
}) | ||
test('Should throw error on route status half open and circuit open', t => { | ||
test('Should throw error on route status half open and circuit open', async t => { | ||
t.plan(15) | ||
@@ -819,3 +793,3 @@ | ||
fastify.get('/', opts, (req, reply) => { | ||
t.equal(typeof req._cbTime, 'number') | ||
t.assert.strictEqual(typeof req._cbTime, 'number') | ||
setTimeout(() => { | ||
@@ -828,5 +802,5 @@ reply.send(new Error('kaboom')) | ||
fastify.inject('/?error=true', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 500) | ||
t.same({ | ||
t.assert.ifError(err) | ||
t.assert.strictEqual(res.statusCode, 500) | ||
t.assert.deepStrictEqual({ | ||
error: 'Internal Server Error', | ||
@@ -839,5 +813,5 @@ message: 'kaboom', | ||
fastify.inject('/?error=true', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 500) | ||
t.same(res.json(), { err: 'custom error' }) | ||
t.assert.ifError(err) | ||
t.assert.strictEqual(res.statusCode, 500) | ||
t.assert.deepStrictEqual(res.json(), { err: 'custom error' }) | ||
}) | ||
@@ -847,5 +821,5 @@ | ||
fastify.inject('/?error=true', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 500) | ||
t.same({ | ||
t.assert.ifError(err) | ||
t.assert.strictEqual(res.statusCode, 500) | ||
t.assert.deepStrictEqual({ | ||
error: 'Internal Server Error', | ||
@@ -858,7 +832,9 @@ message: 'kaboom', | ||
fastify.inject('/?error=true', (err, res) => { | ||
t.equal(null, err) | ||
t.equal(res.statusCode, 500) | ||
t.same(res.json(), { err: 'custom error' }) | ||
t.assert.strictEqual(null, err) | ||
t.assert.strictEqual(res.statusCode, 500) | ||
t.assert.deepStrictEqual(res.json(), { err: 'custom error' }) | ||
}) | ||
}, 1000) | ||
await sleep(1200) | ||
}) |
38980
12
1007