connected user manager logic works correctly

This commit is contained in:
Henry Oswald 2014-07-14 12:41:06 +01:00
parent d737197e95
commit 13aaf042d9
2 changed files with 12 additions and 9 deletions

View file

@ -1,6 +1,7 @@
_ = require("underscore")
async = require("async")
Settings = require('settings-sharelatex')
logger = require("logger-sharelatex")
redis = require('redis')
rclient = redis.createClient(Settings.redis.web.port, Settings.redis.web.host)
rclient.auth(Settings.redis.web.password)
@ -20,8 +21,12 @@ module.exports =
(cb)->
rclient.sadd buildProjectSetKey(project_id), user_id, cb
(cb)->
rclient.setex buildUserKey(project_id, user_id), new Date(), ONE_HOUR_IN_S * 6, cb
], callback
ttl = ONE_HOUR_IN_S * 6
rclient.setex buildUserKey(project_id, user_id), ttl, new Date(), cb
], (err)->
if err?
logger.err err:err, project_id:project_id, user_id:user_id, "problem marking user as connected"
callback(err)
markUserAsDisconnected: (project_id, user_id, callback)->
logger.log project_id:project_id, user_id:user_id, "marking user as disconnected"
@ -32,8 +37,6 @@ module.exports =
rclient.del buildUserKey(project_id, user_id), cb
], callback
_getConnectedUser: (project_id, user_id, callback)->
rclient.get buildUserKey(project_id, user_id), (err, result)->
if !result?
@ -45,8 +48,7 @@ module.exports =
getConnectedUsers: (project_id, callback)->
self = @
rclient.get "connected_users_list:#{project_id}", (err, results)->
rclient.smembers buildProjectSetKey(project_id), (err, results)->
jobs = results.map (user_id)->
(cb)->
self._getConnectedUser(project_id, user_id, cb)

View file

@ -23,6 +23,7 @@ describe "ConnectedUsersManager", ->
get: sinon.stub()
srem:sinon.stub()
del:sinon.stub()
smembers:sinon.stub()
tk.freeze(new Date())
@ConnectedUsersManager = SandboxedModule.require modulePath, requires:
@ -45,7 +46,7 @@ describe "ConnectedUsersManager", ->
it "should set a key with the date and give it a ttl", (done)->
@ConnectedUsersManager.markUserAsConnected @project_id, @user_id, (err)=>
console.log @rClient.setex.args[0], "connected_user:#{@project_id}:#{@user_id}"
@rClient.setex.calledWith("connected_user:#{@project_id}:#{@user_id}", new Date(), 60 * 60 * 6).should.equal true
@rClient.setex.calledWith("connected_user:#{@project_id}:#{@user_id}", 60 * 60 * 6, new Date()).should.equal true
done()
it "should push the user_id on to the project list", (done)->
@ -92,7 +93,7 @@ describe "ConnectedUsersManager", ->
beforeEach ->
@users = ["1234", "5678", "9123"]
@rClient.get.callsArgWith(1, null, @users)
@rClient.smembers.callsArgWith(1, null, @users)
@ConnectedUsersManager._getConnectedUser = sinon.stub()
@ConnectedUsersManager._getConnectedUser.withArgs(@project_id, @users[0]).callsArgWith(2, null, {connected:true, user_id:@users[0]})
@ConnectedUsersManager._getConnectedUser.withArgs(@project_id, @users[1]).callsArgWith(2, null, {connected:false, user_id:@users[1]})