@@ -17,25 +17,17 @@ let closedClientConnections = 0;
1717
1818let listenSocket ;
1919
20+ /* 0 to 1-less than max */
2021function getRandomInt ( max ) {
2122 return Math . floor ( Math . random ( ) * Math . floor ( max ) ) ;
2223}
2324
24- /* Perform random websockets/ws action */
25- function performRandomClientAction ( ws ) {
26- uWS . print ( 'Random client action' ) ;
27- ws . send ( 'test message from client' ) ;
28- uWS . print ( 'Random client action, done' ) ;
29- }
30-
31- /* Perform random uWebSockets.js action */
32-
3325function establishNewConnection ( ) {
3426 const ws = new WebSocket ( 'ws://localhost:' + port ) ;
3527
3628 ws . on ( 'open' , ( ) => {
3729 /* Open more connections */
38- if ( ++ openedClientConnections < 1 ) {
30+ if ( ++ openedClientConnections < 1000 ) {
3931 establishNewConnection ( ) ;
4032 } else {
4133 /* Stop listening */
@@ -46,8 +38,8 @@ function establishNewConnection() {
4638 performRandomClientAction ( ws ) ;
4739 } ) ;
4840
41+ /* It seems you can get messages after close?! */
4942 ws . on ( 'message' , ( data ) => {
50- console . log ( 'client got message: ' + data ) ;
5143 performRandomClientAction ( ws ) ;
5244 } ) ;
5345
@@ -57,20 +49,51 @@ function establishNewConnection() {
5749 } ) ;
5850}
5951
52+ /* Perform random websockets/ws action */
53+ function performRandomClientAction ( ws ) {
54+ /* 0, 1 but never 2 */
55+ switch ( getRandomInt ( 2 ) ) {
56+ case 0 : {
57+ ws . close ( ) ;
58+ break ;
59+ }
60+ case 1 : {
61+ /* Length should be random from small to huge */
62+ try {
63+ ws . send ( 'a test message' ) ;
64+ } catch ( e ) {
65+ /* Apparently websockets/ws can throw at any moment */
6066
67+ }
68+ break ;
69+ }
70+ case 2 : {
71+ /* This should correspond to hard us_socket_close */
72+ ws . terminate ( ) ;
73+ break ;
74+ }
75+ }
76+ }
77+
78+ /* Perform random uWebSockets.js action */
6179function performRandomServerAction ( ws ) {
62- //uWS.print('Random server action');
63- switch ( getRandomInt ( 1 ) ) {
80+ /* 0, 1 but never 2 */
81+ switch ( getRandomInt ( 2 ) ) {
6482 case 0 : {
6583 ws . close ( ) ;
6684 break ;
6785 }
6886 case 1 : {
87+ /* Length should be random from small to huge */
6988 ws . send ( 'a test message' , false ) ;
7089 break ;
7190 }
91+ case 2 : {
92+ /* This should correspond to hard us_socket_close */
93+ ws . terminate ( ) ;
94+ break ;
95+ }
7296 }
73- //uWS.print('Done with random server action');
7497}
7598
7699const app = uWS . /*SSL*/ App ( {
@@ -83,21 +106,26 @@ const app = uWS./*SSL*/App({
83106 idleTimeout : 10 ,
84107
85108 open : ( ws , req ) => {
86- uWS . print ( 'Server open event' ) ;
109+ /* Doing a terminate here will be interesting */
87110 performRandomServerAction ( ws ) ;
88- uWS . print ( 'Server open event, returning' ) ;
89111 } ,
90112 message : ( ws , message , isBinary ) => {
91- console . log ( 'server got message: ' + message . byteLength ) ;
92113 performRandomServerAction ( ws ) ;
93114 } ,
94115 drain : ( ws ) => {
95116 // todo: dont send over a certain backpressure
96117 //performRandomServerAction(ws);
97118 } ,
98119 close : ( ws , code , message ) => {
99- console . log ( 'server was closed' ) ;
100- //performRandomServerAction(ws);
120+ try {
121+ performRandomServerAction ( ws ) ;
122+ } catch ( e ) {
123+ /* We expect to land here always, since socket is invalid */
124+ return ;
125+ }
126+ /* We should never land here */
127+ uWS . print ( 'ERROR: Did not throw in close!' ) ;
128+ process . exit ( - 1 ) ;
101129 }
102130} ) . any ( '/*' , ( res , req ) => {
103131 res . end ( 'Nothing to see here!' ) ;
0 commit comments