console.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. var should = require('should');
  2. var pomelo = require('../../');
  3. var consoleModule = require('../../lib/modules/console');
  4. describe('console module test', function() {
  5. describe('#monitorHandler', function() {
  6. it('should execute the corresponding command with different signals', function() {
  7. var flag;
  8. var rs;
  9. var opts = {
  10. app: {
  11. components: {
  12. __connector__: {
  13. blacklist: []
  14. }
  15. },
  16. stop: function(value) {
  17. flag = value;
  18. },
  19. addCrons: function(array) {
  20. rs = array;
  21. },
  22. removeCrons: function(array) {
  23. rs = array;
  24. },
  25. isFrontend: function() {
  26. return true;
  27. }
  28. }
  29. };
  30. var module = new consoleModule(opts);
  31. var agent1 = {
  32. type: 'area'
  33. };
  34. var msg1 = {signal: 'stop'};
  35. module.monitorHandler(agent1, msg1);
  36. flag.should.eql(true);
  37. var msg2 = {signal: 'list'};
  38. var agent2 = {
  39. type: 'chat',
  40. id: 'chat-server-1'
  41. };
  42. module.monitorHandler(agent2, msg2, function(obj) {
  43. obj.serverId.should.eql('chat-server-1');
  44. obj.body.serverType.should.eql('chat');
  45. });
  46. var msg3 = {signal: 'addCron'};
  47. module.monitorHandler(agent2, msg3, null);
  48. rs.length.should.eql(1);
  49. var msg4 = {signal: 'removeCron'};
  50. module.monitorHandler(agent2, msg4, null);
  51. rs.length.should.eql(1);
  52. var msg5 = {signal: 'blacklist', blacklist: ['127.0.0.1']};
  53. module.monitorHandler(agent1, msg5, null);
  54. opts.app.components.__connector__.blacklist.length.should.eql(1);
  55. });
  56. });
  57. describe('#clientHandler', function() {
  58. var _exit;
  59. var _setTimeout;
  60. var exitCount = 0;
  61. before(function(done) {
  62. _exit = process.exit;
  63. _setTimeout = setTimeout;
  64. done();
  65. });
  66. after(function(done) {
  67. process.exit = _exit;
  68. setTimeout = _setTimeout;
  69. done();
  70. });
  71. var opts = {
  72. app: {
  73. clusterSeq: {},
  74. stop: function(value) {
  75. return value;
  76. },
  77. getServerById: function() {
  78. return {
  79. host: '127.0.0.1'
  80. };
  81. },
  82. getServers: function() {
  83. return {
  84. 'chat-server-1': {
  85. }
  86. }
  87. },
  88. get: function(value) {
  89. switch(value) {
  90. case 'main':
  91. return __dirname + '/../../index.js';
  92. case 'env':
  93. return 'dev';
  94. }
  95. },
  96. set: function(value) {
  97. return value;
  98. },
  99. getServersByType: function() {
  100. return [{id: 'chat-server-1'}];
  101. }
  102. }
  103. };
  104. var module = new consoleModule(opts);
  105. it('should execute kill command', function(done) {
  106. var msg = {signal: 'kill'};
  107. process.exit = function() {exitCount++;};
  108. setTimeout = function(cb, timeout) {
  109. if (timeout > 3000) {
  110. timeout = 3000;
  111. }
  112. _setTimeout(cb, timeout);
  113. };
  114. var agent1 = {
  115. request: function(recordId, moduleId, msg, cb) {
  116. cb('chat-server-1');
  117. },
  118. idMap: {
  119. 'chat-server-1': {
  120. type: 'chat',
  121. id: 'chat-server-1'
  122. }
  123. }
  124. };
  125. module.clientHandler(agent1, msg, function(err, result) {
  126. should.not.exist(err);
  127. should.exist(result.code);
  128. });
  129. var agent2 = {
  130. request: function(recordId, moduleId, msg, cb) {
  131. cb(null);
  132. },
  133. idMap: {
  134. 'chat-server-1': {
  135. type: 'chat',
  136. id: 'chat-server-1'
  137. }
  138. }
  139. };
  140. module.clientHandler(agent2, msg, function(err, result) {
  141. should.not.exist(err);
  142. should.exist(result.code);
  143. result.code.should.eql('remained');
  144. done();
  145. });
  146. });
  147. it('should execute stop command', function(done) {
  148. var msg1 = {signal: 'stop', ids: ['chat-server-1']};
  149. var msg2 = {signal: 'stop', ids:[]};
  150. var agent = {
  151. notifyById: function(serverId, moduleId, msg) {
  152. },
  153. notifyAll: function(moduleId, msg) {
  154. }
  155. };
  156. module.clientHandler(agent, msg1, function(err, result) {
  157. result.status.should.eql('part');
  158. });
  159. module.clientHandler(agent, msg2, function(err, result) {
  160. result.status.should.eql('all');
  161. done();
  162. });
  163. });
  164. it('should execute list command', function() {
  165. var msg = {signal: 'list'};
  166. var agent = {
  167. request: function(recordId, moduleId, msg, cb) {
  168. cb({serverId: 'chat-server-1', body: {'server':{}}});
  169. },
  170. idMap: {
  171. 'chat-server-1': {
  172. type: 'chat',
  173. id: 'chat-server-1'
  174. }
  175. }
  176. };
  177. module.clientHandler(agent, msg, function(err, result) {
  178. should.exist(result.msg);
  179. });
  180. });
  181. it('should execute add command', function() {
  182. var msg1 = {signal: 'add', args: ['host=127.0.0.1', 'port=88888', 'clusterCount=2']};
  183. var msg2 = {signal: 'add', args: ['host=127.0.0.1', 'port=88888', 'id=chat-server-1', 'serverType=chat']};
  184. var agent = {};
  185. module.clientHandler(agent, msg1, function(err, result) {
  186. should.not.exist(err);
  187. result.length.should.eql(0);
  188. });
  189. module.clientHandler(agent, msg2, function(err, result) {
  190. result.status.should.eql('ok');
  191. });
  192. });
  193. it('should execute blacklist command', function() {
  194. var msg1 = {signal: 'blacklist', args: ['127.0.0.1']};
  195. var msg2 = {signal: 'blacklist', args: ['abc']};
  196. var agent = {
  197. notifyAll: function(moduleId, msg) {
  198. }
  199. };
  200. module.clientHandler(agent, msg1, function(err, result) {
  201. result.status.should.eql('ok');
  202. });
  203. module.clientHandler(agent, msg2, function(err, result) {
  204. should.exist(err);
  205. });
  206. });
  207. it('should execute restart command', function() {
  208. var msg1 = {signal: 'restart', ids:['chat-server-1']};
  209. var msg2 = {signal: 'restart', type:'chat', ids:[]};
  210. var agent = {
  211. request: function(recordId, moduleId, msg, cb) {
  212. cb(null);
  213. }
  214. };
  215. module.clientHandler(agent, msg1, function(err, result) {
  216. should.exist(err);
  217. });
  218. module.clientHandler(agent, msg2, function(err, result) {
  219. should.exist(err);
  220. });
  221. });
  222. });
  223. });