channelRemote.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. var should = require('should');
  2. var pomelo = require('../../');
  3. var remote = require('../../lib/common/remote/frontend/channelRemote');
  4. var SessionService = require('../../lib/common/service/sessionService');
  5. var ChannelService = require('../../lib/common/service/channelService');
  6. var countDownLatch = require('../../lib/util/countDownLatch');
  7. var MockChannelManager = require('../manager/mockChannelManager');
  8. var mockBase = process.cwd() + '/test';
  9. var WAIT_TIME = 200;
  10. describe('channel remote test', function() {
  11. describe('#pushMessage', function() {
  12. it('should push message the the specified clients', function(done) {
  13. var sids = [1, 2, 3, 4, 5, 6];
  14. var uids = [11, 12, 13];
  15. var frontendId = 'frontend-server-id';
  16. var mockRoute = 'mock-route-string';
  17. var mockMsg = {msg: 'some test msg'};
  18. var invokeCount = 0;
  19. var invokeUids = [];
  20. var sessionService = new SessionService();
  21. sessionService.sendMessageByUid = function(uid, msg) {
  22. mockMsg.should.eql(msg);
  23. invokeCount++;
  24. invokeUids.push(uid);
  25. };
  26. var session;
  27. for(var i=0, l=sids.length, j=0; i<l; i++) {
  28. session = sessionService.create(sids[i], frontendId);
  29. if(i % 2) {
  30. sessionService.bind(session.id, uids[j]);
  31. j++;
  32. }
  33. }
  34. var app = pomelo.createApp({base: mockBase});
  35. app.components.__connector__ = {
  36. send: function(reqId, route, msg, recvs, opts, cb) {
  37. app.components.__pushScheduler__.schedule(reqId, route, msg, recvs, opts, cb);
  38. }
  39. };
  40. app.components.__connector__.connector = {};
  41. app.components.__pushScheduler__ = {
  42. schedule: function(reqId, route, msg, recvs, opts, cb) {
  43. mockMsg.should.eql(msg);
  44. invokeCount += recvs.length;
  45. var sess;
  46. for(var i=0; i<recvs.length; i++) {
  47. sess = sessionService.get(recvs[i]);
  48. if(sess) {
  49. invokeUids.push(sess.uid);
  50. }
  51. }
  52. cb();
  53. }
  54. };
  55. app.set('sessionService', sessionService);
  56. var channelRemote = remote(app);
  57. channelRemote.pushMessage(mockRoute, mockMsg, uids, {isPush: true}, function() {
  58. invokeCount.should.equal(uids.length);
  59. invokeUids.length.should.equal(uids.length);
  60. for(var i=0, l=uids.length; i<l; i++) {
  61. invokeUids.should.include(uids[i]);
  62. }
  63. done();
  64. });
  65. });
  66. });
  67. describe('#broadcast', function() {
  68. it('should broadcast to all the client connected', function(done) {
  69. var sids = [1, 2, 3, 4, 5];
  70. var uids = [11, 12, 13, 14, 15];
  71. var frontendId = 'frontend-server-id';
  72. var mockRoute = 'mock-route-string';
  73. var mockMsg = {msg: 'some test msg'};
  74. var invokeCount = 0;
  75. var sessionService = new SessionService();
  76. var channelService = new ChannelService();
  77. var session;
  78. for(var i=0, l=sids.length; i<l; i++) {
  79. session = sessionService.create(sids[i], frontendId);
  80. if(i % 2) {
  81. session.bind(uids[i]);
  82. }
  83. }
  84. var app = pomelo.createApp({base: mockBase});
  85. app.components.__connector__ = {
  86. send: function(reqId, route, msg, recvs, opts, cb) {
  87. app.components.__pushScheduler__.schedule(reqId, route, msg, recvs, opts, cb);
  88. }
  89. };
  90. app.components.__connector__.connector = {};
  91. app.components.__pushScheduler__ = {
  92. schedule: function(reqId, route, msg, recvs, opts, cb) {
  93. invokeCount++;
  94. mockMsg.should.eql(msg);
  95. should.exist(opts);
  96. should.equal(opts.type, 'broadcast');
  97. cb();
  98. }
  99. };
  100. app.set('sessionService', sessionService);
  101. app.set('channelService', channelService);
  102. var channelRemote = remote(app);
  103. channelRemote.broadcast(mockRoute, mockMsg, {type: 'broadcast'}, function() {
  104. invokeCount.should.equal(1);
  105. done();
  106. });
  107. });
  108. it('should broadcast to all the binded client connected', function(done) {
  109. var sids = [1, 2, 3, 4, 5, 6];
  110. var uids = [11, 12, 13];
  111. var frontendId = 'frontend-server-id';
  112. var mockRoute = 'mock-route-string';
  113. var mockMsg = {msg: 'some test msg'};
  114. var invokeCount = 0;
  115. var invokeUids = [];
  116. var sessionService = new SessionService();
  117. var channelService = new ChannelService();
  118. var session;
  119. for(var i=0, l=sids.length, j=0; i<l; i++) {
  120. session = sessionService.create(sids[i], frontendId);
  121. if(i % 2) {
  122. session.bind(uids[j]);
  123. j++;
  124. }
  125. }
  126. var app = pomelo.createApp({base: mockBase});
  127. app.components.__connector__ = {
  128. send: function(reqId, route, msg, recvs, opts, cb) {
  129. app.components.__pushScheduler__.schedule(reqId, route, msg, recvs, opts, cb);
  130. }
  131. };
  132. app.components.__connector__.connector = {};
  133. app.components.__pushScheduler__ = {
  134. schedule: function(reqId, route, msg, recvs, opts, cb) {
  135. invokeCount++;
  136. mockMsg.should.eql(msg);
  137. should.exist(opts);
  138. should.equal(opts.type, 'broadcast');
  139. true.should.equal(opts.userOptions.binded);
  140. cb();
  141. }
  142. };
  143. app.set('sessionService', sessionService);
  144. app.set('channelService', channelService);
  145. var channelRemote = remote(app);
  146. channelRemote.broadcast(mockRoute, mockMsg, {type: 'broadcast', userOptions: {binded: true}}, function() {
  147. invokeCount.should.equal(1);
  148. done();
  149. });
  150. });
  151. });
  152. });