Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Test.ts 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { describe, it, beforeEach } from "mocha";
  2. import { expect } from "chai";
  3. import { RPCServer } from '../src/Backend'
  4. import * as uuidv4 from "uuid/v4"
  5. import { RPCSocket } from "../src/Frontend";
  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. })