Skip to content

Commit fe7541a

Browse files
author
Henry Mollman
committed
Added additional tests, removed unnecessary messages in logs
1 parent f193650 commit fe7541a

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

lib/models/mongo/mongoose-control.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,15 @@ mongooseControl.stop = function (cb) {
7373
}
7474

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

8080
mongooseControl._exitIfFailedToReconnect = function() {
81-
log.error({message: 'Lost connection to ' + process.env.MONGO})
81+
log.error({message: 'Failed to connect to ' + process.env.MONGO + ' failed to establish a connection to mongodb'})
8282
setTimeout(function () {
8383
if (!mongoose.connection.readyState) {
84+
log.fatal({message: 'Exiting nodejs process due to mongodb connection failure'})
8485
process.exit(1)
8586
}
8687
}, 10000)

unit/models/mongo/mongoose-control.js

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var Code = require('code')
1111
var fs = require('fs')
1212
var mongoose = require('mongoose')
1313
var sinon = require('sinon')
14+
var clock
1415

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

@@ -108,11 +109,20 @@ describe('mongoose-control', function () {
108109

109110
beforeEach(function (done) {
110111
sinon.stub(mongoose.connection, 'on').yields()
112+
sinon.stub(process, 'exit')
111113
sinon.stub(mongooseControl, '_exitIfFailedToReconnect')
112114
sinon.stub(mongooseControl, '_exitIfFailedToOpen')
113115
done()
114116
})
115117

118+
afterEach(function (done) {
119+
mongooseControl._exitIfFailedToReconnect.restore()
120+
mongooseControl._exitIfFailedToOpen.restore()
121+
mongoose.connection.on.restore()
122+
process.exit.restore()
123+
done()
124+
})
125+
116126
it('should exit if it cannot connect', function (done) {
117127
mongooseControl.start(function (err) {
118128
expect(err).to.not.exist()
@@ -124,13 +134,45 @@ describe('mongoose-control', function () {
124134

125135
it('should attempt a retry if connection existed', function (done) {
126136
mongoose.connection._hasOpened = true
127-
mongooseControl.start(function (err) {
128-
expect(err).to.not.exist()
129-
sinon.assert.notCalled(mongooseControl._exitIfFailedToOpen)
130-
sinon.assert.calledOnce(mongooseControl._exitIfFailedToReconnect)
131-
done()
132-
})
137+
mongooseControl.start(function (err) {
138+
expect(err).to.not.exist()
139+
sinon.assert.notCalled(mongooseControl._exitIfFailedToOpen)
140+
sinon.assert.calledOnce(mongooseControl._exitIfFailedToReconnect)
141+
done()
133142
})
143+
})
144+
})
145+
146+
describe('exiting node process when db disconnects', function () {
147+
148+
beforeEach(function (done) {
149+
clock = sinon.useFakeTimers()
150+
sinon.stub(mongoose.connection, 'on').yields()
151+
sinon.stub(process, 'exit')
152+
done()
153+
})
154+
155+
afterEach(function (done) {
156+
mongoose.connection.on.restore()
157+
process.exit.restore()
158+
clock.restore()
159+
done()
160+
})
161+
162+
it('should exit immediately if it cannot connect', function (done) {
163+
mongooseControl._exitIfFailedToOpen()
164+
sinon.assert.calledOnce(process.exit)
165+
done()
166+
})
167+
168+
it('should attempt to reconnect when it was connected once', function (done) {
169+
mongooseControl._exitIfFailedToReconnect()
170+
clock.tick(1000)
171+
sinon.assert.notCalled(process.exit)
172+
clock.tick(10000)
173+
sinon.assert.calledOnce(process.exit)
174+
done()
175+
})
134176
})
135177
})
136178
})

0 commit comments

Comments
 (0)