Skip to content

Commit

Permalink
fix: order of event arguments on the connection class
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Sep 30, 2019
1 parent cd9d372 commit 2e6c317
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
6 changes: 3 additions & 3 deletions adonis-typings/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ declare module '@ioc:Adonis/Lucid/Database' {
/**
* When error is received on a given connection
*/
on (event: 'error', callback: (connection: ConnectionContract, error: Error) => void): this
on (event: 'error', callback: (error: Error, connection: ConnectionContract) => void): this

/**
* Add a new connection to the list of managed connection. You must call
Expand Down Expand Up @@ -504,9 +504,9 @@ declare module '@ioc:Adonis/Lucid/Database' {
* List of emitted events
*/
on (event: 'connect', callback: (connection: ConnectionContract) => void): this
on (event: 'error', callback: (connection: ConnectionContract, error: Error) => void): this
on (event: 'error', callback: (error: Error, connection: ConnectionContract) => void): this
on (event: 'disconnect', callback: (connection: ConnectionContract) => void): this
on (event: 'disconnect:error', callback: (connection: ConnectionContract, error: Error) => void): this
on (event: 'disconnect:error', callback: (error: Error, connection: ConnectionContract) => void): this

/**
* Make knex connection
Expand Down
4 changes: 2 additions & 2 deletions src/Connection/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ export class ConnectionManager extends EventEmitter implements ConnectionManager
/**
* Listens for error event to proxy it to the client
*/
connection.on('error', ($connection, error) => {
this.emit('error', $connection, error)
connection.on('error', (error, $connection) => {
this.emit('error', error, $connection)
})
}

Expand Down
50 changes: 50 additions & 0 deletions test/connection/connection-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import test from 'japa'

import { Connection } from '../../src/Connection'
import { ConnectionManager } from '../../src/Connection/Manager'
import { getConfig, setup, cleanup, getLogger } from '../../test-helpers'

Expand Down Expand Up @@ -96,4 +97,53 @@ test.group('ConnectionManager', (group) => {
await manager.release('primary')
assert.isFalse(manager.has('primary'))
})

test('proxy connect event', (assert, done) => {
assert.plan(1)

const manager = new ConnectionManager(getLogger())
manager.add('primary', getConfig())

manager.on('connect', (connection) => {
assert.instanceOf(connection, Connection)
done()
})

manager.connect('primary')
})

test('proxy disconnect event', async (assert, done) => {
assert.plan(1)

const manager = new ConnectionManager(getLogger())
manager.add('primary', getConfig())

manager.on('disconnect', (connection) => {
assert.instanceOf(connection, Connection)
done()
})

manager.connect('primary')
await manager.close('primary')
})

test('proxy error event', async (assert, done) => {
assert.plan(3)

const manager = new ConnectionManager(getLogger())
manager.add('primary', Object.assign({}, getConfig(), { client: null }))

manager.on('error', ({ message }, connection) => {
try {
assert.equal(message, 'knex: Required configuration option \'client\' is missing.')
assert.instanceOf(connection, Connection)
done()
} catch (error) {
done(error)
}
})

const fn = () => manager.connect('primary')
assert.throw(fn, /knex: Required configuration option/)
})
})
13 changes: 12 additions & 1 deletion test/connection/connection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,24 @@ test.group('Connection | setup', (group) => {
await connection.disconnect()
})

test('raise error when unable to make connection', (assert) => {
test('raise error when unable to make connection', (assert, done) => {
assert.plan(2)

const connection = new Connection(
'primary',
Object.assign({}, getConfig(), { client: null }),
getLogger(),
)

connection.on('error', ({ message }) => {
try {
assert.equal(message, 'knex: Required configuration option \'client\' is missing.')
done()
} catch (error) {
done(error)
}
})

const fn = () => connection.connect()
assert.throw(fn, /knex: Required configuration option/)
})
Expand Down

0 comments on commit 2e6c317

Please sign in to comment.