Unverified Commit 43b9aebe authored by Tim Kinnane's avatar Tim Kinnane Committed by GitHub
Browse files

Merge pull request #6 from mikaelmello/fix-driver-timeout-tests

Remove 'connected' listener after connection times out in driver.connect
parents c8c48425 43bfbbe5
...@@ -16,7 +16,7 @@ let clock ...@@ -16,7 +16,7 @@ let clock
describe('driver', () => { describe('driver', () => {
silence() // suppress log during tests (disable this while developing tests) silence() // suppress log during tests (disable this while developing tests)
before(() => utils.setup()) // add user accounts for bot and mock user before(() => utils.setup()) // add user accounts for bot and mock user
/*
describe('.connect', () => { describe('.connect', () => {
context('with localhost connection', () => { context('with localhost connection', () => {
it('without args, returns a promise', () => { it('without args, returns a promise', () => {
...@@ -91,7 +91,7 @@ describe('driver', () => { ...@@ -91,7 +91,7 @@ describe('driver', () => {
}) })
}) })
}) })
*/
// describe('disconnect', () => { // describe('disconnect', () => {
// Disabled for now, as only Asteroid v2 has a disconnect method // Disabled for now, as only Asteroid v2 has a disconnect method
// it('disconnects from asteroid', async () => { // it('disconnects from asteroid', async () => {
......
...@@ -132,20 +132,26 @@ export function connect (options: IConnectOptions = {}, callback?: ICallback): P ...@@ -132,20 +132,26 @@ export function connect (options: IConnectOptions = {}, callback?: ICallback): P
setupMethodCache(asteroid) // init instance for later caching method calls setupMethodCache(asteroid) // init instance for later caching method calls
asteroid.on('connected', () => events.emit('connected')) asteroid.on('connected', () => events.emit('connected'))
asteroid.on('reconnected', () => events.emit('reconnected')) asteroid.on('reconnected', () => events.emit('reconnected'))
// let cancelled = false let cancelled = false
const rejectionTimeout = setTimeout(function () { const rejectionTimeout = setTimeout(function () {
logger.info(`[connect] Timeout (${config.timeout})`) logger.info(`[connect] Timeout (${config.timeout})`)
// cancelled = true
const err = new Error('Asteroid connection timeout') const err = new Error('Asteroid connection timeout')
cancelled = true
events.removeAllListeners('connected')
callback ? callback(err, asteroid) : reject(err) callback ? callback(err, asteroid) : reject(err)
}, config.timeout) }, config.timeout)
events.once('connected', () => {
logger.info('[connect] Connected') // if to avoid condition where timeout happens before listener to 'connected' is added
// if (cancelled) return asteroid.ddp.disconnect() // cancel if already rejected // and this listener is not removed (because it was added after the removal)
clearTimeout(rejectionTimeout) if (!cancelled) {
if (callback) callback(null, asteroid) events.once('connected', () => {
resolve(asteroid) logger.info('[connect] Connected')
}) // if (cancelled) return asteroid.ddp.disconnect() // cancel if already rejected
clearTimeout(rejectionTimeout)
if (callback) callback(null, asteroid)
resolve(asteroid)
})
}
}) })
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment