1
1
package datadog .common .socket ;
2
2
3
+ import static org .junit .jupiter .api .Assertions .*;
3
4
import static org .junit .jupiter .api .Assertions .assertTimeoutPreemptively ;
4
- import static org .junit .jupiter .api .Assertions .fail ;
5
5
6
6
import datadog .trace .api .Config ;
7
7
import java .io .IOException ;
8
+ import java .io .InputStream ;
8
9
import java .net .InetSocketAddress ;
10
+ import java .net .SocketException ;
9
11
import java .net .StandardProtocolFamily ;
10
12
import java .net .UnixDomainSocketAddress ;
11
13
import java .nio .channels .ServerSocketChannel ;
@@ -28,32 +30,92 @@ public void testTimeout() throws Exception {
28
30
return ;
29
31
}
30
32
31
- int testTimeout = 3000 ;
32
33
Path socketPath = getSocketPath ();
33
34
UnixDomainSocketAddress socketAddress = UnixDomainSocketAddress .of (socketPath );
34
35
startServer (socketAddress );
35
36
TunnelingJdkSocket clientSocket = createClient (socketPath );
37
+ InputStream inputStream = clientSocket .getInputStream ();
36
38
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 ());
41
42
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
43
57
// 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 ());
46
61
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" );
49
64
} catch (AssertionError e ) {
50
- infiniteTimeOut = true ;
65
+ // Expected
51
66
}
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 ;
54
84
}
55
85
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
+
56
108
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
+
57
119
isServerRunning .set (false );
58
120
}
59
121
0 commit comments