Commit 0b689a5b authored by Phillip Webb's avatar Phillip Webb

Polish MetricsFilter registration async support

Closes gh-11348
parent b6af06a5
...@@ -119,18 +119,18 @@ public class MetricsAutoConfigurationIntegrationTests { ...@@ -119,18 +119,18 @@ public class MetricsAutoConfigurationIntegrationTests {
} }
@Test @Test
public void asyncRequestMappingIsInstrumented() throws InterruptedException, BrokenBarrierException { public void asyncRequestMappingIsInstrumented()
Thread backgroundRequest = new Thread(() -> this.loopback.getForObject("/api/async", String.class)); throws InterruptedException, BrokenBarrierException {
Thread backgroundRequest = new Thread(
() -> this.loopback.getForObject("/api/async", String.class));
backgroundRequest.start(); backgroundRequest.start();
this.cyclicBarrier.await(); this.cyclicBarrier.await();
MockClock.clock(this.registry).addSeconds(2); MockClock.clock(this.registry).addSeconds(2);
this.cyclicBarrier.await(); this.cyclicBarrier.await();
backgroundRequest.join(); backgroundRequest.join();
assertThat(this.registry.find("http.server.requests").tags("uri", "/api/async")
assertThat(this.registry.find("http.server.requests") .timer()).matches(t -> t.count() == 1)
.tags("uri", "/api/async").timer()) .matches(t -> t.totalTime(TimeUnit.SECONDS) == 2);
.matches(t -> t.count() == 1)
.matches(t -> t.totalTime(TimeUnit.SECONDS) == 2);
} }
@Configuration @Configuration
...@@ -156,10 +156,12 @@ public class MetricsAutoConfigurationIntegrationTests { ...@@ -156,10 +156,12 @@ public class MetricsAutoConfigurationIntegrationTests {
public CyclicBarrier cyclicBarrier() { public CyclicBarrier cyclicBarrier() {
return new CyclicBarrier(2); return new CyclicBarrier(2);
} }
} }
@RestController @RestController
static class PersonController { static class PersonController {
private final CyclicBarrier cyclicBarrier; private final CyclicBarrier cyclicBarrier;
PersonController(CyclicBarrier cyclicBarrier) { PersonController(CyclicBarrier cyclicBarrier) {
...@@ -172,18 +174,22 @@ public class MetricsAutoConfigurationIntegrationTests { ...@@ -172,18 +174,22 @@ public class MetricsAutoConfigurationIntegrationTests {
} }
@GetMapping("/api/async") @GetMapping("/api/async")
CompletableFuture<String> asyncHello() throws BrokenBarrierException, InterruptedException { CompletableFuture<String> asyncHello()
throws BrokenBarrierException, InterruptedException {
this.cyclicBarrier.await(); this.cyclicBarrier.await();
return CompletableFuture.supplyAsync(() -> { return CompletableFuture.supplyAsync(this::awaitAndHello);
try { }
this.cyclicBarrier.await();
} private String awaitAndHello() {
catch (InterruptedException | BrokenBarrierException e) { try {
throw new RuntimeException(e); this.cyclicBarrier.await();
}
return "async-hello"; return "async-hello";
}); }
catch (InterruptedException | BrokenBarrierException ex) {
throw new RuntimeException(ex);
}
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment