channelService.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. var should = require('should');
  2. var pomelo = require('../../');
  3. var ChannelService = require('../../lib/common/service/channelService');
  4. var channelName = 'test_channel';
  5. var mockBase = process.cwd() + '/test';
  6. var mockApp = {serverId: 'test-server-1'};
  7. describe('channel manager test', function() {
  8. describe('#createChannel', function() {
  9. it('should create and return a channel with the specified name', function() {
  10. var channelService = new ChannelService(mockApp);
  11. var channel = channelService.createChannel(channelName);
  12. should.exist(channel);
  13. channelName.should.equal(channel.name);
  14. });
  15. it('should return the same channel if the name has already existed', function() {
  16. var channelService = new ChannelService(mockApp);
  17. var channel = channelService.createChannel(channelName);
  18. should.exist(channel);
  19. channelName.should.equal(channel.name);
  20. var channel2 = channelService.createChannel(channelName);
  21. channel.should.equal(channel2);
  22. });
  23. });
  24. describe('#destroyChannel', function() {
  25. it('should delete the channel instance', function() {
  26. var channelService = new ChannelService(mockApp);
  27. var channel = channelService.createChannel(channelName);
  28. should.exist(channel);
  29. channelName.should.equal(channel.name);
  30. channelService.destroyChannel(channelName);
  31. var channel2 = channelService.createChannel(channelName);
  32. channel.should.not.equal(channel2);
  33. });
  34. });
  35. describe('#getChannel', function() {
  36. it('should return the channel with the specified name if it exists', function() {
  37. var channelService = new ChannelService(mockApp);
  38. channelService.createChannel(channelName);
  39. var channel = channelService.getChannel(channelName);
  40. should.exist(channel);
  41. channelName.should.equal(channel.name);
  42. });
  43. it('should return undefined if the channel dose not exist', function() {
  44. var channelService = new ChannelService(mockApp);
  45. var channel = channelService.getChannel(channelName);
  46. should.not.exist(channel);
  47. });
  48. it('should create and return a new channel if create parameter is set', function() {
  49. var channelService = new ChannelService(mockApp);
  50. var channel = channelService.getChannel(channelName, true);
  51. should.exist(channel);
  52. channelName.should.equal(channel.name);
  53. });
  54. });
  55. describe('#pushMessageByUids', function() {
  56. it('should push message to the right frontend server', function(done) {
  57. var sid1 = 'sid1', sid2 = 'sid2';
  58. var uid1 = 'uid1', uid2 = 'uid2', uid3 = 'uid3';
  59. var orgRoute = 'test.route.string';
  60. var mockUids = [
  61. {sid: sid1, uid: uid1},
  62. {sid: sid2, uid: uid2},
  63. {sid: sid2, uid: uid3}
  64. ];
  65. var mockMsg = {key: 'some remote message'};
  66. var uidMap = {};
  67. for(var i in mockUids) {
  68. uidMap[mockUids[i].uid] = mockUids[i];
  69. }
  70. var invokeCount = 0;
  71. var mockRpcInvoke = function(sid, rmsg, cb) {
  72. invokeCount++;
  73. var args = rmsg.args;
  74. var route = args[0];
  75. var msg = args[1];
  76. var uids = args[2];
  77. mockMsg.should.eql(msg);
  78. for(var j=0, l=uids.length; j<l; j++) {
  79. var uid = uids[j];
  80. var r2 = uidMap[uid];
  81. r2.sid.should.equal(sid);
  82. }
  83. cb();
  84. };
  85. var app = pomelo.createApp({base: mockBase});
  86. app.rpcInvoke = mockRpcInvoke;
  87. var channelService = new ChannelService(app);
  88. channelService.pushMessageByUids(orgRoute, mockMsg, mockUids, function() {
  89. invokeCount.should.equal(2);
  90. done();
  91. });
  92. });
  93. it('should return an err if uids is empty', function(done) {
  94. var mockMsg = {key: 'some remote message'};
  95. var app = pomelo.createApp({base: mockBase});
  96. var channelService = new ChannelService(app);
  97. channelService.pushMessageByUids(mockMsg, null, function(err) {
  98. should.exist(err);
  99. err.message.should.equal('uids should not be empty');
  100. done();
  101. });
  102. });
  103. it('should return err if all message fail to push', function(done) {
  104. var sid1 = 'sid1', sid2 = 'sid2';
  105. var uid1 = 'uid1', uid2 = 'uid2', uid3 = 'uid3';
  106. var mockUids = [
  107. {sid: sid1, uid: uid1},
  108. {sid: sid2, uid: uid2},
  109. {sid: sid2, uid: uid3}
  110. ];
  111. var mockMsg = {key: 'some remote message'};
  112. var uidMap = {};
  113. for(var i in mockUids) {
  114. uidMap[mockUids[i].uid] = mockUids[i];
  115. }
  116. var invokeCount = 0;
  117. var mockRpcInvoke = function(sid, rmsg, cb) {
  118. invokeCount++;
  119. cb(new Error('[TestMockError] mock rpc error'));
  120. };
  121. var app = pomelo.createApp({base: mockBase});
  122. app.rpcInvoke = mockRpcInvoke;
  123. var channelService = new ChannelService(app);
  124. channelService.pushMessageByUids(mockMsg, mockUids, function(err) {
  125. invokeCount.should.equal(2);
  126. should.exist(err);
  127. err.message.should.equal('all uids push message fail');
  128. done();
  129. });
  130. });
  131. it('should return fail uid list if fail to push messge to some of the uids', function(done) {
  132. var sid1 = 'sid1', sid2 = 'sid2';
  133. var uid1 = 'uid1', uid2 = 'uid2', uid3 = 'uid3';
  134. var mockUids = [{sid: sid1, uid: uid1}, {sid: sid2, uid: uid2}, {sid: sid2, uid: uid3}];
  135. var mockMsg = {key: 'some remote message'};
  136. var uidMap = {};
  137. for(var i in mockUids) {
  138. uidMap[mockUids[i].uid] = mockUids[i];
  139. }
  140. var invokeCount = 0;
  141. var mockRpcInvoke = function(sid, rmsg, cb) {
  142. invokeCount++;
  143. if(rmsg.args[2].indexOf(uid1) >= 0) {
  144. cb(null, [uid1]);
  145. } else if(rmsg.args[2].indexOf(uid3) >= 0) {
  146. cb(null, [uid3]);
  147. } else {
  148. cb();
  149. }
  150. };
  151. var app = pomelo.createApp({base: mockBase});
  152. app.rpcInvoke = mockRpcInvoke;
  153. var channelService = new ChannelService(app);
  154. channelService.pushMessageByUids(mockMsg, mockUids, function(err, fails) {
  155. invokeCount.should.equal(2);
  156. should.not.exist(err);
  157. should.exist(fails);
  158. fails.length.should.equal(2);
  159. fails.should.include(uid1);
  160. fails.should.include(uid3);
  161. done();
  162. });
  163. });
  164. });
  165. describe('#broadcast', function() {
  166. it('should push message to all specified frontend servers', function(done) {
  167. var mockServers = [
  168. {id: 'connector-1', serverType: 'connector', other: 'xxx1'},
  169. {id: 'connector-2', serverType: 'connector', other: 'xxx2'},
  170. {id: 'area-1', serverType: 'area', other: 'yyy1'},
  171. {id: 'gate-1', serverType: 'gate', other: 'zzz1'},
  172. {id: 'gate-2', serverType: 'gate', other: 'xxx1'},
  173. {id: 'gate-3', serverType: 'gate', other: 'yyy1'}
  174. ];
  175. var connectorIds = ['connector-1', 'connector-2'];
  176. var mockSType = 'connector';
  177. var mockRoute = 'test.route.string';
  178. var mockBinded = true;
  179. var opts = {binded: mockBinded};
  180. var mockMsg = {key: 'some remote message'};
  181. var invokeCount = 0;
  182. var sids = [];
  183. var mockRpcInvoke = function(sid, rmsg, cb) {
  184. invokeCount++;
  185. var args = rmsg.args;
  186. var route = args[0];
  187. var msg = args[1];
  188. var opts = args[2];
  189. mockMsg.should.eql(msg);
  190. mockRoute.should.equal(route);
  191. should.exist(opts);
  192. mockBinded.should.equal(opts.userOptions.binded);
  193. sids.push(sid);
  194. cb();
  195. };
  196. var app = pomelo.createApp({base: mockBase});
  197. app.rpcInvoke = mockRpcInvoke;
  198. app.addServers(mockServers);
  199. var channelService = new ChannelService(app);
  200. channelService.broadcast(mockSType, mockRoute, mockMsg,
  201. opts, function() {
  202. invokeCount.should.equal(2);
  203. sids.length.should.equal(connectorIds.length);
  204. for(var i=0, l=connectorIds.length; i<l; i++) {
  205. sids.should.include(connectorIds[i]);
  206. }
  207. done();
  208. });
  209. });
  210. });
  211. });