@@ -519,16 +519,28 @@ describe('client submit', function() {
519
519
if ( err ) return done ( err ) ;
520
520
var docCallback ;
521
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.
522
528
backend . use ( 'commit' , function ( request , callback ) {
523
529
if ( request . op . op [ 0 ] . na === 2 ) docCallback = callback ;
524
530
if ( request . op . op [ 0 ] . na === 7 ) doc2Callback = callback ;
525
531
532
+ // Wait until both ops have been applied to the same snapshot and are about to be committed
526
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
527
536
docCallback ( ) ;
528
537
}
529
538
} ) ;
530
539
doc . submitOp ( { p : [ 'age' ] , na : 2 } , function ( error ) {
531
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
532
544
doc2Callback ( ) ;
533
545
} ) ;
534
546
doc2 . submitOp ( { p : [ 'age' ] , na : 7 } , function ( error ) {
0 commit comments