1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-25 05:23:13 +03:00

Make tests wait for syncs to happen

Add lots of calls to `syncPromise` to cope with the fact that sync responses
are now handled asynchronously, which makes them prone to races otherwise.

Also a quick sanity-check in crypto to make one of the test failures less
cryptic.
This commit is contained in:
Richard van der Hoff
2017-08-08 10:58:19 +01:00
parent 8563dd5860
commit ab8d06bb86
7 changed files with 81 additions and 49 deletions

View File

@@ -77,7 +77,10 @@ TestClient.prototype.start = function() {
pendingEventOrdering: 'detached', pendingEventOrdering: 'detached',
}); });
return this.httpBackend.flushAllExpected().then(() => { return Promise.all([
this.httpBackend.flushAllExpected(),
testUtils.syncPromise(this.client),
]).then(() => {
console.log(this + ': started'); console.log(this + ': started');
}); });
}; };
@@ -199,5 +202,7 @@ TestClient.prototype.flushSync = function() {
return Promise.all([ return Promise.all([
this.httpBackend.flush('/sync', 1), this.httpBackend.flush('/sync', 1),
testUtils.syncPromise(this.client), testUtils.syncPromise(this.client),
]); ]).then(() => {
console.log(`${this}: flushSync completed`);
});
}; };

View File

@@ -691,7 +691,7 @@ describe("MatrixClient crypto", function() {
[bobUserId]: {}, [bobUserId]: {},
}, },
}); });
return aliTestClient.httpBackend.flush('/keys/query', 1); return aliTestClient.httpBackend.flushAllExpected();
}); });
}); });

View File

