From 14f345a01cfd4667e6f1c61060203a955733f9e1 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Thu, 25 Jul 2024 10:52:02 +0530 Subject: [PATCH] test: fix failing tests --- test/connection/connection.spec.ts | 5 +- test/database/db_check.spec.ts | 17 +++--- .../db_connection_count_check.spec.ts | 52 ++++++++++++------- 3 files changed, 48 insertions(+), 26 deletions(-) diff --git a/test/connection/connection.spec.ts b/test/connection/connection.spec.ts index e49d4162..d1922ce0 100644 --- a/test/connection/connection.spec.ts +++ b/test/connection/connection.spec.ts @@ -144,7 +144,10 @@ test.group('Connection | setup', (group) => { const connection = new Connection('primary', config, logger) connection.connect() - connection.on('disconnect', () => { + connection.readPool?.on('poolDestroySuccess', () => { + disconnectEmitsCount++ + }) + connection.pool?.on('poolDestroySuccess', () => { disconnectEmitsCount++ }) diff --git a/test/database/db_check.spec.ts b/test/database/db_check.spec.ts index e1369b44..5266e372 100644 --- a/test/database/db_check.spec.ts +++ b/test/database/db_check.spec.ts @@ -22,26 +22,27 @@ test.group('Db connection check', (group) => { await cleanup() }) - test('perform health check for a connection', async ({ assert }) => { + test('perform health check for a connection', async ({ assert, cleanup: teardown }) => { const config = { connection: 'primary', connections: { primary: getConfig() }, } const db = new Database(config, logger, createEmitter()) + teardown(async () => { + await db.manager.closeAll() + }) const healthCheck = new DbCheck(db.connection()) const result = await healthCheck.run() assert.containsSubset(result, { message: 'Successfully connected to the database server', status: 'ok', - meta: { connection: { name: 'primary', dialect: config.connections.primary.client } }, + meta: { connection: { name: 'primary', dialect: db.connection().dialect.name } }, }) - - await db.manager.closeAll() }) - test('report error when unable to connect', async ({ assert }) => { + test('report error when unable to connect', async ({ assert, cleanup: teardown }) => { const config = { connection: 'primary', connections: { @@ -56,16 +57,18 @@ test.group('Db connection check', (group) => { } const db = new Database(config, logger, createEmitter()) + teardown(async () => { + await db.manager.closeAll() + }) const healthCheck = new DbCheck(db.connection()) const result = await healthCheck.run() + assert.containsSubset(result, { message: 'Connection failed', status: 'error', meta: { connection: { name: 'primary', dialect: 'mysql' } }, }) assert.equal(result.meta?.error.code, 'ECONNREFUSED') - - await db.manager.closeAll() }) }) diff --git a/test/database/db_connection_count_check.spec.ts b/test/database/db_connection_count_check.spec.ts index e8865643..0bfa8efb 100644 --- a/test/database/db_connection_count_check.spec.ts +++ b/test/database/db_connection_count_check.spec.ts @@ -22,15 +22,21 @@ test.group('Db connection count check', (group) => { await cleanup() }) - test('return error when failure threshold has been crossed', async ({ assert }) => { + test('return error when failure threshold has been crossed', async ({ + assert, + cleanup: teardown, + }) => { const config = { connection: 'primary', connections: { primary: getConfig() }, } const db = new Database(config, logger, createEmitter()) - const client = db.connection() + teardown(async () => { + await db.manager.closeAll() + }) + const client = db.connection() const healthCheck = new DbConnectionCountCheck(client).compute(async () => { return 20 }) @@ -48,19 +54,23 @@ test.group('Db connection count check', (group) => { }, }, }) - - await db.manager.closeAll() }) - test('return warning when warning threshold has been crossed', async ({ assert }) => { + test('return warning when warning threshold has been crossed', async ({ + assert, + cleanup: teardown, + }) => { const config = { connection: 'primary', connections: { primary: getConfig() }, } const db = new Database(config, logger, createEmitter()) - const client = db.connection() + teardown(async () => { + await db.manager.closeAll() + }) + const client = db.connection() const healthCheck = new DbConnectionCountCheck(client).compute(async () => { return 12 }) @@ -78,17 +88,22 @@ test.group('Db connection count check', (group) => { }, }, }) - - await db.manager.closeAll() }) - test('return success when unable to compute connections count', async ({ assert }) => { + test('return success when unable to compute connections count', async ({ + assert, + cleanup: teardown, + }) => { const config = { connection: 'primary', connections: { primary: getConfig() }, } const db = new Database(config, logger, createEmitter()) + teardown(async () => { + await db.manager.closeAll() + }) + const client = db.connection() const healthCheck = new DbConnectionCountCheck(client).compute(async () => { @@ -103,17 +118,19 @@ test.group('Db connection count check', (group) => { connection: { name: 'primary', dialect: client.dialect.name }, }, }) - - await db.manager.closeAll() }) - test('get PostgreSQL connections count', async ({ assert }) => { + test('get PostgreSQL connections count', async ({ assert, cleanup: teardown }) => { const config = { connection: 'primary', connections: { primary: getConfig() }, } const db = new Database(config, logger, createEmitter()) + teardown(async () => { + await db.manager.closeAll() + }) + const client = db.connection() const healthCheck = new DbConnectionCountCheck(client) @@ -133,19 +150,20 @@ test.group('Db connection count check', (group) => { }, }, }) - - await db.manager.closeAll() }).skip(process.env.DB !== 'pg', 'Only for PostgreSQL') - test('get MySQL connections count', async ({ assert }) => { + test('get MySQL connections count', async ({ assert, cleanup: teardown }) => { const config = { connection: 'primary', connections: { primary: getConfig() }, } const db = new Database(config, logger, createEmitter()) - const client = db.connection() + teardown(async () => { + await db.manager.closeAll() + }) + const client = db.connection() const healthCheck = new DbConnectionCountCheck(client) const result = await healthCheck.run() @@ -163,7 +181,5 @@ test.group('Db connection count check', (group) => { }, }, }) - - await db.manager.closeAll() }).skip(!['mysql', 'mysql_legacy'].includes(process.env.DB!), 'Only for MySQL') })