Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make tests wait for syncs to happen #517

Merged
merged 1 commit into from
Aug 8, 2017
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: 7 additions & 2 deletions spec/TestClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
};
Expand Down Expand Up @@ -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`);
});
};
2 changes: 1 addition & 1 deletion spec/integ/matrix-client-crypto.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ describe("MatrixClient crypto", function() {
[bobUserId]: {},
},
});
return aliTestClient.httpBackend.flush('/keys/query', 1);
return aliTestClient.httpBackend.flushAllExpected();
});
});

Expand Down
29 changes: 17 additions & 12 deletions spec/integ/matrix-client-event-emitter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.",
);
Expand All @@ -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);

Expand Down Expand Up @@ -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",
);
Expand All @@ -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);

Expand All @@ -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",
);
Expand All @@ -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;
Expand All @@ -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();
});
});
});
Expand Down
33 changes: 20 additions & 13 deletions spec/integ/matrix-client-event-timeline.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down
18 changes: 8 additions & 10 deletions spec/integ/matrix-client-syncing.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}
});
35 changes: 24 additions & 11 deletions spec/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
};

/**
Expand Down
4 changes: 4 additions & 0 deletions src/crypto/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down