Bläddra i källkod

better test

master
peter 5 år sedan
förälder
incheckning
26aa50deec
5 ändrade filer med 27 tillägg och 40 borttagningar
  1. 0
    13
      Index.ts
  2. 0
    7
      index.ts
  3. 10
    10
      src/Backend.ts
  4. 2
    2
      src/Types.ts
  5. 15
    8
      test/Test.ts

+ 0
- 13
Index.ts Visa fil

@@ -1,13 +0,0 @@
1
-import * as Back from './src/Backend';
2
-import * as Front from './src/Frontend';
3
-import * as Types from './src/Types'; 
4
-import * as Utils from './src/Utils'; 
5
-import * as Interfaces from './src/Interfaces';
6
-
7
-export {
8
-    Back as Backend,
9
-    Front as Frontend,
10
-    Types, 
11
-    Utils, 
12
-    Interfaces,
13
-}

+ 0
- 7
index.ts Visa fil

@@ -1,7 +0,0 @@
1
-import * as Back from './src/backend/RPCSocketServer';
2
-import * as Front from './src/frontend/RPCSocket';
3
-
4
-export {
5
-    Back as Backend,
6
-    Front as Frontend
7
-}

+ 10
- 10
src/Backend.ts Visa fil

@@ -27,20 +27,20 @@ export class RPCServer<
27 27
         
28 28
         if(!conf.visibility) this.visibility = "127.0.0.1"        
29 29
         
30
-        if(!conf.errorHandler) this.errorHandler = 
31
-        (socket:I.Socket) => (error:any) => { 
30
+        this.errorHandler = (socket:I.Socket) => (error:any) => { 
31
+            if(conf.errorHandler) conf.errorHandler(socket, error)
32 32
             socket.destroy(); 
33 33
             console.error("Caught websocket error", String(error)) 
34 34
         }
35 35
         
36
-        if(!conf.closeHandler) this.closeHandler = 
37
-        (socket:I.Socket) => () => { 
38
-            console.log("Socket on port "+socket.port+" closing") 
36
+        this.closeHandler = (socket:I.Socket) => { 
37
+            if(!conf.closeHandler) console.log("Socket on port "+socket.port+" closing") 
38
+            else conf.closeHandler(socket)
39 39
         }
40 40
         
41
-        if(!conf.connectionHandler) this.connectionHandler = 
42
-        (socket:I.Socket) => { 
43
-            console.log("New websocket connection on port "+socket.port)
41
+        this.connectionHandler = (socket:I.Socket) => { 
42
+            if(!conf.connectionHandler) console.log("New websocket connection on port "+socket.port)
43
+            else conf.connectionHandler(socket)
44 44
         }
45 45
 
46 46
         let badRPC 
@@ -60,8 +60,8 @@ export class RPCServer<
60 60
         try{
61 61
             this.io.attach(this.ws)
62 62
             this.io.on('socket', (socket:I.Socket) => {
63
-                socket.on('error', this.errorHandler(socket))
64
-                socket.on('close', this.closeHandler(socket))
63
+                socket.on('error', (err) => this.errorHandler(socket, err))
64
+                socket.on('close', ()=> this.closeHandler(socket))
65 65
                 this.connectionHandler(socket)
66 66
                 this.initRPCs(socket)
67 67
             })

+ 2
- 2
src/Types.ts Visa fil

@@ -6,8 +6,8 @@ export type Arg = string
6 6
 export type Name = Arg
7 7
 export type Owner = Name
8 8
 export type ConnectionHandler = (socket:I.Socket) => void
9
-export type ErrorHandler = (socket:I.Socket) => (error:any) => void
10
-export type CloseHandler = (socket:I.Socket) => () =>  void
9
+export type ErrorHandler = (socket:I.Socket, error:any) => void
10
+export type CloseHandler = (socket:I.Socket) =>  void
11 11
 export type SocketConf = {
12 12
     connectionHandler?: ConnectionHandler
13 13
     errorHandler?: ErrorHandler

+ 15
- 8
test/Test.ts Visa fil

@@ -21,7 +21,7 @@ function makeServer(){
21 21
                     return {
22 22
                         result: "Success",
23 23
                         uuid: uuidv4(),
24
-                        topic: ""
24
+                        topic: "test"
25 25
                     }
26 26
                 }
27 27
             },{
@@ -31,25 +31,32 @@ function makeServer(){
31 31
                     return {
32 32
                         result: "Success",
33 33
                         uuid: uuidv4(),
34
-                        topic: ""
34
+                        topic: "test"
35 35
                     }
36 36
                 },
37 37
                 onClose: (res, rpc) => { 
38
-                    console.log("Specific close handler for", rpc.name, res)
38
+                    console.log("onClose", rpc.name === 'subscribe' && res?"OK":"")
39 39
                     subcallback = null 
40 40
                 },
41
-                onCallback: (...args:any) => { console.log.apply(console, args) }
41
+                onCallback: (...args:any) => { 
42
+                    console.log("onCallback", args[0] === "test" && args[1] === "callback"?"OK":"")                    
43
+                }
42 44
             },
43 45
             function add(...args:number[]):number {return args.reduce((a,b)=>a+b, 0)},
44 46
             function triggerCallback(...messages:any[]):number {return subcallback.apply({}, messages)},
45 47
         ]
46
-    }])
48
+    }],{
49
+        connectionHandler: (socket) => { console.log("connectionHandler OK") },
50
+        closeHandler: (socket) => { console.log("closeHandler OK") },
51
+        errorHandler: (socket, err) => { console.error("errorHandler OK SO YOU SHOULDN'T SEE THIS"); throw err }
52
+    })
47 53
 }
48 54
 
49
-let server: RPCServer<{ topic: string}>
50 55
 
51 56
 describe('RPCServer', () => {
52
-    before(async(done) => {
57
+    let server: RPCServer<{ topic: string}>
58
+
59
+    before((done) => {
53 60
         server = makeServer()
54 61
         done()
55 62
     })
@@ -84,7 +91,7 @@ describe('RPCSocket', () => {
84 91
         return await client.connect()
85 92
     })
86 93
 
87
-    after(async() => {
94
+    after(() => {
88 95
         client.destroy()
89 96
         server.destroy()
90 97
     })

Laddar…
Avbryt
Spara