Browse Source

test

master
peter 5 years ago
parent
commit
597be1b80f
7 changed files with 878 additions and 130 deletions
  1. 853
    1
      package-lock.json
  2. 9
    3
      package.json
  3. 8
    2
      src/Backend.ts
  4. 1
    0
      src/Frontend.ts
  5. 7
    4
      src/Interfaces.ts
  6. 0
    84
      test/TestBackend.ts
  7. 0
    36
      test/TestFrontend.ts

+ 853
- 1
package-lock.json
File diff suppressed because it is too large
View File


+ 9
- 3
package.json View File

@@ -9,18 +9,24 @@
9 9
     "backend": "node js/test/TestBackend.js",
10 10
     "frontend": "node js/test/TestFrontend.js",
11 11
     "build": "npm run clean && tsc && npm run webpack",
12
-    "clean": "rm -rf js"
12
+    "clean": "rm -rf js",
13
+    "test": "mocha js/test/Test.js"
13 14
   },
14 15
   "author": "",
15 16
   "license": "ISC",
16 17
   "devDependencies": {
18
+    "@types/expect": "^1.20.4",
17 19
     "@types/express": "^4.17.0",
20
+    "@types/mocha": "^5.2.7",
18 21
     "@types/node": "^11.13.19",
22
+    "chai": "^4.2.0",
23
+    "mocha": "^6.2.0",
24
+    "terser-webpack-plugin": "^2.1.0",
19 25
     "ts-loader": "^5.3.3",
26
+    "ts-mocha": "^6.0.0",
20 27
     "typescript": "^3.5.3",
21 28
     "webpack": "^4.40.2",
22
-    "webpack-cli": "^3.3.9",
23
-    "terser-webpack-plugin": "^2.1.0"
29
+    "webpack-cli": "^3.3.9"
24 30
   },
25 31
   "dependencies": {
26 32
     "bsock": "^0.1.9",

+ 8
- 2
src/Backend.ts View File

@@ -9,7 +9,8 @@ import * as I from './Interfaces';
9 9
 
10 10
 export class RPCServer<
11 11
     SubResType = {}
12
->{
12
+> implements I.Destroyable{
13
+        
13 14
     private ws = http.createServer()
14 15
     private io = bsock.createServer()
15 16
     private visibility:T.Visibility
@@ -34,7 +35,7 @@ export class RPCServer<
34 35
         
35 36
         if(!conf.closeHandler) this.closeHandler = 
36 37
         (socket:I.Socket) => () => { 
37
-            console.log("Socket on port "+socket.port+"closing") 
38
+            console.log("Socket on port "+socket.port+" closing") 
38 39
         }
39 40
         
40 41
         if(!conf.connectionHandler) this.connectionHandler = 
@@ -78,4 +79,9 @@ export class RPCServer<
78 79
             ...this.exporters.flatMap(exporter => U.rpcHooker(socket, exporter))
79 80
         ]
80 81
     }
82
+
83
+    async destroy(): Promise<void> {
84
+        this.io.close()
85
+        this.ws.close()
86
+    }
81 87
 }

+ 1
- 0
src/Frontend.ts View File

@@ -47,6 +47,7 @@ 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', () => {})
50 51
 
51 52
         const info:T.ExtendedRpcInfo[] = await this.info()
