123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- var should = require('should');
- var pomelo = require('../../');
- var SessionService = require('../../lib/common/service/sessionService');
- describe('session service test', function() {
- describe('#bind', function() {
- it('should get session by uid after binded', function(done) {
- var service = new SessionService();
- var sid = 1, fid = 'frontend-server-1', socket = {};
- var uid = 'changchang';
- var eventCount = 0;
- var session = service.create(sid, fid, socket);
- should.exist(session);
- session.should.eql(service.get(sid));
- session.on('bind', function(euid) {
- eventCount++;
- uid.should.equal(euid);
- });
- service.bind(sid, uid, function(err) {
- should.not.exist(err);
- var sessions = service.getByUid(uid);
- should.exist(sessions);
- sessions.length.should.equal(1);
- session.should.eql(sessions[0]);
- eventCount.should.equal(1);
- service.bind(sid, uid, function(err) {
- should.not.exist(err);
- done();
- });
- });
- });
- it('should fail if already binded uid', function(done) {
- var service = new SessionService();
- var sid = 1, fid = 'frontend-server-1', socket = {};
- var uid = 'py', test_uid = 'test';
- var session = service.create(sid, fid, socket);
- service.bind(sid, uid, null);
- service.bind(sid, test_uid, function(err) {
- should.exist(err);
- done();
- });
- });
- it('should fail if try to bind a session not exist', function(done) {
- var service = new SessionService();
- var sid = 1, uid = 'changchang';
- service.bind(sid, uid, function(err) {
- should.exist(err);
- done();
- });
- });
- });
- describe('#unbind', function() {
- it('should fail unbind session if session not exist', function(done) {
- var service = new SessionService();
- var sid = 1;
- var uid = 'py';
-
- service.unbind(sid, uid, function(err) {
- should.exist(err);
- done();
- });
- });
- it('should fail unbind session if session not binded', function(done) {
- var service = new SessionService();
- var sid = 1, fid = 'frontend-server-1', socket = {};
- var uid = 'py';
- var session = service.create(sid, fid, socket);
- service.unbind(sid, uid, function(err) {
- should.exist(err);
- done();
- });
- });
- it('should fail to get session after session unbinded', function(done) {
- var service = new SessionService();
- var sid = 1, fid = 'frontend-server-1', socket = {};
- var uid = 'py';
- var session = service.create(sid, fid, socket);
- service.bind(sid, uid, null);
- service.unbind(sid, uid, function(err) {
- should.not.exist(err);
- var sessions = service.getByUid(uid);
- should.not.exist(sessions);
- done();
- });
- });
- });
- describe('#remove', function() {
- it('should not get the session after remove', function(done) {
- var service = new SessionService();
- var sid = 1, fid = 'frontend-server-1', socket = {};
- var uid = 'changchang';
- var session = service.create(sid, fid, socket);
- service.bind(sid, uid, function(err) {
- service.remove(sid);
- should.not.exist(service.get(sid));
- should.not.exist(service.getByUid(uid));
- done();
- });
- });
- });
- describe('#import', function() {
- it('should update the session with the key/value pair', function(done) {
- var service = new SessionService();
- var sid = 1, fid = 'frontend-server-1', socket = {};
- var key = 'key-1', value = 'value-1';
- var session = service.create(sid, fid, socket);
- service.import(sid, key, value, function(err) {
- should.not.exist(err);
- value.should.eql(session.get(key));
- done();
- });
- });
- it('should fail if try to update a session not exist', function(done) {
- var service = new SessionService();
- var sid = 1;
- var key = 'key-1', value = 'value-1';
- service.import(sid, key, value, function(err) {
- should.exist(err);
- done();
- });
- });
- it('should update the session with the key/value pairs', function(done) {
- var service = new SessionService();
- var sid = 1, fid = 'frontend-server-1', socket = {};
- var key = 'key-1', value = 'value-1', key2 = 'key-2', value2 = {};
- var settings = {};
- settings[key] = value;
- settings[key2] = value2;
- var session = service.create(sid, fid, socket);
- service.importAll(sid, settings, function(err) {
- should.not.exist(err);
- value.should.eql(session.get(key));
- value2.should.eql(session.get(key2));
- done();
- });
- });
- it('should fail if try to update a session not exist', function(done) {
- var service = new SessionService();
- var sid = 1;
- var key = 'key-1', value = 'value-1';
- service.import(sid, key, value, function(err) {
- should.exist(err);
- done();
- });
- });
- it('should fail if try to update a session not exist', function(done) {
- var service = new SessionService();
- var sid = 1;
- var key = 'key-1', value = 'value-1', key2 = 'key-2', value2 = {};
- var settings = {};
- settings[key] = value;
- settings[key2] = value2;
- service.importAll(sid, settings, function(err) {
- should.exist(err);
- done();
- });
- });
- });
- describe('#kick', function() {
- it('should kick the sessions', 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.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();
- });
- });
- });
- });
- it('should ok if kick a session not exist', function(done) {
- var service = new SessionService();
- var uid = 'changchang';
- service.kick(uid, function(err) {
- should.not.exist(err);
- done();
- });
- });
- it('should kick session by sid', function(done) {
- var service = new SessionService();
- var sid = 1, fid = 'frontend-server-1';
- var socket = {
- emit: function(){},
- disconnect: function(){}
- };
- var eventCount = 0;
- var session = service.create(sid, fid, socket);
- session.on('closed', function() {
- eventCount++;
- });
- service.kickBySessionId(sid, function(err) {
- should.not.exist(err);
- should.not.exist(service.get(sid));
- eventCount.should.equal(1);
- done();
- });
- });
- it('should ok if kick a session not exist', function(done) {
- var service = new SessionService();
- var sid = 1;
- service.kickBySessionId(sid, function(err) {
- should.not.exist(err);
- done();
- });
- });
- });
- describe('#forEachSession', function() {
- it('should iterate all created sessions', function(done) {
- var service = new SessionService();
- var sid = 1, fid = 'frontend-server-1', socket = {};
- var eventCount = 0;
- var outter_session = service.create(sid, fid, socket);
- service.forEachSession(function(session) {
- should.exist(session);
- outter_session.id.should.eql(session.id);
- done();
- });
- });
- });
- describe('#forEachBindedSession', function() {
- it('should iterate all binded sessions', function(done) {
- var service = new SessionService();
- var sid = 1, fid = 'frontend-server-1', socket = {};
- var uid = 'py';
- var outter_session = service.create(sid, fid, socket);
- service.bind(sid, uid, null);
- service.forEachBindedSession(function(session) {
- should.exist(session);
- outter_session.id.should.eql(session.id);
- outter_session.uid.should.eql(session.uid);
- done();
- });
- });
- });
- });
- describe('frontend session test', function() {
- describe('#bind', function() {
- it('should get session by uid after binded', function(done) {
- var service = new SessionService();
- var sid = 1, fid = 'frontend-server-1', socket = {};
- var uid = 'changchang';
- var eventCount = 0;
- var session = service.create(sid, fid, socket);
- var fsession = session.toFrontendSession();
- should.exist(fsession);
- fsession.on('bind', function(euid) {
- eventCount++;
- uid.should.equal(euid);
- });
- fsession.bind(uid, function(err) {
- should.not.exist(err);
- var sessions = service.getByUid(uid);
- should.exist(sessions);
- sessions.length.should.equal(1);
- session.should.eql(sessions[0]);
- eventCount.should.equal(1);
- done();
- });
- });
- });
- describe('#unbind', function() {
- it('should fail to get session after session unbinded', function(done) {
- var service = new SessionService();
- var sid = 1, fid = 'frontend-server-1', socket = {};
- var uid = 'py';
- var session = service.create(sid, fid, socket);
- var fsession = session.toFrontendSession();
- fsession.bind(uid, null);
- fsession.unbind(uid, function(err) {
- should.not.exist(err);
- var sessions = service.getByUid(uid);
- should.not.exist(sessions);
- done();
- });
- });
- });
- describe('#set/get', function() {
- it('should update the key/value pair in frontend session but not session',
- function() {
- var service = new SessionService();
- var sid = 1, fid = 'frontend-server-1', socket = {};
- var key = 'key-1', value = 'value-1';
- var session = service.create(sid, fid, socket);
- var fsession = session.toFrontendSession();
- fsession.set(key, value);
- should.not.exist(session.get(key));
- value.should.eql(fsession.get(key));
- });
- });
- describe('#push', function() {
- it('should push the specified key/value pair to session', function(done) {
- var service = new SessionService();
- var sid = 1, fid = 'frontend-server-1', socket = {};
- var key = 'key-1', value = 'value-1', key2 = 'key-2', value2 = {};
- var session = service.create(sid, fid, socket);
- var fsession = session.toFrontendSession();
- fsession.set(key, value);
- fsession.set(key2, value2);
- fsession.push(key, function(err) {
- should.not.exist(err);
- value.should.eql(session.get(key));
- should.not.exist(session.get(key2));
- done();
- });
- });
- it('should push all the key/value pairs to session', function(done) {
- var service = new SessionService();
- var sid = 1, fid = 'frontend-server-1', socket = {};
- var key = 'key-1', value = 'value-1', key2 = 'key-2', value2 = {};
- var session = service.create(sid, fid, socket);
- var fsession = session.toFrontendSession();
- fsession.set(key, value);
- fsession.set(key2, value2);
- fsession.pushAll(function(err) {
- should.not.exist(err);
- value.should.eql(session.get(key));
- value2.should.eql(session.get(key2));
- done();
- });
- });
- });
-
- describe('#export', function() {
- it('should equal frontend session after export', function(done) {
- var service = new SessionService();
- var sid = 1, fid = 'frontend-server-1', socket = {};
- var uid = 'py';
- var session = service.create(sid, fid, socket);
- var fsession = session.toFrontendSession();
- var esession = fsession.export();
- esession.id.should.eql(fsession.id);
- esession.frontendId.should.eql(fsession.frontendId);
- done();
- });
- });
- });
|