@@ -11,6 +11,7 @@ var Code = require('code')
1111var fs = require ( 'fs' )
1212var mongoose = require ( 'mongoose' )
1313var sinon = require ( 'sinon' )
14+ var clock
1415
1516var 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