52 53
         info.forEach(i => {

+ 7
- 4
src/Interfaces.ts View File

@@ -6,13 +6,16 @@ export interface Exporter<T = {}>{
6 6
     exportRPCs() : T.RPC<T>[]
7 7
 }
8 8
 
9
-export interface Socket {
9
+export interface Socket extends Destroyable {
10 10
     port: number
11 11
     hook: (rpcname: T.Name, ...args: T.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>
15
-    on: T.OnFunction
16
-    destroy: ()=>void
17
-    close: ()=>void
15
+    on: T.OnFunction    
16
+    close() : void
17
+}
18
+
19
+export interface Destroyable{
20
+    destroy() : void
18 21
 }

+ 0
- 84
test/TestBackend.ts View File

@@ -1,84 +0,0 @@
1
-import { RPCServer } from '../src/Backend'
2
-import { SubscriptionResponse, ErrorResponse, SuccessResponse } from '../src/Types'
3
-import { HookRPC } from '../src/Types'
4
-import * as uuidv4 from "uuid/v4"
5
-import { makeSubResponse } from '../src/Utils'
6
-
7
-let subcallback
8
-
9
-new RPCServer(20000, [{
10
-    name: "HelloWorldRPCGroup",
11
-    exportRPCs: () => [
12
-        {
13
-            name: 'echo',
14
-            call: async (s:string) => s,
15
-        },{
16
-            name: 'simpleSubscribe',
17
-            hook: async(callback) => {
18
-                subcallback =  callback
19
-                return makeSubResponse()
20
-            }
21
-        },{
22
-            name: 'subscribe',
23
-            hook: async (callback):Promise<any> => {
24
-                subcallback = callback
25
-                return makeSubResponse()
26
-            },
27
-            onClose: (res, rpc) => { 
28
-                console.log("Specific close handler for", rpc.name, res)
29
-                subcallback = null 
30
-            },
31
-            onCallback: (...args) => { console.log.apply(console, args) }
32
-        },
33
-        function add(...args:number[]):number {return args.reduce((a,b)=>a+b, 0)},
34
-        function triggerCallback(...messages:any[]):number {return subcallback.apply({}, messages)},
35
-    ]
36
-}])
37
-
38
-new RPCServer<{ topic: string }>(20001, [{
39
-    name: "HelloWorldRPCGroup",
40
-    exportRPCs: () => [
41
-        {
42
-            name: 'echo',
43
-            call: async (s:string) => s,
44
-        },{
45
-            name: 'simpleSubscribe',
46
-            hook: async(callback) => {
47
-                subcallback =  callback
48
-                return {
49
-                    result: "Success",
50
-                    uuid: uuidv4(),
51
-                    topic: ""
52
-                }
53
-            }
54
-        },{
55
-            name: 'subscribe',
56
-            hook: async (callback) => {
57
-                subcallback = callback
58
-                return {
59
-                    result: "Success",
60
-                    uuid: uuidv4(),
61
-                    topic: ""
62
-                }
63
-            },
64
-            onClose: (res, rpc) => { 
65
-                console.log("Specific close handler for", rpc.name, res)
66
-                subcallback = null 
67
-            },
68
-            onCallback: (...args:any) => { console.log.apply(console, args) }
69
-        },
70
-        function add(...args:number[]):number {return args.reduce((a,b)=>a+b, 0)},
71
-        function triggerCallback(...messages:any[]):number {return subcallback.apply({}, messages)},
72
-    ]
73
-}])
74
-
75
-try{
76
-    new RPCServer(20001, [{
77
-        name: "bad",
78
-        exportRPCs: ()  => [
79
-            (aaa,bbb,ccc) => { return aaa+bbb+ccc }
80
-        ]
81
-    }])
82
-}catch(badRPCError){
83
-    console.log("expected bad-RPC error happened: "+ !!badRPCError, String(badRPCError))
84
-}

+ 0
- 36
test/TestFrontend.ts View File

@@ -1,36 +0,0 @@
1
-import { RPCSocket } from '../src/Frontend'
2
-const client = new RPCSocket(20000, 'localhost')
3
-client.connect().then(async _ => {
4
-    await client.info().then(console.log)
5
-    
6
-    await client["HelloWorldRPCGroup"].echo("x")
7
-    .then(hopefullyX => console.log("echo('x') returned x: ", hopefullyX === "x", hopefullyX))
8
-
9
-    await client["HelloWorldRPCGroup"].add(1,2,3)
10
-    .then(hopefully6 => console.log("add(1,2,3) returned 6: ", hopefully6 === 6, hopefully6))
11
-
12
-    let counter = 0
13
-    const handler = (s) => {
14
-        counter++
15
-        if(counter === 3)
16
-        console.log("subscribe call counter met", counter === 3)
17
-    }
18
-
19
-    await client["HelloWorldRPCGroup"].subscribe(handler)
20
-    await Promise.all([
21
-        client["HelloWorldRPCGroup"].triggerCallback("test1", "test1", "test1", ),
22
-        client["HelloWorldRPCGroup"].triggerCallback("test2", "test2", "test2", ),
23
-        client["HelloWorldRPCGroup"].triggerCallback("test3", "test3", "test3", ),
24
-    ])
25
-
26
-    counter = 0
27
-    const simplehandler = (s) => {
28
-        counter++
29
-        if(counter === 3)
30
-            console.log("simpleSubscribe call counter met", counter === 3)
31
-    }
32
-    await client["HelloWorldRPCGroup"].simpleSubscribe(simplehandler)
33
-    client["HelloWorldRPCGroup"].triggerCallback("simple1", "simple1", "simple1", )
34
-    client["HelloWorldRPCGroup"].triggerCallback("simple2", "simple2", "simple2", )
35
-    client["HelloWorldRPCGroup"].triggerCallback("simple3", "simple3", "simple3", )
36
-})

Loading…
Cancel
Save