Skip to content

Commit

Permalink
First draft of timeout test.
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahchen6 committed Jan 30, 2025
1 parent 896fe0b commit 3bb147c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions utils/socket-utils/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
apply from: "$rootDir/gradle/java.gradle"
apply plugin: "idea"

ext {
minJavaVersionForTests = JavaVersion.VERSION_17
}

sourceSets {
main_java17 {
java.srcDirs "${project.projectDir}/src/main/java17"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package datadog.common.socket;

import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class TunnelingJdkSocketTest {

@Test
public void testTimeout() throws Exception {
Assertions.assertEquals(1 + 1, 3); // should fail

// create test path
Path socketPath = Files.createTempFile("testSocket", null);
// create client socket
TunnelingJdkSocket clientSocket = createClient(socketPath);

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

// clean up
clientSocket.close();
Files.deleteIfExists(socketPath);
}

private TunnelingJdkSocket createClient(Path socketPath) throws IOException {
// create client socket
TunnelingJdkSocket clientSocket = new TunnelingJdkSocket(socketPath);
// set timeout to one second
clientSocket.setSoTimeout(1000);
return clientSocket;
}
}

0 comments on commit 3bb147c

Please sign in to comment.