From 5f7841526f20f9ea4f7f617bd4ecbf30e3a45872 Mon Sep 17 00:00:00 2001 From: Jakob Ackermann Date: Wed, 17 Jun 2020 09:29:12 +0100 Subject: [PATCH] [misc] RoomManager: emitOnCompletion: properly handle Promise rejections ``` result = Promise.all([]) # rejection 1 result.then () -> RoomEvents.emit(eventName) # rejection 2 result.catch (err) -> RoomEvents.emit(eventName, err) # handle r1 ``` As shown above, the second rejection remains unhandled. The fix is to chain the `.catch()` onto the `.then()` Promise. --- .../real-time/app/coffee/RoomManager.coffee | 6 ++--- .../test/unit/coffee/RoomManagerTests.coffee | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/services/real-time/app/coffee/RoomManager.coffee b/services/real-time/app/coffee/RoomManager.coffee index cad3fd0a12..25684ed558 100644 --- a/services/real-time/app/coffee/RoomManager.coffee +++ b/services/real-time/app/coffee/RoomManager.coffee @@ -39,9 +39,9 @@ module.exports = RoomManager = @leaveEntity client, entity, id emitOnCompletion: (promiseList, eventName) -> - result = Promise.all(promiseList) - result.then () -> RoomEvents.emit(eventName) - result.catch (err) -> RoomEvents.emit(eventName, err) + Promise.all(promiseList) + .then(() -> RoomEvents.emit(eventName)) + .catch((err) -> RoomEvents.emit(eventName, err)) eventSource: () -> return RoomEvents diff --git a/services/real-time/test/unit/coffee/RoomManagerTests.coffee b/services/real-time/test/unit/coffee/RoomManagerTests.coffee index fc8375ae54..c81663576d 100644 --- a/services/real-time/test/unit/coffee/RoomManagerTests.coffee +++ b/services/real-time/test/unit/coffee/RoomManagerTests.coffee @@ -1,4 +1,5 @@ chai = require('chai') +expect = chai.expect should = chai.should() sinon = require("sinon") modulePath = "../../../app/js/RoomManager.js" @@ -20,6 +21,29 @@ describe 'RoomManager', -> sinon.spy(@RoomEvents, 'emit') sinon.spy(@RoomEvents, 'once') + describe "emitOnCompletion", -> + describe "when a subscribe errors", -> + afterEach () -> + process.removeListener("unhandledRejection", @onUnhandled) + + beforeEach (done) -> + @onUnhandled = (error) => + @unhandledError = error + done(new Error("unhandledRejection: #{error.message}")) + process.on("unhandledRejection", @onUnhandled) + + reject = undefined + subscribePromise = new Promise((_, r) -> reject = r) + promises = [subscribePromise] + eventName = "project-subscribed-123" + @RoomEvents.once eventName, () -> + setTimeout(done, 100) + @RoomManager.emitOnCompletion(promises, eventName) + setTimeout(() -> reject(new Error("subscribe failed"))) + + it "should keep going", () -> + expect(@unhandledError).to.not.exist + describe "joinProject", -> describe "when the project room is empty", ->