Skip to content

Commit 51ced6e

Browse files
authored
Merge pull request #73 from Simperium/fix/stop-re-sending-duplicate-changes
Test: Stop re-sending duplicate changes
2 parents 671b3df + e4d5f70 commit 51ced6e

File tree

3 files changed

+83
-50
lines changed

3 files changed

+83
-50
lines changed

package-lock.json

Lines changed: 51 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/simperium/channel.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ internal.handleChangeError = function( err, change, acknowledged ) {
240240

241241
break;
242242
case CODE_DUPLICATE_CHANGE:
243+
// no need to do anything else here - we already got this
243244
internal.updateAcknowledged.call( this, acknowledged );
244245
break;
245246
case CODE_EMPTY_RESPONSE: // Change causes no change, just acknowledge it

test/simperium/channel_test.js

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)