sessionService.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. var should = require('should');
  2. var pomelo = require('../../');
  3. var SessionService = require('../../lib/common/service/sessionService');
  4. describe('session service test', function() {
  5. describe('#bind', function() {
  6. it('should get session by uid after binded', function(done) {
  7. var service = new SessionService();
  8. var sid = 1, fid = 'frontend-server-1', socket = {};
  9. var uid = 'changchang';
  10. var eventCount = 0;
  11. var session = service.create(sid, fid, socket);
  12. should.exist(session);
  13. session.should.eql(service.get(sid));
  14. session.on('bind', function(euid) {
  15. eventCount++;
  16. uid.should.equal(euid);
  17. });
  18. service.bind(sid, uid, function(err) {
  19. should.not.exist(err);
  20. var sessions = service.getByUid(uid);
  21. should.exist(sessions);
  22. sessions.length.should.equal(1);
  23. session.should.eql(sessions[0]);
  24. eventCount.should.equal(1);
  25. service.bind(sid, uid, function(err) {
  26. should.not.exist(err);
  27. done();
  28. });
  29. });
  30. });
  31. it('should fail if already binded uid', function(done) {
  32. var service = new SessionService();
  33. var sid = 1, fid = 'frontend-server-1', socket = {};
  34. var uid = 'py', test_uid = 'test';
  35. var session = service.create(sid, fid, socket);
  36. service.bind(sid, uid, null);
  37. service.bind(sid, test_uid, function(err) {
  38. should.exist(err);
  39. done();
  40. });
  41. });
  42. it('should fail if try to bind a session not exist', function(done) {
  43. var service = new SessionService();
  44. var sid = 1, uid = 'changchang';
  45. service.bind(sid, uid, function(err) {
  46. should.exist(err);
  47. done();
  48. });
  49. });
  50. });
  51. describe('#unbind', function() {
  52. it('should fail unbind session if session not exist', function(done) {
  53. var service = new SessionService();
  54. var sid = 1;
  55. var uid = 'py';
  56. service.unbind(sid, uid, function(err) {
  57. should.exist(err);
  58. done();
  59. });
  60. });
  61. it('should fail unbind session if session not binded', function(done) {
  62. var service = new SessionService();
  63. var sid = 1, fid = 'frontend-server-1', socket = {};
  64. var uid = 'py';
  65. var session = service.create(sid, fid, socket);
  66. service.unbind(sid, uid, function(err) {
  67. should.exist(err);
  68. done();
  69. });
  70. });
  71. it('should fail to get session after session unbinded', function(done) {
  72. var service = new SessionService();
  73. var sid = 1, fid = 'frontend-server-1', socket = {};
  74. var uid = 'py';
  75. var session = service.create(sid, fid, socket);
  76. service.bind(sid, uid, null);
  77. service.unbind(sid, uid, function(err) {
  78. should.not.exist(err);
  79. var sessions = service.getByUid(uid);
  80. should.not.exist(sessions);
  81. done();
  82. });
  83. });
  84. });
  85. describe('#remove', function() {
  86. it('should not get the session after remove', function(done) {
  87. var service = new SessionService();
  88. var sid = 1, fid = 'frontend-server-1', socket = {};
  89. var uid = 'changchang';
  90. var session = service.create(sid, fid, socket);
  91. service.bind(sid, uid, function(err) {
  92. service.remove(sid);
  93. should.not.exist(service.get(sid));
  94. should.not.exist(service.getByUid(uid));
  95. done();
  96. });
  97. });
  98. });
  99. describe('#import', function() {
  100. it('should update the session with the key/value pair', function(done) {
  101. var service = new SessionService();
  102. var sid = 1, fid = 'frontend-server-1', socket = {};
  103. var key = 'key-1', value = 'value-1';
  104. var session = service.create(sid, fid, socket);
  105. service.import(sid, key, value, function(err) {
  106. should.not.exist(err);
  107. value.should.eql(session.get(key));
  108. done();
  109. });
  110. });
  111. it('should fail if try to update a session not exist', function(done) {
  112. var service = new SessionService();
  113. var sid = 1;
  114. var key = 'key-1', value = 'value-1';
  115. service.import(sid, key, value, function(err) {
  116. should.exist(err);
  117. done();
  118. });
  119. });
  120. it('should update the session with the key/value pairs', function(done) {
  121. var service = new SessionService();
  122. var sid = 1, fid = 'frontend-server-1', socket = {};
  123. var key = 'key-1', value = 'value-1', key2 = 'key-2', value2 = {};
  124. var settings = {};
  125. settings[key] = value;
  126. settings[key2] = value2;
  127. var session = service.create(sid, fid, socket);
  128. service.importAll(sid, settings, function(err) {
  129. should.not.exist(err);
  130. value.should.eql(session.get(key));
  131. value2.should.eql(session.get(key2));
  132. done();
  133. });
  134. });
  135. it('should fail if try to update a session not exist', function(done) {
  136. var service = new SessionService();
  137. var sid = 1;
  138. var key = 'key-1', value = 'value-1';
  139. service.import(sid, key, value, function(err) {
  140. should.exist(err);
  141. done();
  142. });
  143. });
  144. it('should fail if try to update a session not exist', function(done) {
  145. var service = new SessionService();
  146. var sid = 1;
  147. var key = 'key-1', value = 'value-1', key2 = 'key-2', value2 = {};
  148. var settings = {};
  149. settings[key] = value;
  150. settings[key2] = value2;
  151. service.importAll(sid, settings, function(err) {
  152. should.exist(err);
  153. done();
  154. });
  155. });
  156. });
  157. describe('#kick', function() {
  158. it('should kick the sessions', function(done) {
  159. var service = new SessionService();
  160. var sid1 = 1, fid1 = 'frontend-server-1';
  161. var sid2 = 2, fid2 = 'frontend-server-1';
  162. var socket = {
  163. emit: function(){},
  164. disconnect: function(){}
  165. };
  166. var uid = 'changchang';
  167. var eventCount = 0;
  168. var session1 = service.create(sid1, fid1, socket);
  169. var session2 = service.create(sid2, fid2, socket);
  170. session1.on('closed', function() {
  171. eventCount++;
  172. });
  173. session2.on('closed', function() {
  174. eventCount++;
  175. });
  176. service.bind(sid1, uid, function(err) {
  177. service.bind(sid2, uid, function(err) {
  178. service.kick(uid, function(err) {
  179. should.not.exist(err);
  180. should.not.exist(service.get(sid1));
  181. should.not.exist(service.get(sid2));
  182. should.not.exist(service.getByUid(uid));
  183. eventCount.should.equal(2);
  184. done();
  185. });
  186. });
  187. });
  188. });
  189. it('should kick the session by sessionId', function(done) {
  190. var service = new SessionService();
  191. var sid1 = 1, fid1 = 'frontend-server-1';
  192. var sid2 = 2, fid2 = 'frontend-server-1';
  193. var socket = {
  194. emit: function(){},
  195. disconnect: function(){}
  196. };
  197. var uid = 'changchang';
  198. var eventCount = 0;
  199. var session1 = service.create(sid1, fid1, socket);
  200. var session2 = service.create(sid2, fid2, socket);
  201. session1.on('closed', function() {
  202. eventCount++;
  203. });
  204. session2.on('closed', function() {
  205. eventCount++;
  206. });
  207. service.bind(sid1, uid, function(err) {
  208. service.bind(sid2, uid, function(err) {
  209. service.kickBySessionId(sid1, function(err) {
  210. should.not.exist(err);
  211. should.not.exist(service.get(sid1));
  212. should.exist(service.get(sid2));
  213. should.exist(service.getByUid(uid));
  214. eventCount.should.equal(1);
  215. done();
  216. });
  217. });
  218. });
  219. });
  220. it('should ok if kick a session not exist', function(done) {
  221. var service = new SessionService();
  222. var uid = 'changchang';
  223. service.kick(uid, function(err) {
  224. should.not.exist(err);
  225. done();
  226. });
  227. });
  228. it('should kick session by sid', function(done) {
  229. var service = new SessionService();
  230. var sid = 1, fid = 'frontend-server-1';
  231. var socket = {
  232. emit: function(){},
  233. disconnect: function(){}
  234. };
  235. var eventCount = 0;
  236. var session = service.create(sid, fid, socket);
  237. session.on('closed', function() {
  238. eventCount++;
  239. });
  240. service.kickBySessionId(sid, function(err) {
  241. should.not.exist(err);
  242. should.not.exist(service.get(sid));
  243. eventCount.should.equal(1);
  244. done();
  245. });
  246. });
  247. it('should ok if kick a session not exist', function(done) {
  248. var service = new SessionService();
  249. var sid = 1;
  250. service.kickBySessionId(sid, function(err) {
  251. should.not.exist(err);
  252. done();
  253. });
  254. });
  255. });
  256. describe('#forEachSession', function() {
  257. it('should iterate all created sessions', function(done) {
  258. var service = new SessionService();
  259. var sid = 1, fid = 'frontend-server-1', socket = {};
  260. var eventCount = 0;
  261. var outter_session = service.create(sid, fid, socket);
  262. service.forEachSession(function(session) {
  263. should.exist(session);
  264. outter_session.id.should.eql(session.id);
  265. done();
  266. });
  267. });
  268. });
  269. describe('#forEachBindedSession', function() {
  270. it('should iterate all binded sessions', function(done) {
  271. var service = new SessionService();
  272. var sid = 1, fid = 'frontend-server-1', socket = {};
  273. var uid = 'py';
  274. var outter_session = service.create(sid, fid, socket);
  275. service.bind(sid, uid, null);
  276. service.forEachBindedSession(function(session) {
  277. should.exist(session);
  278. outter_session.id.should.eql(session.id);
  279. outter_session.uid.should.eql(session.uid);
  280. done();
  281. });
  282. });
  283. });
  284. });
  285. describe('frontend session test', function() {
  286. describe('#bind', function() {
  287. it('should get session by uid after binded', function(done) {
  288. var service = new SessionService();
  289. var sid = 1, fid = 'frontend-server-1', socket = {};
  290. var uid = 'changchang';
  291. var eventCount = 0;
  292. var session = service.create(sid, fid, socket);
  293. var fsession = session.toFrontendSession();
  294. should.exist(fsession);
  295. fsession.on('bind', function(euid) {
  296. eventCount++;
  297. uid.should.equal(euid);
  298. });
  299. fsession.bind(uid, function(err) {
  300. should.not.exist(err);
  301. var sessions = service.getByUid(uid);
  302. should.exist(sessions);
  303. sessions.length.should.equal(1);
  304. session.should.eql(sessions[0]);
  305. eventCount.should.equal(1);
  306. done();
  307. });
  308. });
  309. });
  310. describe('#unbind', function() {
  311. it('should fail to get session after session unbinded', function(done) {
  312. var service = new SessionService();
  313. var sid = 1, fid = 'frontend-server-1', socket = {};
  314. var uid = 'py';
  315. var session = service.create(sid, fid, socket);
  316. var fsession = session.toFrontendSession();
  317. fsession.bind(uid, null);
  318. fsession.unbind(uid, function(err) {
  319. should.not.exist(err);
  320. var sessions = service.getByUid(uid);
  321. should.not.exist(sessions);
  322. done();
  323. });
  324. });
  325. });
  326. describe('#set/get', function() {
  327. it('should update the key/value pair in frontend session but not session',
  328. function() {
  329. var service = new SessionService();
  330. var sid = 1, fid = 'frontend-server-1', socket = {};
  331. var key = 'key-1', value = 'value-1';
  332. var session = service.create(sid, fid, socket);
  333. var fsession = session.toFrontendSession();
  334. fsession.set(key, value);
  335. should.not.exist(session.get(key));
  336. value.should.eql(fsession.get(key));
  337. });
  338. });
  339. describe('#push', function() {
  340. it('should push the specified key/value pair to session', function(done) {
  341. var service = new SessionService();
  342. var sid = 1, fid = 'frontend-server-1', socket = {};
  343. var key = 'key-1', value = 'value-1', key2 = 'key-2', value2 = {};
  344. var session = service.create(sid, fid, socket);
  345. var fsession = session.toFrontendSession();
  346. fsession.set(key, value);
  347. fsession.set(key2, value2);
  348. fsession.push(key, function(err) {
  349. should.not.exist(err);
  350. value.should.eql(session.get(key));
  351. should.not.exist(session.get(key2));
  352. done();
  353. });
  354. });
  355. it('should push all the key/value pairs to session', function(done) {
  356. var service = new SessionService();
  357. var sid = 1, fid = 'frontend-server-1', socket = {};
  358. var key = 'key-1', value = 'value-1', key2 = 'key-2', value2 = {};
  359. var session = service.create(sid, fid, socket);
  360. var fsession = session.toFrontendSession();
  361. fsession.set(key, value);
  362. fsession.set(key2, value2);
  363. fsession.pushAll(function(err) {
  364. should.not.exist(err);
  365. value.should.eql(session.get(key));
  366. value2.should.eql(session.get(key2));
  367. done();
  368. });
  369. });
  370. });
  371. describe('#export', function() {
  372. it('should equal frontend session after export', function(done) {
  373. var service = new SessionService();
  374. var sid = 1, fid = 'frontend-server-1', socket = {};
  375. var uid = 'py';
  376. var session = service.create(sid, fid, socket);
  377. var fsession = session.toFrontendSession();
  378. var esession = fsession.export();
  379. esession.id.should.eql(fsession.id);
  380. esession.frontendId.should.eql(fsession.frontendId);
  381. done();
  382. });
  383. });
  384. });