@@ -169,7 +169,7 @@ describe("MatrixClient events", function() {
}); });
}); });
it("should emit Room events", function(done) { it("should emit Room events", function() {
httpBackend.when("GET", "/sync").respond(200, SYNC_DATA); httpBackend.when("GET", "/sync").respond(200, SYNC_DATA);
httpBackend.when("GET", "/sync").respond(200, NEXT_SYNC_DATA); httpBackend.when("GET", "/sync").respond(200, NEXT_SYNC_DATA);
let roomInvokeCount = 0; let roomInvokeCount = 0;
@@ -189,7 +189,10 @@ describe("MatrixClient events", function() {
client.startClient(); client.startClient();
httpBackend.flushAllExpected().done(function() { return Promise.all([
httpBackend.flushAllExpected(),
utils.syncPromise(client, 2),
]).then(function() {
expect(roomInvokeCount).toEqual( expect(roomInvokeCount).toEqual(
1, "Room fired wrong number of times.", 1, "Room fired wrong number of times.",
); );
@@ -199,11 +202,10 @@ describe("MatrixClient events", function() {
expect(timelineFireCount).toEqual( expect(timelineFireCount).toEqual(
3, "Room.timeline fired the wrong number of times", 3, "Room.timeline fired the wrong number of times",
); );
done();
}); });
}); });
it("should emit RoomState events", function(done) { it("should emit RoomState events", function() {
httpBackend.when("GET", "/sync").respond(200, SYNC_DATA); httpBackend.when("GET", "/sync").respond(200, SYNC_DATA);
httpBackend.when("GET", "/sync").respond(200, NEXT_SYNC_DATA); httpBackend.when("GET", "/sync").respond(200, NEXT_SYNC_DATA);
@@ -238,7 +240,10 @@ describe("MatrixClient events", function() {
client.startClient(); client.startClient();
httpBackend.flushAllExpected().done(function() { return Promise.all([
httpBackend.flushAllExpected(),
utils.syncPromise(client, 2),
]).then(function() {
expect(membersInvokeCount).toEqual( expect(membersInvokeCount).toEqual(
1, "RoomState.members fired wrong number of times", 1, "RoomState.members fired wrong number of times",
); );
@@ -248,11 +253,10 @@ describe("MatrixClient events", function() {
expect(eventsInvokeCount).toEqual( expect(eventsInvokeCount).toEqual(
2, "RoomState.events fired wrong number of times", 2, "RoomState.events fired wrong number of times",
); );
done();
}); });
}); });
it("should emit RoomMember events", function(done) { it("should emit RoomMember events", function() {
httpBackend.when("GET", "/sync").respond(200, SYNC_DATA); httpBackend.when("GET", "/sync").respond(200, SYNC_DATA);
httpBackend.when("GET", "/sync").respond(200, NEXT_SYNC_DATA); httpBackend.when("GET", "/sync").respond(200, NEXT_SYNC_DATA);
@@ -277,7 +281,10 @@ describe("MatrixClient events", function() {
client.startClient(); client.startClient();
httpBackend.flushAllExpected().done(function() { return Promise.all([
httpBackend.flushAllExpected(),
utils.syncPromise(client, 2),
]).then(function() {
expect(typingInvokeCount).toEqual( expect(typingInvokeCount).toEqual(
1, "RoomMember.typing fired wrong number of times", 1, "RoomMember.typing fired wrong number of times",
); );
@@ -290,11 +297,10 @@ describe("MatrixClient events", function() {
expect(membershipInvokeCount).toEqual( expect(membershipInvokeCount).toEqual(
1, "RoomMember.membership fired wrong number of times", 1, "RoomMember.membership fired wrong number of times",
); );
done();
}); });
}); });
it("should emit Session.logged_out on M_UNKNOWN_TOKEN", function(done) { it("should emit Session.logged_out on M_UNKNOWN_TOKEN", function() {
httpBackend.when("GET", "/sync").respond(401, { errcode: 'M_UNKNOWN_TOKEN' }); httpBackend.when("GET", "/sync").respond(401, { errcode: 'M_UNKNOWN_TOKEN' });
let sessionLoggedOutCount = 0; let sessionLoggedOutCount = 0;
@@ -304,11 +310,10 @@ describe("MatrixClient events", function() {
client.startClient(); client.startClient();
httpBackend.flushAllExpected().done(function() { return httpBackend.flushAllExpected().then(function() {
expect(sessionLoggedOutCount).toEqual( expect(sessionLoggedOutCount).toEqual(
1, "Session.logged_out fired wrong number of times", 1, "Session.logged_out fired wrong number of times",
); );
done();
}); });
}); });
}); });

View File

@@ -195,21 +195,19 @@ describe("getEventTimeline support", function() {
}, },
}); });
return Promise.all([
httpBackend.flushAllExpected(),
utils.syncPromise(client, 2),
]);
}).then(function() {
expect(room.timeline.length).toEqual(1);
expect(room.timeline[0].event).toEqual(EVENTS[1]);
httpBackend.when("GET", "/messages").respond(200, { httpBackend.when("GET", "/messages").respond(200, {
chunk: [EVENTS[0]], chunk: [EVENTS[0]],
start: "pagin_start", start: "pagin_start",
end: "pagin_end", end: "pagin_end",
}); });
return httpBackend.flush("/sync", 2);
}).then(() => {
// the sync isn't processed immediately; give the promise chain
// a chance to complete.
return Promise.delay(0);
}).then(function() {
expect(room.timeline.length).toEqual(1);
expect(room.timeline[0].event).toEqual(EVENTS[1]);
httpBackend.flush("/messages", 1); httpBackend.flush("/messages", 1);
return client.scrollback(room); return client.scrollback(room);
}).then(function() { }).then(function() {
@@ -301,7 +299,10 @@ describe("MatrixClient event timelines", function() {
}, },
}); });
return httpBackend.flush("/sync").then(function() { return Promise.all([
httpBackend.flush("/sync"),
utils.syncPromise(client),
]).then(function() {
return client.getEventTimeline(timelineSet, EVENTS[0].event_id); return client.getEventTimeline(timelineSet, EVENTS[0].event_id);
}).then(function(tl) { }).then(function(tl) {
expect(tl.getEvents().length).toEqual(2); expect(tl.getEvents().length).toEqual(2);
@@ -726,7 +727,10 @@ describe("MatrixClient event timelines", function() {
}; };
httpBackend.when("GET", "/sync").respond(200, syncData); httpBackend.when("GET", "/sync").respond(200, syncData);
httpBackend.flushAllExpected().then(function() { Promise.all([
httpBackend.flushAllExpected(),
utils.syncPromise(client),
]).then(function() {
const room = client.getRoom(roomId); const room = client.getRoom(roomId);
const tl = room.getLiveTimeline(); const tl = room.getLiveTimeline();
expect(tl.getEvents().length).toEqual(3); expect(tl.getEvents().length).toEqual(3);
@@ -751,7 +755,10 @@ describe("MatrixClient event timelines", function() {
}; };
httpBackend.when("GET", "/sync").respond(200, sync2); httpBackend.when("GET", "/sync").respond(200, sync2);
return httpBackend.flushAllExpected(); return Promise.all([
httpBackend.flushAllExpected(),
utils.syncPromise(client),
]);
}).then(function() { }).then(function() {
const room = client.getRoom(roomId); const room = client.getRoom(roomId);
const tl = room.getLiveTimeline(); const tl = room.getLiveTimeline();

View File

@@ -468,7 +468,10 @@ describe("MatrixClient syncing", function() {
httpBackend.when("GET", "/sync").respond(200, syncData); httpBackend.when("GET", "/sync").respond(200, syncData);
client.startClient(); client.startClient();
return httpBackend.flushAllExpected(); return Promise.all([
httpBackend.flushAllExpected(),
awaitSyncEvent(),
]);
}); });
it("should set the back-pagination token on new rooms", function() { it("should set the back-pagination token on new rooms", function() {
@@ -496,6 +499,7 @@ describe("MatrixClient syncing", function() {
awaitSyncEvent(), awaitSyncEvent(),
]).then(function() { ]).then(function() {
const room = client.getRoom(roomTwo); const room = client.getRoom(roomTwo);
expect(room).toExist();
const tok = room.getLiveTimeline() const tok = room.getLiveTimeline()
.getPaginationToken(EventTimeline.BACKWARDS); .getPaginationToken(EventTimeline.BACKWARDS);
expect(tok).toEqual("roomtwotok"); expect(tok).toEqual("roomtwotok");
@@ -727,15 +731,9 @@ describe("MatrixClient syncing", function() {
* waits for the MatrixClient to emit one or more 'sync' events. * waits for the MatrixClient to emit one or more 'sync' events.
* *
* @param {Number?} numSyncs number of syncs to wait for * @param {Number?} numSyncs number of syncs to wait for
* @returns {Promise} promise which resolves after the sync events have happened
*/ */
async function awaitSyncEvent(numSyncs) { function awaitSyncEvent(numSyncs) {
if (numSyncs === undefined) { return utils.syncPromise(client, numSyncs);
numSyncs = 1;
}
while (numSyncs > 0) {
await utils.syncPromise(client);
numSyncs--;
}
} }
}); });

View File

@@ -12,19 +12,32 @@ const MatrixEvent = sdk.MatrixEvent;
* Return a promise that is resolved when the client next emits a * Return a promise that is resolved when the client next emits a
* SYNCING event. * SYNCING event.
* @param {Object} client The client * @param {Object} client The client
* @param {Number=} count Number of syncs to wait for (default 1)
* @return {Promise} Resolves once the client has emitted a SYNCING event * @return {Promise} Resolves once the client has emitted a SYNCING event
*/ */
module.exports.syncPromise = function(client) { module.exports.syncPromise = function(client, count) {
const def = Promise.defer(); if (count === undefined) {
const cb = (state) => { count = 1;
if (state == 'SYNCING') { }
def.resolve(); if (count <= 0) {
} else { return Promise.resolve();
client.once('sync', cb); }
}
}; const p = new Promise((resolve, reject) => {
client.once('sync', cb); const cb = (state) => {
return def.promise; console.log(`${Date.now()} syncPromise(${count}): ${state}`);
if (state == 'SYNCING') {
resolve();
} else {
client.once('sync', cb);
}
};
client.once('sync', cb);
});
return p.then(() => {
return module.exports.syncPromise(client, count-1);
});
}; };
/** /**

View File

@@ -614,6 +614,10 @@ Crypto.prototype.setRoomEncryption = async function(roomId, config, inhibitDevic
console.log("Enabling encryption in " + roomId + "; " + console.log("Enabling encryption in " + roomId + "; " +
"starting to track device lists for all users therein"); "starting to track device lists for all users therein");
const room = this._clientStore.getRoom(roomId); const room = this._clientStore.getRoom(roomId);
if (!room) {
throw new Error(`Unable to enable encryption in unknown room ${roomId}`);
}
const members = room.getJoinedMembers(); const members = room.getJoinedMembers();
members.forEach((m) => { members.forEach((m) => {
this._deviceList.startTrackingDeviceList(m.userId); this._deviceList.startTrackingDeviceList(m.userId);