You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Test.ts 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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. import { RPCExporter } from "../src/Interfaces";
  7. const add = (...args:number[]) => {return args.reduce((a,b)=>a+b, 0)}
  8. function makeServer(){
  9. let subcallback
  10. return new RPCServer<{ topic: string }>(21010, [{
  11. name: "test",
  12. exportRPCs: () => [
  13. {
  14. name: 'echo',
  15. call: async (s:string) => s,
  16. },{
  17. name: 'simpleSubscribe',
  18. hook: async(callback) => {
  19. subcallback = callback
  20. return makeSubResponse<{topic: string}>({topic: "test"})
  21. }
  22. },{
  23. name: 'subscribe',
  24. hook: async (callback) => {
  25. subcallback = callback
  26. return makeSubResponse<{topic: string}>({topic: "test"})
  27. },
  28. onClose: (res, rpc) => {
  29. console.log("onClose", rpc.name === 'subscribe' && res?"OK":"")
  30. subcallback = null
  31. },
  32. onCallback: (...args:any) => {
  33. console.log("onCallback", args[0] === "test" && args[1] === "callback"?"OK":"")
  34. }
  35. },
  36. add,
  37. function triggerCallback(...messages:any[]):number {return subcallback.apply({}, messages)},
  38. ]
  39. }],{
  40. connectionHandler: (socket) => { },
  41. closeHandler: (socket) => { },
  42. errorHandler: (socket, err) => { throw err }
  43. })
  44. }
  45. describe('RPCServer', () => {
  46. let server: RPCServer<{ topic: string }, any>
  47. before(() => {
  48. server = makeServer()
  49. })
  50. after(() => {
  51. server.destroy()
  52. })
  53. it('should be able to use all kinds of RPC definitions', (done) => {
  54. const echo = (x) => x
  55. const server = new RPCServer(21003, [{
  56. name: 'HelloWorldRPCGroup',
  57. exportRPCs: () => [
  58. echo, //named function variable
  59. function echof(x){ return x }, //named function
  60. {
  61. name: 'echoExplicit', //describing object
  62. call: async (x,y,z) => [x,y,z]
  63. }
  64. ]
  65. }])
  66. const client = new RPCSocket(21003, 'localhost')
  67. client.connect().then(async () => {
  68. const r0 = await client['HelloWorldRPCGroup'].echo('Hello')
  69. const r1 = await client['HelloWorldRPCGroup'].echof('World')
  70. const r2 = await client['HelloWorldRPCGroup'].echoExplicit('R','P','C!')
  71. if(r0 === 'Hello' && r1 === 'World' && r2.join('') ==='RPC!'){
  72. client.destroy()
  73. server.destroy()
  74. done()
  75. }
  76. })
  77. })
  78. it('new RPCServer() should fail on bad RPC', (done) => {
  79. try{
  80. new RPCServer(20001, [{
  81. name: "bad",
  82. exportRPCs: () => [
  83. (aaa,bbb,ccc) => { return aaa+bbb+ccc }
  84. ]
  85. }])
  86. done(new Error("Didn't fail with bad RPC"))
  87. }catch(badRPCError){
  88. done()
  89. }
  90. })
  91. })
  92. describe('RPCSocket', () => {
  93. let client: RPCSocket
  94. let server: RPCServer<{topic: string}>
  95. before(async() => {
  96. server = makeServer()
  97. client = new RPCSocket(21010, "localhost")
  98. return await client.connect()
  99. })
  100. after(() => {
  101. client.destroy()
  102. server.destroy()
  103. })
  104. it('should have rpc echo', (done) => {
  105. client['test'].echo("x").then(x => {
  106. if(x === 'x')
  107. done()
  108. else
  109. done(new Error('echo RPC response did not match'))
  110. })
  111. })
  112. it('should add up to 6', (done) => {
  113. client['test'].add(1,2,3).then(x => {
  114. if(x === 6)
  115. done()
  116. else
  117. done(new Error('add RPC response did not match'))
  118. })
  119. })
  120. it('should subscribe with success', (done) => {
  121. client['test'].simpleSubscribe(console.log).then(res => {
  122. if(res.result === 'Success'){
  123. done()
  124. }else{
  125. console.error(res)
  126. done(new Error('Subscribe did not return success'))
  127. }
  128. })
  129. })
  130. it('subscribe should call back', (done) => {
  131. client['test'].subscribe((...args: any) => {
  132. if(args[0] === "test" && args[1] === "callback")
  133. done()
  134. else
  135. done(new Error("Bad callback value "+ args))
  136. }).then( async () => {
  137. await client['test'].triggerCallback("test", "callback")
  138. })
  139. })
  140. it('simpleSubscribe should call back', (done) => {
  141. client['test'].simpleSubscribe((...args: any) => {
  142. if(args[0] === "test_" && args[1] === "callback_")
  143. done()
  144. else
  145. done(new Error("Bad callback value "+ args))
  146. }).then( async () => {
  147. await client['test'].triggerCallback("test_", "callback_")
  148. })
  149. })
  150. })
  151. describe('It should do unhook', () => {
  152. let candy = "OK"
  153. let cb: Function
  154. let cb2: Function
  155. let client: RPCSocket
  156. let server: RPCServer<{topic: string}>
  157. before(async() => {
  158. server = new RPCServer<{ topic: string }>(21010, [{
  159. name: "test",
  160. exportRPCs: () => [{
  161. name: 'subscribe',
  162. hook: async(callback):Promise<SubscriptionResponse<{topic:string}>> => {
  163. cb = <Function> callback
  164. return {
  165. result: "Success",
  166. uuid: uuidv4(),
  167. topic: "test"
  168. }
  169. }
  170. },
  171. {
  172. name: 'subscribeWithParam',
  173. hook: async(param, callback):Promise<SubscriptionResponse<{topic:string}>> => {
  174. if(param != "OK"){
  175. console.log("param was"+ param);
  176. return {
  177. result: "Success",
  178. uuid: "no",
  179. topic: "test"
  180. }
  181. }
  182. cb2 = <Function> callback
  183. return {
  184. result: "Success",
  185. uuid: "OK",
  186. topic: "test"
  187. }
  188. }
  189. },
  190. function checkCandy():string { cb(candy); return candy },
  191. function stealCandy():string { candy = "_OK"; cb(candy); cb = () => {}; return candy }
  192. ]
  193. }],{
  194. connectionHandler: (socket) => { },
  195. closeHandler: (socket) => { },
  196. errorHandler: (socket, err) => { throw err }
  197. })
  198. client = new RPCSocket(21010, "localhost")
  199. return await client.connect()
  200. })
  201. after(() => {
  202. client.destroy()
  203. server.destroy()
  204. })
  205. it('Subscribe with param', (done) => {
  206. client['test'].subscribeWithParam("OK", c => {}).then( async (res: SubscriptionResponse) => {
  207. if(res.uuid === "OK"){
  208. done()
  209. }else
  210. done(new Error("Results did not match "+res.uuid))
  211. })
  212. })
  213. it('Unhook+unsubscribe should stop callbacks', (done) => {
  214. client['test'].subscribe(c => {}).then( async (res: SubscriptionResponse) => {
  215. const r1 = await client['test'].checkCandy()
  216. const r3 = await client['test'].stealCandy()
  217. client.unhook(res.uuid)
  218. const r2 = await client['test'].checkCandy()
  219. const r4 = await client['test'].checkCandy()
  220. if(r1 === "OK" && r3 === "_OK" && r2 === "_OK" && r4 === "_OK")
  221. done()
  222. else
  223. done(new Error("Results did not match: "+[r1,r2,r3,r4]))
  224. })
  225. })
  226. })
  227. type SesameTestIfc = {
  228. test: {
  229. checkCandy: ()=>Promise<string>
  230. subscribe: (callback) => Promise<SubscriptionResponse<{ topic: string; }>>
  231. }
  232. }
  233. describe('Sesame should unlock the socket', () => {
  234. let candy = "OK"
  235. let client: RPCSocket & SesameTestIfc
  236. let server: RPCServer
  237. let cb = (...args) => {}
  238. before((done) => {
  239. server = new RPCServer(21004, [{
  240. name: "test",
  241. exportRPCs: () => [
  242. {
  243. name: 'subscribe',
  244. hook: async(callback) => {
  245. cb = callback
  246. return <SubscriptionResponse>{
  247. result: "Success",
  248. uuid: uuidv4(),
  249. topic: 'test'
  250. }
  251. }
  252. },
  253. async function checkCandy():Promise<string> { cb(candy); cb=()=>{}; return candy },
  254. async function manyParams(a,b,c,d) {return [a,b,c,d]}
  255. ]}
  256. ],{
  257. sesame: (_sesame) => _sesame === 'sesame!'
  258. })
  259. const sock = new RPCSocket(21004, "localhost")
  260. sock.connect<SesameTestIfc>('sesame!').then(cli => {
  261. client = cli
  262. done()
  263. })
  264. })
  265. after(() => {
  266. client.destroy()
  267. server.destroy()
  268. })
  269. it('should work with sesame', (done) => {
  270. client.test.checkCandy().then(c => done())
  271. })
  272. it('should work with multiple params', (done) => {
  273. client.test['manyParams']('a','b','c','d').then(c => {
  274. if(c[0] == 'a' && c[1] === 'b' && c[2] === 'c' && c[3] === 'd')
  275. done()
  276. })
  277. })
  278. it('should not work without sesame', (done) => {
  279. const sock = new RPCSocket(21004, "localhost")
  280. sock.connect<SesameTestIfc>( /* no sesame */).then(async (cli) => {
  281. if(!cli.test)
  282. done()
  283. else{
  284. done(new Error("Function supposed to be removed without sesame"))
  285. }
  286. cli.destroy()
  287. sock.destroy()
  288. })
  289. })
  290. it('should fail with wrong sesame', (done) => {
  291. const sock = new RPCSocket(21004, "localhost")
  292. sock.connect<SesameTestIfc>('abasd').then(async (cli) => {
  293. if(!cli.test)
  294. done()
  295. else{
  296. done(new Error("Function supposed to be removed without sesame"))
  297. }
  298. cli.destroy()
  299. sock.destroy()
  300. })
  301. })
  302. it('callback should work with sesame', (done) => {
  303. client.test.subscribe((c) => {
  304. if(c === candy){
  305. done()
  306. }
  307. }).then(d => {
  308. if(d.result !== 'Success')
  309. done('unexpected valid response')
  310. client.test.checkCandy()
  311. })
  312. })
  313. })
  314. describe('Error handling', ()=>{
  315. let createUser = async( user: {a:any,b:any}) => {
  316. throw new Error("BAD BAD BAD")
  317. }
  318. it("RPC throws on client without handler", (done)=>{
  319. let server = new RPCServer(21004, [ {
  320. name: 'createUser' as 'createUser',
  321. exportRPCs: () => [{
  322. name: 'createUser' as 'createUser',
  323. call: createUser
  324. }]}], {
  325. })
  326. let sock = new RPCSocket(21004, 'localhost')
  327. sock.connect().then((cli) => {
  328. cli["createUser"]["createUser"]({
  329. a:'a',
  330. b:'b'
  331. })
  332. .then(r => {
  333. if(r != null)
  334. done("UNEXPECTED RESULT " + r)
  335. })
  336. .catch((e) => {
  337. //console.log("EXPECTED CLIENT EXCEPTION", String(e));
  338. done()
  339. })
  340. .finally(() => {
  341. cli.destroy()
  342. sock.destroy()
  343. server.destroy()
  344. })
  345. })
  346. })
  347. it("RPC throws on server with handler", (done)=>{
  348. let server = new RPCServer(21004, [ {
  349. name: 'createUser' as 'createUser',
  350. exportRPCs: () => [{
  351. name: 'createUser' as 'createUser',
  352. call: createUser
  353. }]}], {
  354. errorHandler: (socket, e, rpcName, args) => {
  355. done()
  356. }
  357. })
  358. let sock = new RPCSocket(21004, 'localhost')
  359. sock.connect().then((cli) => {
  360. cli["createUser"]["createUser"]({
  361. a:'a',
  362. b:'b'
  363. })
  364. .then(r => {
  365. if(r != null)
  366. done("UNEXPECTED RESULT " + r)
  367. })
  368. .catch((e) => {
  369. done("UNEXPECTED CLIENT ERROR " + e)
  370. done(e)
  371. })
  372. .finally(() => {
  373. cli.destroy()
  374. sock.destroy()
  375. server.destroy()
  376. })
  377. })
  378. })
  379. })
  380. describe("Errorhandler functionality", ()=>{
  381. let createUser = async( user: {a:any,b:any}) => {
  382. throw new Error("BAD BAD BAD")
  383. }
  384. it("correct values are passed to the handler", (done)=>{
  385. let server = new RPCServer(21004, [ {
  386. name: 'createUser' as 'createUser',
  387. exportRPCs: () => [{
  388. name: 'createUser' as 'createUser',
  389. call: createUser
  390. }]}], {
  391. errorHandler: (socket, e, rpcName, args) => {
  392. if(e.message === "BAD BAD BAD" && rpcName === "createUser" && args[0]['a'] === 'a' && args[0]['b'] === 'b')
  393. done()
  394. }
  395. })
  396. let sock = new RPCSocket(21004, 'localhost')
  397. sock.connect().then((cli) => {
  398. cli["createUser"]["createUser"]({
  399. a:'a',
  400. b:'b'
  401. })
  402. .then(r => {
  403. if(r != null)
  404. done("UNEXPECTED RESULT " + r)
  405. })
  406. .catch((e) => {
  407. done("UNEXPECTED CLIENT ERROR " + e)
  408. done(e)
  409. })
  410. .finally(() => {
  411. cli.destroy()
  412. sock.destroy()
  413. server.destroy()
  414. })
  415. })
  416. })
  417. it("handler sees sesame", (done)=>{
  418. let sesame = "AAAAAAAAAAAAAAA"
  419. let server = new RPCServer(21004, [ {
  420. name: 'createUser' as 'createUser',
  421. exportRPCs: () => [{
  422. name: 'createUser' as 'createUser',
  423. call: createUser
  424. }]}], {
  425. sesame: sesame,
  426. errorHandler: (socket, e, rpcName, args) => {
  427. if(e.message === "BAD BAD BAD" && rpcName === "createUser" && args[0] === sesame && args[1]['a'] === 'a' && args[1]['b'] === 'b')
  428. done()
  429. }
  430. })
  431. let sock = new RPCSocket(21004, 'localhost')
  432. sock.connect(sesame).then((cli) => {
  433. cli["createUser"]["createUser"]({
  434. a:'a',
  435. b:'b'
  436. })
  437. .then(r => {
  438. if(r != null)
  439. done("UNEXPECTED RESULT " + r)
  440. })
  441. .catch((e) => {
  442. done("UNEXPECTED CLIENT ERROR " + e)
  443. done(e)
  444. })
  445. .finally(() => {
  446. cli.destroy()
  447. sock.destroy()
  448. server.destroy()
  449. })
  450. })
  451. })
  452. })
  453. type myExporterIfc = {
  454. MyExporter: {
  455. myRPC: ()=>Promise<string>
  456. }
  457. }
  458. describe("Class binding", ()=>{
  459. let exporter1 : MyExporter
  460. let serv : RPCServer<{}, myExporterIfc>
  461. let sock: RPCSocket & myExporterIfc
  462. let allowed = true
  463. class MyExporter implements RPCExporter<myExporterIfc>{
  464. name = "MyExporter" as "MyExporter";
  465. exportRPCs = () => [
  466. this.myRPC
  467. ]
  468. myRPC = async () => {
  469. serv.setExporters([new MyOtherExporter])
  470. return "Hello World"
  471. }
  472. }
  473. class MyOtherExporter implements RPCExporter<myExporterIfc>{
  474. name = "MyExporter" as "MyExporter";
  475. exportRPCs = () => [
  476. this.myRPC
  477. ]
  478. myRPC = async () => {
  479. return "Hello Borld"
  480. }
  481. }
  482. before(done => {
  483. exporter1 = new MyExporter()
  484. serv = new RPCServer<{}, myExporterIfc>(21004, [exporter1], {
  485. accessFilter: async (sesame,exporter) => {
  486. switch(exporter.name){
  487. case "MyExporter":
  488. if(!allowed) return false
  489. allowed = false
  490. return sesame==='xxx';
  491. default:
  492. return false
  493. }
  494. },
  495. sesame: "xxx"
  496. })
  497. done()
  498. })
  499. beforeEach((done)=>{
  500. const s = new RPCSocket(21004, 'localhost')
  501. s.connect<myExporterIfc>("xxx").then(conn => {
  502. sock = conn
  503. done()
  504. })
  505. })
  506. afterEach(done => {
  507. sock.destroy()
  508. done()
  509. })
  510. after(() => {
  511. serv.destroy()
  512. })
  513. it("binds correctly", (done)=>{
  514. sock['MyExporter'].myRPC().then((res) => {
  515. done(new Error(res))
  516. }).catch(e => {
  517. //job will time out because of setExporters
  518. allowed = true
  519. done()
  520. })
  521. })
  522. it("changes exporters", (done) => {
  523. sock['MyExporter'].myRPC().then((res) => {
  524. if(res === "Hello Borld")
  525. done()
  526. else
  527. done(new Error(res))
  528. })
  529. })
  530. it("use sesameFilter", (done) => {
  531. if(!sock['MyExporter']) done()
  532. else done(new Error("RPC supposed to be gone"))
  533. })
  534. })
  535. describe("attaching handlers before connecting", ()=>{
  536. it("fires error if server is unreachable", (done)=>{
  537. const sock = new RPCSocket(21004, 'localhost')
  538. let errorHandleCount = 0
  539. sock.on('error', (err) => {
  540. //attached listener fires first
  541. if(errorHandleCount != 0){
  542. console.log("Error handler didn't fire first");
  543. }else{
  544. errorHandleCount++
  545. }
  546. })
  547. sock.connect().then(_ => {
  548. console.log("Unexpected successful connect")
  549. }).catch(e => {
  550. //catch clause fires second
  551. if(errorHandleCount != 1){
  552. console.log("catch clause didn't fire second");
  553. }else{
  554. sock.destroy()
  555. done()
  556. }
  557. })
  558. })
  559. it("fires error if call is unknown", (done)=>{
  560. const serv = new RPCServer(21004)
  561. const sock = new RPCSocket(21004, 'localhost')
  562. sock.on('error', (err) => {
  563. sock.destroy()
  564. serv.destroy()
  565. done()
  566. })
  567. sock.connect().then(_ => {
  568. sock.call("unknownRPC123", "AAAAA").catch(e => { /* ignore */})
  569. }).catch(e => {
  570. console.log("unexpected connect catch clause");
  571. done(e)
  572. })
  573. })
  574. it("demands catch on method invocation if call is unknown", (done)=>{
  575. const serv = new RPCServer(21004)
  576. const sock = new RPCSocket(21004, 'localhost')
  577. sock.connect().then(_ => {
  578. sock.call("unknownRPC123", "AAAAA").catch(e => {
  579. sock.destroy()
  580. serv.destroy()
  581. done()
  582. })
  583. }).catch(e => {
  584. console.log("unexpected connect catch clause");
  585. done(e)
  586. })
  587. })
  588. })