Skip to content

Commit b200274

Browse files
committed
Second draft of timeout test.
1 parent 53f8666 commit b200274

File tree

1 file changed

+28
-59
lines changed

1 file changed

+28
-59
lines changed

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

Lines changed: 28 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,107 +3,76 @@
33
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
44

55
import java.io.IOException;
6+
import java.net.InetSocketAddress;
7+
import java.net.StandardProtocolFamily;
68
import java.net.UnixDomainSocketAddress;
9+
import java.nio.ByteBuffer;
710
import java.nio.channels.ServerSocketChannel;
11+
import java.nio.channels.SocketChannel;
812
import java.nio.file.Files;
913
import java.nio.file.Path;
1014
import java.time.Duration;
1115
import java.util.concurrent.atomic.AtomicBoolean;
12-
import org.junit.jupiter.api.Assertions;
1316
import org.junit.jupiter.api.Test;
1417

1518
public class TunnelingJdkSocketTest {
1619

17-
private final AtomicBoolean running = new AtomicBoolean(false);
20+
private static final AtomicBoolean is_server_running = new AtomicBoolean(false);
1821

1922
@Test
2023
public void testTimeout() throws Exception {
21-
Assertions.assertEquals(1 + 1, 2);
22-
23-
// set test socket path
24+
// set socket path and address
2425
Path socketPath = getSocketPath();
25-
// start server
26-
startServer(socketPath);
26+
UnixDomainSocketAddress socketAddress = UnixDomainSocketAddress.of(socketPath);
2727

28-
// timeout after two seconds if server doesn't start
29-
long startTime = System.currentTimeMillis();
30-
long timeout = 2000;
31-
while (!running.get()) {
32-
Thread.sleep(100);
33-
if (System.currentTimeMillis() - startTime > timeout) {
34-
System.out.println("Timeout waiting for server to start.");
35-
break;
36-
}
37-
}
28+
// start server in a separate thread
29+
startServer(socketAddress, false);
3830

39-
// create client socket
31+
// create client
4032
TunnelingJdkSocket clientSocket = createClient(socketPath);
4133

42-
// attempt to read from empty socket (read should block indefinitely)
43-
System.out.println("Test is starting...");
44-
assertTimeoutPreemptively(Duration.ofSeconds(5), () -> clientSocket.getInputStream().read());
34+
// expect a failure after three seconds because timeout is not supported yet
35+
assertTimeoutPreemptively(Duration.ofMillis(3000), () -> clientSocket.getInputStream().read());
4536

46-
// clean up client, server, and path
37+
// clean up
4738
clientSocket.close();
48-
running.set(false);
49-
Files.deleteIfExists(socketPath);
50-
System.out.println("Client, server, and path cleaned.");
39+
is_server_running.set(false);
5140
}
5241

5342
private Path getSocketPath() throws IOException {
54-
Path socketPath = Files.createTempFile("testSocket", ".sock");
43+
Path socketPath = Files.createTempFile("testSocket", null);
5544
Files.delete(socketPath);
5645
socketPath.toFile().deleteOnExit();
5746
return socketPath;
5847
}
5948

60-
private void startServer(Path socketPath) {
49+
private static void startServer(UnixDomainSocketAddress socketAddress, boolean sendMessage) {
6150
Thread serverThread =
6251
new Thread(
6352
() -> {
64-
// open and bind server to socketPath
65-
try (ServerSocketChannel serverChannel = ServerSocketChannel.open()) {
66-
System.out.println("serverChannel is open.");
67-
serverChannel.configureBlocking(false);
68-
System.out.println("serverChannel is not blocking.");
69-
serverChannel.socket().bind(UnixDomainSocketAddress.of(socketPath));
70-
// accept connections made to the server
71-
running.set(true);
72-
System.out.println("Server is running and ready to accept connections.");
73-
while (running.get()) {
74-
serverChannel.accept();
75-
System.out.println("Server is accepting connections.");
53+
try (ServerSocketChannel serverChannel =
54+
ServerSocketChannel.open(StandardProtocolFamily.UNIX)) {
55+
serverChannel.bind(socketAddress);
56+
is_server_running.set(true);
57+
58+
// wait for client connection
59+
while (is_server_running.get()) {
60+
SocketChannel clientChannel = serverChannel.accept();
61+
if (sendMessage) {
62+
clientChannel.write(ByteBuffer.wrap("Hello!".getBytes()));
63+
}
7664
}
7765
} catch (IOException e) {
78-
System.out.println("Server encountered error with accepting a connection.");
79-
// clean up server and path
80-
running.set(false);
81-
try {
82-
Files.deleteIfExists(socketPath);
83-
} catch (IOException ex) {
84-
throw new RuntimeException(ex);
85-
}
8666
throw new RuntimeException(e);
8767
}
8868
});
89-
90-
// start server in separate thread
9169
serverThread.start();
9270
}
9371

9472
private TunnelingJdkSocket createClient(Path socketPath) throws IOException {
95-
// create client socket
9673
TunnelingJdkSocket clientSocket = new TunnelingJdkSocket(socketPath);
97-
// set timeout to one second
74+
clientSocket.connect(new InetSocketAddress("localhost", 0));
9875
clientSocket.setSoTimeout(1000);
99-
System.out.println("Client set timeout.");
100-
101-
if (clientSocket.isConnected()) {
102-
System.out.println("Client connected successfully.");
103-
} else {
104-
System.out.println("Client failed to connect.");
105-
}
106-
10776
return clientSocket;
10877
}
10978
}

0 commit comments

Comments
 (0)