Skip to content

Commit

Permalink
fix Issue NetEase#355 & update test cases, Thanks to @mingshun
Browse files Browse the repository at this point in the history
a bug for SessionService#kick

Change-Id: I7dd8ea504b235b36ea830110d3d7560196445056
  • Loading branch information
cynron committed Jan 2, 2014
1 parent 8656912 commit 4f51631
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 15 deletions.
15 changes: 11 additions & 4 deletions lib/common/service/sessionService.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ SessionService.prototype.getByUid = function(uid) {
*/
SessionService.prototype.remove = function(sid) {
var session = this.sessions[sid];
var uid = session.uid;
if(session) {
var uid = session.uid;
delete this.sessions[session.id];

var sessions = this.uidMap[uid];
Expand Down Expand Up @@ -260,9 +260,16 @@ SessionService.prototype.kick = function(uid, reason, cb) {

if(sessions) {
// notify client
for(var i=0, l=sessions.length; i<l; i++) {
sessions[i].closed(reason);
}
var sids = [];
var self = this;
sessions.forEach(function(session) {
sids.push(session.id);
});

sids.forEach(function(sid) {
self.sessions[sid].closed(reason);
});

process.nextTick(function() {
utils.invokeCallback(cb);
});
Expand Down
68 changes: 57 additions & 11 deletions test/service/sessionService.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,28 +190,74 @@ describe('session service test', function() {
});

describe('#kick', function() {
it('should kick the session', function(done) {
it('should kick the sessions', function(done) {
var service = new SessionService();
var sid = 1, fid = 'frontend-server-1';
var sid1 = 1, fid1 = 'frontend-server-1';
var sid2 = 2, fid2 = 'frontend-server-1';

var socket = {
emit: function(){},
disconnect: function(){}
};
var uid = 'changchang';
var eventCount = 0;

var session = service.create(sid, fid, socket);
session.on('closed', function() {
var session1 = service.create(sid1, fid1, socket);
var session2 = service.create(sid2, fid2, socket);
session1.on('closed', function() {
eventCount++;
});

service.bind(sid, uid, function(err) {
service.kick(uid, function(err) {
should.not.exist(err);
should.not.exist(service.get(sid));
should.not.exist(service.getByUid(uid));
eventCount.should.equal(1);
done();
session2.on('closed', function() {
eventCount++;
});

service.bind(sid1, uid, function(err) {
service.bind(sid2, uid, function(err) {
service.kick(uid, function(err) {
should.not.exist(err);
should.not.exist(service.get(sid1));
should.not.exist(service.get(sid2));
should.not.exist(service.getByUid(uid));
eventCount.should.equal(2);
done();
});
});
});
});

it('should kick the session by sessionId', function(done) {
var service = new SessionService();
var sid1 = 1, fid1 = 'frontend-server-1';
var sid2 = 2, fid2 = 'frontend-server-1';

var socket = {
emit: function(){},
disconnect: function(){}
};
var uid = 'changchang';
var eventCount = 0;

var session1 = service.create(sid1, fid1, socket);
var session2 = service.create(sid2, fid2, socket);
session1.on('closed', function() {
eventCount++;
});

session2.on('closed', function() {
eventCount++;
});

service.bind(sid1, uid, function(err) {
service.bind(sid2, uid, function(err) {
service.kickBySessionId(sid1, function(err) {
should.not.exist(err);
should.not.exist(service.get(sid1));
should.exist(service.get(sid2));
should.exist(service.getByUid(uid));
eventCount.should.equal(1);
done();
});
});
});
});
Expand Down

0 comments on commit 4f51631

Please sign in to comment.