Skip to content

Commit

Permalink
Updates test after further testing on Linux.
Browse files Browse the repository at this point in the history
Signed-off-by: Santiago Pericas-Geertsen <[email protected]>
  • Loading branch information
spericas committed Aug 23, 2024
1 parent f02ea09 commit 8ab700d
Showing 1 changed file with 17 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.net.SocketException;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.logging.Logger;

import io.helidon.common.http.Http;
Expand All @@ -28,9 +27,9 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.fail;

/**
* Tests support for idle connection timeouts.
Expand Down Expand Up @@ -84,33 +83,26 @@ public void testIdleConnectionClosed() throws Exception {
String res = client.receive();
assertThat(res, containsString("Hello World!"));

// second request while connection is active
client.request(Http.Method.GET,
"/hello",
null);
res = client.receive();
assertThat(res, containsString("Hello World!"));

// wait for connection to time out due to inactivity
Thread.sleep(2 * IDLE_TIMEOUT);

// now fail attempting to use connection again
assertEventuallyThrows(SocketException.class, () -> {
client.request(Http.Method.GET,
"/hello",
null);
client.receive();
return null;
}, 5 * IDLE_TIMEOUT);
}
}

private static void assertEventuallyThrows(Class<?> exc, Callable<?> runnable, long millis)
throws InterruptedException {
long start = System.currentTimeMillis();
do {
// try again and either get nothing or an exception
try {
runnable.call();
} catch (Throwable t) {
if (t.getClass().equals(exc)) {
return;
}
client.request(Http.Method.GET,
"/hello",
null);
res = client.receive();
assertThat(res, is(""));
} catch (SocketException e) {
// falls through as possible outcome
}
Thread.sleep(millis / 3);
} while (System.currentTimeMillis() - start <= millis);
fail("Predicate failed after " + millis + " milliseconds");
}
}
}

0 comments on commit 8ab700d

Please sign in to comment.