Skip to content

Commit e45010c

Browse files
authored
Merge pull request #245 from share/snapshot-request-test
Improve test coverage for `SnapshotRequest.onConnectionStateChanged`
2 parents c23ab1a + 70cb681 commit e45010c

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

lib/client/snapshot-request.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,12 @@ SnapshotRequest.prototype.send = function () {
4444
};
4545

4646
SnapshotRequest.prototype._onConnectionStateChanged = function () {
47-
if (this.connection.canSend && !this.sent) {
48-
this.send();
49-
} else if (!this.connection.canSend) {
47+
if (this.connection.canSend) {
48+
if (!this.sent) this.send();
49+
} else {
50+
// If the connection can't send, then we've had a disconnection, and even if we've already sent
51+
// the request previously, we need to re-send it over this reconnected client, so reset the
52+
// sent flag to false.
5053
this.sent = false;
5154
}
5255
};

test/client/snapshot-request.js

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,50 @@ describe('SnapshotRequest', function () {
214214
it('can drop its connection and reconnect, and the callback is just called once', function (done) {
215215
var connection = backend.connect();
216216

217-
connection.fetchSnapshot('books', 'don-quixote', function (error) {
218-
if (error) return done(error);
219-
done();
217+
// Here we hook into middleware to make sure that we get the following flow:
218+
// - Connection established
219+
// - Connection attempts to fetch a snapshot
220+
// - Snapshot is about to be returned
221+
// - Connection is dropped before the snapshot is returned
222+
// - Connection is re-established
223+
// - Connection re-requests the snapshot
224+
// - This time the fetch operation is allowed to complete (because of the connectionInterrupted flag)
225+
// - The done callback is called just once (if it's called twice, then mocha will complain)
226+
var connectionInterrupted = false;
227+
backend.use(backend.MIDDLEWARE_ACTIONS.readSnapshots, function (request, callback) {
228+
if (!connectionInterrupted) {
229+
connection.close();
230+
backend.connect(connection);
231+
connectionInterrupted = true;
232+
}
233+
234+
callback();
235+
});
236+
237+
connection.fetchSnapshot('books', 'don-quixote', done);
238+
});
239+
240+
it('cannot send the same request twice over a connection', function (done) {
241+
var connection = backend.connect();
242+
243+
// Here we hook into the middleware to make sure that we get the following flow:
244+
// - Attempt to fetch a snapshot
245+
// - The snapshot request is temporarily stored on the Connection
246+
// - Snapshot is about to be returned (ie the request was already successfully sent)
247+
// - We attempt to resend the request again
248+
// - The done callback is call just once, because the second request does not get sent
249+
// (if the done callback is called twice, then mocha will complain)
250+
var hasResent = false;
251+
backend.use(backend.MIDDLEWARE_ACTIONS.readSnapshots, function (request, callback) {
252+
if (!hasResent) {
253+
connection._snapshotRequests[1]._onConnectionStateChanged();
254+
hasResent = true;
255+
}
256+
257+
callback();
220258
});
221259

222-
connection.close();
223-
backend.connect(connection);
260+
connection.fetchSnapshot('books', 'don-quixote', done);
224261
});
225262

226263
describe('readSnapshots middleware', function () {

0 commit comments

Comments
 (0)