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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. import { describe, it, Func } from "mocha";
  2. import { RPCServer, RPCSocket, SubscriptionResponse, makeSubResponse } from '../Index'
  3. import * as uuidv4 from "uuid/v4"
  4. const add = (...args:number[]) => {return args.reduce((a,b)=>a+b, 0)}
  5. function makeServer(){
  6. let subcallback
  7. return new RPCServer<{ topic: string }>(21000, [{
  8. name: "test",
  9. exportRPCs: () => [
  10. {
  11. name: 'echo',
  12. call: async (s:string) => s,
  13. },{
  14. name: 'simpleSubscribe',
  15. hook: async(callback) => {
  16. subcallback = callback
  17. return makeSubResponse<{topic: string}>({topic: "test"})
  18. }
  19. },{
  20. name: 'subscribe',
  21. hook: async (callback) => {
  22. subcallback = callback
  23. return makeSubResponse<{topic: string}>({topic: "test"})
  24. },
  25. onClose: (res, rpc) => {
  26. console.log("onClose", rpc.name === 'subscribe' && res?"OK":"")
  27. subcallback = null
  28. },
  29. onCallback: (...args:any) => {
  30. console.log("onCallback", args[0] === "test" && args[1] === "callback"?"OK":"")
  31. }
  32. },
  33. add,
  34. function triggerCallback(...messages:any[]):number {return subcallback.apply({}, messages)},
  35. ]
  36. }],{
  37. connectionHandler: (socket) => { console.log("connectionHandler OK") },
  38. closeHandler: (socket) => { console.log("closeHandler OK") },
  39. errorHandler: (socket, err) => { console.error("errorHandler OK SO YOU SHOULDN'T SEE THIS"); throw err }
  40. })
  41. }
  42. describe('RPCServer', () => {
  43. let server: RPCServer<{ topic: string }, any>
  44. before((done) => {
  45. server = makeServer()
  46. done()
  47. })
  48. after(async() => {
  49. await server.destroy()
  50. })
  51. it('should be able to use all kinds of RPC definitions', (done) => {
  52. const echo = (x) => x
  53. const server = new RPCServer(21003, [{
  54. name: 'HelloWorldRPCGroup',
  55. exportRPCs: () => [
  56. echo, //named function variable
  57. function echof(x){ return x }, //named function
  58. {
  59. name: 'echoExplicit', //describing object
  60. call: async (x,y,z) => [x,y,z]
  61. }
  62. ]
  63. }])
  64. const client = new RPCSocket(21003, 'localhost')
  65. client.connect().then(async () => {
  66. const r0 = await client['HelloWorldRPCGroup'].echo('Hello')
  67. const r1 = await client['HelloWorldRPCGroup'].echof('World')
  68. const r2 = await client['HelloWorldRPCGroup'].echoExplicit('R','P','C!')
  69. if(r0 === 'Hello' && r1 === 'World' && r2.join('') ==='RPC!'){
  70. client.destroy()
  71. server.destroy()
  72. done()
  73. }
  74. })
  75. })
  76. it('new RPCServer() should fail on bad RPC', (done) => {
  77. try{
  78. new RPCServer(20001, [{
  79. name: "bad",
  80. exportRPCs: () => [
  81. (aaa,bbb,ccc) => { return aaa+bbb+ccc }
  82. ]
  83. }])
  84. done(new Error("Didn't fail with bad RPC"))
  85. }catch(badRPCError){
  86. done()
  87. }
  88. })
  89. })
  90. describe('RPCSocket', () => {
  91. let client: RPCSocket
  92. let server: RPCServer<{topic: string}>
  93. before(async() => {
  94. server = makeServer()
  95. client = new RPCSocket(21000, "localhost")
  96. return await client.connect()
  97. })
  98. after(() => {
  99. client.destroy()
  100. server.destroy()
  101. })
  102. it('should have rpc echo', (done) => {
  103. client['test'].echo("x").then(x => {
  104. if(x === 'x')
  105. done()
  106. else
  107. done(new Error('echo RPC response did not match'))
  108. })
  109. })
  110. it('should add up to 6', (done) => {
  111. client['test'].add(1,2,3).then(x => {
  112. if(x === 6)
  113. done()
  114. else
  115. done(new Error('add RPC response did not match'))
  116. })
  117. })
  118. it('should subscribe with success', (done) => {
  119. client['test'].simpleSubscribe(console.log).then(res => {
  120. if(res.result === 'Success'){
  121. done()
  122. }else{
  123. console.error(res)
  124. done(new Error('Subscribe did not return success'))
  125. }
  126. })
  127. })
  128. it('subscribe should call back', (done) => {
  129. client['test'].subscribe((...args: any) => {
  130. if(args[0] === "test" && args[1] === "callback")
  131. done()
  132. else
  133. done(new Error("Bad callback value "+ args))
  134. }).then( async () => {
  135. await client['test'].triggerCallback("test", "callback")
  136. })
  137. })
  138. it('simpleSubscribe should call back', (done) => {
  139. client['test'].simpleSubscribe((...args: any) => {
  140. if(args[0] === "test_" && args[1] === "callback_")
  141. done()
  142. else
  143. done(new Error("Bad callback value "+ args))
  144. }).then( async () => {
  145. await client['test'].triggerCallback("test_", "callback_")
  146. })
  147. })
  148. })
  149. describe('It should do unhook', () => {
  150. let candy = "OK"
  151. let cb: Function
  152. let client: RPCSocket
  153. let server: RPCServer<{topic: string}>
  154. before(async() => {
  155. server = new RPCServer<{ topic: string }>(21000, [{
  156. name: "test",
  157. exportRPCs: () => [{
  158. name: 'subscribe',
  159. hook: async(callback):Promise<SubscriptionResponse<{topic:string}>> => {
  160. cb = <Function> callback
  161. return {
  162. result: "Success",
  163. uuid: uuidv4(),
  164. topic: "test"
  165. }
  166. }
  167. },
  168. function checkCandy():string { cb(candy); return candy },
  169. function stealCandy():string { candy = "_OK"; cb(candy); cb = (...any) => console.log.apply(console,["Server:", ...any]); return candy }
  170. ]
  171. }],{
  172. connectionHandler: (socket) => { console.log("connectionHandler OK") },
  173. closeHandler: (socket) => { console.log("closeHandler OK") },
  174. errorHandler: (socket, err) => { console.error("errorHandler OK SO YOU SHOULDN'T SEE THIS"); throw err }
  175. })
  176. client = new RPCSocket(21000, "localhost")
  177. return await client.connect()
  178. })
  179. after(() => {
  180. client.destroy()
  181. server.destroy()
  182. })
  183. it('Unhook+unsubscribe should stop callbacks', (done) => {
  184. client['test'].subscribe(c => console.log("Client: "+c)).then( async (res: SubscriptionResponse) => {
  185. const r1 = await client['test'].checkCandy()
  186. const r3 = await client['test'].stealCandy()
  187. client.unhook(res.uuid)
  188. console.log("---- No client output below this line")
  189. const r2 = await client['test'].checkCandy()
  190. const r4 = await client['test'].checkCandy()
  191. console.log("---- More output below")
  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. if(d === candy)
  252. done("should not be able to get candy")
  253. done()
  254. }).finally(() => {
  255. sock.destroy()
  256. })
  257. })
  258. })
  259. it('callback should work with sesame', (done) => {
  260. client.test.subscribe((c) => {
  261. if(c === candy){
  262. done()
  263. }
  264. }).then(d => {
  265. if(d.result !== 'Success')
  266. done('expected valid response')
  267. client.test.checkCandy()
  268. })
  269. })
  270. it('callback should not work without sesame', (done) => {
  271. const sock = new RPCSocket(21004, "localhost")
  272. sock.connect<SesameTestIfc>( /* no sesame */).then(async (c) => {
  273. c.test.subscribe((c) => {
  274. console.log("CALLBACK TRIGGERED UNEXPECTED");
  275. if(c === candy)
  276. done("super not")
  277. }).then(async d => {
  278. await client.test.checkCandy()
  279. if(d == null){
  280. done()
  281. }else
  282. done('unexpected valid response '+(d) )
  283. c.destroy()
  284. })
  285. })
  286. })
  287. })
  288. /*
  289. class myServer{
  290. server = new RPCServer(21004, [ {
  291. name: 'createUser' as 'createUser',
  292. exportRPCs: () => [{
  293. name: 'createUser' as 'createUser',
  294. call: this.createUser
  295. }]
  296. }
  297. ])
  298. createUser = async( user: {a:any,b:any}) => {
  299. console.log(user)
  300. return user
  301. }
  302. }
  303. describe('Should pass the createUser edge case', ()=>{
  304. let server
  305. before(()=>{
  306. server = new myServer()
  307. })
  308. after(()=>{
  309. server.server.destroy()
  310. })
  311. it("should work", async ()=>{
  312. let sock = new RPCSocket(21004, 'localhost')
  313. let client = await sock.connect()
  314. client["createUser"]["createUser"]({
  315. a:'a',
  316. b:'b'
  317. }).then(console.log)
  318. })
  319. })
  320. */