@@ -416,6 +416,37 @@ describe( 'Channel', function() {
416416 channel . handleMessage ( 'c:' + JSON . stringify ( [ { error : 405 , id : 'thing' , ccids : [ 'abc' ] } ] ) ) ;
417417 } ) ;
418418
419+ it ( 'should stop sending duplicate changes after receiving a 409' , done => {
420+ const change = { o : 'M' , id : 'thing' , sv : 1 , ccid : 'duplicate' , v : diff ( { } , { key : 'value' } ) } ;
421+
422+ channel . localQueue . queue ( change ) ;
423+
424+ channel . once ( 'send' , ( ) => {
425+ // we should sent out our change the first time
426+ bucket . once ( 'error' , done ) ;
427+ channel . localQueue . once ( 'queued' , ( ) => done ( 'Should not queue duplicate changes' ) ) ;
428+ channel . once ( 'acknowledge' , ( ) => done ( ) ) ;
429+
430+ channel . handleMessage ( 'c:' + JSON . stringify ( [ {
431+ id : 'thing' ,
432+ error : 409 ,
433+ ccids : [ 'duplicate' ]
434+ } ] ) ) ;
435+ } ) ;
436+ } ) ;
437+
438+ it ( 'should acknowledge sent changes when receiving 409' , ( done ) => {
439+ channel . localQueue . sent [ 'mock-id' ] = { fake : 'change' , ccid : 'dup-ccid' , id : 'mock-id' } ;
440+
441+ bucket . once ( 'error' , done ) ;
442+
443+ // when we get the 409 we should acknowledge the change and clear the sent queue
444+ channel . once ( 'sent' , ( ) => done ( 'Should not send a duplicate change' ) ) ;
445+ channel . once ( 'acknowledge' , ( ) => done ( ) ) ;
446+
447+ channel . handleMessage ( 'c:[{"error": 409, "ccids":["dup-ccid"], "id": "mock-id"}]' ) ;
448+ } ) ;
449+
419450 describe ( 'with synced object' , ( ) => {
420451 beforeEach ( ( done ) => {
421452 var data = { title : 'hola mundo' } ;
@@ -437,37 +468,6 @@ describe( 'Channel', function() {
437468 done ( )
438469 } ) ;
439470 } ) ;
440-
441- it ( 'should handle a 409' , ( done ) => {
442- const expectedChange = { fake : 'change' , ccid : 'dup-ccid' , id : 'mock-id' } ;
443- channel . localQueue . sent [ 'mock-id' ] = { fake : 'change' , ccid : 'dup-ccid' , id : 'mock-id' } ;
444-
445- /**
446- * If the 409 is not handled the bucket will error
447- */
448- bucket . once ( 'error' , ( e ) => {
449- done ( e ) ;
450- } ) ;
451-
452- /**
453- * After successfully handling the 409 we should have an acknowledged
454- * local change matching the duplicated ccid error.
455- */
456- channel . once ( 'acknowledge' , ( id , change ) => {
457- try {
458- equal ( id , 'mock-id' ) ;
459- deepEqual ( expectedChange , change )
460- done ( ) ;
461- } catch ( error ) {
462- done ( error ) ;
463- }
464- } ) ;
465-
466- /**
467- * Simulate receiving a 409
468- */
469- channel . handleMessage ( 'c:[{"error": 409, "ccids":["dup-ccid"], "id": "mock-id"}]' ) ;
470- } ) ;
471471 } ) ;
472472 } ) ;
473473
0 commit comments