@@ -214,13 +214,50 @@ describe('SnapshotRequest', function () {
214
214
it ( 'can drop its connection and reconnect, and the callback is just called once' , function ( done ) {
215
215
var connection = backend . connect ( ) ;
216
216
217
- connection . fetchSnapshot ( 'books' , 'don-quixote' , function ( error ) {
218
- if ( error ) return done ( error ) ;
219
- done ( ) ;
217
+ // Here we hook into middleware to make sure that we get the following flow:
218
+ // - Connection established
219
+ // - Connection attempts to fetch a snapshot
220
+ // - Snapshot is about to be returned
221
+ // - Connection is dropped before the snapshot is returned
222
+ // - Connection is re-established
223
+ // - Connection re-requests the snapshot
224
+ // - This time the fetch operation is allowed to complete (because of the connectionInterrupted flag)
225
+ // - The done callback is called just once (if it's called twice, then mocha will complain)
226
+ var connectionInterrupted = false ;
227
+ backend . use ( backend . MIDDLEWARE_ACTIONS . readSnapshots , function ( request , callback ) {
228
+ if ( ! connectionInterrupted ) {
229
+ connection . close ( ) ;
230
+ backend . connect ( connection ) ;
231
+ connectionInterrupted = true ;
232
+ }
233
+
234
+ callback ( ) ;
235
+ } ) ;
236
+
237
+ connection . fetchSnapshot ( 'books' , 'don-quixote' , done ) ;
238
+ } ) ;
239
+
240
+ it ( 'cannot send the same request twice over a connection' , function ( done ) {
241
+ var connection = backend . connect ( ) ;
242
+
243
+ // Here we hook into the middleware to make sure that we get the following flow:
244
+ // - Attempt to fetch a snapshot
245
+ // - The snapshot request is temporarily stored on the Connection
246
+ // - Snapshot is about to be returned (ie the request was already successfully sent)
247
+ // - We attempt to resend the request again
248
+ // - The done callback is call just once, because the second request does not get sent
249
+ // (if the done callback is called twice, then mocha will complain)
250
+ var hasResent = false ;
251
+ backend . use ( backend . MIDDLEWARE_ACTIONS . readSnapshots , function ( request , callback ) {
252
+ if ( ! hasResent ) {
253
+ connection . _snapshotRequests [ 1 ] . _onConnectionStateChanged ( ) ;
254
+ hasResent = true ;
255
+ }
256
+
257
+ callback ( ) ;
220
258
} ) ;
221
259
222
- connection . close ( ) ;
223
- backend . connect ( connection ) ;
260
+ connection . fetchSnapshot ( 'books' , 'don-quixote' , done ) ;
224
261
} ) ;
225
262
226
263
describe ( 'readSnapshots middleware' , function ( ) {
0 commit comments