Skip to content

Commit

Permalink
Added more unit tests and fixed linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry Mollman committed Oct 11, 2016
1 parent f193650 commit bfa69b9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
7 changes: 4 additions & 3 deletions lib/models/mongo/mongoose-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,15 @@ mongooseControl.stop = function (cb) {
}

mongooseControl._exitIfFailedToOpen = function () {
log.fatal({message: 'Failed to connect to ' + process.env.MONGO}, 'failed to establish a connection to mongodb')
log.fatal({message: 'Failed to connect to ' + process.env.MONGO + ' failed to establish a connection to mongodb'})
process.exit(1)
}

mongooseControl._exitIfFailedToReconnect = function() {
log.error({message: 'Lost connection to ' + process.env.MONGO})
mongooseControl._exitIfFailedToReconnect = function () {
log.error({message: 'Failed to connect to ' + process.env.MONGO + ' failed to establish a connection to mongodb'})
setTimeout(function () {
if (!mongoose.connection.readyState) {
log.fatal({message: 'Exiting nodejs process due to mongodb connection failure'})
process.exit(1)
}
}, 10000)
Expand Down
54 changes: 47 additions & 7 deletions unit/models/mongo/mongoose-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var Code = require('code')
var fs = require('fs')
var mongoose = require('mongoose')
var sinon = require('sinon')
var clock

var mongooseControl = require('models/mongo/mongoose-control')

Expand Down Expand Up @@ -105,14 +106,22 @@ describe('mongoose-control', function () {
})

describe('handling mongodb disconnect events', function () {

beforeEach(function (done) {
sinon.stub(mongoose.connection, 'on').yields()
sinon.stub(process, 'exit')
sinon.stub(mongooseControl, '_exitIfFailedToReconnect')
sinon.stub(mongooseControl, '_exitIfFailedToOpen')
done()
})

afterEach(function (done) {
mongooseControl._exitIfFailedToReconnect.restore()
mongooseControl._exitIfFailedToOpen.restore()
mongoose.connection.on.restore()
process.exit.restore()
done()
})

it('should exit if it cannot connect', function (done) {
mongooseControl.start(function (err) {
expect(err).to.not.exist()
Expand All @@ -124,13 +133,44 @@ describe('mongoose-control', function () {

it('should attempt a retry if connection existed', function (done) {
mongoose.connection._hasOpened = true
mongooseControl.start(function (err) {
expect(err).to.not.exist()
sinon.assert.notCalled(mongooseControl._exitIfFailedToOpen)
sinon.assert.calledOnce(mongooseControl._exitIfFailedToReconnect)
done()
})
mongooseControl.start(function (err) {
expect(err).to.not.exist()
sinon.assert.notCalled(mongooseControl._exitIfFailedToOpen)
sinon.assert.calledOnce(mongooseControl._exitIfFailedToReconnect)
done()
})
})
})

describe('exiting node process when db disconnects', function () {
beforeEach(function (done) {
clock = sinon.useFakeTimers()
sinon.stub(mongoose.connection, 'on').yields()
sinon.stub(process, 'exit')
done()
})

afterEach(function (done) {
mongoose.connection.on.restore()
process.exit.restore()
clock.restore()
done()
})

it('should exit immediately if it cannot connect', function (done) {
mongooseControl._exitIfFailedToOpen()
sinon.assert.calledOnce(process.exit)
done()
})

it('should attempt to reconnect when it was connected once', function (done) {
mongooseControl._exitIfFailedToReconnect()
clock.tick(1000)
sinon.assert.notCalled(process.exit)
clock.tick(10000)
sinon.assert.calledOnce(process.exit)
done()
})
})
})
})

0 comments on commit bfa69b9

Please sign in to comment.