Skip to content

Commit

Permalink
test: fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jul 25, 2024
1 parent 2e002b7 commit 14f345a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 26 deletions.
5 changes: 4 additions & 1 deletion test/connection/connection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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++
})

Expand Down
17 changes: 10 additions & 7 deletions test/database/db_check.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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()
})
})
52 changes: 34 additions & 18 deletions test/database/db_connection_count_check.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand All @@ -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
})
Expand All @@ -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 () => {
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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')
})

0 comments on commit 14f345a

Please sign in to comment.