quasi unhook test
This commit is contained in:
+2
-3
@@ -17,8 +17,8 @@ export class RPCSocket implements I.Socket{
|
|||||||
Object.defineProperty(this, 'socket', {value: undefined, writable: true})
|
Object.defineProperty(this, 'socket', {value: undefined, writable: true})
|
||||||
}
|
}
|
||||||
|
|
||||||
public hook(name: T.Name, args: T.Arg){
|
public hook(name: T.Name, handler: (...args:any[]) => any | Promise<any>){
|
||||||
return this.socket.hook(name, args)
|
return this.socket.hook(name, handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
public unhook(name: T.Name){
|
public unhook(name: T.Name){
|
||||||
@@ -47,7 +47,6 @@ export class RPCSocket implements I.Socket{
|
|||||||
|
|
||||||
public async connect(){
|
public async connect(){
|
||||||
this.socket = await bsock.connect(this.port, this.server, this.tls)
|
this.socket = await bsock.connect(this.port, this.server, this.tls)
|
||||||
this.on('error', () => {})
|
|
||||||
|
|
||||||
const info:T.ExtendedRpcInfo[] = await this.info()
|
const info:T.ExtendedRpcInfo[] = await this.info()
|
||||||
info.forEach(i => {
|
info.forEach(i => {
|
||||||
|
|||||||
+1
-1
@@ -8,7 +8,7 @@ export interface Exporter<T = {}>{
|
|||||||
|
|
||||||
export interface Socket extends Destroyable {
|
export interface Socket extends Destroyable {
|
||||||
port: number
|
port: number
|
||||||
hook: (rpcname: T.Name, ...args: T.Any[]) => I.Socket
|
hook: (rpcname: T.Name, handler: (...args:any[]) => any | Promise<any>) => I.Socket
|
||||||
unhook: (rpcname:T.Name) => I.Socket
|
unhook: (rpcname:T.Name) => I.Socket
|
||||||
call: (rpcname:T.Name, ...args: T.Any[]) => Promise<T.Any>
|
call: (rpcname:T.Name, ...args: T.Any[]) => Promise<T.Any>
|
||||||
fire: (rpcname:T.Name, ...args: T.Any[]) => Promise<T.Any>
|
fire: (rpcname:T.Name, ...args: T.Any[]) => Promise<T.Any>
|
||||||
|
|||||||
+57
-4
@@ -1,10 +1,9 @@
|
|||||||
import { describe, it, beforeEach } from "mocha";
|
import { describe, it } from "mocha";
|
||||||
import { expect } from "chai";
|
|
||||||
|
|
||||||
|
|
||||||
import { RPCServer } from '../src/Backend'
|
import { RPCServer } from '../src/Backend'
|
||||||
import * as uuidv4 from "uuid/v4"
|
import * as uuidv4 from "uuid/v4"
|
||||||
import { RPCSocket } from "../src/Frontend";
|
import { RPCSocket } from "../src/Frontend";
|
||||||
|
import { SubscriptionResponse } from "../src/Types";
|
||||||
|
|
||||||
function makeServer(){
|
function makeServer(){
|
||||||
let subcallback
|
let subcallback
|
||||||
@@ -138,5 +137,59 @@ describe('RPCSocket', () => {
|
|||||||
await client['test'].triggerCallback("test_", "callback_")
|
await client['test'].triggerCallback("test_", "callback_")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('It should do unhook', () => {
|
||||||
|
let candy = "OK"
|
||||||
|
let cb: Function
|
||||||
|
let client: RPCSocket
|
||||||
|
let server: RPCServer<{topic: string}>
|
||||||
|
|
||||||
|
before(async() => {
|
||||||
|
server = new RPCServer<{ topic: string }>(20000, [{
|
||||||
|
name: "test",
|
||||||
|
exportRPCs: () => [{
|
||||||
|
name: 'subscribe',
|
||||||
|
hook: async(callback) => {
|
||||||
|
cb = callback
|
||||||
|
return {
|
||||||
|
result: "Success",
|
||||||
|
uuid: uuidv4(),
|
||||||
|
topic: "test"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
function checkCandy():string { cb(candy); return candy },
|
||||||
|
function stealCandy():string { candy = "_OK"; cb(candy); cb = (...any) => console.log.apply(console,["Server:", ...any]); return candy }
|
||||||
|
]
|
||||||
|
}],{
|
||||||
|
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 }
|
||||||
|
})
|
||||||
|
client = new RPCSocket(20000, "localhost")
|
||||||
|
return await client.connect()
|
||||||
|
})
|
||||||
|
|
||||||
|
after(() => {
|
||||||
|
client.destroy()
|
||||||
|
server.destroy()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Unhook+unsubscribe should stop callbacks', (done) => {
|
||||||
|
client['test'].subscribe(c => console.log("Client: "+c)).then( async (res: SubscriptionResponse) => {
|
||||||
|
const r1 = await client['test'].checkCandy()
|
||||||
|
const r3 = await client['test'].stealCandy()
|
||||||
|
client.unhook(res.uuid)
|
||||||
|
console.log("---- No client output below this line")
|
||||||
|
const r2 = await client['test'].checkCandy()
|
||||||
|
const r4 = await client['test'].checkCandy()
|
||||||
|
console.log("---- More output below")
|
||||||
|
|
||||||
|
if(r1 === "OK" && r3 === "_OK" && r2 === "_OK" && r4 === "_OK")
|
||||||
|
done()
|
||||||
|
else
|
||||||
|
done(new Error("Results did not match: "+[r1,r2,r3,r4]))
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
Reference in New Issue
Block a user