@bigtyphoon/melo 1.7.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (296) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +55 -0
  3. package/bin/commadtest.ts +10 -0
  4. package/bin/commands/add.ts +43 -0
  5. package/bin/commands/init.ts +292 -0
  6. package/bin/commands/kill.ts +21 -0
  7. package/bin/commands/list.ts +60 -0
  8. package/bin/commands/masterha.ts +40 -0
  9. package/bin/commands/restart.ts +48 -0
  10. package/bin/commands/start.ts +65 -0
  11. package/bin/commands/stop.ts +26 -0
  12. package/bin/melo.ts +30 -0
  13. package/bin/utils/constants.ts +27 -0
  14. package/bin/utils/utils.ts +130 -0
  15. package/dist/bin/commadtest.js +9 -0
  16. package/dist/bin/commands/add.js +40 -0
  17. package/dist/bin/commands/init.js +279 -0
  18. package/dist/bin/commands/kill.js +21 -0
  19. package/dist/bin/commands/list.js +65 -0
  20. package/dist/bin/commands/masterha.js +36 -0
  21. package/dist/bin/commands/restart.js +45 -0
  22. package/dist/bin/commands/start.js +58 -0
  23. package/dist/bin/commands/stop.js +20 -0
  24. package/dist/bin/melo.js +26 -0
  25. package/dist/bin/utils/constants.js +28 -0
  26. package/dist/bin/utils/utils.js +134 -0
  27. package/dist/lib/application.js +888 -0
  28. package/dist/lib/common/manager/appManager.js +112 -0
  29. package/dist/lib/common/manager/taskManager.js +39 -0
  30. package/dist/lib/common/remote/backend/msgRemote.js +63 -0
  31. package/dist/lib/common/remote/frontend/channelRemote.js +78 -0
  32. package/dist/lib/common/remote/frontend/sessionRemote.js +76 -0
  33. package/dist/lib/common/service/backendSessionService.js +337 -0
  34. package/dist/lib/common/service/channelService.js +514 -0
  35. package/dist/lib/common/service/connectionService.js +95 -0
  36. package/dist/lib/common/service/filterService.js +112 -0
  37. package/dist/lib/common/service/handlerService.js +187 -0
  38. package/dist/lib/common/service/sessionService.js +610 -0
  39. package/dist/lib/components/backendSession.js +14 -0
  40. package/dist/lib/components/channel.js +13 -0
  41. package/dist/lib/components/connection.js +12 -0
  42. package/dist/lib/components/connector.js +437 -0
  43. package/dist/lib/components/dictionary.js +93 -0
  44. package/dist/lib/components/master.js +39 -0
  45. package/dist/lib/components/monitor.js +25 -0
  46. package/dist/lib/components/protobuf.js +156 -0
  47. package/dist/lib/components/proxy.js +236 -0
  48. package/dist/lib/components/pushScheduler.js +62 -0
  49. package/dist/lib/components/remote.js +127 -0
  50. package/dist/lib/components/server.js +63 -0
  51. package/dist/lib/components/session.js +20 -0
  52. package/dist/lib/connectors/commands/handshake.js +119 -0
  53. package/dist/lib/connectors/commands/heartbeat.js +67 -0
  54. package/dist/lib/connectors/commands/kick.js +15 -0
  55. package/dist/lib/connectors/common/coder.js +90 -0
  56. package/dist/lib/connectors/common/handler.js +57 -0
  57. package/dist/lib/connectors/hybrid/IHybridSocket.js +3 -0
  58. package/dist/lib/connectors/hybrid/switcher.js +100 -0
  59. package/dist/lib/connectors/hybrid/tcpprocessor.js +40 -0
  60. package/dist/lib/connectors/hybrid/tcpsocket.js +171 -0
  61. package/dist/lib/connectors/hybrid/wsprocessor.js +49 -0
  62. package/dist/lib/connectors/hybridconnector.js +89 -0
  63. package/dist/lib/connectors/hybridsocket.js +139 -0
  64. package/dist/lib/connectors/mqtt/generate.js +113 -0
  65. package/dist/lib/connectors/mqtt/mqttadaptor.js +81 -0
  66. package/dist/lib/connectors/mqtt/protocol.js +48 -0
  67. package/dist/lib/connectors/mqttconnector.js +107 -0
  68. package/dist/lib/connectors/mqttsocket.js +59 -0
  69. package/dist/lib/connectors/sioconnector.js +135 -0
  70. package/dist/lib/connectors/siosocket.js +69 -0
  71. package/dist/lib/connectors/udpconnector.js +76 -0
  72. package/dist/lib/connectors/udpsocket.js +93 -0
  73. package/dist/lib/filters/handler/serial.js +44 -0
  74. package/dist/lib/filters/handler/time.js +32 -0
  75. package/dist/lib/filters/handler/timeout.js +45 -0
  76. package/dist/lib/filters/handler/toobusy.js +36 -0
  77. package/dist/lib/filters/rpc/rpcLog.js +43 -0
  78. package/dist/lib/filters/rpc/toobusy.js +41 -0
  79. package/dist/lib/index.js +81 -0
  80. package/dist/lib/interfaces/IComponent.js +3 -0
  81. package/dist/lib/interfaces/IConnector.js +3 -0
  82. package/dist/lib/interfaces/IHandlerFilter.js +3 -0
  83. package/dist/lib/interfaces/ILifeCycle.js +3 -0
  84. package/dist/lib/interfaces/IPlugin.js +3 -0
  85. package/dist/lib/interfaces/IPushScheduler.js +3 -0
  86. package/dist/lib/interfaces/ISocket.js +3 -0
  87. package/dist/lib/interfaces/IStore.js +3 -0
  88. package/dist/lib/interfaces/define.js +3 -0
  89. package/dist/lib/master/master.js +129 -0
  90. package/dist/lib/master/starter.js +236 -0
  91. package/dist/lib/master/watchdog.js +120 -0
  92. package/dist/lib/melo.js +125 -0
  93. package/dist/lib/modules/console.js +436 -0
  94. package/dist/lib/modules/masterwatcher.js +98 -0
  95. package/dist/lib/modules/monitorwatcher.js +124 -0
  96. package/dist/lib/modules/onlineUser.js +69 -0
  97. package/dist/lib/modules/restartNotifyModule.js +107 -0
  98. package/dist/lib/modules/watchServer.js +737 -0
  99. package/dist/lib/monitor/monitor.js +80 -0
  100. package/dist/lib/pushSchedulers/buffer.js +96 -0
  101. package/dist/lib/pushSchedulers/direct.js +58 -0
  102. package/dist/lib/pushSchedulers/multi.js +80 -0
  103. package/dist/lib/server/server.js +500 -0
  104. package/dist/lib/util/appUtil.js +306 -0
  105. package/dist/lib/util/constants.js +117 -0
  106. package/dist/lib/util/countDownLatch.js +51 -0
  107. package/dist/lib/util/events.js +20 -0
  108. package/dist/lib/util/handlerHelper.js +8 -0
  109. package/dist/lib/util/log.js +14 -0
  110. package/dist/lib/util/moduleUtil.js +101 -0
  111. package/dist/lib/util/pathUtil.js +134 -0
  112. package/dist/lib/util/remoterHelper.js +8 -0
  113. package/dist/lib/util/utils.js +358 -0
  114. package/dist/test/application.js +522 -0
  115. package/dist/test/config/log4js.json +28 -0
  116. package/dist/test/config/master.json +9 -0
  117. package/dist/test/config/servers.json +6 -0
  118. package/dist/test/filters/handler/serial.js +41 -0
  119. package/dist/test/filters/handler/time.js +41 -0
  120. package/dist/test/filters/handler/timeout.js +41 -0
  121. package/dist/test/filters/handler/toobusy.js +57 -0
  122. package/dist/test/filters/rpc/rpcLog.js +22 -0
  123. package/dist/test/filters/rpc/toobusy.js +39 -0
  124. package/dist/test/manager/mockChannelManager.js +77 -0
  125. package/dist/test/manager/taskManager.js +68 -0
  126. package/dist/test/mock-base/app/servers/other-file +0 -0
  127. package/dist/test/mock-plugin/components/mockPlugin.js +10 -0
  128. package/dist/test/mock-plugin/events/mockEvent.js +12 -0
  129. package/dist/test/modules/console.js +242 -0
  130. package/dist/test/pomelo.js +19 -0
  131. package/dist/test/remote/channelRemote.js +159 -0
  132. package/dist/test/service/channel.js +134 -0
  133. package/dist/test/service/channelService.js +216 -0
  134. package/dist/test/service/connectionService.js +114 -0
  135. package/dist/test/service/filterService.js +144 -0
  136. package/dist/test/service/handlerService.js +65 -0
  137. package/dist/test/service/sessionService.js +387 -0
  138. package/dist/test/util/countDownLatch.js +70 -0
  139. package/dist/test/util/pathUtil.js +108 -0
  140. package/dist/test/util/utils.js +140 -0
  141. package/lib/application.ts +1240 -0
  142. package/lib/common/manager/appManager.ts +118 -0
  143. package/lib/common/manager/taskManager.ts +50 -0
  144. package/lib/common/remote/backend/msgRemote.ts +134 -0
  145. package/lib/common/remote/frontend/channelRemote.ts +91 -0
  146. package/lib/common/remote/frontend/sessionRemote.ts +91 -0
  147. package/lib/common/service/backendSessionService.ts +388 -0
  148. package/lib/common/service/channelService.ts +609 -0
  149. package/lib/common/service/connectionService.ts +112 -0
  150. package/lib/common/service/filterService.ts +118 -0
  151. package/lib/common/service/handlerService.ts +224 -0
  152. package/lib/common/service/sessionService.ts +731 -0
  153. package/lib/components/backendSession.ts +14 -0
  154. package/lib/components/channel.ts +11 -0
  155. package/lib/components/connection.ts +13 -0
  156. package/lib/components/connector.ts +533 -0
  157. package/lib/components/dictionary.ts +121 -0
  158. package/lib/components/master.ts +41 -0
  159. package/lib/components/monitor.ts +30 -0
  160. package/lib/components/protobuf.ts +208 -0
  161. package/lib/components/proxy.ts +282 -0
  162. package/lib/components/pushScheduler.ts +70 -0
  163. package/lib/components/remote.ts +166 -0
  164. package/lib/components/server.ts +71 -0
  165. package/lib/components/session.ts +22 -0
  166. package/lib/connectors/commands/handshake.ts +155 -0
  167. package/lib/connectors/commands/heartbeat.ts +83 -0
  168. package/lib/connectors/commands/kick.ts +11 -0
  169. package/lib/connectors/common/coder.ts +93 -0
  170. package/lib/connectors/common/handler.ts +62 -0
  171. package/lib/connectors/hybrid/IHybridSocket.ts +9 -0
  172. package/lib/connectors/hybrid/switcher.ts +142 -0
  173. package/lib/connectors/hybrid/tcpprocessor.ts +43 -0
  174. package/lib/connectors/hybrid/tcpsocket.ts +223 -0
  175. package/lib/connectors/hybrid/wsprocessor.ts +57 -0
  176. package/lib/connectors/hybridconnector.ts +134 -0
  177. package/lib/connectors/hybridsocket.ts +168 -0
  178. package/lib/connectors/mqtt/generate.ts +103 -0
  179. package/lib/connectors/mqtt/mqttadaptor.ts +114 -0
  180. package/lib/connectors/mqtt/protocol.ts +49 -0
  181. package/lib/connectors/mqttconnector.ts +134 -0
  182. package/lib/connectors/mqttsocket.ts +79 -0
  183. package/lib/connectors/sioconnector.ts +161 -0
  184. package/lib/connectors/siosocket.ts +85 -0
  185. package/lib/connectors/udpconnector.ts +113 -0
  186. package/lib/connectors/udpsocket.ts +110 -0
  187. package/lib/filters/handler/serial.ts +46 -0
  188. package/lib/filters/handler/time.ts +35 -0
  189. package/lib/filters/handler/timeout.ts +50 -0
  190. package/lib/filters/handler/toobusy.ts +37 -0
  191. package/lib/filters/rpc/rpcLog.ts +42 -0
  192. package/lib/filters/rpc/toobusy.ts +41 -0
  193. package/lib/index.ts +74 -0
  194. package/lib/interfaces/IComponent.ts +47 -0
  195. package/lib/interfaces/IConnector.ts +20 -0
  196. package/lib/interfaces/IHandlerFilter.ts +15 -0
  197. package/lib/interfaces/ILifeCycle.ts +16 -0
  198. package/lib/interfaces/IPlugin.ts +65 -0
  199. package/lib/interfaces/IPushScheduler.ts +52 -0
  200. package/lib/interfaces/ISocket.ts +26 -0
  201. package/lib/interfaces/IStore.ts +10 -0
  202. package/lib/interfaces/define.ts +15 -0
  203. package/lib/master/master.ts +148 -0
  204. package/lib/master/starter.ts +234 -0
  205. package/lib/master/watchdog.ts +135 -0
  206. package/lib/melo.ts +152 -0
  207. package/lib/modules/console.ts +465 -0
  208. package/lib/modules/masterwatcher.ts +120 -0
  209. package/lib/modules/monitorwatcher.ts +151 -0
  210. package/lib/modules/onlineUser.ts +78 -0
  211. package/lib/modules/restartNotifyModule.ts +128 -0
  212. package/lib/modules/watchServer.ts +766 -0
  213. package/lib/monitor/monitor.ts +99 -0
  214. package/lib/pushSchedulers/buffer.ts +117 -0
  215. package/lib/pushSchedulers/direct.ts +65 -0
  216. package/lib/pushSchedulers/multi.ts +94 -0
  217. package/lib/server/server.ts +554 -0
  218. package/lib/util/appUtil.ts +313 -0
  219. package/lib/util/constants.ts +154 -0
  220. package/lib/util/countDownLatch.ts +72 -0
  221. package/lib/util/events.ts +15 -0
  222. package/lib/util/handlerHelper.ts +5 -0
  223. package/lib/util/log.ts +11 -0
  224. package/lib/util/moduleUtil.ts +110 -0
  225. package/lib/util/pathUtil.ts +132 -0
  226. package/lib/util/remoterHelper.ts +68 -0
  227. package/lib/util/utils.ts +365 -0
  228. package/package.json +93 -0
  229. package/template/game-server/.vscode/launch.json +27 -0
  230. package/template/game-server/app/servers/connector/handler/entryHandler.ts +50 -0
  231. package/template/game-server/app/servers/connector/remote/authRemoter.ts +36 -0
  232. package/template/game-server/app.ts +30 -0
  233. package/template/game-server/app.ts.mqtt +32 -0
  234. package/template/game-server/app.ts.sio +36 -0
  235. package/template/game-server/app.ts.sio.wss +34 -0
  236. package/template/game-server/app.ts.udp +31 -0
  237. package/template/game-server/app.ts.wss +40 -0
  238. package/template/game-server/config/adminServer.json +5 -0
  239. package/template/game-server/config/adminUser.json +22 -0
  240. package/template/game-server/config/clientProtos.json +1 -0
  241. package/template/game-server/config/dictionary.json +1 -0
  242. package/template/game-server/config/log4js.json +150 -0
  243. package/template/game-server/config/master.json +8 -0
  244. package/template/game-server/config/serverProtos.json +1 -0
  245. package/template/game-server/config/servers.json +12 -0
  246. package/template/game-server/copy.js +5 -0
  247. package/template/game-server/package.json +28 -0
  248. package/template/game-server/preload.ts +40 -0
  249. package/template/game-server/tsconfig.json +36 -0
  250. package/template/npm-install.bat +4 -0
  251. package/template/npm-install.sh +5 -0
  252. package/template/readme.md +31 -0
  253. package/template/shared/server.crt +15 -0
  254. package/template/shared/server.key +15 -0
  255. package/template/web-server/app.js +25 -0
  256. package/template/web-server/app.js.https +36 -0
  257. package/template/web-server/bin/component.bat +1 -0
  258. package/template/web-server/bin/component.sh +1 -0
  259. package/template/web-server/package.json +10 -0
  260. package/template/web-server/public/css/base.css +76 -0
  261. package/template/web-server/public/image/logo.png +0 -0
  262. package/template/web-server/public/image/sp.png +0 -0
  263. package/template/web-server/public/index.html +57 -0
  264. package/template/web-server/public/index.html.sio +58 -0
  265. package/template/web-server/public/js/lib/build/build.js +1730 -0
  266. package/template/web-server/public/js/lib/build/build.js.wss +1721 -0
  267. package/template/web-server/public/js/lib/component.json +6 -0
  268. package/template/web-server/public/js/lib/local/boot/component.json +11 -0
  269. package/template/web-server/public/js/lib/local/boot/index.js +11 -0
  270. package/template/web-server/public/js/lib/meloclient.js +456 -0
  271. package/template/web-server/public/js/lib/meloclient.js.wss +456 -0
  272. package/template/web-server/public/js/lib/socket.io.js +3 -0
  273. package/test/application.ts +607 -0
  274. package/test/filters/handler/serial.ts +47 -0
  275. package/test/filters/handler/time.ts +47 -0
  276. package/test/filters/handler/timeout.ts +46 -0
  277. package/test/filters/handler/toobusy.ts +59 -0
  278. package/test/filters/rpc/rpcLog.ts +20 -0
  279. package/test/filters/rpc/toobusy.ts +40 -0
  280. package/test/manager/mockChannelManager.ts +92 -0
  281. package/test/manager/taskManager.ts +78 -0
  282. package/test/mock-base/app/servers/other-file +0 -0
  283. package/test/mock-plugin/components/mockPlugin.ts +8 -0
  284. package/test/mock-plugin/events/mockEvent.ts +12 -0
  285. package/test/modules/console.ts +264 -0
  286. package/test/pomelo.ts +18 -0
  287. package/test/remote/channelRemote.ts +176 -0
  288. package/test/service/channel.ts +161 -0
  289. package/test/service/channelService.ts +243 -0
  290. package/test/service/connectionService.ts +142 -0
  291. package/test/service/filterService.ts +165 -0
  292. package/test/service/handlerService.ts +77 -0
  293. package/test/service/sessionService.ts +464 -0
  294. package/test/util/countDownLatch.ts +81 -0
  295. package/test/util/pathUtil.ts +122 -0
  296. package/test/util/utils.ts +165 -0
