Skip to content

Commit cf18ea0

Browse files
committed
test: handle aggregate balanced pool errors
Signed-off-by: marko1olo <barsukdana@gmail.com>
1 parent ba12bb1 commit cf18ea0

1 file changed

Lines changed: 29 additions & 11 deletions

File tree

test/node-test/balanced-pool.js

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,30 @@ const cases = [
568568

569569
]
570570

571+
function errorMatchesPort (err, port) {
572+
if (err?.port === port || err?.socket?.remotePort === port) {
573+
return true
574+
}
575+
576+
if (Array.isArray(err?.errors)) {
577+
return err.errors.some(err => errorMatchesPort(err, port))
578+
}
579+
580+
return false
581+
}
582+
583+
function getRequestErrorCode (err) {
584+
if (err?.code === 'ECONNREFUSED' || err?.code === 'UND_ERR_SOCKET') {
585+
return err.code
586+
}
587+
588+
if (Array.isArray(err?.errors)) {
589+
return err.errors.map(getRequestErrorCode).find(code => code === 'ECONNREFUSED' || code === 'UND_ERR_SOCKET')
590+
}
591+
592+
return err?.code
593+
}
594+
571595
describe('weighted round robin', () => {
572596
for (const [index, { config, expected, expectedRatios, iterations = 9, expectedConnectionRefusedErrors = 0, expectedSocketErrors = 0, maxWeightPerServer, errorPenalty = 10, skip = false }] of cases.entries()) {
573597
test(`case ${index}`, { skip }, async (t) => {
@@ -604,23 +628,17 @@ describe('weighted round robin', () => {
604628
try {
605629
await client.request({ path: '/', method: 'GET' })
606630
} catch (e) {
607-
const serverWithError =
608-
servers.find(server => server.port === e.port) ||
609-
servers.find(server => {
610-
if (typeof AggregateError === 'function' && e instanceof AggregateError) {
611-
return e.errors.some(e => server.port === (e.socket?.remotePort ?? e.port))
612-
}
613-
614-
return server.port === e.socket.remotePort
615-
})
631+
const serverWithError = servers.find(server => errorMatchesPort(e, server.port))
632+
assert.ok(serverWithError, `expected request error to match one of the test servers, got ${e.code}`)
616633

617634
serverWithError.requestsCount++
618635

619-
if (e.code === 'ECONNREFUSED') {
636+
const errorCode = getRequestErrorCode(e)
637+
if (errorCode === 'ECONNREFUSED') {
620638
requestLog.push(`${serverWithError.name}/connectionRefused`)
621639
connectionRefusedErrors++
622640
}
623-
if (e.code === 'UND_ERR_SOCKET') {
641+
if (errorCode === 'UND_ERR_SOCKET') {
624642
requestLog.push(`${serverWithError.name}/socketError`)
625643

626644
socketErrors++

0 commit comments

Comments
 (0)