Browse Source

quasi unhook test

master
peter 4 years ago
parent
commit
83aa31c091
3 changed files with 60 additions and 8 deletions
  1. 2
    3
      src/Frontend.ts
  2. 1
    1
      src/Interfaces.ts
  3. 57
    4
      test/Test.ts

+ 2
- 3
src/Frontend.ts View File

@@ -17,8 +17,8 @@ export class RPCSocket implements I.Socket{
17 17
         Object.defineProperty(this, 'socket', {value: undefined, writable: true})
18 18
     }
19 19
 
20
-    public hook(name: T.Name, args: T.Arg){
21
-        return this.socket.hook(name, args)
20
+    public hook(name: T.Name, handler: (...args:any[]) => any | Promise<any>){
21
+        return this.socket.hook(name, handler)
22 22
     }
23 23
 
24 24
     public unhook(name: T.Name){
@@ -47,7 +47,6 @@ export class RPCSocket implements I.Socket{
47 47
     
48 48
     public async connect(){
49 49
         this.socket = await bsock.connect(this.port, this.server, this.tls)
50
-        this.on('error', () => {})
51 50
 
52 51
         const info:T.ExtendedRpcInfo[] = await this.info()
53 52
         info.forEach(i => {

+ 1
- 1
src/Interfaces.ts View File

@@ -8,7 +8,7 @@ export interface Exporter<T = {}>{
8 8
 
9 9
 export interface Socket extends Destroyable {
10 10
     port: number
11
-    hook: (rpcname: T.Name, ...args: T.Any[]) => I.Socket
11
+    hook: (rpcname: T.Name, handler: (...args:any[]) => any | Promise<any>) => I.Socket
12 12
     unhook: (rpcname:T.Name) => I.Socket
13 13
     call: (rpcname:T.Name, ...args: T.Any[]) => Promise<T.Any>
14 14
     fire: (rpcname:T.Name, ...args: T.Any[]) => Promise<T.Any>

+ 57
- 4
test/Test.ts View File

@@ -1,10 +1,9 @@
1
-import { describe, it, beforeEach } from "mocha";
2
-import { expect } from "chai";
3
-
1
+import { describe, it } from "mocha";
4 2
 
5 3
 import { RPCServer } from '../src/Backend'
6 4
 import * as uuidv4 from "uuid/v4"
7 5
 import { RPCSocket } from "../src/Frontend";
6
+import { SubscriptionResponse } from "../src/Types";
8 7
 
9 8
 function makeServer(){
10 9
     let subcallback
@@ -138,5 +137,59 @@ describe('RPCSocket', () => {
138 137
             await client['test'].triggerCallback("test_", "callback_")
139 138
         })
140 139
     })
141
-
142 140
 })
141
+
142
+describe('It should do unhook', () => {
143
+    let candy = "OK"
144
+    let cb: Function
145
+    let client: RPCSocket
146
+    let server: RPCServer<{topic: string}> 
147
+
148
+    before(async() => {
149
+        server = new RPCServer<{ topic: string }>(20000, [{
150
+            name: "test",
151
+            exportRPCs: () => [{
152
+                name: 'subscribe',
153
+                hook: async(callback) => {
154
+                    cb = callback
155
+                    return {
156
+                        result: "Success",
157
+                        uuid: uuidv4(),
158
+                        topic: "test"
159
+                    }
160
+                }
161
+            }, 
162
+            function checkCandy():string { cb(candy); return candy },
163
+            function stealCandy():string { candy = "_OK"; cb(candy); cb = (...any) => console.log.apply(console,["Server:", ...any]); return candy }
164
+        ]
165
+        }],{
166
+            connectionHandler: (socket) => { console.log("connectionHandler OK") },
167
+            closeHandler: (socket) => { console.log("closeHandler OK") },
168
+            errorHandler: (socket, err) => { console.error("errorHandler OK SO YOU SHOULDN'T SEE THIS"); throw err }
169
+        })
170
+        client = new RPCSocket(20000, "localhost")
171
+        return await client.connect()
172
+    })
173
+
174
+    after(() => {
175
+        client.destroy()
176
+        server.destroy()
177
+    })
178
+
179
+    it('Unhook+unsubscribe should stop callbacks', (done) => {
180
+        client['test'].subscribe(c => console.log("Client: "+c)).then( async (res: SubscriptionResponse) => {
181
+            const r1 = await client['test'].checkCandy()
182
+            const r3 = await client['test'].stealCandy()
183
+            client.unhook(res.uuid)
184
+            console.log("---- No client output below this line")
185
+            const r2 = await client['test'].checkCandy()
186
+            const r4 = await client['test'].checkCandy()
187
+            console.log("---- More output below")
188
+
189
+            if(r1 === "OK" && r3 === "_OK" && r2 === "_OK" && r4 === "_OK")
190
+                done()
191
+            else
192
+                done(new Error("Results did not match: "+[r1,r2,r3,r4]))
193
+        })
194
+    })
195
+})

Loading…
Cancel
Save