Skip to content

Commit

Permalink
Add a unit test for connecting to legacy clients
Browse files Browse the repository at this point in the history
This change adds a "legacy" version of ShareDB as a development
dependency so that we can unit test the new handshake logic against old
clients.
  • Loading branch information
Alec Gibson committed Feb 13, 2020
1 parent f3ebed3 commit bb17e13
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"lolex": "^5.1.1",
"mocha": "^6.2.2",
"nyc": "^14.1.1",
"sharedb-legacy": "npm:sharedb@=1.1.0",
"sinon": "^7.5.0"
},
"scripts": {
Expand Down
27 changes: 27 additions & 0 deletions test/client/connection.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var expect = require('chai').expect;
var Backend = require('../../lib/backend');
var Connection = require('../../lib/client/connection');
var LegacyConnection = require('sharedb-legacy/lib/client').Connection;
var StreamSocket = require('../../lib/stream-socket');

describe('client connection', function() {
beforeEach(function() {
Expand Down Expand Up @@ -217,6 +219,31 @@ describe('client connection', function() {
});
});

it('still connects to legacy clients, whose ID changes on reconnection', function(done) {
var currentBackend = this.backend;
var socket = new StreamSocket();
var legacyClient = new LegacyConnection(socket);
currentBackend.connect(legacyClient);

var doc = legacyClient.get('test', '123');
doc.create({foo: 'bar'}, function(error) {
if (error) return done(error);
var initialId = legacyClient.id;
expect(initialId).to.equal(legacyClient.agent.clientId);
expect(legacyClient.agent.src).to.be.null;
legacyClient.close();
currentBackend.connect(legacyClient);
doc.submitOp({p: ['baz'], oi: 'qux'}, function(error) {
if (error) return done(error);
var newId = legacyClient.id;
expect(newId).not.to.equal(initialId);
expect(newId).to.equal(legacyClient.agent.clientId);
expect(legacyClient.agent.src).to.be.null;
done();
});
});
});

it('errors when submitting an op with a very large seq', function(done) {
this.backend.connect(null, null, function(connection) {
var doc = connection.get('test', '123');
Expand Down

0 comments on commit bb17e13

Please sign in to comment.