Bug 1089722 Handle Loop rooms being deleted in the backend, and sending appropriate notifications. r=mikedeboer

This commit is contained in:
Mark Banner 2014-11-26 17:52:55 +00:00
Родитель 4d1df5a441
Коммит 307d4b0952
2 изменённых файлов: 60 добавлений и 18 удалений

Просмотреть файл

@ -176,18 +176,31 @@ let LoopRoomsInternal = {
for (let room of roomsList) {
// See if we already have this room in our cache.
let orig = this.rooms.get(room.roomToken);
if (orig) {
checkForParticipantsUpdate(orig, room);
}
// Remove the `currSize` for posterity.
if ("currSize" in room) {
delete room.currSize;
}
this.rooms.set(room.roomToken, room);
let eventName = orig ? "update" : "add";
eventEmitter.emit(eventName, room);
eventEmitter.emit(eventName + ":" + room.roomToken, room);
if (room.deleted) {
// If this client deleted the room, then we'll already have
// deleted the room in the function below.
if (orig) {
this.rooms.delete(room.roomToken);
}
eventEmitter.emit("delete", room);
eventEmitter.emit("delete:" + room.roomToken, room);
} else {
if (orig) {
checkForParticipantsUpdate(orig, room);
}
// Remove the `currSize` for posterity.
if ("currSize" in room) {
delete room.currSize;
}
this.rooms.set(room.roomToken, room);
let eventName = orig ? "update" : "add";
eventEmitter.emit(eventName, room);
eventEmitter.emit(eventName + ":" + room.roomToken, room);
}
}
// If there's no rooms in the list, remove the guest created room flag, so that
@ -230,13 +243,22 @@ let LoopRoomsInternal = {
let data = JSON.parse(response.body);
room.roomToken = roomToken;
checkForParticipantsUpdate(room, data);
extend(room, data);
this.rooms.set(roomToken, room);
let eventName = !needsUpdate ? "update" : "add";
eventEmitter.emit(eventName, room);
eventEmitter.emit(eventName + ":" + roomToken, room);
if (data.deleted) {
this.rooms.delete(room.roomToken);
extend(room, data);
eventEmitter.emit("delete", room);
eventEmitter.emit("delete:" + room.roomToken, room);
} else {
checkForParticipantsUpdate(room, data);
extend(room, data);
this.rooms.set(roomToken, room);
let eventName = !needsUpdate ? "update" : "add";
eventEmitter.emit(eventName, room);
eventEmitter.emit(eventName + ":" + roomToken, room);
}
callback(null, room);
}, err => callback(err)).catch(err => callback(err));
},
@ -323,8 +345,8 @@ let LoopRoomsInternal = {
MozLoopService.hawkRequest(this.sessionType, url, "DELETE")
.then(response => {
this.rooms.delete(roomToken);
eventEmitter.emit("delete", room);
callback(null, room);
// We'll emit an update when the push notification is received.
}, error => callback(error)).catch(error => callback(error));
},

Просмотреть файл

@ -83,6 +83,9 @@ const kRoomUpdates = {
displayName: "Ruharb",
roomConnectionId: "5de6281c-6568-455f-af08-c0b0a973100e"
}]
},
"5": {
deleted: true
}
};
@ -118,6 +121,7 @@ const compareRooms = function(room1, room2) {
// LoopRooms emits various events. Test if they work as expected here.
let gExpectedAdds = [];
let gExpectedUpdates = [];
let gExpectedDeletes = [];
let gExpectedJoins = {};
let gExpectedLeaves = {};
@ -136,6 +140,12 @@ const onRoomUpdated = function(e, room) {
gExpectedUpdates.splice(idx, 1);
};
const onRoomDeleted = function(e, room) {
let idx = gExpectedDeletes.indexOf(room.roomToken);
Assert.ok(idx > -1, "Deleted room should be expected");
gExpectedDeletes.splice(idx, 1);
}
const onRoomJoined = function(e, roomToken, participant) {
let participants = gExpectedJoins[roomToken];
Assert.ok(participants, "Participant should be expected to join");
@ -191,6 +201,7 @@ add_task(function* setup_server() {
let qs = parseQueryString(req.queryString);
let room = kRooms.get("_nxD4V4FflQ");
room.participants = kRoomUpdates[qs.version].participants;
room.deleted = kRoomUpdates[qs.version].deleted;
res.write(JSON.stringify([room]));
} else {
res.write(JSON.stringify([...kRooms.values()]));
@ -386,10 +397,17 @@ add_task(function* test_renameRoom() {
Assert.equal(renameData.roomName, "fakeName");
});
add_task(function* test_roomDeleteNotifications() {
gExpectedDeletes.push("_nxD4V4FflQ");
roomsPushNotification("5");
yield waitForCondition(() => gExpectedDeletes.length === 0);
});
// Test if the event emitter implementation doesn't leak and is working as expected.
add_task(function* () {
Assert.strictEqual(gExpectedAdds.length, 0, "No room additions should be expected anymore");
Assert.strictEqual(gExpectedUpdates.length, 0, "No room updates should be expected anymore");
Assert.strictEqual(gExpectedDeletes.length, 0, "No room deletes should be expected anymore");
});
function run_test() {
@ -397,6 +415,7 @@ function run_test() {
LoopRooms.on("add", onRoomAdded);
LoopRooms.on("update", onRoomUpdated);
LoopRooms.on("delete", onRoomDeleted);
LoopRooms.on("joined", onRoomJoined);
LoopRooms.on("left", onRoomLeft);
@ -409,6 +428,7 @@ function run_test() {
LoopRooms.off("add", onRoomAdded);
LoopRooms.off("update", onRoomUpdated);
LoopRooms.off("delete", onRoomDeleted);
LoopRooms.off("joined", onRoomJoined);
LoopRooms.off("left", onRoomLeft);
});