Search in sources :

Example 1 with IDLE_CONNECTIONS

use of reactor.netty.Metrics.IDLE_CONNECTIONS in project reactor-netty by reactor.

the class PooledConnectionProviderDefaultMetricsTest method doTest.

private void doTest(HttpServer server, HttpClient client, String poolName, boolean clientMetricsEnabled, int expectedMaxConnection, int expectedMaxPendingAcquire) throws Exception {
    disposableServer = server.handle((req, res) -> res.header("Connection", "close").sendString(Mono.just("test"))).bindNow();
    AtomicBoolean metrics = new AtomicBoolean(false);
    CountDownLatch latch = new CountDownLatch(1);
    boolean isSecured = client.configuration().sslProvider() != null;
    client.doOnResponse((res, conn) -> {
        conn.channel().closeFuture().addListener(f -> latch.countDown());
        double totalConnections = getGaugeValue(CONNECTION_PROVIDER_PREFIX + TOTAL_CONNECTIONS, poolName);
        double activeConnections = getGaugeValue(CONNECTION_PROVIDER_PREFIX + ACTIVE_CONNECTIONS, poolName);
        double maxConnections = getGaugeValue(CONNECTION_PROVIDER_PREFIX + MAX_CONNECTIONS, poolName);
        double idleConnections = getGaugeValue(CONNECTION_PROVIDER_PREFIX + IDLE_CONNECTIONS, poolName);
        double pendingConnections = getGaugeValue(CONNECTION_PROVIDER_PREFIX + PENDING_CONNECTIONS, poolName);
        double maxPendingConnections = getGaugeValue(CONNECTION_PROVIDER_PREFIX + MAX_PENDING_CONNECTIONS, poolName);
        if (totalConnections == 1 && activeConnections == 1 && idleConnections == 0 && pendingConnections == 0 && maxConnections == expectedMaxConnection && maxPendingConnections == expectedMaxPendingAcquire) {
            metrics.set(true);
        }
        if (isSecured) {
            double activeStreams = getGaugeValue(CONNECTION_PROVIDER_PREFIX + ACTIVE_STREAMS, "http2." + poolName);
            double pendingStreams = getGaugeValue(CONNECTION_PROVIDER_PREFIX + PENDING_STREAMS, "http2." + poolName);
            if (activeStreams == 1 && pendingStreams == 0) {
                metrics.set(true);
            }
        }
    }).metrics(clientMetricsEnabled, Function.identity()).get().uri("/").responseContent().aggregate().asString().block(Duration.ofSeconds(30));
    assertThat(latch.await(30, TimeUnit.SECONDS)).as("latch await").isTrue();
    assertThat(metrics.get()).isTrue();
    if (isSecured) {
        assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + TOTAL_CONNECTIONS, poolName)).isEqualTo(1);
        assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + IDLE_CONNECTIONS, poolName)).isEqualTo(1);
        assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + ACTIVE_STREAMS, "http2." + poolName)).isEqualTo(0);
        assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + PENDING_STREAMS, "http2." + poolName)).isEqualTo(0);
    } else {
        assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + TOTAL_CONNECTIONS, poolName)).isEqualTo(0);
        assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + IDLE_CONNECTIONS, poolName)).isEqualTo(0);
    }
    assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + ACTIVE_CONNECTIONS, poolName)).isEqualTo(0);
    assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + PENDING_CONNECTIONS, poolName)).isEqualTo(0);
    assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + MAX_CONNECTIONS, poolName)).isEqualTo(expectedMaxConnection);
    assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + MAX_PENDING_CONNECTIONS, poolName)).isEqualTo(expectedMaxPendingAcquire);
}
Also used : TOTAL_CONNECTIONS(reactor.netty.Metrics.TOTAL_CONNECTIONS) BeforeEach(org.junit.jupiter.api.BeforeEach) MAX_CONNECTIONS(reactor.netty.Metrics.MAX_CONNECTIONS) SimpleMeterRegistry(io.micrometer.api.instrument.simple.SimpleMeterRegistry) HttpProtocol(reactor.netty.http.HttpProtocol) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Http2SslContextSpec(reactor.netty.http.Http2SslContextSpec) BaseHttpTest(reactor.netty.BaseHttpTest) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NAME(reactor.netty.Metrics.NAME) Function(java.util.function.Function) PENDING_STREAMS(reactor.netty.Metrics.PENDING_STREAMS) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) CONNECTION_PROVIDER_PREFIX(reactor.netty.Metrics.CONNECTION_PROVIDER_PREFIX) BeforeAll(org.junit.jupiter.api.BeforeAll) ACTIVE_CONNECTIONS(reactor.netty.Metrics.ACTIVE_CONNECTIONS) Duration(java.time.Duration) PENDING_CONNECTIONS(reactor.netty.Metrics.PENDING_CONNECTIONS) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) Mono(reactor.core.publisher.Mono) CertificateException(java.security.cert.CertificateException) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) IDLE_CONNECTIONS(reactor.netty.Metrics.IDLE_CONNECTIONS) CountDownLatch(java.util.concurrent.CountDownLatch) AfterEach(org.junit.jupiter.api.AfterEach) HttpServer(reactor.netty.http.server.HttpServer) MAX_PENDING_CONNECTIONS(reactor.netty.Metrics.MAX_PENDING_CONNECTIONS) MeterRegistry(io.micrometer.api.instrument.MeterRegistry) Metrics(io.micrometer.api.instrument.Metrics) ACTIVE_STREAMS(reactor.netty.Metrics.ACTIVE_STREAMS) Gauge(io.micrometer.api.instrument.Gauge) HttpClient(reactor.netty.http.client.HttpClient) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

Gauge (io.micrometer.api.instrument.Gauge)1 MeterRegistry (io.micrometer.api.instrument.MeterRegistry)1 Metrics (io.micrometer.api.instrument.Metrics)1 SimpleMeterRegistry (io.micrometer.api.instrument.simple.SimpleMeterRegistry)1 InsecureTrustManagerFactory (io.netty.handler.ssl.util.InsecureTrustManagerFactory)1 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)1 CertificateException (java.security.cert.CertificateException)1 Duration (java.time.Duration)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Function (java.util.function.Function)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 AfterEach (org.junit.jupiter.api.AfterEach)1 BeforeAll (org.junit.jupiter.api.BeforeAll)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1 Mono (reactor.core.publisher.Mono)1 BaseHttpTest (reactor.netty.BaseHttpTest)1 ACTIVE_CONNECTIONS (reactor.netty.Metrics.ACTIVE_CONNECTIONS)1