Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Test.ts 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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: ""
  22. }
  23. }
  24. },{
  25. name: 'subscribe',
  26. hook: async (callback) => {
  27. subcallback = callback
  28. return {
  29. result: "Success",
  30. uuid: uuidv4(),
  31. topic: ""
  32. }
  33. },
  34. onClose: (res, rpc) => {
  35. console.log("Specific close handler for", rpc.name, res)
  36. subcallback = null
  37. },
  38. onCallback: (...args:any) => { console.log.apply(console, args) }
  39. },
  40. function add(...args:number[]):number {return args.reduce((a,b)=>a+b, 0)},
  41. function triggerCallback(...messages:any[]):number {return subcallback.apply({}, messages)},
  42. ]
  43. }])
  44. }
  45. let server: RPCServer<{ topic: string}>
  46. describe('RPCServer', () => {
  47. before(async(done) => {
  48. server = makeServer()
  49. done()
  50. })
  51. after(async() => {
  52. await server.destroy()
  53. })
  54. it('new RPCServer() should fail on bad RPC', (done) => {
  55. try{
  56. new RPCServer(20001, [{
  57. name: "bad",
  58. exportRPCs: () => [
  59. (aaa,bbb,ccc) => { return aaa+bbb+ccc }
  60. ]
  61. }])
  62. done(new Error("Didn't fail with bad RPC"))
  63. }catch(badRPCError){
  64. done()
  65. }
  66. })
  67. })
  68. describe('RPCSocket', () => {
  69. let client: RPCSocket
  70. let server: RPCServer<{topic: string}>
  71. before(async() => {
  72. server = makeServer()
  73. client = new RPCSocket(20000, "localhost")
  74. return await client.connect()
  75. })
  76. after(async() => {
  77. client.destroy()
  78. server.destroy()
  79. })
  80. it('should have rpc echo', (done) => {
  81. client['test'].echo("x").then(x => {
  82. if(x === 'x')
  83. done()
  84. else
  85. done(new Error('echo RPC response did not match'))
  86. })
  87. })
  88. it('should subscribe with success', (done) => {
  89. client['test'].simpleSubscribe(console.log).then(res => {
  90. if(res.result === 'Success'){
  91. done()
  92. }else{
  93. console.error(res)
  94. done(new Error('Subscribe did not return success'))
  95. }
  96. })
  97. })
  98. it('subscribe should call back', (done) => {
  99. client['test'].subscribe((...args: any) => {
  100. if(args[0] === "test" && args[1] === "callback")
  101. done()
  102. else
  103. done(new Error("Bad callback value "+ args))
  104. }).then( async () => {
  105. await client['test'].triggerCallback("test", "callback")
  106. })
  107. })
  108. it('simpleSubscribe should call back', (done) => {
  109. client['test'].simpleSubscribe((...args: any) => {
  110. if(args[0] === "test_" && args[1] === "callback_")
  111. done()
  112. else
  113. done(new Error("Bad callback value "+ args))
  114. }).then( async () => {
  115. await client['test'].triggerCallback("test_", "callback_")
  116. })
  117. })
  118. })