您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Test.ts 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. import { describe, it, Func } from "mocha";
  2. import { RPCServer, RPCSocket, SubscriptionResponse, makeSubResponse } from '../Index'
  3. import * as uuidv4 from "uuid/v4"
  4. import { doesNotReject } from "assert";
  5. import { Socket } from "dgram";
  6. const add = (...args:number[]) => {return args.reduce((a,b)=>a+b, 0)}
  7. function makeServer(){
  8. let subcallback
  9. return new RPCServer<{ topic: string }>(21010, [{
  10. name: "test",
  11. exportRPCs: () => [
  12. {
  13. name: 'echo',
  14. call: async (s:string) => s,
  15. },{
  16. name: 'simpleSubscribe',
  17. hook: async(callback) => {
  18. subcallback = callback
  19. return makeSubResponse<{topic: string}>({topic: "test"})
  20. }
  21. },{
  22. name: 'subscribe',
  23. hook: async (callback) => {
  24. subcallback = callback
  25. return makeSubResponse<{topic: string}>({topic: "test"})
  26. },
  27. onClose: (res, rpc) => {
  28. console.log("onClose", rpc.name === 'subscribe' && res?"OK":"")
  29. subcallback = null
  30. },
  31. onCallback: (...args:any) => {
  32. console.log("onCallback", args[0] === "test" && args[1] === "callback"?"OK":"")
  33. }
  34. },
  35. add,
  36. function triggerCallback(...messages:any[]):number {return subcallback.apply({}, messages)},
  37. ]
  38. }],{
  39. connectionHandler: (socket) => { },
  40. closeHandler: (socket) => { },
  41. errorHandler: (socket, err) => { throw err }
  42. })
  43. }
  44. describe('RPCServer', () => {
  45. let server: RPCServer<{ topic: string }, any>
  46. before(() => {
  47. server = makeServer()
  48. })
  49. after(() => {
  50. server.destroy()
  51. })
  52. it('should be able to use all kinds of RPC definitions', (done) => {
  53. const echo = (x) => x
  54. const server = new RPCServer(21003, [{
  55. name: 'HelloWorldRPCGroup',
  56. exportRPCs: () => [
  57. echo, //named function variable
  58. function echof(x){ return x }, //named function
  59. {
  60. name: 'echoExplicit', //describing object
  61. call: async (x,y,z) => [x,y,z]
  62. }
  63. ]
  64. }])
  65. const client = new RPCSocket(21003, 'localhost')
  66. client.connect().then(async () => {
  67. const r0 = await client['HelloWorldRPCGroup'].echo('Hello')
  68. const r1 = await client['HelloWorldRPCGroup'].echof('World')
  69. const r2 = await client['HelloWorldRPCGroup'].echoExplicit('R','P','C!')
  70. if(r0 === 'Hello' && r1 === 'World' && r2.join('') ==='RPC!'){
  71. client.destroy()
  72. server.destroy()
  73. done()
  74. }
  75. })
  76. })
  77. it('new RPCServer() should fail on bad RPC', (done) => {
  78. try{
  79. new RPCServer(20001, [{
  80. name: "bad",
  81. exportRPCs: () => [
  82. (aaa,bbb,ccc) => { return aaa+bbb+ccc }
  83. ]
  84. }])
  85. done(new Error("Didn't fail with bad RPC"))
  86. }catch(badRPCError){
  87. done()
  88. }
  89. })
  90. })
  91. describe('RPCSocket', () => {
  92. let client: RPCSocket
  93. let server: RPCServer<{topic: string}>
  94. before(async() => {
  95. server = makeServer()
  96. client = new RPCSocket(21010, "localhost")
  97. return await client.connect()
  98. })
  99. after(() => {
  100. client.destroy()
  101. server.destroy()
  102. })
  103. it('should have rpc echo', (done) => {
  104. client['test'].echo("x").then(x => {
  105. if(x === 'x')
  106. done()
  107. else
  108. done(new Error('echo RPC response did not match'))
  109. })
  110. })
  111. it('should add up to 6', (done) => {
  112. client['test'].add(1,2,3).then(x => {
  113. if(x === 6)
  114. done()
  115. else
  116. done(new Error('add RPC response did not match'))
  117. })
  118. })
  119. it('should subscribe with success', (done) => {
  120. client['test'].simpleSubscribe(console.log).then(res => {
  121. if(res.result === 'Success'){
  122. done()
  123. }else{
  124. console.error(res)
  125. done(new Error('Subscribe did not return success'))
  126. }
  127. })
  128. })
  129. it('subscribe should call back', (done) => {
  130. client['test'].subscribe((...args: any) => {
  131. if(args[0] === "test" && args[1] === "callback")
  132. done()
  133. else
  134. done(new Error("Bad callback value "+ args))
  135. }).then( async () => {
  136. await client['test'].triggerCallback("test", "callback")
  137. })
  138. })
  139. it('simpleSubscribe should call back', (done) => {
  140. client['test'].simpleSubscribe((...args: any) => {
  141. if(args[0] === "test_" && args[1] === "callback_")
  142. done()
  143. else
  144. done(new Error("Bad callback value "+ args))
  145. }).then( async () => {
  146. await client['test'].triggerCallback("test_", "callback_")
  147. })
  148. })
  149. })
  150. describe('It should do unhook', () => {
  151. let candy = "OK"
  152. let cb: Function
  153. let client: RPCSocket
  154. let server: RPCServer<{topic: string}>
  155. before(async() => {
  156. server = new RPCServer<{ topic: string }>(21010, [{
  157. name: "test",
  158. exportRPCs: () => [{
  159. name: 'subscribe',
  160. hook: async(callback):Promise<SubscriptionResponse<{topic:string}>> => {
  161. cb = <Function> callback
  162. return {
  163. result: "Success",
  164. uuid: uuidv4(),
  165. topic: "test"
  166. }
  167. }
  168. },
  169. function checkCandy():string { cb(candy); return candy },
  170. function stealCandy():string { candy = "_OK"; cb(candy); cb = () => {}; return candy }
  171. ]
  172. }],{
  173. connectionHandler: (socket) => { },
  174. closeHandler: (socket) => { },
  175. errorHandler: (socket, err) => { throw err }
  176. })
  177. client = new RPCSocket(21010, "localhost")
  178. return await client.connect()
  179. })
  180. after(() => {
  181. client.destroy()
  182. server.destroy()
  183. })
  184. it('Unhook+unsubscribe should stop callbacks', (done) => {
  185. client['test'].subscribe(c => {}).then( async (res: SubscriptionResponse) => {
  186. const r1 = await client['test'].checkCandy()
  187. const r3 = await client['test'].stealCandy()
  188. client.unhook(res.uuid)
  189. const r2 = await client['test'].checkCandy()
  190. const r4 = await client['test'].checkCandy()
  191. if(r1 === "OK" && r3 === "_OK" && r2 === "_OK" && r4 === "_OK")
  192. done()
  193. else
  194. done(new Error("Results did not match: "+[r1,r2,r3,r4]))
  195. })
  196. })
  197. })
  198. type SesameTestIfc = {
  199. test: {
  200. checkCandy: ()=>Promise<string>
  201. subscribe: (callback) => Promise<SubscriptionResponse<{ topic: string; }>>
  202. }
  203. }
  204. describe('Sesame should unlock the socket', () => {
  205. let candy = "OK"
  206. let client: RPCSocket & SesameTestIfc
  207. let server: RPCServer
  208. let cb = (...args) => {}
  209. before((done) => {
  210. server = new RPCServer(21004, [{
  211. name: "test",
  212. exportRPCs: () => [
  213. {
  214. name: 'subscribe',
  215. hook: async(callback) => {
  216. cb = callback
  217. return <SubscriptionResponse>{
  218. result: "Success",
  219. uuid: uuidv4(),
  220. topic: 'test'
  221. }
  222. }
  223. },
  224. async function checkCandy():Promise<string> { cb(candy); cb=()=>{}; return candy },
  225. async function manyParams(a,b,c,d) {return [a,b,c,d]}
  226. ]}
  227. ],{
  228. sesame: (_sesame) => _sesame === 'sesame!'
  229. })
  230. const sock = new RPCSocket(21004, "localhost")
  231. sock.connect<SesameTestIfc>('sesame!').then(cli => {
  232. client = cli
  233. done()
  234. })
  235. })
  236. after(() => {
  237. client.destroy()
  238. server.destroy()
  239. })
  240. it('should work with sesame', (done) => {
  241. client.test.checkCandy().then(c => done())
  242. })
  243. it('should work with multiple params', (done) => {
  244. client.test['manyParams']('a','b','c','d').then(c => {
  245. if(c[0] == 'a' && c[1] === 'b' && c[2] === 'c' && c[3] === 'd')
  246. done()
  247. })
  248. })
  249. it('should not work without sesame', (done) => {
  250. const sock = new RPCSocket(21004, "localhost")
  251. sock.connect<SesameTestIfc>( /* no sesame */).then(async (cli) => {
  252. cli.test.checkCandy().then(d => {
  253. done(d)
  254. }).catch(e => {
  255. //console.log("EXPECTED CLIENT EXCEPTION", String(e));
  256. done()
  257. }).finally(() => {
  258. cli.destroy()
  259. sock.destroy()
  260. })
  261. })
  262. })
  263. it('should fail with wrong sesame', (done) => {
  264. const sock = new RPCSocket(21004, "localhost")
  265. sock.connect<SesameTestIfc>('abasd').then(async (cli) => {
  266. cli.test.checkCandy().then(d => {
  267. done("should not be able to get candy")
  268. }).catch(e => {
  269. //console.log("EXPECTED CLIENT EXCEPTION", String(e));
  270. done()
  271. }).finally(() => {
  272. sock.destroy()
  273. cli.destroy()
  274. })
  275. })
  276. })
  277. it('callback should work with sesame', (done) => {
  278. client.test.subscribe((c) => {
  279. if(c === candy){
  280. done()
  281. }
  282. }).then(d => {
  283. if(d.result !== 'Success')
  284. done('unexpected valid response')
  285. client.test.checkCandy()
  286. })
  287. })
  288. it('callback should not work without sesame', (done) => {
  289. const sock = new RPCSocket(21004, "localhost")
  290. sock.connect<SesameTestIfc>( /* no sesame */).then(async (cli) => {
  291. cli.test.subscribe((c) => {
  292. console.log("CALLBACK TRIGGERED UNEXPECTED");
  293. if(c === candy)
  294. done("super not")
  295. }).then(async d => {
  296. await client.test.checkCandy()
  297. if(d == null){
  298. done()
  299. }else
  300. done('unexpected valid response '+(d) )
  301. }).finally(() => {
  302. sock.destroy()
  303. })
  304. })
  305. })
  306. })
  307. describe('Error handling', ()=>{
  308. let createUser = async( user: {a:any,b:any}) => {
  309. throw new Error("BAD BAD BAD")
  310. }
  311. it("RPC throws on client without handler", (done)=>{
  312. let server = new RPCServer(21004, [ {
  313. name: 'createUser' as 'createUser',
  314. exportRPCs: () => [{
  315. name: 'createUser' as 'createUser',
  316. call: createUser
  317. }]}], {
  318. })
  319. let sock = new RPCSocket(21004, 'localhost')
  320. sock.connect().then((cli) => {
  321. cli["createUser"]["createUser"]({
  322. a:'a',
  323. b:'b'
  324. })
  325. .then(r => {
  326. if(r != null)
  327. done("UNEXPECTED RESULT " + r)
  328. })
  329. .catch((e) => {
  330. //console.log("EXPECTED CLIENT EXCEPTION", String(e));
  331. done()
  332. })
  333. .finally(() => {
  334. cli.destroy()
  335. sock.destroy()
  336. server.destroy()
  337. })
  338. })
  339. })
  340. it("RPC throws on server with handler", (done)=>{
  341. let server = new RPCServer(21004, [ {
  342. name: 'createUser' as 'createUser',
  343. exportRPCs: () => [{
  344. name: 'createUser' as 'createUser',
  345. call: createUser
  346. }]}], {
  347. errorHandler: (socket, e, rpcName, args) => {
  348. done()
  349. }
  350. })
  351. let sock = new RPCSocket(21004, 'localhost')
  352. sock.connect().then((cli) => {
  353. cli["createUser"]["createUser"]({
  354. a:'a',
  355. b:'b'
  356. })
  357. .then(r => {
  358. if(r != null)
  359. done("UNEXPECTED RESULT " + r)
  360. })
  361. .catch((e) => {
  362. done("UNEXPECTED CLIENT ERROR " + e)
  363. done(e)
  364. })
  365. .finally(() => {
  366. cli.destroy()
  367. sock.destroy()
  368. server.destroy()
  369. })
  370. })
  371. })
  372. })
  373. describe("Errorhandler functionality", ()=>{
  374. let createUser = async( user: {a:any,b:any}) => {
  375. throw new Error("BAD BAD BAD")
  376. }
  377. it("correct values are passed to the handler", (done)=>{
  378. let server = new RPCServer(21004, [ {
  379. name: 'createUser' as 'createUser',
  380. exportRPCs: () => [{
  381. name: 'createUser' as 'createUser',
  382. call: createUser
  383. }]}], {
  384. errorHandler: (socket, e, rpcName, args) => {
  385. if(e.message === "BAD BAD BAD" && rpcName === "createUser" && args[0]['a'] === 'a' && args[0]['b'] === 'b')
  386. done()
  387. }
  388. })
  389. let sock = new RPCSocket(21004, 'localhost')
  390. sock.connect().then((cli) => {
  391. cli["createUser"]["createUser"]({
  392. a:'a',
  393. b:'b'
  394. })
  395. .then(r => {
  396. if(r != null)
  397. done("UNEXPECTED RESULT " + r)
  398. })
  399. .catch((e) => {
  400. done("UNEXPECTED CLIENT ERROR " + e)
  401. done(e)
  402. })
  403. .finally(() => {
  404. cli.destroy()
  405. sock.destroy()
  406. server.destroy()
  407. })
  408. })
  409. })
  410. it("handler sees sesame", (done)=>{
  411. let sesame = "AAAAAAAAAAAAAAA"
  412. let server = new RPCServer(21004, [ {
  413. name: 'createUser' as 'createUser',
  414. exportRPCs: () => [{
  415. name: 'createUser' as 'createUser',
  416. call: createUser
  417. }]}], {
  418. sesame: sesame,
  419. errorHandler: (socket, e, rpcName, args) => {
  420. if(e.message === "BAD BAD BAD" && rpcName === "createUser" && args[0] === sesame && args[1]['a'] === 'a' && args[1]['b'] === 'b')
  421. done()
  422. }
  423. })
  424. let sock = new RPCSocket(21004, 'localhost')
  425. sock.connect(sesame).then((cli) => {
  426. cli["createUser"]["createUser"]({
  427. a:'a',
  428. b:'b'
  429. })
  430. .then(r => {
  431. if(r != null)
  432. done("UNEXPECTED RESULT " + r)
  433. })
  434. .catch((e) => {
  435. done("UNEXPECTED CLIENT ERROR " + e)
  436. done(e)
  437. })
  438. .finally(() => {
  439. cli.destroy()
  440. sock.destroy()
  441. server.destroy()
  442. })
  443. })
  444. })
  445. })