@@ -0,0 +1,464 @@
1
+ import * as should from 'should';
2
+ // import { describe, it } from "mocha-typescript"
3
+ let pomelo = require('../../lib/index');
4
+ import { SessionService, Session, FrontendSession } from '../../lib/common/service/sessionService';
5
+ import { SID, FRONTENDID, UID } from '../../lib/util/constants';
6
+ import { ISocket } from '../../lib//interfaces/ISocket';
7
+
8
+ describe('session service test', function () {
9
+ describe('#bind', function () {
10
+ it('should get session by uid after binded', function (done: Mocha.Done) {
11
+ let service = new SessionService();
12
+ let sid = 1, fid = 'frontend-server-1', socket: ISocket = <any>{};
13
+ let uid = 'changchang';
14
+ let eventCount = 0;
15
+
16
+ let session = service.create(sid, fid, socket);
17
+
18
+ should.exist(session);
19
+
20
+ session.should.eql(service.get(sid));
21
+
22
+ session.on('bind', function (euid: number) {
23
+ eventCount++;
24
+ uid.should.equal(euid);
25
+ });
26
+
27
+ service.bind(sid, uid, function (err: Error) {
28
+ should.not.exist(err);
29
+ let sessions = service.getByUid(uid);
30
+ should.exist(sessions);
31
+ sessions.length.should.equal(1);
32
+ session.should.eql(sessions[0]);
33
+ eventCount.should.equal(1);
34
+ service.bind(sid, uid, function (err: Error) {
35
+ should.not.exist(err);
36
+ done();
37
+ });
38
+ });
39
+ });
40
+ it('should fail if already binded uid', function (done: Mocha.Done) {
41
+ let service = new SessionService();
42
+ let sid = 1, fid = 'frontend-server-1', socket: ISocket = <any>{};
43
+ let uid = 'py', test_uid = 'test';
44
+
45
+ let session = service.create(sid, fid, socket);
46
+
47
+ service.bind(sid, uid, null);
48
+
49
+ service.bind(sid, test_uid, function (err: Error) {
50
+ should.exist(err);
51
+ done();
52
+ });
53
+ });
54
+ it('should fail if try to bind a session not exist', function (done: Mocha.Done) {
55
+ let service = new SessionService();
56
+ let sid = 1, uid = 'changchang';
57
+
58
+ service.bind(sid, uid, function (err: Error) {
59
+ should.exist(err);
60
+ done();
61
+ });
62
+ });
63
+ });
64
+
65
+ describe('#unbind', function () {
66
+ it('should fail unbind session if session not exist', function (done: Mocha.Done) {
67
+ let service = new SessionService();
68
+ let sid = 1;
69
+ let uid = 'py';
70
+
71
+ service.unbind(sid, uid, function (err: Error) {
72
+ should.exist(err);
73
+ done();
74
+ });
75
+ });
76
+ it('should fail unbind session if session not binded', function (done: Mocha.Done) {
77
+ let service = new SessionService();
78
+ let sid = 1, fid = 'frontend-server-1', socket: ISocket = <any>{};
79
+ let uid = 'py';
80
+
81
+ let session = service.create(sid, fid, socket);
82
+
83
+ service.unbind(sid, uid, function (err: Error) {
84
+ should.exist(err);
85
+ done();
86
+ });
87
+ });
88
+ it('should fail to get session after session unbinded', function (done: Mocha.Done) {
89
+ let service = new SessionService();
90
+ let sid = 1, fid = 'frontend-server-1', socket: ISocket = <any>{};
91
+ let uid = 'py';
92
+
93
+ let session = service.create(sid, fid, socket);
94
+ service.bind(sid, uid, null);
95
+
96
+ service.unbind(sid, uid, function (err: Error) {
97
+ should.not.exist(err);
98
+ let sessions = service.getByUid(uid);
99
+ should.not.exist(sessions);
100
+ done();
101
+ });
102
+ });
103
+ });
104
+
105
+ describe('#remove', function () {
106
+ it('should not get the session after remove', function (done: Mocha.Done) {
107
+ let service = new SessionService();
108
+ let sid = 1, fid = 'frontend-server-1', socket: ISocket = <any>{};
109
+ let uid = 'changchang';
110
+
111
+ let session = service.create(sid, fid, socket);
112
+
113
+ service.bind(sid, uid, function (err: Error) {
114
+ service.remove(sid);
115
+ should.not.exist(service.get(sid));
116
+ should.not.exist(service.getByUid(uid));
117
+ done();
118
+ });
119
+ });
120
+ });
121
+
122
+ describe('#import', function () {
123
+ it('should update the session with the key/value pair', function (done: Mocha.Done) {
124
+ let service = new SessionService();
125
+ let sid = 1, fid = 'frontend-server-1', socket: ISocket = <any>{};
126
+ let key = 'key-1', value = 'value-1';
127
+
128
+ let session = service.create(sid, fid, socket);
129
+
130
+ service.import(sid, key, value, function (err: Error) {
131
+ should.not.exist(err);
132
+ value.should.eql(session.get(key));
133
+ done();
134
+ });
135
+ });
136
+
137
+ it('should fail if try to update a session not exist', function (done: Mocha.Done) {
138
+ let service = new SessionService();
139
+ let sid = 1;
140
+ let key = 'key-1', value = 'value-1';
141
+
142
+ service.import(sid, key, value, function (err: Error) {
143
+ should.exist(err);
144
+ done();
145
+ });
146
+ });
147
+
148
+ it('should update the session with the key/value pairs', function (done: Mocha.Done) {
149
+ let service = new SessionService();
150
+ let sid = 1, fid = 'frontend-server-1', socket: ISocket = <any>{};
151
+ let key = 'key-1', value = 'value-1', key2 = 'key-2', value2 = {};
152
+
153
+ let settings: { [key: string]: any } = {};
154
+ settings[key] = value;
155
+ settings[key2] = value2;
156
+
157
+ let session = service.create(sid, fid, socket);
158
+
159
+ service.importAll(sid, settings, function (err: Error) {
160
+ should.not.exist(err);
161
+ value.should.eql(session.get(key));
162
+ value2.should.eql(session.get(key2));
163
+ done();
164
+ });
165
+ });
166
+
167
+ it('should fail if try to update a session not exist', function (done: Mocha.Done) {
168
+ let service = new SessionService();
169
+ let sid = 1;
170
+ let key = 'key-1', value = 'value-1';
171
+
172
+ service.import(sid, key, value, function (err: Error) {
173
+ should.exist(err);
174
+ done();
175
+ });
176
+ });
177
+
178
+ it('should fail if try to update a session not exist', function (done: Mocha.Done) {
179
+ let service = new SessionService();
180
+ let sid = 1;
181
+ let key = 'key-1', value = 'value-1', key2 = 'key-2', value2 = {};
182
+
183
+ let settings: { [key: string]: any } = {};
184
+ settings[key] = value;
185
+ settings[key2] = value2;
186
+
187
+ service.importAll(sid, settings, function (err: Error) {
188
+ should.exist(err);
189
+ done();
190
+ });
191
+ });
192
+ });
193
+
194
+ describe('#kick', function () {
195
+ it('should kick the sessions', function (done: Mocha.Done) {
196
+ let service = new SessionService();
197
+ let sid1 = 1, fid1 = 'frontend-server-1';
198
+ let sid2 = 2, fid2 = 'frontend-server-1';
199
+ let socket: ISocket = <any>{
200
+ emit: function () { },
201
+ disconnect: function () { }
202
+ };
203
+ let uid = 'changchang';
204
+ let eventCount = 0;
205
+
206
+ let session1 = service.create(sid1, fid1, socket);
207
+ let session2 = service.create(sid2, fid2, socket);
208
+ session1.on('closed', function () {
209
+ eventCount++;
210
+ });
211
+
212
+ session2.on('closed', function () {
213
+ eventCount++;
214
+ });
215
+
216
+ service.bind(sid1, uid, function (err: Error) {
217
+ service.bind(sid2, uid, function (err: Error) {
218
+ service.kick(uid, null, function (err: Error) {
219
+ should.not.exist(err);
220
+ should.not.exist(service.get(sid1));
221
+ should.not.exist(service.get(sid2));
222
+ should.not.exist(service.getByUid(uid));
223
+ eventCount.should.equal(2);
224
+ done();
225
+ });
226
+ });
227
+ });
228
+ });
229
+
230
+ it('should kick the session by sessionId', function (done: Mocha.Done) {
231
+ let service = new SessionService();
232
+ let sid1 = 1, fid1 = 'frontend-server-1';
233
+ let sid2 = 2, fid2 = 'frontend-server-1';
234
+
235
+ let socket: ISocket = <any>{
236
+ emit: function () { },
237
+ disconnect: function () { }
238
+ };
239
+ let uid = 'changchang';
240
+ let eventCount = 0;
241
+
242
+ let session1 = service.create(sid1, fid1, socket);
243
+ let session2 = service.create(sid2, fid2, socket);
244
+ session1.on('closed', function () {
245
+ eventCount++;
246
+ });
247
+
248
+ session2.on('closed', function () {
249
+ eventCount++;
250
+ });
251
+
252
+ service.bind(sid1, uid, function (err: Error) {
253
+ service.bind(sid2, uid, function (err: Error) {
254
+ service.kickBySessionId(sid1, null, function (err: Error) {
255
+ should.not.exist(err);
256
+ should.not.exist(service.get(sid1));
257
+ should.exist(service.get(sid2));
258
+ should.exist(service.getByUid(uid));
259
+ eventCount.should.equal(1);
260
+ done();
261
+ });
262
+ });
263
+ });
264
+ });
265
+
266
+ it('should ok if kick a session not exist', function (done: Mocha.Done) {
267
+ let service = new SessionService();
268
+ let uid = 'changchang';
269
+
270
+ service.kick(uid, null, function (err: Error) {
271
+ should.not.exist(err);
272
+ done();
273
+ });
274
+ });
275
+
276
+ it('should kick session by sid', function (done: Mocha.Done) {
277
+ let service = new SessionService();
278
+ let sid = 1, fid = 'frontend-server-1';
279
+ let socket: ISocket = <any>{
280
+ emit: function () { },
281
+ disconnect: function () { }
282
+ };
283
+ let eventCount = 0;
284
+
285
+ let session = service.create(sid, fid, socket);
286
+ session.on('closed', function () {
287
+ eventCount++;
288
+ });
289
+
290
+ service.kickBySessionId(sid, null, function (err: Error) {
291
+ should.not.exist(err);
292
+ should.not.exist(service.get(sid));
293
+ eventCount.should.equal(1);
294
+ done();
295
+ });
296
+ });
297
+
298
+ it('should ok if kick a session not exist', function (done: Mocha.Done) {
299
+ let service = new SessionService();
300
+ let sid = 1;
301
+
302
+ service.kickBySessionId(sid, null, function (err: Error) {
303
+ should.not.exist(err);
304
+ done();
305
+ });
306
+ });
307
+ });
308
+
309
+ describe('#forEachSession', function () {
310
+ it('should iterate all created sessions', function (done: Mocha.Done) {
311
+ let service = new SessionService();
312
+ let sid = 1, fid = 'frontend-server-1', socket: ISocket = <any>{};
313
+ let eventCount = 0;
314
+
315
+ let outter_session = service.create(sid, fid, socket);
316
+
317
+ service.forEachSession(function (session: any) {
318
+ should.exist(session);
319
+ outter_session.id.should.eql(session.id);
320
+ done();
321
+ });
322
+ });
323
+ });
324
+
325
+ describe('#forEachBindedSession', function () {
326
+ it('should iterate all binded sessions', function (done: Mocha.Done) {
327
+ let service = new SessionService();
328
+ let sid = 1, fid = 'frontend-server-1', socket: ISocket = <any>{};
329
+ let uid = 'py';
330
+
331
+ let outter_session = service.create(sid, fid, socket);
332
+ service.bind(sid, uid, null);
333
+
334
+ service.forEachBindedSession(function (session: any) {
335
+ should.exist(session);
336
+ outter_session.id.should.eql(session.id);
337
+ outter_session.uid.should.eql(session.uid);
338
+ done();
339
+ });
340
+ });
341
+ });
342
+ });
343
+
344
+ describe('frontend session test', function () {
345
+ describe('#bind', function () {
346
+ it('should get session by uid after binded', function (done: Mocha.Done) {
347
+ let service = new SessionService();
348
+ let sid = 1, fid = 'frontend-server-1', socket: ISocket = <any>{};
349
+ let uid = 'changchang';
350
+ let eventCount = 0;
351
+
352
+ let session = service.create(sid, fid, socket);
353
+ let fsession = session.toFrontendSession();
354
+
355
+ should.exist(fsession);
356
+
357
+ fsession.on('bind', function (euid: number) {
358
+ eventCount++;
359
+ uid.should.equal(euid);
360
+ });
361
+
362
+ fsession.bind(uid, function (err: Error) {
363
+ should.not.exist(err);
364
+ let sessions = service.getByUid(uid);
365
+ should.exist(sessions);
366
+ sessions.length.should.equal(1);
367
+ session.should.eql(sessions[0]);
368
+ eventCount.should.equal(1);
369
+ done();
370
+ });
371
+ });
372
+ });
373
+
374
+ describe('#unbind', function () {
375
+ it('should fail to get session after session unbinded', function (done: Mocha.Done) {
376
+ let service = new SessionService();
377
+ let sid = 1, fid = 'frontend-server-1', socket: ISocket = <any>{};
378
+ let uid = 'py';
379
+
380
+ let session = service.create(sid, fid, socket);
381
+ let fsession = session.toFrontendSession();
382
+
383
+ fsession.bind(uid, null);
384
+ fsession.unbind(uid, function (err: Error) {
385
+ should.not.exist(err);
386
+ let sessions = service.getByUid(uid);
387
+ should.not.exist(sessions);
388
+ done();
389
+ });
390
+ });
391
+ });
392
+
393
+ describe('#set/get', function () {
394
+ it('should update the key/value pair in frontend session but not session',
395
+ function () {
396
+ let service = new SessionService();
397
+ let sid = 1, fid = 'frontend-server-1', socket: ISocket = <any>{};
398
+ let key = 'key-1', value = 'value-1';
399
+
400
+ let session = service.create(sid, fid, socket);
401
+ let fsession = session.toFrontendSession();
402
+
403
+ fsession.set(key, value);
404
+
405
+ should.not.exist(session.get(key));
406
+ value.should.eql(fsession.get(key));
407
+ });
408
+ });
409
+
410
+ describe('#push', function () {
411
+ it('should push the specified key/value pair to session', function (done: Mocha.Done) {
412
+ let service = new SessionService();
413
+ let sid = 1, fid = 'frontend-server-1', socket: ISocket = <any>{};
414
+ let key = 'key-1', value = 'value-1', key2 = 'key-2', value2 = {};
415
+
416
+ let session = service.create(sid, fid, socket);
417
+ let fsession = session.toFrontendSession();
418
+
419
+ fsession.set(key, value);
420
+ fsession.set(key2, value2);
421
+
422
+ fsession.push(key, function (err: Error) {
423
+ should.not.exist(err);
424
+ value.should.eql(session.get(key));
425
+ should.not.exist(session.get(key2));
426
+ done();
427
+ });
428
+ });
429
+
430
+ it('should push all the key/value pairs to session', function (done: Mocha.Done) {
431
+ let service = new SessionService();
432
+ let sid = 1, fid = 'frontend-server-1', socket: ISocket = <any>{};
433
+ let key = 'key-1', value = 'value-1', key2 = 'key-2', value2 = {};
434
+
435
+ let session = service.create(sid, fid, socket);
436
+ let fsession = session.toFrontendSession();
437
+
438
+ fsession.set(key, value);
439
+ fsession.set(key2, value2);
440
+
441
+ fsession.pushAll(function (err: Error) {
442
+ should.not.exist(err);
443
+ value.should.eql(session.get(key));
444
+ value2.should.eql(session.get(key2));
445
+ done();
446
+ });
447
+ });
448
+ });
449
+
450
+ describe('#export', function () {
451
+ it('should equal frontend session after export', function (done: Mocha.Done) {
452
+ let service = new SessionService();
453
+ let sid = 1, fid = 'frontend-server-1', socket: ISocket = <any>{};
454
+ let uid = 'py';
455
+
456
+ let session = service.create(sid, fid, socket);
457
+ let fsession = session.toFrontendSession();
458
+ let esession: FrontendSession = <any>fsession.export();
459
+ esession.id.should.eql(fsession.id);
460
+ esession.frontendId.should.eql(fsession.frontendId);
461
+ done();
462
+ });
463
+ });
464
+ });
@@ -0,0 +1,81 @@
1
+ let CountDownLatch = require('../../lib/util/countDownLatch');
2
+ import 'should';
3
+ // import { describe, it } from "mocha-typescript"
4
+
5
+ let cbCreator = (function () {
6
+ let count = 0;
7
+
8
+ return {
9
+ callback: function () {
10
+ count++;
11
+ },
12
+ getCount: function () {
13
+ return count;
14
+ },
15
+ count: count
16
+ };
17
+ })();
18
+
19
+ describe('countdown latch test', function () {
20
+ let countDownLatch1;
21
+ let countDownLatch2;
22
+
23
+ describe('#count down', function () {
24
+ it('should invoke the callback after the done method was invoked the specified times', function (done: Mocha.Done) {
25
+ let n = 3, doneCount = 0;
26
+ let cdl = CountDownLatch.createCountDownLatch(n, function () {
27
+ doneCount.should.equal(n);
28
+ done();
29
+ });
30
+
31
+ for (let i = 0; i < n; i++) {
32
+ doneCount++;
33
+ cdl.done();
34
+ }
35
+ });
36
+
37
+ it('should throw exception if pass a negative or zero to the create method', function () {
38
+ (function () {
39
+ CountDownLatch.createCountDownLatch(-1, function () {
40
+ });
41
+ }).should.throw();
42
+
43
+ (function () {
44
+ CountDownLatch.createCountDownLatch(0, function () {
45
+ });
46
+ }).should.throw();
47
+ });
48
+
49
+ it('should throw exception if pass illegal cb to the create method', function () {
50
+ (function () {
51
+ CountDownLatch.createCountDownLatch(1, null);
52
+ }).should.throw();
53
+ });
54
+
55
+ it('should throw exception if try to invoke done metho of a latch that has fired cb', function () {
56
+ let n = 3;
57
+ let cdl = CountDownLatch.createCountDownLatch(n, function () {
58
+ });
59
+
60
+ for (let i = 0; i < n; i++) {
61
+ cdl.done();
62
+ }
63
+
64
+ (function () {
65
+ cdl.done();
66
+ }).should.throw();
67
+ });
68
+
69
+ it('should invoke the callback if timeout', function () {
70
+ let n = 3;
71
+ let cdl = CountDownLatch.createCountDownLatch(n, { timeout: 3000 }, function (isTimeout: boolean) {
72
+ isTimeout.should.equal(true);
73
+ });
74
+
75
+ for (let i = 0; i < n - 1; i++) {
76
+ cdl.done();
77
+ }
78
+ });
79
+
80
+ });
81
+ });
@@ -0,0 +1,122 @@
1
+ let pathUtil = require('../../lib/util/pathUtil');
2
+ let utils = require('../../lib/util/utils');
3
+ import * as should from 'should';
4
+ // import { describe, it } from "mocha-typescript"
5
+ let fs = require('fs');
6
+
7
+ let mockBase = process.cwd() + '/test/mock-base';
8
+
9
+ describe('path util test', function () {
10
+ it('ok', () => {
11
+
12
+ })
13
+ if (process.platform === 'win32') {
14
+ return
15
+ }
16
+ describe('#getSysRemotePath', function () {
17
+ it('should return the system remote service path for frontend server', function () {
18
+ let role = 'frontend';
19
+ let expectSuffix = '/common/remote/frontend';
20
+ let p = pathUtil.getSysRemotePath(role);
21
+ should.exist(p);
22
+ fs.existsSync(p).should.be.true;
23
+ utils.endsWith(p, expectSuffix).should.be.true;
24
+ });
25
+
26
+ it('should return the system remote service path for backend server', function () {
27
+ let role = 'backend';
28
+ let expectSuffix = '/common/remote/backend';
29
+ let p = pathUtil.getSysRemotePath(role);
30
+ should.exist(p);
31
+ fs.existsSync(p).should.be.true;
32
+ utils.endsWith(p, expectSuffix).should.be.true;
33
+ });
34
+
35
+ });
36
+
37
+ describe('#getUserRemotePath', function () {
38
+ it('should return user remote service path for the associated server type', function () {
39
+ let serverType = 'connector';
40
+ let expectSuffix = '/app/servers/connector/remote';
41
+ let p = pathUtil.getUserRemotePath(mockBase, serverType);
42
+ should.exist(p);
43
+ fs.existsSync(p).should.be.true;
44
+ utils.endsWith(p, expectSuffix).should.be.true;
45
+ });
46
+
47
+ it('should return null if the directory not exist', function () {
48
+ let serverType = 'area';
49
+ let p = pathUtil.getUserRemotePath(mockBase, serverType);
50
+ should.not.exist(p);
51
+
52
+ serverType = 'some-dir-not-exist';
53
+ p = pathUtil.getUserRemotePath(mockBase, serverType);
54
+ should.not.exist(p);
55
+ });
56
+ });
57
+
58
+ describe('#listUserRemoteDir', function () {
59
+ it('should return sub-direcotry name list of servers/ directory', function () {
60
+ let expectNames = ['connector', 'area'];
61
+ let p = pathUtil.listUserRemoteDir(mockBase);
62
+ should.exist(p);
63
+ expectNames.length.should.equal(p.length);
64
+ for (let i = 0, l = expectNames.length; i < l; i++) {
65
+ p.should.containEql(expectNames[i]);
66
+ }
67
+ });
68
+
69
+ it('should throw err if the servers/ illegal', function () {
70
+ (function () {
71
+ pathUtil.listUserRemoteDir('some illegal base');
72
+ }).should.throw();
73
+ });
74
+ });
75
+
76
+ describe('#remotePathRecord', function () {
77
+ let namespace = 'user';
78
+ let serverType = 'connector';
79
+ let path = '/some/path/to/remote';
80
+ let r = pathUtil.remotePathRecord(namespace, serverType, path);
81
+ should.exist(r);
82
+ namespace.should.equal(r.namespace);
83
+ serverType.should.equal(r.serverType);
84
+ path.should.equal(r.path);
85
+ });
86
+
87
+ describe('#getHandlerPath', function () {
88
+ it('should return user handler path for the associated server type', function () {
89
+ let serverType = 'connector';
90
+ let expectSuffix = '/app/servers/connector/handler';
91
+ let p = pathUtil.getHandlerPath(mockBase, serverType);
92
+ should.exist(p);
93
+ fs.existsSync(p).should.be.true;
94
+ utils.endsWith(p, expectSuffix).should.be.true;
95
+ });
96
+
97
+ it('should return null if the directory not exist', function () {
98
+ let serverType = 'area';
99
+ let p = pathUtil.getHandlerPath(mockBase, serverType);
100
+ should.not.exist(p);
101
+
102
+ serverType = 'some-dir-not-exist';
103
+ p = pathUtil.getHandlerPath(mockBase, serverType);
104
+ should.not.exist(p);
105
+ });
106
+ });
107
+
108
+ describe('#getScriptPath', function () {
109
+ let p = pathUtil.getScriptPath(mockBase);
110
+ let expectSuffix = '/scripts';
111
+ should.exist(p);
112
+ utils.endsWith(p, expectSuffix).should.be.true;
113
+ });
114
+
115
+ describe('#getLogPath', function () {
116
+ let p = pathUtil.getLogPath(mockBase);
117
+ let expectSuffix = '/logs';
118
+ should.exist(p);
119
+ utils.endsWith(p, expectSuffix).should.be.true;
120
+ });
121
+
122
+ });