@@ -7,87 +7,111 @@ const WebsocketServer = require('../../../lib/servers/WebsocketServer');
7
7
const port = require ( '../../ports-map' ) . WebsocketServer ;
8
8
9
9
describe ( 'WebsocketServer' , ( ) => {
10
+ let server ;
10
11
let socketServer ;
11
12
let listeningApp ;
12
13
13
- beforeAll ( ( done ) => {
14
+ beforeEach ( ( done ) => {
14
15
// eslint-disable-next-line new-cap
15
16
const app = new express ( ) ;
16
17
17
18
listeningApp = http . createServer ( app ) ;
18
19
listeningApp . listen ( port , 'localhost' , ( ) => {
19
- const server = {
20
+ server = {
20
21
log : {
21
22
error : ( ) => { } ,
22
23
debug : ( ) => { } ,
23
24
} ,
24
25
sockPath : '/ws-server' ,
25
26
listeningApp,
27
+ heartbeatInterval : 800 ,
26
28
} ;
27
-
28
29
socketServer = new WebsocketServer ( server ) ;
29
-
30
30
done ( ) ;
31
31
} ) ;
32
32
} ) ;
33
33
34
- describe ( 'server' , ( ) => {
35
- it ( 'should recieve connection, send message, and close client' , ( done ) => {
36
- const data = [ ] ;
37
-
38
- let headers ;
39
- socketServer . onConnection ( ( connection , h ) => {
40
- headers = h ;
41
- data . push ( 'open' ) ;
42
- socketServer . send ( connection , 'hello world' ) ;
43
- setTimeout ( ( ) => {
44
- // the server closes the connection with the client
45
- socketServer . close ( connection ) ;
46
- } , 1000 ) ;
47
- } ) ;
34
+ it ( 'should recieve connection, send message, and close client' , ( done ) => {
35
+ const data = [ ] ;
48
36
49
- // eslint-disable-next-line new-cap
50
- const client = new ws ( `http://localhost:${ port } /ws-server` ) ;
37
+ let headers ;
38
+ socketServer . onConnection ( ( connection , h ) => {
39
+ headers = h ;
40
+ data . push ( 'open' ) ;
41
+ socketServer . send ( connection , 'hello world' ) ;
42
+ setTimeout ( ( ) => {
43
+ // the server closes the connection with the client
44
+ socketServer . close ( connection ) ;
45
+ } , 1000 ) ;
46
+ } ) ;
51
47
52
- client . onmessage = ( e ) => {
53
- data . push ( e . data ) ;
54
- } ;
48
+ // eslint-disable-next-line new-cap
49
+ const client = new ws ( `http://localhost:${ port } /ws-server` ) ;
55
50
56
- client . onclose = ( ) => {
57
- data . push ( 'close' ) ;
58
- } ;
51
+ client . onmessage = ( e ) => {
52
+ data . push ( e . data ) ;
53
+ } ;
59
54
60
- setTimeout ( ( ) => {
61
- expect ( headers . host ) . toMatchSnapshot ( ) ;
62
- expect ( data ) . toMatchSnapshot ( ) ;
63
- done ( ) ;
64
- } , 3000 ) ;
55
+ client . onclose = ( ) => {
56
+ data . push ( 'close' ) ;
57
+ } ;
58
+
59
+ // the heartbeat interval was shortened greatly above
60
+ // so that the client is quickly pinged
61
+ client . on ( 'ping' , ( ) => {
62
+ data . push ( 'ping' ) ;
65
63
} ) ;
66
64
67
- it ( 'should receive client close event' , ( done ) => {
68
- let receivedClientClose = false ;
69
- socketServer . onConnection ( ( connection ) => {
70
- socketServer . onConnectionClose ( connection , ( ) => {
71
- receivedClientClose = true ;
72
- } ) ;
65
+ setTimeout ( ( ) => {
66
+ expect ( headers . host ) . toMatchSnapshot ( ) ;
67
+ expect ( data ) . toMatchSnapshot ( ) ;
68
+ done ( ) ;
69
+ } , 3000 ) ;
70
+ } ) ;
71
+
72
+ it ( 'should receive client close event' , ( done ) => {
73
+ let receivedClientClose = false ;
74
+ socketServer . onConnection ( ( connection ) => {
75
+ socketServer . onConnectionClose ( connection , ( ) => {
76
+ receivedClientClose = true ;
73
77
} ) ;
78
+ } ) ;
74
79
75
- // eslint-disable-next-line new-cap
76
- const client = new ws ( `http://localhost:${ port } /ws-server` ) ;
80
+ // eslint-disable-next-line new-cap
81
+ const client = new ws ( `http://localhost:${ port } /ws-server` ) ;
77
82
78
- setTimeout ( ( ) => {
79
- // the client closes itself, the server does not close it
80
- client . close ( ) ;
81
- } , 1000 ) ;
83
+ setTimeout ( ( ) => {
84
+ // the client closes itself, the server does not close it
85
+ client . close ( ) ;
86
+ } , 1000 ) ;
82
87
83
- setTimeout ( ( ) => {
84
- expect ( receivedClientClose ) . toBeTruthy ( ) ;
85
- done ( ) ;
86
- } , 3000 ) ;
88
+ setTimeout ( ( ) => {
89
+ expect ( receivedClientClose ) . toBeTruthy ( ) ;
90
+ done ( ) ;
91
+ } , 3000 ) ;
92
+ } ) ;
93
+
94
+ it ( 'should terminate a client that is not alive' , ( done ) => {
95
+ let receivedClientClose = false ;
96
+ socketServer . onConnection ( ( connection ) => {
97
+ // this makes the server think the client did not respond
98
+ // to a ping in time, so the server will terminate it
99
+ connection . isAlive = false ;
100
+ socketServer . onConnectionClose ( connection , ( ) => {
101
+ receivedClientClose = true ;
102
+ } ) ;
87
103
} ) ;
104
+
105
+ // eslint-disable-next-line new-cap, no-unused-vars
106
+ const client = new ws ( `http://localhost:${ port } /ws-server` ) ;
107
+
108
+ setTimeout ( ( ) => {
109
+ expect ( receivedClientClose ) . toBeTruthy ( ) ;
110
+ done ( ) ;
111
+ } , 3000 ) ;
88
112
} ) ;
89
113
90
- afterAll ( ( done ) => {
114
+ afterEach ( ( done ) => {
91
115
listeningApp . close ( done ) ;
92
116
} ) ;
93
117
} ) ;
0 commit comments