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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import { describe, it } from "mocha";
  2. import { RPCServer } from '../src/Backend'
  3. import * as uuidv4 from "uuid/v4"
  4. import { RPCSocket } from "../src/Frontend";
  5. import { SubscriptionResponse } from "../src/Types";
  6. function makeServer(){
  7. let subcallback
  8. return new RPCServer<{ topic: string }>(20000, [{
  9. name: "test",
  10. exportRPCs: () => [
  11. {
  12. name: 'echo',
  13. call: async (s:string) => s,
  14. },{
  15. name: 'simpleSubscribe',
  16. hook: async(callback) => {
  17. subcallback = callback
  18. return {
  19. result: "Success",
  20. uuid: uuidv4(),
  21. topic: "test"
  22. }
  23. }
  24. },{
  25. name: 'subscribe',
  26. hook: async (callback) => {
  27. subcallback = callback
  28. return {
  29. result: "Success",
  30. uuid: uuidv4(),
  31. topic: "test"
  32. }
  33. },
  34. onClose: (res, rpc) => {
  35. console.log("onClose", rpc.name === 'subscribe' && res?"OK":"")
  36. subcallback = null
  37. },
  38. onCallback: (...args:any) => {
  39. console.log("onCallback", args[0] === "test" && args[1] === "callback"?"OK":"")
  40. }
  41. },
  42. function add(...args:number[]):number {return args.reduce((a,b)=>a+b, 0)},
  43. function triggerCallback(...messages:any[]):number {return subcallback.apply({}, messages)},
  44. ]
  45. }],{
  46. connectionHandler: (socket) => { console.log("connectionHandler OK") },
  47. closeHandler: (socket) => { console.log("closeHandler OK") },
  48. errorHandler: (socket, err) => { console.error("errorHandler OK SO YOU SHOULDN'T SEE THIS"); throw err }
  49. })
  50. }
  51. describe('RPCServer', () => {
  52. let server: RPCServer<{ topic: string}>
  53. before((done) => {
  54. server = makeServer()
  55. done()
  56. })
  57. after(async() => {
  58. await server.destroy()
  59. })
  60. it('new RPCServer() should fail on bad RPC', (done) => {
  61. try{
  62. new RPCServer(20001, [{
  63. name: "bad",
  64. exportRPCs: () => [
  65. (aaa,bbb,ccc) => { return aaa+bbb+ccc }
  66. ]
  67. }])
  68. done(new Error("Didn't fail with bad RPC"))
  69. }catch(badRPCError){
  70. done()
  71. }
  72. })
  73. })
  74. describe('RPCSocket', () => {
  75. let client: RPCSocket
  76. let server: RPCServer<{topic: string}>
  77. before(async() => {
  78. server = makeServer()
  79. client = new RPCSocket(20000, "localhost")
  80. return await client.connect()
  81. })
  82. after(() => {
  83. client.destroy()
  84. server.destroy()
  85. })
  86. it('should have rpc echo', (done) => {
  87. client['test'].echo("x").then(x => {
  88. if(x === 'x')
  89. done()
  90. else
  91. done(new Error('echo RPC response did not match'))
  92. })
  93. })
  94. it('should subscribe with success', (done) => {
  95. client['test'].simpleSubscribe(console.log).then(res => {
  96. if(res.result === 'Success'){
  97. done()
  98. }else{
  99. console.error(res)
  100. done(new Error('Subscribe did not return success'))
  101. }
  102. })
  103. })
  104. it('subscribe should call back', (done) => {
  105. client['test'].subscribe((...args: any) => {
  106. if(args[0] === "test" && args[1] === "callback")
  107. done()
  108. else
  109. done(new Error("Bad callback value "+ args))
  110. }).then( async () => {
  111. await client['test'].triggerCallback("test", "callback")
  112. })
  113. })
  114. it('simpleSubscribe should call back', (done) => {
  115. client['test'].simpleSubscribe((...args: any) => {
  116. if(args[0] === "test_" && args[1] === "callback_")
  117. done()
  118. else
  119. done(new Error("Bad callback value "+ args))
  120. }).then( async () => {
  121. await client['test'].triggerCallback("test_", "callback_")
  122. })
  123. })
  124. })
  125. describe('It should do unhook', () => {
  126. let candy = "OK"
  127. let cb: Function
  128. let client: RPCSocket
  129. let server: RPCServer<{topic: string}>
  130. before(async() => {
  131. server = new RPCServer<{ topic: string }>(20000, [{
  132. name: "test",
  133. exportRPCs: () => [{
  134. name: 'subscribe',
  135. hook: async(callback) => {
  136. cb = callback
  137. return {
  138. result: "Success",
  139. uuid: uuidv4(),
  140. topic: "test"
  141. }
  142. }
  143. },
  144. function checkCandy():string { cb(candy); return candy },
  145. function stealCandy():string { candy = "_OK"; cb(candy); cb = (...any) => console.log.apply(console,["Server:", ...any]); return candy }
  146. ]
  147. }],{
  148. connectionHandler: (socket) => { console.log("connectionHandler OK") },
  149. closeHandler: (socket) => { console.log("closeHandler OK") },
  150. errorHandler: (socket, err) => { console.error("errorHandler OK SO YOU SHOULDN'T SEE THIS"); throw err }
  151. })
  152. client = new RPCSocket(20000, "localhost")
  153. return await client.connect()
  154. })
  155. after(() => {
  156. client.destroy()
  157. server.destroy()
  158. })
  159. it('Unhook+unsubscribe should stop callbacks', (done) => {
  160. client['test'].subscribe(c => console.log("Client: "+c)).then( async (res: SubscriptionResponse) => {
  161. const r1 = await client['test'].checkCandy()
  162. const r3 = await client['test'].stealCandy()
  163. client.unhook(res.uuid)
  164. console.log("---- No client output below this line")
  165. const r2 = await client['test'].checkCandy()
  166. const r4 = await client['test'].checkCandy()
  167. console.log("---- More output below")
  168. if(r1 === "OK" && r3 === "_OK" && r2 === "_OK" && r4 === "_OK")
  169. done()
  170. else
  171. done(new Error("Results did not match: "+[r1,r2,r3,r4]))
  172. })
  173. })
  174. })