Skip to content

Commit 3bb147c

Browse files
committed
First draft of timeout test.
1 parent 896fe0b commit 3bb147c

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

utils/socket-utils/build.gradle

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
apply from: "$rootDir/gradle/java.gradle"
22
apply plugin: "idea"
33

4+
ext {
5+
minJavaVersionForTests = JavaVersion.VERSION_17
6+
}
7+
48
sourceSets {
59
main_java17 {
610
java.srcDirs "${project.projectDir}/src/main/java17"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package datadog.common.socket;
2+
3+
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
4+
5+
import java.io.IOException;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.time.Duration;
9+
import org.junit.jupiter.api.Assertions;
10+
import org.junit.jupiter.api.Test;
11+
12+
public class TunnelingJdkSocketTest {
13+
14+
@Test
15+
public void testTimeout() throws Exception {
16+
Assertions.assertEquals(1 + 1, 3); // should fail
17+
18+
// create test path
19+
Path socketPath = Files.createTempFile("testSocket", null);
20+
// create client socket
21+
TunnelingJdkSocket clientSocket = createClient(socketPath);
22+
23+
// attempt to read from empty socket (read should block indefinitely)
24+
assertTimeoutPreemptively(Duration.ofSeconds(5), () -> clientSocket.getInputStream().read());
25+
26+
// clean up
27+
clientSocket.close();
28+
Files.deleteIfExists(socketPath);
29+
}
30+
31+
private TunnelingJdkSocket createClient(Path socketPath) throws IOException {
32+
// create client socket
33+
TunnelingJdkSocket clientSocket = new TunnelingJdkSocket(socketPath);
34+
// set timeout to one second
35+
clientSocket.setSoTimeout(1000);
36+
return clientSocket;
37+
}
38+
}

0 commit comments

Comments
 (0)