Skip to content

Commit 671b3df

Browse files
authored
Unit Test: 409 server error (#74)
* Unit test for handling a 409 for a change sent be the client * Add handler for duplicate change error
1 parent 62fc13e commit 671b3df

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/simperium/channel.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { v4 as uuid } from 'uuid'
66

77
const UNKNOWN_CV = '?';
88
const CODE_INVALID_VERSION = 405;
9+
const CODE_DUPLICATE_CHANGE = 409;
910
const CODE_EMPTY_RESPONSE = 412;
1011
const CODE_INVALID_DIFF = 440;
1112

@@ -237,6 +238,9 @@ internal.handleChangeError = function( err, change, acknowledged ) {
237238
this.localQueue.dequeueChangesFor( change.id );
238239
}
239240

241+
break;
242+
case CODE_DUPLICATE_CHANGE:
243+
internal.updateAcknowledged.call( this, acknowledged );
240244
break;
241245
case CODE_EMPTY_RESPONSE: // Change causes no change, just acknowledge it
242246
internal.updateAcknowledged.call( this, acknowledged );

test/simperium/channel_test.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ describe( 'Channel', function() {
420420
beforeEach( ( done ) => {
421421
var data = { title: 'hola mundo' };
422422

423-
channel.on( 'acknowledge', function() {
423+
channel.once( 'acknowledge', function() {
424424
done();
425425
} );
426426

@@ -437,6 +437,37 @@ describe( 'Channel', function() {
437437
done()
438438
} );
439439
} );
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+
} );
440471
} );
441472
} );
442473

0 commit comments

Comments
 (0)