|
@@ -0,0 +1,135 @@
|
|
1
|
+import { describe, it, beforeEach } from "mocha";
|
|
2
|
+import { expect } from "chai";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+import { RPCServer } from '../src/Backend'
|
|
6
|
+import * as uuidv4 from "uuid/v4"
|
|
7
|
+import { RPCSocket } from "../src/Frontend";
|
|
8
|
+
|
|
9
|
+function makeServer(){
|
|
10
|
+ let subcallback
|
|
11
|
+ return new RPCServer<{ topic: string }>(20000, [{
|
|
12
|
+ name: "test",
|
|
13
|
+ exportRPCs: () => [
|
|
14
|
+ {
|
|
15
|
+ name: 'echo',
|
|
16
|
+ call: async (s:string) => s,
|
|
17
|
+ },{
|
|
18
|
+ name: 'simpleSubscribe',
|
|
19
|
+ hook: async(callback) => {
|
|
20
|
+ subcallback = callback
|
|
21
|
+ return {
|
|
22
|
+ result: "Success",
|
|
23
|
+ uuid: uuidv4(),
|
|
24
|
+ topic: ""
|
|
25
|
+ }
|
|
26
|
+ }
|
|
27
|
+ },{
|
|
28
|
+ name: 'subscribe',
|
|
29
|
+ hook: async (callback) => {
|
|
30
|
+ subcallback = callback
|
|
31
|
+ return {
|
|
32
|
+ result: "Success",
|
|
33
|
+ uuid: uuidv4(),
|
|
34
|
+ topic: ""
|
|
35
|
+ }
|
|
36
|
+ },
|
|
37
|
+ onClose: (res, rpc) => {
|
|
38
|
+ console.log("Specific close handler for", rpc.name, res)
|
|
39
|
+ subcallback = null
|
|
40
|
+ },
|
|
41
|
+ onCallback: (...args:any) => { console.log.apply(console, args) }
|
|
42
|
+ },
|
|
43
|
+ function add(...args:number[]):number {return args.reduce((a,b)=>a+b, 0)},
|
|
44
|
+ function triggerCallback(...messages:any[]):number {return subcallback.apply({}, messages)},
|
|
45
|
+ ]
|
|
46
|
+ }])
|
|
47
|
+}
|
|
48
|
+
|
|
49
|
+let server: RPCServer<{ topic: string}>
|
|
50
|
+
|
|
51
|
+describe('RPCServer', () => {
|
|
52
|
+ before(async(done) => {
|
|
53
|
+ server = makeServer()
|
|
54
|
+ done()
|
|
55
|
+ })
|
|
56
|
+
|
|
57
|
+ after(async() => {
|
|
58
|
+ await server.destroy()
|
|
59
|
+ })
|
|
60
|
+
|
|
61
|
+ it('new RPCServer() should fail on bad RPC', (done) => {
|
|
62
|
+ try{
|
|
63
|
+ new RPCServer(20001, [{
|
|
64
|
+ name: "bad",
|
|
65
|
+ exportRPCs: () => [
|
|
66
|
+ (aaa,bbb,ccc) => { return aaa+bbb+ccc }
|
|
67
|
+ ]
|
|
68
|
+ }])
|
|
69
|
+ done(new Error("Didn't fail with bad RPC"))
|
|
70
|
+ }catch(badRPCError){
|
|
71
|
+ done()
|
|
72
|
+ }
|
|
73
|
+ })
|
|
74
|
+})
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+describe('RPCSocket', () => {
|
|
78
|
+ let client: RPCSocket
|
|
79
|
+ let server: RPCServer<{topic: string}>
|
|
80
|
+
|
|
81
|
+ before(async() => {
|
|
82
|
+ server = makeServer()
|
|
83
|
+ client = new RPCSocket(20000, "localhost")
|
|
84
|
+ return await client.connect()
|
|
85
|
+ })
|
|
86
|
+
|
|
87
|
+ after(async() => {
|
|
88
|
+ client.destroy()
|
|
89
|
+ server.destroy()
|
|
90
|
+ })
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+ it('should have rpc echo', (done) => {
|
|
94
|
+ client['test'].echo("x").then(x => {
|
|
95
|
+ if(x === 'x')
|
|
96
|
+ done()
|
|
97
|
+ else
|
|
98
|
+ done(new Error('echo RPC response did not match'))
|
|
99
|
+ })
|
|
100
|
+ })
|
|
101
|
+
|
|
102
|
+ it('should subscribe with success', (done) => {
|
|
103
|
+ client['test'].simpleSubscribe(console.log).then(res => {
|
|
104
|
+ if(res.result === 'Success'){
|
|
105
|
+ done()
|
|
106
|
+ }else{
|
|
107
|
+ console.error(res)
|
|
108
|
+ done(new Error('Subscribe did not return success'))
|
|
109
|
+ }
|
|
110
|
+ })
|
|
111
|
+ })
|
|
112
|
+
|
|
113
|
+ it('subscribe should call back', (done) => {
|
|
114
|
+ client['test'].subscribe((...args: any) => {
|
|
115
|
+ if(args[0] === "test" && args[1] === "callback")
|
|
116
|
+ done()
|
|
117
|
+ else
|
|
118
|
+ done(new Error("Bad callback value "+ args))
|
|
119
|
+ }).then( async () => {
|
|
120
|
+ await client['test'].triggerCallback("test", "callback")
|
|
121
|
+ })
|
|
122
|
+ })
|
|
123
|
+
|
|
124
|
+ it('simpleSubscribe should call back', (done) => {
|
|
125
|
+ client['test'].simpleSubscribe((...args: any) => {
|
|
126
|
+ if(args[0] === "test_" && args[1] === "callback_")
|
|
127
|
+ done()
|
|
128
|
+ else
|
|
129
|
+ done(new Error("Bad callback value "+ args))
|
|
130
|
+ }).then( async () => {
|
|
131
|
+ await client['test'].triggerCallback("test_", "callback_")
|
|
132
|
+ })
|
|
133
|
+ })
|
|
134
|
+
|
|
135
|
+})
|