@@ -4,6 +4,7 @@ const WebSocket = require('ws');
44const expectedCloseReasons = {
55 invalidFrame : 'Received invalid WebSocket frame' ,
66 tcpFinBeforeClose : 'Received TCP FIN before WebSocket close frame' ,
7+ timeout : 'WebSocket timed out from inactivity' ,
78} ;
89
910let closeTestsPassed = true ;
@@ -12,6 +13,8 @@ let closeTestsPassed = true;
1213const port = 9001 ;
1314
1415const server = uWS . App ( ) . ws ( '/*' , {
16+ idleTimeout : 8 , // Set idle timeout to 8 seconds
17+ sendPingsAutomatically : false , // Needed for timeout
1518 open : ( ws ) => {
1619 console . log ( 'A WebSocket connected!' ) ;
1720 } ,
@@ -27,6 +30,8 @@ const server = uWS.App().ws('/*', {
2730 console . log ( 'Test passed: Invalid WebSocket frame' ) ;
2831 } else if ( reason === expectedCloseReasons . tcpFinBeforeClose ) {
2932 console . log ( 'Test passed: TCP FIN before WebSocket close frame' ) ;
33+ } else if ( reason === expectedCloseReasons . timeout ) {
34+ console . log ( 'Test passed: WebSocket timed out from inactivity' ) ;
3035 } else {
3136 console . error ( 'Test failed: Unexpected close reason:' , reason ) ;
3237 closeTestsPassed = false ;
@@ -40,15 +45,17 @@ const server = uWS.App().ws('/*', {
4045 if ( token ) {
4146 console . log ( 'Listening to port' , port ) ;
4247
43- const triggerClosure = ( malformedData , useEnd ) => {
48+ const triggerClosure = ( malformedData , useEnd , useTimeout ) => {
4449 const client = new WebSocket ( `ws://localhost:${ port } ` ) ;
4550
4651 client . on ( 'open' , ( ) => {
4752 console . log ( 'Client connected to server' ) ;
48-
4953 if ( useEnd ) {
5054 // Trigger "Received TCP FIN before WebSocket close frame"
5155 client . _socket . end ( ) ;
56+ } else if ( useTimeout ) {
57+ // Trigger timeout by keeping connection idle
58+ console . log ( 'Starting timeout test: Keeping connection open for timeout' ) ;
5259 } else {
5360 // Trigger "Received invalid WebSocket frame"
5461 client . _socket . write ( malformedData ) ;
@@ -66,15 +73,21 @@ const server = uWS.App().ws('/*', {
6673
6774 // Test 1: Trigger invalid WebSocket frame
6875 const malformedData = Buffer . from ( [ 0xFF , 0x80 , 0x00 , 0x00 , 0x00 , 0x01 ] ) ;
69- triggerClosure ( malformedData , false ) ;
76+ triggerClosure ( malformedData , false , false ) ;
7077
71- // Wait for a short period before triggering the next test to ensure the server handles sequentially
78+ // Wait for Test 1 to complete before triggering the next test
7279 setTimeout ( ( ) => {
7380 // Test 2: Trigger TCP FIN before WebSocket close frame
74- triggerClosure ( null , true ) ;
75- } , 1000 ) ; // Adjust the delay as necessary
81+ triggerClosure ( null , true , false ) ;
82+
83+ // Wait for Test 2 to complete before starting Test 3
84+ setTimeout ( ( ) => {
85+ // Test 3: Trigger timeout (idleTimeout = 8 should close after ~32s)
86+ triggerClosure ( null , false , true ) ;
87+ } , 1000 ) ;
88+ } , 1000 ) ;
7689
77- // Allow some time for tests to run before exiting
90+ // Allow time for all tests to complete before exiting
7891 setTimeout ( ( ) => {
7992 if ( closeTestsPassed ) {
8093 console . log ( 'All tests passed.' ) ;
@@ -83,7 +96,7 @@ const server = uWS.App().ws('/*', {
8396 console . error ( 'Some tests failed.' ) ;
8497 process . exit ( 1 ) ;
8598 }
86- } , 3000 ) ; // Adjust the delay as necessary
99+ } , 16000 ) ; // Increased to 40s to account for timeout test (~32s + buffer)
87100 } else {
88101 console . log ( 'Failed to listen to port' , port ) ;
89102 process . exit ( 1 ) ;
0 commit comments