@@ -509,25 +509,44 @@ describe('client submit', function() {
509
509
} ) ;
510
510
511
511
it ( 'submits fail above the backend.maxSubmitRetries threshold' , function ( done ) {
512
+ var backend = this . backend ;
512
513
this . backend . maxSubmitRetries = 0 ;
513
514
var doc = this . backend . connect ( ) . get ( 'dogs' , 'fido' ) ;
514
515
var doc2 = this . backend . connect ( ) . get ( 'dogs' , 'fido' ) ;
515
516
doc . create ( { age : 3 } , function ( err ) {
516
517
if ( err ) return done ( err ) ;
517
518
doc2 . fetch ( function ( err ) {
518
519
if ( err ) return done ( err ) ;
519
- var count = 0 ;
520
- var cb = function ( err ) {
521
- count ++ ;
522
- if ( count === 1 ) {
523
- if ( err ) return done ( err ) ;
524
- } else {
525
- expect ( err ) . ok ( ) ;
526
- done ( ) ;
520
+ var docCallback ;
521
+ var doc2Callback ;
522
+ // The submit retry happens just after an op is committed. This hook into the middleware
523
+ // catches both ops just before they're about to be committed. This ensures that both ops
524
+ // are certainly working on the same snapshot (ie one op hasn't been committed before the
525
+ // other fetches the snapshot to apply to). By storing the callbacks, we can then
526
+ // manually trigger the callbacks, first calling doc, and when we know that's been committed,
527
+ // we then commit doc2.
528
+ backend . use ( 'commit' , function ( request , callback ) {
529
+ if ( request . op . op [ 0 ] . na === 2 ) docCallback = callback ;
530
+ if ( request . op . op [ 0 ] . na === 7 ) doc2Callback = callback ;
531
+
532
+ // Wait until both ops have been applied to the same snapshot and are about to be committed
533
+ if ( docCallback && doc2Callback ) {
534
+ // Trigger the first op's commit and then the second one later, which will cause the
535
+ // second op to retry
536
+ docCallback ( ) ;
527
537
}
528
- } ;
529
- doc . submitOp ( { p : [ 'age' ] , na : 2 } , cb ) ;
530
- doc2 . submitOp ( { p : [ 'age' ] , na : 7 } , cb ) ;
538
+ } ) ;
539
+ doc . submitOp ( { p : [ 'age' ] , na : 2 } , function ( error ) {
540
+ if ( error ) return done ( error ) ;
541
+ // When we know the first op has been committed, we try to commit the second op, which will
542
+ // fail because it's working on an out-of-date snapshot. It will retry, but exceed the
543
+ // maxSubmitRetries limit of 0
544
+ doc2Callback ( ) ;
545
+ } ) ;
546
+ doc2 . submitOp ( { p : [ 'age' ] , na : 7 } , function ( error ) {
547
+ expect ( error ) . ok ( ) ;
548
+ done ( ) ;
549
+ } ) ;
531
550
} ) ;
532
551
} ) ;
533
552
} ) ;
0 commit comments