11package datadog .common .socket ;
22
3+ import static org .junit .jupiter .api .Assertions .*;
34import static org .junit .jupiter .api .Assertions .assertTimeoutPreemptively ;
4- import static org .junit .jupiter .api .Assertions .fail ;
55
66import datadog .trace .api .Config ;
77import java .io .IOException ;
8+ import java .io .InputStream ;
89import java .net .InetSocketAddress ;
10+ import java .net .SocketException ;
911import java .net .StandardProtocolFamily ;
1012import java .net .UnixDomainSocketAddress ;
1113import java .nio .channels .ServerSocketChannel ;
@@ -28,32 +30,92 @@ public void testTimeout() throws Exception {
2830 return ;
2931 }
3032
31- int testTimeout = 3000 ;
3233 Path socketPath = getSocketPath ();
3334 UnixDomainSocketAddress socketAddress = UnixDomainSocketAddress .of (socketPath );
3435 startServer (socketAddress );
3536 TunnelingJdkSocket clientSocket = createClient (socketPath );
37+ InputStream inputStream = clientSocket .getInputStream ();
3638
37- // Test that the socket unblocks when timeout is set to >0
38- clientSocket .setSoTimeout (1000 );
39- assertTimeoutPreemptively (
40- Duration .ofMillis (testTimeout ), () -> clientSocket .getInputStream ().read ());
39+ int testTimeout = 1000 ;
40+ clientSocket .setSoTimeout (testTimeout );
41+ assertEquals (testTimeout , clientSocket .getSoTimeout ());
4142
42- // Test that the socket blocks indefinitely when timeout is set to 0, per
43+ long startTime = System .currentTimeMillis ();
44+ int readResult = inputStream .read ();
45+ long endTime = System .currentTimeMillis ();
46+ long readDuration = endTime - startTime ;
47+ int timeVariance = 100 ;
48+ assertTrue (readDuration >= testTimeout && readDuration <= testTimeout + timeVariance );
49+ assertEquals (0 , readResult );
50+
51+ int newTimeout = testTimeout / 2 ;
52+ clientSocket .setSoTimeout (newTimeout );
53+ assertEquals (newTimeout , clientSocket .getSoTimeout ());
54+ assertTimeoutPreemptively (Duration .ofMillis (testTimeout ), () -> inputStream .read ());
55+
56+ // The socket should block indefinitely when timeout is set to 0, per
4357 // https://docs.oracle.com/en/java/javase/16/docs/api//java.base/java/net/Socket.html#setSoTimeout(int).
44- clientSocket .setSoTimeout (0 );
45- boolean infiniteTimeOut = false ;
58+ int infiniteTimeout = 0 ;
59+ clientSocket .setSoTimeout (infiniteTimeout );
60+ assertEquals (infiniteTimeout , clientSocket .getSoTimeout ());
4661 try {
47- assertTimeoutPreemptively (
48- Duration . ofMillis ( testTimeout ), () -> clientSocket . getInputStream (). read () );
62+ assertTimeoutPreemptively (Duration . ofMillis ( testTimeout ), () -> inputStream . read ());
63+ fail ( "Read should block indefinitely with infinite timeout" );
4964 } catch (AssertionError e ) {
50- infiniteTimeOut = true ;
65+ // Expected
5166 }
52- if (!infiniteTimeOut ) {
53- fail ("Test failed: Expected infinite blocking when timeout is set to 0." );
67+
68+ int invalidTimeout = -1 ;
69+ assertThrows (IllegalArgumentException .class , () -> clientSocket .setSoTimeout (invalidTimeout ));
70+
71+ clientSocket .close ();
72+ assertThrows (SocketException .class , () -> clientSocket .setSoTimeout (testTimeout ));
73+ assertThrows (SocketException .class , () -> clientSocket .getSoTimeout ());
74+
75+ isServerRunning .set (false );
76+ }
77+
78+ @ Test
79+ public void testBufferSizes () throws Exception {
80+ if (!Config .get ().isJdkSocketEnabled ()) {
81+ System .out .println (
82+ "TunnelingJdkSocket usage is disabled. Enable it by setting the property 'JDK_SOCKET_ENABLED' to 'true'." );
83+ return ;
5484 }
5585
86+ Path socketPath = getSocketPath ();
87+ UnixDomainSocketAddress socketAddress = UnixDomainSocketAddress .of (socketPath );
88+ startServer (socketAddress );
89+ TunnelingJdkSocket clientSocket = createClient (socketPath );
90+
91+ assertEquals (TunnelingJdkSocket .DEFAULT_BUFFER_SIZE , clientSocket .getSendBufferSize ());
92+ assertEquals (TunnelingJdkSocket .DEFAULT_BUFFER_SIZE , clientSocket .getReceiveBufferSize ());
93+ assertEquals (TunnelingJdkSocket .DEFAULT_BUFFER_SIZE , clientSocket .getStreamBufferSize ());
94+
95+ int newBufferSize = TunnelingJdkSocket .DEFAULT_BUFFER_SIZE / 2 ;
96+ clientSocket .setSendBufferSize (newBufferSize );
97+ clientSocket .setReceiveBufferSize (newBufferSize / 2 );
98+ assertEquals (newBufferSize , clientSocket .getSendBufferSize ());
99+ assertEquals (newBufferSize / 2 , clientSocket .getReceiveBufferSize ());
100+ assertEquals (newBufferSize , clientSocket .getStreamBufferSize ());
101+
102+ int invalidBufferSize = -1 ;
103+ assertThrows (
104+ IllegalArgumentException .class , () -> clientSocket .setSendBufferSize (invalidBufferSize ));
105+ assertThrows (
106+ IllegalArgumentException .class , () -> clientSocket .setReceiveBufferSize (invalidBufferSize ));
107+
56108 clientSocket .close ();
109+ assertThrows (
110+ SocketException .class ,
111+ () -> clientSocket .setSendBufferSize (TunnelingJdkSocket .DEFAULT_BUFFER_SIZE ));
112+ assertThrows (
113+ SocketException .class ,
114+ () -> clientSocket .setReceiveBufferSize (TunnelingJdkSocket .DEFAULT_BUFFER_SIZE ));
115+ assertThrows (SocketException .class , () -> clientSocket .getSendBufferSize ());
116+ assertThrows (SocketException .class , () -> clientSocket .getReceiveBufferSize ());
117+ assertThrows (SocketException .class , () -> clientSocket .getStreamBufferSize ());
118+
57119 isServerRunning .set (false );
58120 }
59121
0 commit comments