Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/client/snapshot-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,12 @@ SnapshotRequest.prototype.send = function () {
};

SnapshotRequest.prototype._onConnectionStateChanged = function () {
if (this.connection.canSend && !this.sent) {
this.send();
} else if (!this.connection.canSend) {
if (this.connection.canSend) {
if (!this.sent) this.send();
} else {
// If the connection can't send, then we've had a disconnection, and even if we've already sent
// the request previously, we need to re-send it over this reconnected client, so reset the
// sent flag to false.
this.sent = false;
}
};
Expand Down
47 changes: 42 additions & 5 deletions test/client/snapshot-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,50 @@ describe('SnapshotRequest', function () {
it('can drop its connection and reconnect, and the callback is just called once', function (done) {
var connection = backend.connect();

connection.fetchSnapshot('books', 'don-quixote', function (error) {
if (error) return done(error);
done();
// Here we hook into middleware to make sure that we get the following flow:
// - Connection established
// - Connection attempts to fetch a snapshot
// - Snapshot is about to be returned
// - Connection is dropped before the snapshot is returned
// - Connection is re-established
// - Connection re-requests the snapshot
// - This time the fetch operation is allowed to complete (because of the connectionInterrupted flag)
// - The done callback is called just once (if it's called twice, then mocha will complain)
var connectionInterrupted = false;
backend.use(backend.MIDDLEWARE_ACTIONS.readSnapshots, function (request, callback) {
if (!connectionInterrupted) {
connection.close();
backend.connect(connection);
connectionInterrupted = true;
}

callback();
});

connection.fetchSnapshot('books', 'don-quixote', done);
});

it('cannot send the same request twice over a connection', function (done) {
var connection = backend.connect();

// Here we hook into the middleware to make sure that we get the following flow:
// - Attempt to fetch a snapshot
// - The snapshot request is temporarily stored on the Connection
// - Snapshot is about to be returned (ie the request was already successfully sent)
// - We attempt to resend the request again
// - The done callback is call just once, because the second request does not get sent
// (if the done callback is called twice, then mocha will complain)
var hasResent = false;
backend.use(backend.MIDDLEWARE_ACTIONS.readSnapshots, function (request, callback) {
if (!hasResent) {
connection._snapshotRequests[1]._onConnectionStateChanged();
hasResent = true;
}

callback();
});

connection.close();
backend.connect(connection);
connection.fetchSnapshot('books', 'don-quixote', done);
});

describe('readSnapshots middleware', function () {
Expand Down