diff --git a/reactor-netty-http/src/test/java/reactor/netty/resources/DefaultPooledConnectionProviderTest.java b/reactor-netty-http/src/test/java/reactor/netty/resources/DefaultPooledConnectionProviderTest.java index c76ccd1465..3b0bff3437 100644 --- a/reactor-netty-http/src/test/java/reactor/netty/resources/DefaultPooledConnectionProviderTest.java +++ b/reactor-netty-http/src/test/java/reactor/netty/resources/DefaultPooledConnectionProviderTest.java @@ -670,12 +670,23 @@ void testDisposeInactivePoolsInBackground(boolean enableEvictInBackground, boole } MeterRegistrarImpl meterRegistrar; - String metricsName = ""; + CountDownLatch meterRemoved = new CountDownLatch(2); + String pendingTime = isHttp2 ? ".pending.streams.time" : ".pending.connections.time"; + String metricsName1 = CONNECTION_PROVIDER_PREFIX + ACTIVE_CONNECTIONS; + String metricsName2 = CONNECTION_PROVIDER_PREFIX + pendingTime; + String metricsTagName = ""; if (isBuiltInMetrics) { meterRegistrar = null; builder.metrics(true); - metricsName = isHttp2 ? "http2.testDisposeInactivePoolsInBackground" : "testDisposeInactivePoolsInBackground"; + metricsTagName = isHttp2 ? "http2.testDisposeInactivePoolsInBackground" : "testDisposeInactivePoolsInBackground"; + + registry.config().onMeterRemoved(meter -> { + if (metricsName1.equals(meter.getId().getName()) || + metricsName2.equals(meter.getId().getName())) { + meterRemoved.countDown(); + } + }); } else { meterRegistrar = new MeterRegistrarImpl(); @@ -711,18 +722,15 @@ void testDisposeInactivePoolsInBackground(boolean enableEvictInBackground, boole InetSocketAddress sa = (InetSocketAddress) serverAddress.get(); String address = sa.getHostString() + ":" + sa.getPort(); - String pendingTime = isHttp2 ? ".pending.streams.time" : ".pending.connections.time"; assertThat(provider.channelPools.size()).isEqualTo(1); if (meterRegistrar != null) { assertThat(meterRegistrar.registered.get()).isTrue(); } else { - assertGauge(registry, CONNECTION_PROVIDER_PREFIX + ACTIVE_CONNECTIONS, - REMOTE_ADDRESS, address, NAME, metricsName).isNotNull(); + assertGauge(registry, metricsName1, REMOTE_ADDRESS, address, NAME, metricsTagName).isNotNull(); - assertTimer(registry, CONNECTION_PROVIDER_PREFIX + pendingTime, - REMOTE_ADDRESS, address, NAME, metricsName).isNotNull(); + assertTimer(registry, metricsName2, REMOTE_ADDRESS, address, NAME, metricsTagName).isNotNull(); } if (enableEvictInBackground) { @@ -737,22 +745,23 @@ void testDisposeInactivePoolsInBackground(boolean enableEvictInBackground, boole assertThat(provider.isDisposed()).isEqualTo(enableEvictInBackground); if (meterRegistrar != null) { + if (enableEvictInBackground) { + assertThat(meterRegistrar.latch.await(30, TimeUnit.SECONDS)).isTrue(); + } assertThat(meterRegistrar.deRegistered.get()).isEqualTo(enableEvictInBackground); } else { if (enableEvictInBackground) { - assertGauge(registry, CONNECTION_PROVIDER_PREFIX + ACTIVE_CONNECTIONS, - REMOTE_ADDRESS, address, NAME, metricsName).isNull(); + assertThat(meterRemoved.await(30, TimeUnit.SECONDS)).isTrue(); + + assertGauge(registry, metricsName1, REMOTE_ADDRESS, address, NAME, metricsTagName).isNull(); - assertTimer(registry, CONNECTION_PROVIDER_PREFIX + pendingTime, - REMOTE_ADDRESS, address, NAME, metricsName).isNull(); + assertTimer(registry, metricsName2, REMOTE_ADDRESS, address, NAME, metricsTagName).isNull(); } else { - assertGauge(registry, CONNECTION_PROVIDER_PREFIX + ACTIVE_CONNECTIONS, - REMOTE_ADDRESS, address, NAME, metricsName).isNotNull(); + assertGauge(registry, metricsName1, REMOTE_ADDRESS, address, NAME, metricsTagName).isNotNull(); - assertTimer(registry, CONNECTION_PROVIDER_PREFIX + pendingTime, - REMOTE_ADDRESS, address, NAME, metricsName).isNotNull(); + assertTimer(registry, metricsName2, REMOTE_ADDRESS, address, NAME, metricsTagName).isNotNull(); } } } @@ -888,6 +897,7 @@ void testHttp2PoolAndGoAway() { static final class MeterRegistrarImpl implements ConnectionProvider.MeterRegistrar { AtomicBoolean registered = new AtomicBoolean(); AtomicBoolean deRegistered = new AtomicBoolean(); + final CountDownLatch latch = new CountDownLatch(1); MeterRegistrarImpl() { } @@ -900,6 +910,7 @@ public void registerMetrics(String poolName, String id, SocketAddress remoteAddr @Override public void deRegisterMetrics(String poolName, String id, SocketAddress remoteAddress) { deRegistered.compareAndSet(false, true); + latch.countDown(); } } }