Merge pull request #156 from overleaf/jpa-fully-handle-subscribe-error

[misc] RoomManager: emitOnCompletion: properly handle Promise rejections
This commit is contained in:
Jakob Ackermann 2020-06-22 11:35:03 +02:00 committed by GitHub
commit 9c1335720c
2 changed files with 27 additions and 3 deletions

View file

@ -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

View file

@ -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", ->