123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- import { describe, it, beforeEach } from "mocha";
- import { expect } from "chai";
-
-
- import { RPCServer } from '../src/Backend'
- import * as uuidv4 from "uuid/v4"
- import { RPCSocket } from "../src/Frontend";
-
- function makeServer(){
- let subcallback
- return new RPCServer<{ topic: string }>(20000, [{
- name: "test",
- exportRPCs: () => [
- {
- name: 'echo',
- call: async (s:string) => s,
- },{
- name: 'simpleSubscribe',
- hook: async(callback) => {
- subcallback = callback
- return {
- result: "Success",
- uuid: uuidv4(),
- topic: "test"
- }
- }
- },{
- name: 'subscribe',
- hook: async (callback) => {
- subcallback = callback
- return {
- result: "Success",
- uuid: uuidv4(),
- topic: "test"
- }
- },
- onClose: (res, rpc) => {
- console.log("onClose", rpc.name === 'subscribe' && res?"OK":"")
- subcallback = null
- },
- onCallback: (...args:any) => {
- console.log("onCallback", args[0] === "test" && args[1] === "callback"?"OK":"")
- }
- },
- function add(...args:number[]):number {return args.reduce((a,b)=>a+b, 0)},
- function triggerCallback(...messages:any[]):number {return subcallback.apply({}, messages)},
- ]
- }],{
- connectionHandler: (socket) => { console.log("connectionHandler OK") },
- closeHandler: (socket) => { console.log("closeHandler OK") },
- errorHandler: (socket, err) => { console.error("errorHandler OK SO YOU SHOULDN'T SEE THIS"); throw err }
- })
- }
-
-
- describe('RPCServer', () => {
- let server: RPCServer<{ topic: string}>
-
- before((done) => {
- server = makeServer()
- done()
- })
-
- after(async() => {
- await server.destroy()
- })
-
- it('new RPCServer() should fail on bad RPC', (done) => {
- try{
- new RPCServer(20001, [{
- name: "bad",
- exportRPCs: () => [
- (aaa,bbb,ccc) => { return aaa+bbb+ccc }
- ]
- }])
- done(new Error("Didn't fail with bad RPC"))
- }catch(badRPCError){
- done()
- }
- })
- })
-
-
- describe('RPCSocket', () => {
- let client: RPCSocket
- let server: RPCServer<{topic: string}>
-
- before(async() => {
- server = makeServer()
- client = new RPCSocket(20000, "localhost")
- return await client.connect()
- })
-
- after(() => {
- client.destroy()
- server.destroy()
- })
-
-
- it('should have rpc echo', (done) => {
- client['test'].echo("x").then(x => {
- if(x === 'x')
- done()
- else
- done(new Error('echo RPC response did not match'))
- })
- })
-
- it('should subscribe with success', (done) => {
- client['test'].simpleSubscribe(console.log).then(res => {
- if(res.result === 'Success'){
- done()
- }else{
- console.error(res)
- done(new Error('Subscribe did not return success'))
- }
- })
- })
-
- it('subscribe should call back', (done) => {
- client['test'].subscribe((...args: any) => {
- if(args[0] === "test" && args[1] === "callback")
- done()
- else
- done(new Error("Bad callback value "+ args))
- }).then( async () => {
- await client['test'].triggerCallback("test", "callback")
- })
- })
-
- it('simpleSubscribe should call back', (done) => {
- client['test'].simpleSubscribe((...args: any) => {
- if(args[0] === "test_" && args[1] === "callback_")
- done()
- else
- done(new Error("Bad callback value "+ args))
- }).then( async () => {
- await client['test'].triggerCallback("test_", "callback_")
- })
- })
-
- })
|