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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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 }>(21000, [{
  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((done) => {
  47. server = makeServer()
  48. done()
  49. })
  50. after(async() => {
  51. await 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(21000, "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 client: RPCSocket
  155. let server: RPCServer<{topic: string}>
  156. before(async() => {
  157. server = new RPCServer<{ topic: string }>(21000, [{
  158. name: "test",
  159. exportRPCs: () => [{
  160. name: 'subscribe',
  161. hook: async(callback):Promise<SubscriptionResponse<{topic:string}>> => {
  162. cb = <Function> callback
  163. return {
  164. result: "Success",
  165. uuid: uuidv4(),
  166. topic: "test"
  167. }
  168. }
  169. },
  170. function checkCandy():string { cb(candy); return candy },
  171. function stealCandy():string { candy = "_OK"; cb(candy); cb = () => {}; return candy }
  172. ]
  173. }],{
  174. connectionHandler: (socket) => { },
  175. closeHandler: (socket) => { },
  176. errorHandler: (socket, err) => { throw err }
  177. })
  178. client = new RPCSocket(21000, "localhost")
  179. return await client.connect()
  180. })
  181. after(() => {
  182. client.destroy()
  183. server.destroy()
  184. })
  185. it('Unhook+unsubscribe should stop callbacks', (done) => {
  186. client['test'].subscribe(c => {}).then( async (res: SubscriptionResponse) => {
  187. const r1 = await client['test'].checkCandy()
  188. const r3 = await client['test'].stealCandy()
  189. client.unhook(res.uuid)
  190. const r2 = await client['test'].checkCandy()
  191. const r4 = await client['test'].checkCandy()
  192. if(r1 === "OK" && r3 === "_OK" && r2 === "_OK" && r4 === "_OK")
  193. done()
  194. else
  195. done(new Error("Results did not match: "+[r1,r2,r3,r4]))
  196. })
  197. })
  198. })
  199. type SesameTestIfc = {
  200. test: {
  201. checkCandy: ()=>Promise<string>
  202. subscribe: (callback) => Promise<SubscriptionResponse<{ topic: string; }>>
  203. }
  204. }
  205. describe('Sesame should unlock the socket', () => {
  206. let candy = "OK"
  207. let client: RPCSocket & SesameTestIfc
  208. let server: RPCServer
  209. let cb = (...args) => {}
  210. before(async() => {
  211. server = new RPCServer(21004, [{
  212. name: "test",
  213. exportRPCs: () => [
  214. {
  215. name: 'subscribe',
  216. hook: async(callback) => {
  217. cb = callback
  218. return <SubscriptionResponse>{
  219. result: "Success",
  220. uuid: uuidv4(),
  221. topic: 'test'
  222. }
  223. }
  224. },
  225. async function checkCandy():Promise<string> { cb(candy); cb=()=>{}; return candy },
  226. async function manyParams(a,b,c,d) {return [a,b,c,d]}
  227. ]}
  228. ],{
  229. sesame: (_sesame) => _sesame === 'sesame!'
  230. })
  231. const sock = new RPCSocket(21004, "localhost")
  232. client = await sock.connect<SesameTestIfc>('sesame!')
  233. })
  234. after(() => {
  235. client.destroy()
  236. server.destroy()
  237. })
  238. it('should work with sesame', (done) => {
  239. client.test.checkCandy().then(c => done())
  240. })
  241. it('should work with multiple params', (done) => {
  242. client.test['manyParams']('a','b','c','d').then(c => {
  243. if(c[0] == 'a' && c[1] === 'b' && c[2] === 'c' && c[3] === 'd')
  244. done()
  245. })
  246. })
  247. it('should not work without sesame', (done) => {
  248. const sock = new RPCSocket(21004, "localhost")
  249. sock.connect<SesameTestIfc>( /* no sesame */).then(async (c) => {
  250. c.test.checkCandy().then(d => {
  251. done()
  252. }).catch(e => {
  253. //console.log("EXPECTED CLIENT EXCEPTION", String(e));
  254. done()
  255. }).finally(() => {
  256. sock.destroy()
  257. })
  258. })
  259. })
  260. it('should fail with wrong sesame', (done) => {
  261. const sock = new RPCSocket(21004, "localhost")
  262. sock.connect<SesameTestIfc>('abasd').then(async (c) => {
  263. c.test.checkCandy().then(d => {
  264. done("should not be able to get candy")
  265. }).catch(e => {
  266. //console.log("EXPECTED CLIENT EXCEPTION", String(e));
  267. done()
  268. }).finally(() => {
  269. sock.destroy()
  270. })
  271. })
  272. })
  273. it('callback should work with sesame', (done) => {
  274. client.test.subscribe((c) => {
  275. if(c === candy){
  276. done()
  277. }
  278. }).then(d => {
  279. if(d.result !== 'Success')
  280. done('unexpected valid response')
  281. client.test.checkCandy()
  282. })
  283. })
  284. it('callback should not work without sesame', (done) => {
  285. const sock = new RPCSocket(21004, "localhost")
  286. sock.connect<SesameTestIfc>( /* no sesame */).then(async (c) => {
  287. c.test.subscribe((c) => {
  288. console.log("CALLBACK TRIGGERED UNEXPECTED");
  289. if(c === candy)
  290. done("super not")
  291. }).then(async d => {
  292. await client.test.checkCandy()
  293. if(d == null){
  294. done()
  295. }else
  296. done('unexpected valid response '+(d) )
  297. c.destroy()
  298. })
  299. })
  300. })
  301. })
  302. describe('Error handling', ()=>{
  303. let createUser = async( user: {a:any,b:any}) => {
  304. throw new Error("BAD BAD BAD")
  305. }
  306. it("RPC throws on client without handler", (done)=>{
  307. let server = new RPCServer(21004, [ {
  308. name: 'createUser' as 'createUser',
  309. exportRPCs: () => [{
  310. name: 'createUser' as 'createUser',
  311. call: createUser
  312. }]}], {
  313. })
  314. let sock = new RPCSocket(21004, 'localhost')
  315. sock.connect().then((cli) => {
  316. cli["createUser"]["createUser"]({
  317. a:'a',
  318. b:'b'
  319. })
  320. .then(r => {
  321. if(r != null)
  322. done("UNEXPECTED RESULT " + r)
  323. })
  324. .catch((e) => {
  325. //console.log("EXPECTED CLIENT EXCEPTION", String(e));
  326. done()
  327. })
  328. .finally(() => {
  329. sock.destroy()
  330. server.destroy()
  331. })
  332. })
  333. })
  334. it("RPC throws on server with handler", (done)=>{
  335. let server = new RPCServer(21004, [ {
  336. name: 'createUser' as 'createUser',
  337. exportRPCs: () => [{
  338. name: 'createUser' as 'createUser',
  339. call: createUser
  340. }]}], {
  341. errorHandler: (socket, e, rpcName, args) => {
  342. done()
  343. }
  344. })
  345. let sock = new RPCSocket(21004, 'localhost')
  346. sock.connect().then((cli) => {
  347. cli["createUser"]["createUser"]({
  348. a:'a',
  349. b:'b'
  350. })
  351. .then(r => {
  352. if(r != null)
  353. done("UNEXPECTED RESULT " + r)
  354. })
  355. .catch((e) => {
  356. done("UNEXPECTED CLIENT ERROR " + e)
  357. done(e)
  358. })
  359. .finally(() => {
  360. sock.destroy()
  361. server.destroy()
  362. })
  363. })
  364. })
  365. })
  366. describe("Errorhandler functionality", ()=>{
  367. let createUser = async( user: {a:any,b:any}) => {
  368. throw new Error("BAD BAD BAD")
  369. }
  370. it("correct values are passed to the handler", (done)=>{
  371. let server = new RPCServer(21004, [ {
  372. name: 'createUser' as 'createUser',
  373. exportRPCs: () => [{
  374. name: 'createUser' as 'createUser',
  375. call: createUser
  376. }]}], {
  377. errorHandler: (socket, e, rpcName, args) => {
  378. if(e.message === "BAD BAD BAD" && rpcName === "createUser" && args[0]['a'] === 'a' && args[0]['b'] === 'b')
  379. done()
  380. }
  381. })
  382. let sock = new RPCSocket(21004, 'localhost')
  383. sock.connect().then((cli) => {
  384. cli["createUser"]["createUser"]({
  385. a:'a',
  386. b:'b'
  387. })
  388. .then(r => {
  389. if(r != null)
  390. done("UNEXPECTED RESULT " + r)
  391. })
  392. .catch((e) => {
  393. done("UNEXPECTED CLIENT ERROR " + e)
  394. done(e)
  395. })
  396. .finally(() => {
  397. sock.destroy()
  398. server.destroy()
  399. })
  400. })
  401. })
  402. it("handler sees sesame", (done)=>{
  403. let sesame = "AAAAAAAAAAAAAAA"
  404. let server = new RPCServer(21004, [ {
  405. name: 'createUser' as 'createUser',
  406. exportRPCs: () => [{
  407. name: 'createUser' as 'createUser',
  408. call: createUser
  409. }]}], {
  410. sesame: sesame,
  411. errorHandler: (socket, e, rpcName, args) => {
  412. if(e.message === "BAD BAD BAD" && rpcName === "createUser" && args[0] === sesame && args[1]['a'] === 'a' && args[1]['b'] === 'b')
  413. done()
  414. }
  415. })
  416. let sock = new RPCSocket(21004, 'localhost')
  417. sock.connect(sesame).then((cli) => {
  418. cli["createUser"]["createUser"]({
  419. a:'a',
  420. b:'b'
  421. })
  422. .then(r => {
  423. if(r != null)
  424. done("UNEXPECTED RESULT " + r)
  425. })
  426. .catch((e) => {
  427. done("UNEXPECTED CLIENT ERROR " + e)
  428. done(e)
  429. })
  430. .finally(() => {
  431. sock.destroy()
  432. server.destroy()
  433. })
  434. })
  435. })
  436. })