Skip to content

Commit

Permalink
Fix a bunch of races in the tests
Browse files Browse the repository at this point in the history
Once we switch to bluebird, suddenly a load of timing issues come out of the
woodwork. Basically, we need to try harder when flushing requests. Bump to
matrix-mock-request 1.1.0, which provides `flushAllExpected`, and waits for
requests to arrive when given a `numToFlush`; then use `flushAllExpected` in
various places to make the tests more resilient.
  • Loading branch information
richvdh committed Jul 11, 2017
1 parent b2c7804 commit 0ca4d72
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 59 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"istanbul": "^0.4.5",
"jsdoc": "^3.4.0",
"lolex": "^1.5.2",
"matrix-mock-request": "^1.0.0",
"matrix-mock-request": "^1.1.0",
"mocha": "^3.2.0",
"mocha-jenkins-reporter": "^0.3.6",
"rimraf": "^2.5.4",
Expand Down
2 changes: 1 addition & 1 deletion spec/TestClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ TestClient.prototype.start = function() {
pendingEventOrdering: 'detached',
});

return this.httpBackend.flush().then(() => {
return this.httpBackend.flushAllExpected().then(() => {
console.log(this + ': started');
});
};
Expand Down
4 changes: 2 additions & 2 deletions spec/integ/matrix-client-crypto.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ function expectAliClaimKeys() {
// it can take a while to process the key query, so give it some extra
// time, and make sure the claim actually happens rather than ploughing on
// confusingly.
return aliTestClient.httpBackend.flush("/keys/claim", 1, 20).then((r) => {
expect(r).toEqual(1);
return aliTestClient.httpBackend.flush("/keys/claim", 1).then((r) => {
expect(r).toEqual(1, "Ali did not claim Bob's keys");
});
});
}
Expand Down
12 changes: 6 additions & 6 deletions spec/integ/matrix-client-event-emitter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe("MatrixClient events", function() {

client.startClient();

httpBackend.flush().done(function() {
httpBackend.flushAllExpected().done(function() {
expect(expectedEvents.length).toEqual(
0, "Failed to see all events from /sync calls",
);
Expand All @@ -157,7 +157,7 @@ describe("MatrixClient events", function() {
});
client.startClient();

httpBackend.flush().done(function() {
httpBackend.flushAllExpected().done(function() {
expect(fired).toBe(true, "User.presence didn't fire.");
done();
});
Expand All @@ -183,7 +183,7 @@ describe("MatrixClient events", function() {

client.startClient();

httpBackend.flush().done(function() {
httpBackend.flushAllExpected().done(function() {
expect(roomInvokeCount).toEqual(
1, "Room fired wrong number of times.",
);
Expand Down Expand Up @@ -232,7 +232,7 @@ describe("MatrixClient events", function() {

client.startClient();

httpBackend.flush().done(function() {
httpBackend.flushAllExpected().done(function() {
expect(membersInvokeCount).toEqual(
1, "RoomState.members fired wrong number of times",
);
Expand Down Expand Up @@ -271,7 +271,7 @@ describe("MatrixClient events", function() {

client.startClient();

httpBackend.flush().done(function() {
httpBackend.flushAllExpected().done(function() {
expect(typingInvokeCount).toEqual(
1, "RoomMember.typing fired wrong number of times",
);
Expand All @@ -298,7 +298,7 @@ describe("MatrixClient events", function() {

client.startClient();

httpBackend.flush().done(function() {
httpBackend.flushAllExpected().done(function() {
expect(sessionLoggedOutCount).toEqual(
1, "Session.logged_out fired wrong number of times",
);
Expand Down
37 changes: 19 additions & 18 deletions spec/integ/matrix-client-syncing.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe("MatrixClient syncing", function() {

client.startClient();

httpBackend.flush().done(function() {
httpBackend.flushAllExpected().done(function() {
done();
});
});
Expand All @@ -66,7 +66,7 @@ describe("MatrixClient syncing", function() {

client.startClient();

httpBackend.flush().done(function() {
httpBackend.flushAllExpected().done(function() {
done();
});
});
Expand Down Expand Up @@ -133,7 +133,8 @@ describe("MatrixClient syncing", function() {
resolveInvitesToProfiles: true,
});

httpBackend.flush().done(function() {

httpBackend.flushAllExpected().done(function() {
const member = client.getRoom(roomOne).getMember(userC);
expect(member.name).toEqual("The Boss");
expect(
Expand Down Expand Up @@ -161,7 +162,7 @@ describe("MatrixClient syncing", function() {
resolveInvitesToProfiles: true,
});

httpBackend.flush().done(function() {
httpBackend.flushAllExpected().done(function() {
const member = client.getRoom(roomOne).getMember(userC);
expect(member.name).toEqual("The Ghost");
done();
Expand Down Expand Up @@ -193,7 +194,7 @@ describe("MatrixClient syncing", function() {
resolveInvitesToProfiles: true,
});

httpBackend.flush().done(function() {
httpBackend.flushAllExpected().done(function() {
expect(latestFiredName).toEqual("The Ghost");
done();
});
Expand All @@ -210,7 +211,7 @@ describe("MatrixClient syncing", function() {

client.startClient();

httpBackend.flush().done(function() {
httpBackend.flushAllExpected().done(function() {
const member = client.getRoom(roomOne).getMember(userC);
expect(member.name).toEqual(userC);
expect(
Expand Down Expand Up @@ -242,7 +243,7 @@ describe("MatrixClient syncing", function() {

client.startClient();

httpBackend.flush().done(function() {
httpBackend.flushAllExpected().done(function() {
expect(client.getUser(userA).presence).toEqual("online");
expect(client.getUser(userB).presence).toEqual("unavailable");
done();
Expand Down Expand Up @@ -362,7 +363,7 @@ describe("MatrixClient syncing", function() {

client.startClient();

httpBackend.flush().done(function() {
httpBackend.flushAllExpected().done(function() {
const room = client.getRoom(roomOne);
// should have clobbered the name to the one from /events
expect(room.name).toEqual(
Expand All @@ -378,7 +379,7 @@ describe("MatrixClient syncing", function() {

client.startClient();

httpBackend.flush().done(function() {
httpBackend.flushAllExpected().done(function() {
const room = client.getRoom(roomTwo);
// should have added the message from /events
expect(room.timeline.length).toEqual(2);
Expand All @@ -392,7 +393,7 @@ describe("MatrixClient syncing", function() {
httpBackend.when("GET", "/sync").respond(200, nextSyncData);

client.startClient();
httpBackend.flush().done(function() {
httpBackend.flushAllExpected().done(function() {
const room = client.getRoom(roomTwo);
// should use the display name of the other person.
expect(room.name).toEqual(otherDisplayName);
Expand All @@ -406,7 +407,7 @@ describe("MatrixClient syncing", function() {

client.startClient();

httpBackend.flush().done(function() {
httpBackend.flushAllExpected().done(function() {
const room = client.getRoom(roomTwo);
let member = room.getMember(otherUserId);
expect(member).toBeTruthy();
Expand Down Expand Up @@ -449,7 +450,7 @@ describe("MatrixClient syncing", function() {
httpBackend.when("GET", "/sync").respond(200, syncData);

client.startClient();
httpBackend.flush();
return httpBackend.flushAllExpected();
});

it("should set the back-pagination token on new rooms", function() {
Expand All @@ -472,7 +473,7 @@ describe("MatrixClient syncing", function() {

httpBackend.when("GET", "/sync").respond(200, syncData);

return httpBackend.flush().then(function() {
return httpBackend.flushAllExpected().then(function() {
const room = client.getRoom(roomTwo);
const tok = room.getLiveTimeline()
.getPaginationToken(EventTimeline.BACKWARDS);
Expand Down Expand Up @@ -511,7 +512,7 @@ describe("MatrixClient syncing", function() {
expect(tok).toEqual("newerTok");
});

return httpBackend.flush().then(function() {
return httpBackend.flushAllExpected().then(function() {
const room = client.getRoom(roomOne);
const tl = room.getLiveTimeline();
expect(tl.getEvents().length).toEqual(1);
Expand Down Expand Up @@ -587,7 +588,7 @@ describe("MatrixClient syncing", function() {

client.startClient();

httpBackend.flush().done(function() {
httpBackend.flushAllExpected().done(function() {
const room = client.getRoom(roomOne);
expect(room.getReceiptsForEvent(new MatrixEvent(ackEvent))).toEqual([{
type: "m.read",
Expand Down Expand Up @@ -616,7 +617,7 @@ describe("MatrixClient syncing", function() {
beforeEach(function(done) {
client.startClient();

httpBackend.flush().then(function() {
httpBackend.flushAllExpected().then(function() {
// the /sync call from syncLeftRooms ends up in the request
// queue behind the call from the running client; add a response
// to flush the client's one out.
Expand Down Expand Up @@ -647,7 +648,7 @@ describe("MatrixClient syncing", function() {
return q.all([
httpBackend.flush("/filter").then(function() {
// flush the syncs
return httpBackend.flush();
return httpBackend.flushAllExpected();
}),
defer.promise,
]);
Expand Down Expand Up @@ -690,7 +691,7 @@ describe("MatrixClient syncing", function() {
// first flush the filter request; this will make syncLeftRooms
// make its /sync call
httpBackend.flush("/filter").then(function() {
return httpBackend.flush();
return httpBackend.flushAllExpected();
}),
]);
});
Expand Down
61 changes: 30 additions & 31 deletions spec/integ/megolm-integ.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -846,36 +846,6 @@ describe("megolm", function() {
let downloadPromise;
let sendPromise;

aliceTestClient.httpBackend.when(
'PUT', '/sendToDevice/m.room.encrypted/',
).respond(200, function(path, content) {
const m = content.messages['@bob:xyz'].DEVICE_ID;
const ct = m.ciphertext[testSenderKey];
const decrypted = JSON.parse(p2pSession.decrypt(ct.type, ct.body));

expect(decrypted.type).toEqual('m.room_key');
inboundGroupSession = new Olm.InboundGroupSession();
inboundGroupSession.create(decrypted.content.session_key);
return {};
});

aliceTestClient.httpBackend.when(
'PUT', '/send/',
).respond(200, function(path, content) {
const ct = content.ciphertext;
const r = inboundGroupSession.decrypt(ct);
console.log('Decrypted received megolm message', r);

expect(r.message_index).toEqual(0);
const decrypted = JSON.parse(r.plaintext);
expect(decrypted.type).toEqual('m.room.message');
expect(decrypted.content.body).toEqual('test');

return {
event_id: '$event_id',
};
});

return aliceTestClient.start().then(() => {
// establish an olm session with alice
return createOlmSession(testOlmAccount, aliceTestClient);
Expand All @@ -893,9 +863,38 @@ describe("megolm", function() {
aliceTestClient.httpBackend.when('GET', '/sync').respond(200, syncResponse);
return aliceTestClient.flushSync();
}).then(function() {
console.log('Forcing alice to download our device keys');
aliceTestClient.httpBackend.when(
'PUT', '/sendToDevice/m.room.encrypted/',
).respond(200, function(path, content) {
const m = content.messages['@bob:xyz'].DEVICE_ID;
const ct = m.ciphertext[testSenderKey];
const decrypted = JSON.parse(p2pSession.decrypt(ct.type, ct.body));

expect(decrypted.type).toEqual('m.room_key');
inboundGroupSession = new Olm.InboundGroupSession();
inboundGroupSession.create(decrypted.content.session_key);
return {};
});

aliceTestClient.httpBackend.when(
'PUT', '/send/',
).respond(200, function(path, content) {
const ct = content.ciphertext;
const r = inboundGroupSession.decrypt(ct);
console.log('Decrypted received megolm message', r);

expect(r.message_index).toEqual(0);
const decrypted = JSON.parse(r.plaintext);
expect(decrypted.type).toEqual('m.room.message');
expect(decrypted.content.body).toEqual('test');

return {
event_id: '$event_id',
};
});

// this will block
console.log('Forcing alice to download our device keys');
downloadPromise = aliceTestClient.client.downloadKeys(['@bob:xyz']);

// so will this.
Expand Down

0 comments on commit 0ca4d72

Please sign in to comment.