Skip to content

Commit 75d65cd

Browse files
committed
Add server to test.
1 parent 3bb147c commit 75d65cd

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

utils/socket-utils/build.gradle

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ sourceSets {
99
main_java17 {
1010
java.srcDirs "${project.projectDir}/src/main/java17"
1111
}
12+
13+
test_java17 {
14+
java.srcDirs "${project.projectDir}/src/test/java17"
15+
}
1216
}
1317

1418
compileMain_java17Java.configure {

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

+30-1
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,60 @@
33
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
44

55
import java.io.IOException;
6+
import java.nio.channels.ServerSocketChannel;
67
import java.nio.file.Files;
78
import java.nio.file.Path;
89
import java.time.Duration;
10+
import java.util.concurrent.atomic.AtomicBoolean;
11+
import jnr.unixsocket.UnixSocketAddress;
912
import org.junit.jupiter.api.Assertions;
1013
import org.junit.jupiter.api.Test;
1114

1215
public class TunnelingJdkSocketTest {
1316

17+
private final AtomicBoolean running = new AtomicBoolean(false);
18+
1419
@Test
1520
public void testTimeout() throws Exception {
1621
Assertions.assertEquals(1 + 1, 3); // should fail
1722

1823
// create test path
1924
Path socketPath = Files.createTempFile("testSocket", null);
25+
// start server
26+
startServer(socketPath);
2027
// create client socket
2128
TunnelingJdkSocket clientSocket = createClient(socketPath);
2229

2330
// attempt to read from empty socket (read should block indefinitely)
2431
assertTimeoutPreemptively(Duration.ofSeconds(5), () -> clientSocket.getInputStream().read());
2532

26-
// clean up
33+
// clean up client, server, and path
2734
clientSocket.close();
35+
running.set(false);
2836
Files.deleteIfExists(socketPath);
2937
}
3038

39+
private void startServer(Path socketPath) {
40+
Thread serverThread =
41+
new Thread(
42+
() -> {
43+
// open and bind server to socketPath
44+
try (ServerSocketChannel serverChannel = ServerSocketChannel.open()) {
45+
serverChannel.socket().bind(new UnixSocketAddress(socketPath.toFile()));
46+
// accept connections made to the server
47+
running.set(true);
48+
while (running.get()) {
49+
serverChannel.accept();
50+
}
51+
} catch (IOException e) {
52+
throw new RuntimeException(e);
53+
}
54+
});
55+
56+
// start server in separate thread
57+
serverThread.start();
58+
}
59+
3160
private TunnelingJdkSocket createClient(Path socketPath) throws IOException {
3261
// create client socket
3362
TunnelingJdkSocket clientSocket = new TunnelingJdkSocket(socketPath);

0 commit comments

Comments
 (0)