diff --git a/spec/TestClient.js b/spec/TestClient.js index b55e7046ce0..409e065da96 100644 --- a/spec/TestClient.js +++ b/spec/TestClient.js @@ -77,7 +77,10 @@ TestClient.prototype.start = function() { pendingEventOrdering: 'detached', }); - return this.httpBackend.flushAllExpected().then(() => { + return Promise.all([ + this.httpBackend.flushAllExpected(), + testUtils.syncPromise(this.client), + ]).then(() => { console.log(this + ': started'); }); }; @@ -199,5 +202,7 @@ TestClient.prototype.flushSync = function() { return Promise.all([ this.httpBackend.flush('/sync', 1), testUtils.syncPromise(this.client), - ]); + ]).then(() => { + console.log(`${this}: flushSync completed`); + }); }; diff --git a/spec/integ/matrix-client-crypto.spec.js b/spec/integ/matrix-client-crypto.spec.js index 2eafc8b8ee7..7d9fb8181d5 100644 --- a/spec/integ/matrix-client-crypto.spec.js +++ b/spec/integ/matrix-client-crypto.spec.js @@ -691,7 +691,7 @@ describe("MatrixClient crypto", function() { [bobUserId]: {}, }, }); - return aliTestClient.httpBackend.flush('/keys/query', 1); + return aliTestClient.httpBackend.flushAllExpected(); }); }); diff --git a/spec/integ/matrix-client-event-emitter.spec.js b/spec/integ/matrix-client-event-emitter.spec.js index 0335c881395..c494e8bc4d5 100644 --- a/spec/integ/matrix-client-event-emitter.spec.js +++ b/spec/integ/matrix-client-event-emitter.spec.js @@ -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, NEXT_SYNC_DATA); let roomInvokeCount = 0; @@ -189,7 +189,10 @@ describe("MatrixClient events", function() { client.startClient(); - httpBackend.flushAllExpected().done(function() { + return Promise.all([ + httpBackend.flushAllExpected(), + utils.syncPromise(client, 2), + ]).then(function() { expect(roomInvokeCount).toEqual( 1, "Room fired wrong number of times.", ); @@ -199,11 +202,10 @@ describe("MatrixClient events", function() { expect(timelineFireCount).toEqual( 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, NEXT_SYNC_DATA); @@ -238,7 +240,10 @@ describe("MatrixClient events", function() { client.startClient(); - httpBackend.flushAllExpected().done(function() { + return Promise.all([ + httpBackend.flushAllExpected(), + utils.syncPromise(client, 2), + ]).then(function() { expect(membersInvokeCount).toEqual( 1, "RoomState.members fired wrong number of times", ); @@ -248,11 +253,10 @@ describe("MatrixClient events", function() { expect(eventsInvokeCount).toEqual( 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, NEXT_SYNC_DATA); @@ -277,7 +281,10 @@ describe("MatrixClient events", function() { client.startClient(); - httpBackend.flushAllExpected().done(function() { + return Promise.all([ + httpBackend.flushAllExpected(), + utils.syncPromise(client, 2), + ]).then(function() { expect(typingInvokeCount).toEqual( 1, "RoomMember.typing fired wrong number of times", ); @@ -290,11 +297,10 @@ describe("MatrixClient events", function() { expect(membershipInvokeCount).toEqual( 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' }); let sessionLoggedOutCount = 0; @@ -304,11 +310,10 @@ describe("MatrixClient events", function() { client.startClient(); - httpBackend.flushAllExpected().done(function() { + return httpBackend.flushAllExpected().then(function() { expect(sessionLoggedOutCount).toEqual( 1, "Session.logged_out fired wrong number of times", ); - done(); }); }); }); diff --git a/spec/integ/matrix-client-event-timeline.spec.js b/spec/integ/matrix-client-event-timeline.spec.js index b48c67f58e2..6fd3778bebf 100644 --- a/spec/integ/matrix-client-event-timeline.spec.js +++ b/spec/integ/matrix-client-event-timeline.spec.js @@ -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, { chunk: [EVENTS[0]], start: "pagin_start", 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); return client.scrollback(room); }).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); }).then(function(tl) { expect(tl.getEvents().length).toEqual(2); @@ -726,7 +727,10 @@ describe("MatrixClient event timelines", function() { }; 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 tl = room.getLiveTimeline(); expect(tl.getEvents().length).toEqual(3); @@ -751,7 +755,10 @@ describe("MatrixClient event timelines", function() { }; httpBackend.when("GET", "/sync").respond(200, sync2); - return httpBackend.flushAllExpected(); + return Promise.all([ + httpBackend.flushAllExpected(), + utils.syncPromise(client), + ]); }).then(function() { const room = client.getRoom(roomId); const tl = room.getLiveTimeline(); diff --git a/spec/integ/matrix-client-syncing.spec.js b/spec/integ/matrix-client-syncing.spec.js index d174709411f..2ecc20840c8 100644 --- a/spec/integ/matrix-client-syncing.spec.js +++ b/spec/integ/matrix-client-syncing.spec.js @@ -468,7 +468,10 @@ describe("MatrixClient syncing", function() { httpBackend.when("GET", "/sync").respond(200, syncData); client.startClient(); - return httpBackend.flushAllExpected(); + return Promise.all([ + httpBackend.flushAllExpected(), + awaitSyncEvent(), + ]); }); it("should set the back-pagination token on new rooms", function() { @@ -496,6 +499,7 @@ describe("MatrixClient syncing", function() { awaitSyncEvent(), ]).then(function() { const room = client.getRoom(roomTwo); + expect(room).toExist(); const tok = room.getLiveTimeline() .getPaginationToken(EventTimeline.BACKWARDS); expect(tok).toEqual("roomtwotok"); @@ -727,15 +731,9 @@ describe("MatrixClient syncing", function() { * waits for the MatrixClient to emit one or more 'sync' events. * * @param {Number?} numSyncs number of syncs to wait for + * @returns {Promise} promise which resolves after the sync events have happened */ - async function awaitSyncEvent(numSyncs) { - if (numSyncs === undefined) { - numSyncs = 1; - } - - while (numSyncs > 0) { - await utils.syncPromise(client); - numSyncs--; - } + function awaitSyncEvent(numSyncs) { + return utils.syncPromise(client, numSyncs); } }); diff --git a/spec/test-utils.js b/spec/test-utils.js index 1f062851659..2448d9874d2 100644 --- a/spec/test-utils.js +++ b/spec/test-utils.js @@ -12,19 +12,32 @@ const MatrixEvent = sdk.MatrixEvent; * Return a promise that is resolved when the client next emits a * SYNCING event. * @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 */ -module.exports.syncPromise = function(client) { - const def = Promise.defer(); - const cb = (state) => { - if (state == 'SYNCING') { - def.resolve(); - } else { - client.once('sync', cb); - } - }; - client.once('sync', cb); - return def.promise; +module.exports.syncPromise = function(client, count) { + if (count === undefined) { + count = 1; + } + if (count <= 0) { + return Promise.resolve(); + } + + const p = new Promise((resolve, reject) => { + const cb = (state) => { + 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); + }); }; /** diff --git a/src/crypto/index.js b/src/crypto/index.js index 0bc6533d3e0..192258019cf 100644 --- a/src/crypto/index.js +++ b/src/crypto/index.js @@ -614,6 +614,10 @@ Crypto.prototype.setRoomEncryption = async function(roomId, config, inhibitDevic console.log("Enabling encryption in " + roomId + "; " + "starting to track device lists for all users therein"); const room = this._clientStore.getRoom(roomId); + if (!room) { + throw new Error(`Unable to enable encryption in unknown room ${roomId}`); + } + const members = room.getJoinedMembers(); members.forEach((m) => { this._deviceList.startTrackingDeviceList(m.userId);