Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Test.ts 6.5KB

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