Merge pull request #67 from overleaf/bg-fix-join-leave

fix async behaviour of join/leave
This commit is contained in:
Brian Gough 2019-07-29 12:13:58 +01:00 committed by GitHub
commit 1926f97dbf
2 changed files with 28 additions and 4 deletions

View file

@ -46,13 +46,15 @@ module.exports = RoomManager =
joinEntity: (client, entity, id, callback) -> joinEntity: (client, entity, id, callback) ->
beforeCount = @_clientsInRoom(client, id) beforeCount = @_clientsInRoom(client, id)
# client joins room immediately but joinDoc request does not complete
# until room is subscribed
client.join id
# is this a new room? if so, subscribe # is this a new room? if so, subscribe
if beforeCount == 0 if beforeCount == 0
logger.log {entity, id}, "room is now active" logger.log {entity, id}, "room is now active"
RoomEvents.once "#{entity}-subscribed-#{id}", (err) -> RoomEvents.once "#{entity}-subscribed-#{id}", (err) ->
# only allow the client to join when all the relevant channels have subscribed # only allow the client to join when all the relevant channels have subscribed
logger.log {client: client.id, entity, id, beforeCount}, "client joined room after subscribing channel" logger.log {client: client.id, entity, id, beforeCount}, "client joined new room and subscribed to channel"
client.join id
callback(err) callback(err)
RoomEvents.emit "#{entity}-active", id RoomEvents.emit "#{entity}-active", id
IdMap.set(id, entity) IdMap.set(id, entity)

View file

@ -1,10 +1,12 @@
chai = require("chai") chai = require("chai")
expect = chai.expect expect = chai.expect
chai.should() chai.should()
sinon = require("sinon")
RealTimeClient = require "./helpers/RealTimeClient" RealTimeClient = require "./helpers/RealTimeClient"
MockDocUpdaterServer = require "./helpers/MockDocUpdaterServer" MockDocUpdaterServer = require "./helpers/MockDocUpdaterServer"
FixturesManager = require "./helpers/FixturesManager" FixturesManager = require "./helpers/FixturesManager"
logger = require("logger-sharelatex")
async = require "async" async = require "async"
@ -13,9 +15,13 @@ describe "leaveDoc", ->
@lines = ["test", "doc", "lines"] @lines = ["test", "doc", "lines"]
@version = 42 @version = 42
@ops = ["mock", "doc", "ops"] @ops = ["mock", "doc", "ops"]
sinon.spy(logger, "error")
after ->
logger.error.restore() # remove the spy
describe "when joined to a doc", -> describe "when joined to a doc", ->
before (done) -> beforeEach (done) ->
async.series [ async.series [
(cb) => (cb) =>
FixturesManager.setUpProject { FixturesManager.setUpProject {
@ -39,7 +45,7 @@ describe "leaveDoc", ->
], done ], done
describe "then leaving the doc", -> describe "then leaving the doc", ->
before (done) -> beforeEach (done) ->
@client.emit "leaveDoc", @doc_id, (error) -> @client.emit "leaveDoc", @doc_id, (error) ->
throw error if error? throw error if error?
done() done()
@ -48,3 +54,19 @@ describe "leaveDoc", ->
RealTimeClient.getConnectedClient @client.socket.sessionid, (error, client) => RealTimeClient.getConnectedClient @client.socket.sessionid, (error, client) =>
expect(@doc_id in client.rooms).to.equal false expect(@doc_id in client.rooms).to.equal false
done() done()
describe "when sending a leaveDoc request before the previous joinDoc request has completed", ->
beforeEach (done) ->
@client.emit "leaveDoc", @doc_id, () ->
@client.emit "joinDoc", @doc_id, () ->
@client.emit "leaveDoc", @doc_id, (error) ->
throw error if error?
done()
it "should not trigger an error", ->
sinon.assert.neverCalledWith(logger.error, sinon.match.any, "not subscribed - shouldn't happen")
it "should have left the doc room", (done) ->
RealTimeClient.getConnectedClient @client.socket.sessionid, (error, client) =>
expect(@doc_id in client.rooms).to.equal false
done()