|
@@ -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
|
+})
|