@@ -11,6 +11,7 @@ var Code = require('code')
11
11
var fs = require ( 'fs' )
12
12
var mongoose = require ( 'mongoose' )
13
13
var sinon = require ( 'sinon' )
14
+ var clock
14
15
15
16
var mongooseControl = require ( 'models/mongo/mongoose-control' )
16
17
@@ -108,11 +109,20 @@ describe('mongoose-control', function () {
108
109
109
110
beforeEach ( function ( done ) {
110
111
sinon . stub ( mongoose . connection , 'on' ) . yields ( )
112
+ sinon . stub ( process , 'exit' )
111
113
sinon . stub ( mongooseControl , '_exitIfFailedToReconnect' )
112
114
sinon . stub ( mongooseControl , '_exitIfFailedToOpen' )
113
115
done ( )
114
116
} )
115
117
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
+
116
126
it ( 'should exit if it cannot connect' , function ( done ) {
117
127
mongooseControl . start ( function ( err ) {
118
128
expect ( err ) . to . not . exist ( )
@@ -124,13 +134,45 @@ describe('mongoose-control', function () {
124
134
125
135
it ( 'should attempt a retry if connection existed' , function ( done ) {
126
136
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 ( )
133
142
} )
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
+ } )
134
176
} )
135
177
} )
136
178
} )
0 commit comments