channel.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. var should = require('should');
  2. var pomelo = require('../../');
  3. var ChannelService = require('../../lib/common/service/channelService');
  4. var mockBase = process.cwd() + '/test';
  5. var channelName = 'test_channel';
  6. var mockApp = {serverId: 'test-server-1'};
  7. describe('channel test', function() {
  8. describe('#add', function() {
  9. it('should add a member into channel and could fetch it later', function() {
  10. var channelService = new ChannelService(mockApp);
  11. var channel = channelService.createChannel(channelName);
  12. should.exist(channel);
  13. var uid = 'uid1', sid = 'sid1';
  14. channel.add(uid, sid).should.be.true;
  15. var member = channel.getMember(uid);
  16. should.exist(member);
  17. uid.should.equal(member.uid);
  18. sid.should.equal(member.sid);
  19. });
  20. it('should fail if the sid not specified', function() {
  21. var channelService = new ChannelService(mockApp);
  22. var channel = channelService.createChannel(channelName);
  23. should.exist(channel);
  24. var uid = 'uid1';
  25. channel.add(uid, null).should.be.false;
  26. });
  27. it('should fail after the channel has been destroied', function() {
  28. var channelService = new ChannelService(mockApp);
  29. var channel = channelService.createChannel(channelName);
  30. should.exist(channel);
  31. channel.destroy();
  32. var uid = 'uid1', sid = 'sid1';
  33. channel.add(uid, sid).should.be.false;
  34. });
  35. });
  36. describe('#leave', function() {
  37. it('should remove the member from channel when leave', function() {
  38. var channelService = new ChannelService(mockApp);
  39. var channel = channelService.createChannel(channelName);
  40. should.exist(channel);
  41. var uid = 'uid1', sid = 'sid1';
  42. channel.add(uid, sid).should.be.true;
  43. var member = channel.getMember(uid);
  44. should.exist(member);
  45. channel.leave(uid, sid);
  46. member = channel.getMember(uid);
  47. should.not.exist(member);
  48. });
  49. it('should fail if uid or sid not specified', function() {
  50. var channelService = new ChannelService(mockApp);
  51. var channel = channelService.createChannel(channelName);
  52. should.exist(channel);
  53. var uid = 'uid1', sid = 'sid1';
  54. channel.add(uid, sid).should.be.true;
  55. channel.leave(uid, null).should.be.false;
  56. channel.leave(null, sid).should.be.false;
  57. });
  58. });
  59. describe('#getMembers', function() {
  60. it('should return all the members of channel', function() {
  61. var uinfos = [
  62. {uid: 'uid1', sid: 'sid1'},
  63. {uid: 'uid2', sid: 'sid2'},
  64. {uid: 'uid3', sid: 'sid3'}
  65. ];
  66. var channelService = new ChannelService(mockApp);
  67. var channel = channelService.createChannel(channelName);
  68. var i, l, item;
  69. for(i=0, l=uinfos.length; i<l; i++) {
  70. item = uinfos[i];
  71. channel.add(item.uid, item.sid);
  72. }
  73. var members = channel.getMembers();
  74. should.exist(members);
  75. members.length.should.equal(uinfos.length);
  76. for(i=0, l=uinfos.length; i<l; i++) {
  77. item = uinfos[i];
  78. members.should.include(item.uid);
  79. }
  80. });
  81. });
  82. describe('#pushMessage', function() {
  83. it('should push message to the right frontend server by sid', function(done) {
  84. var sid1 = 'sid1', sid2 = 'sid2';
  85. var uid1 = 'uid1', uid2 = 'uid2', uid3 = 'uid3';
  86. var mockUids = [{sid: sid1, uid: uid1}, {sid: sid2, uid: uid2}, {sid: sid2, uid: uid3}];
  87. var mockMsg = {key: 'some remote message'};
  88. var uidMap = {};
  89. for(var i in mockUids) {
  90. uidMap[mockUids[i].uid] = mockUids[i];
  91. }
  92. var invokeCount = 0;
  93. var mockRpcInvoke = function(sid, rmsg, cb) {
  94. invokeCount++;
  95. var args = rmsg.args;
  96. var route = args[0];
  97. var msg = args[1];
  98. var uids = args[2];
  99. mockMsg.should.eql(msg);
  100. for(var j=0, l=uids.length; j<l; j++) {
  101. var uid = uids[j];
  102. var r2 = uidMap[uid];
  103. r2.sid.should.equal(sid);
  104. }
  105. cb();
  106. };
  107. var app = pomelo.createApp({base: mockBase});
  108. app.rpcInvoke = mockRpcInvoke;
  109. var channelService = new ChannelService(app);
  110. var channel = channelService.createChannel(channelName);
  111. for(var i=0, l=mockUids.length; i<l; i++) {
  112. channel.add(mockUids[i].uid, mockUids[i].sid);
  113. }
  114. channel.pushMessage(mockMsg, function() {
  115. invokeCount.should.equal(2);
  116. done();
  117. });
  118. });
  119. it('should fail if channel has destroied', function() {
  120. var channelService = new ChannelService(mockApp);
  121. var channel = channelService.createChannel(channelName);
  122. should.exist(channel);
  123. channel.destroy();
  124. channel.pushMessage({}, function(err) {
  125. should.exist(err);
  126. err.message.should.equal('channel is not running now');
  127. });
  128. });
  129. });
  130. });