Skip to content

Commit

Permalink
Backport test improvements from 2.0.x to 1.7.x
Browse files Browse the repository at this point in the history
fixes gh-2943
  • Loading branch information
jonatan-ivanov committed Jan 5, 2022
1 parent 988b5bd commit ccd3f38
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ void testSuspendExpire() throws Exception {
public void handle(String path, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws ServletException {
request.setHandled(true);
try {
barrier[0].await();
barrier[0].await(5, TimeUnit.SECONDS);

Thread.sleep(dispatchTime);

Expand All @@ -243,7 +243,7 @@ public void handle(String path, Request request, HttpServletRequest httpRequest,
throw new ServletException(x);
} finally {
try {
barrier[1].await();
barrier[1].await(5, TimeUnit.SECONDS);
} catch (Exception ignored) {
}
}
Expand All @@ -256,7 +256,7 @@ public void handle(String path, Request request, HttpServletRequest httpRequest,
"\r\n";
connector.executeRequest(request);

barrier[0].await();
barrier[0].await(5, TimeUnit.SECONDS);

assertThat(registry.get("jetty.server.dispatches.open").longTaskTimer().activeTasks()).isEqualTo(1);

Expand All @@ -281,13 +281,13 @@ public void onError(AsyncEvent event) {
@Override
public void onComplete(AsyncEvent event) {
try {
barrier[2].await();
barrier[2].await(5, TimeUnit.SECONDS);
} catch (Exception ignored) {
}
}
});

barrier[2].await();
barrier[2].await(5, TimeUnit.SECONDS);

assertThat(registry.get("jetty.server.async.expires").counter().count()).isEqualTo(1);
assertThat(registry.get("jetty.server.dispatches.open").longTaskTimer().activeTasks()).isEqualTo(0);
Expand All @@ -308,7 +308,7 @@ void testSuspendComplete() throws Exception {
public void handle(String path, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws ServletException {
request.setHandled(true);
try {
barrier[0].await();
barrier[0].await(5, TimeUnit.SECONDS);

Thread.sleep(dispatchTime);

Expand All @@ -320,7 +320,7 @@ public void handle(String path, Request request, HttpServletRequest httpRequest,
throw new ServletException(x);
} finally {
try {
barrier[1].await();
barrier[1].await(5, TimeUnit.SECONDS);
} catch (Exception ignored) {
}
}
Expand All @@ -333,7 +333,7 @@ public void handle(String path, Request request, HttpServletRequest httpRequest,
"\r\n";
connector.executeRequest(request);

barrier[0].await();
barrier[0].await(5, TimeUnit.SECONDS);
assertThat(registry.get("jetty.server.dispatches.open").longTaskTimer().activeTasks()).isEqualTo(1);

barrier[1].await(5, TimeUnit.SECONDS);
Expand Down Expand Up @@ -364,7 +364,7 @@ public void onComplete(AsyncEvent event) {
long requestTime = 20;
Thread.sleep(requestTime);
asyncHolder.get().complete();
latch.await();
latch.await(5, TimeUnit.SECONDS);

assertThat(registry.get("jetty.server.requests")
.tag("outcome", Outcome.SUCCESS.name())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -103,7 +105,7 @@ public void resourcesWithAnnotationAreTimed() {
}

@Test
public void longTaskTimerSupported() throws InterruptedException, ExecutionException {
public void longTaskTimerSupported() throws InterruptedException, ExecutionException, TimeoutException {
final Future<Response> future = target("long-timed").request().async().get();

/*
Expand All @@ -112,7 +114,7 @@ public void longTaskTimerSupported() throws InterruptedException, ExecutionExcep
* assertions below to fail. Thread.sleep() is not an option, so resort
* to CountDownLatch.)
*/
longTaskRequestStartedLatch.await();
longTaskRequestStartedLatch.await(5, TimeUnit.SECONDS);

// the request is not timed, yet
assertThat(registry.find(METRIC_NAME).tags(tagsFrom("/timed", 200)).timer())
Expand All @@ -126,7 +128,7 @@ public void longTaskTimerSupported() throws InterruptedException, ExecutionExcep

// finish the long running request
longTaskRequestReleaseLatch.countDown();
future.get();
future.get(5, TimeUnit.SECONDS);

// the request is timed after the long running request completed
assertThat(registry.get(METRIC_NAME)
Expand All @@ -137,7 +139,7 @@ public void longTaskTimerSupported() throws InterruptedException, ExecutionExcep

// See gh-2861
@Test
public void longTaskTimerOnlyOneMeter() throws InterruptedException, ExecutionException {
public void longTaskTimerOnlyOneMeter() throws InterruptedException, ExecutionException, TimeoutException {
final Future<Response> future = target("just-long-timed").request().async().get();

/*
Expand All @@ -146,7 +148,7 @@ public void longTaskTimerOnlyOneMeter() throws InterruptedException, ExecutionEx
* assertions below to fail. Thread.sleep() is not an option, so resort
* to CountDownLatch.)
*/
longTaskRequestStartedLatch.await();
longTaskRequestStartedLatch.await(5, TimeUnit.SECONDS);

// the long running task is timed
assertThat(registry.get("long.task.in.request")
Expand All @@ -156,7 +158,7 @@ public void longTaskTimerOnlyOneMeter() throws InterruptedException, ExecutionEx

// finish the long running request
longTaskRequestReleaseLatch.countDown();
future.get();
future.get(5, TimeUnit.SECONDS);

// no meters registered except the one checked above
assertThat(registry.getMeters().size()).isOne();
Expand Down

0 comments on commit ccd3f38

Please sign in to comment.