@@ -14,14 +14,16 @@ server.listen(
1414 common . mustCall ( ( ) => {
1515 const port = server . address ( ) . port ;
1616 const client = new net . Socket ( ) ;
17+
18+ // Set TOS before connection to test caching behavior
1719 client . setTOS ( 0x10 ) ;
1820 client . connect ( port ) ;
1921
2022 client . on (
2123 'connect' ,
2224 common . mustCall ( ( ) => {
2325 // TEST 1: setTOS validation
24- // Should throw if value is not a number or out of range
26+ // Should throw if value is not a number, is NaN, or is out of range (0-255)
2527 assert . throws ( ( ) => client . setTOS ( 'invalid' ) , {
2628 code : 'ERR_INVALID_ARG_TYPE' ,
2729 } ) ;
@@ -35,8 +37,10 @@ server.listen(
3537 code : 'ERR_OUT_OF_RANGE' ,
3638 } ) ;
3739
38- // TEST 2a: verify pre-connect TOS was cached and applied on connect
39- // by checking client.getTOS() in the 'connect' event handler
40+ // TEST 2a: Verify deferred application
41+ // Check if the TOS value set before connect() was cached and applied.
42+ // We mask with 0xFC to check only the high 6 bits (DSCP),
43+ // ignoring the lowest 2 bits (ECN) which the OS may modify or zero out.
4044 const mask = 0xFC ;
4145 const preConnectGot = client . getTOS ( ) ;
4246 assert . strictEqual (
@@ -45,24 +49,24 @@ server.listen(
4549 `Pre-connect TOS should be ${ 0x10 & mask } , got ${ preConnectGot & mask } ` ,
4650 ) ;
4751
48- // TEST 2: setting and getting TOS
52+ // TEST 2b: Setting and getting TOS on an active connection
4953 const tosValue = 0x10 ; // IPTOS_LOWDELAY (16)
5054
5155 // On all platforms, this should succeed (tries both IPv4 and IPv6)
5256 client . setTOS ( tosValue ) ;
5357
5458 // Verify values
55- // Note: Some OSs might mask the value (e.g. Linux sometimes masks ECN bits),
56- // but usually 0x10 should return 0x10.
5759 const got = client . getTOS ( ) ;
58- // Only compare the upper 6 bits (DSCP, bits 7-2) to avoid ECN/OS-masked bits
60+
61+ // Compare only the DSCP bits (7-2) using the mask defined above
5962 assert . strictEqual (
6063 got & mask ,
6164 tosValue & mask ,
6265 `Expected TOS ${ tosValue & mask } , got ${ got & mask } ` ,
6366 ) ;
6467
65- // Test boundary values
68+ // TEST 3: Boundary values
69+ // Check min (0x00), max (0xFF), and arbitrary intermediate values
6670 for ( const boundaryValue of [ 0x00 , 0xFF , 0x3F ] ) {
6771 client . setTOS ( boundaryValue ) ;
6872 const gotBoundary = client . getTOS ( ) ;
0 commit comments