Skip to content

Commit eb44a61

Browse files
committed
Clean code.
1 parent 38a5ccd commit eb44a61

File tree

2 files changed

+8
-17
lines changed

2 files changed

+8
-17
lines changed

utils/socket-utils/src/main/java17/datadog/common/socket/TunnelingJdkSocket.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public InputStream getInputStream() throws IOException {
125125
Selector selector = Selector.open();
126126
unixSocketChannel.configureBlocking(false);
127127
unixSocketChannel.register(selector, SelectionKey.OP_READ);
128-
ByteBuffer buffer = ByteBuffer.allocate(256); // arbitrary buffer size for now
128+
ByteBuffer buffer = ByteBuffer.allocate(256);
129129

130130
try {
131131
if (selector.select(timeout) == 0) {

utils/socket-utils/src/test/java/datadog/common/socket/TunnelingJdkSocketTest.java

+7-16
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.net.InetSocketAddress;
77
import java.net.StandardProtocolFamily;
88
import java.net.UnixDomainSocketAddress;
9-
import java.nio.ByteBuffer;
109
import java.nio.channels.ServerSocketChannel;
1110
import java.nio.channels.SocketChannel;
1211
import java.nio.file.Files;
@@ -18,23 +17,19 @@
1817
public class TunnelingJdkSocketTest {
1918

2019
private static final AtomicBoolean is_server_running = new AtomicBoolean(false);
20+
private final int client_timeout = 1000;
21+
private final int test_timeout = 3000;
2122

2223
@Test
2324
public void testTimeout() throws Exception {
24-
// set socket path and address
2525
Path socketPath = getSocketPath();
2626
UnixDomainSocketAddress socketAddress = UnixDomainSocketAddress.of(socketPath);
27-
28-
// start server in a separate thread
29-
startServer(socketAddress, false);
30-
31-
// create client
27+
startServer(socketAddress);
3228
TunnelingJdkSocket clientSocket = createClient(socketPath);
3329

34-
// will fail after three seconds if timeout (set to one second) is not supported
35-
assertTimeoutPreemptively(Duration.ofMillis(3000), () -> clientSocket.getInputStream().read());
30+
assertTimeoutPreemptively(
31+
Duration.ofMillis(test_timeout), () -> clientSocket.getInputStream().read());
3632

37-
// clean up
3833
clientSocket.close();
3934
is_server_running.set(false);
4035
}
@@ -46,7 +41,7 @@ private Path getSocketPath() throws IOException {
4641
return socketPath;
4742
}
4843

49-
private static void startServer(UnixDomainSocketAddress socketAddress, boolean sendMessage) {
44+
private static void startServer(UnixDomainSocketAddress socketAddress) {
5045
Thread serverThread =
5146
new Thread(
5247
() -> {
@@ -55,12 +50,8 @@ private static void startServer(UnixDomainSocketAddress socketAddress, boolean s
5550
serverChannel.bind(socketAddress);
5651
is_server_running.set(true);
5752

58-
// wait for client connection
5953
while (is_server_running.get()) {
6054
SocketChannel clientChannel = serverChannel.accept();
61-
if (sendMessage) {
62-
clientChannel.write(ByteBuffer.wrap("Hello!".getBytes()));
63-
}
6455
}
6556
} catch (IOException e) {
6657
throw new RuntimeException(e);
@@ -72,7 +63,7 @@ private static void startServer(UnixDomainSocketAddress socketAddress, boolean s
7263
private TunnelingJdkSocket createClient(Path socketPath) throws IOException {
7364
TunnelingJdkSocket clientSocket = new TunnelingJdkSocket(socketPath);
7465
clientSocket.connect(new InetSocketAddress("localhost", 0));
75-
clientSocket.setSoTimeout(1000);
66+
clientSocket.setSoTimeout(client_timeout);
7667
return clientSocket;
7768
}
7869
}

0 commit comments

Comments
 (0)