Search in sources :

Example 16 with StepVerifier

use of reactor.test.StepVerifier in project spring-boot by spring-projects.

the class ConnectionFactoryHealthIndicatorTests method healthIndicatorWhenConnectionValidationFails.

@Test
void healthIndicatorWhenConnectionValidationFails() {
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    given(connectionFactory.getMetadata()).willReturn(() -> "mock");
    Connection connection = mock(Connection.class);
    given(connection.validate(ValidationDepth.REMOTE)).willReturn(Mono.just(false));
    given(connection.close()).willReturn(Mono.empty());
    given(connectionFactory.create()).willAnswer((invocation) -> Mono.just(connection));
    ConnectionFactoryHealthIndicator healthIndicator = new ConnectionFactoryHealthIndicator(connectionFactory);
    healthIndicator.health().as(StepVerifier::create).assertNext((actual) -> {
        assertThat(actual.getStatus()).isEqualTo(Status.DOWN);
        assertThat(actual.getDetails()).containsOnly(entry("database", "mock"), entry("validationQuery", "validate(REMOTE)"));
    }).verifyComplete();
}
Also used : ConnectionFactory(io.r2dbc.spi.ConnectionFactory) Status(org.springframework.boot.actuate.health.Status) StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) H2ConnectionOption(io.r2dbc.h2.H2ConnectionOption) Mono(reactor.core.publisher.Mono) UUID(java.util.UUID) ReactiveHealthIndicator(org.springframework.boot.actuate.health.ReactiveHealthIndicator) Assertions.entry(org.assertj.core.api.Assertions.entry) Connection(io.r2dbc.spi.Connection) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) BDDMockito.given(org.mockito.BDDMockito.given) Result(io.r2dbc.spi.Result) H2ConnectionFactory(io.r2dbc.h2.H2ConnectionFactory) ValidationDepth(io.r2dbc.spi.ValidationDepth) Collections(java.util.Collections) CloseableConnectionFactory(io.r2dbc.h2.CloseableConnectionFactory) Mockito.mock(org.mockito.Mockito.mock) ConnectionFactory(io.r2dbc.spi.ConnectionFactory) H2ConnectionFactory(io.r2dbc.h2.H2ConnectionFactory) CloseableConnectionFactory(io.r2dbc.h2.CloseableConnectionFactory) Connection(io.r2dbc.spi.Connection) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 17 with StepVerifier

use of reactor.test.StepVerifier in project spring-boot by spring-projects.

the class ConnectionFactoryHealthIndicatorTests method healthIndicatorWhenDatabaseUpWithSuccessValidationQuery.

@Test
void healthIndicatorWhenDatabaseUpWithSuccessValidationQuery() {
    CloseableConnectionFactory connectionFactory = createTestDatabase();
    try {
        String customValidationQuery = "SELECT COUNT(*) from HEALTH_TEST";
        String createTableStatement = "CREATE TABLE HEALTH_TEST (id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY)";
        Mono.from(connectionFactory.create()).flatMapMany((it) -> Flux.from(it.createStatement(createTableStatement).execute()).flatMap(Result::getRowsUpdated).thenMany(it.close())).as(StepVerifier::create).verifyComplete();
        ReactiveHealthIndicator healthIndicator = new ConnectionFactoryHealthIndicator(connectionFactory, customValidationQuery);
        healthIndicator.health().as(StepVerifier::create).assertNext((actual) -> {
            assertThat(actual.getStatus()).isEqualTo(Status.UP);
            assertThat(actual.getDetails()).containsOnly(entry("database", "H2"), entry("result", 0L), entry("validationQuery", customValidationQuery));
        }).verifyComplete();
    } finally {
        StepVerifier.create(connectionFactory.close()).verifyComplete();
    }
}
Also used : ReactiveHealthIndicator(org.springframework.boot.actuate.health.ReactiveHealthIndicator) ConnectionFactory(io.r2dbc.spi.ConnectionFactory) Status(org.springframework.boot.actuate.health.Status) StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) H2ConnectionOption(io.r2dbc.h2.H2ConnectionOption) Mono(reactor.core.publisher.Mono) UUID(java.util.UUID) ReactiveHealthIndicator(org.springframework.boot.actuate.health.ReactiveHealthIndicator) Assertions.entry(org.assertj.core.api.Assertions.entry) Connection(io.r2dbc.spi.Connection) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) BDDMockito.given(org.mockito.BDDMockito.given) Result(io.r2dbc.spi.Result) H2ConnectionFactory(io.r2dbc.h2.H2ConnectionFactory) ValidationDepth(io.r2dbc.spi.ValidationDepth) Collections(java.util.Collections) CloseableConnectionFactory(io.r2dbc.h2.CloseableConnectionFactory) Mockito.mock(org.mockito.Mockito.mock) CloseableConnectionFactory(io.r2dbc.h2.CloseableConnectionFactory) StepVerifier(reactor.test.StepVerifier) Result(io.r2dbc.spi.Result) Test(org.junit.jupiter.api.Test)

Example 18 with StepVerifier

use of reactor.test.StepVerifier in project spring-boot by spring-projects.

the class ConnectionFactoryHealthIndicatorTests method healthIndicatorWhenDatabaseUpWithConnectionValidation.

@Test
void healthIndicatorWhenDatabaseUpWithConnectionValidation() {
    CloseableConnectionFactory connectionFactory = createTestDatabase();
    try {
        ConnectionFactoryHealthIndicator healthIndicator = new ConnectionFactoryHealthIndicator(connectionFactory);
        healthIndicator.health().as(StepVerifier::create).assertNext((actual) -> {
            assertThat(actual.getStatus()).isEqualTo(Status.UP);
            assertThat(actual.getDetails()).containsOnly(entry("database", "H2"), entry("validationQuery", "validate(REMOTE)"));
        }).verifyComplete();
    } finally {
        StepVerifier.create(connectionFactory.close()).verifyComplete();
    }
}
Also used : ConnectionFactory(io.r2dbc.spi.ConnectionFactory) Status(org.springframework.boot.actuate.health.Status) StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) H2ConnectionOption(io.r2dbc.h2.H2ConnectionOption) Mono(reactor.core.publisher.Mono) UUID(java.util.UUID) ReactiveHealthIndicator(org.springframework.boot.actuate.health.ReactiveHealthIndicator) Assertions.entry(org.assertj.core.api.Assertions.entry) Connection(io.r2dbc.spi.Connection) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) BDDMockito.given(org.mockito.BDDMockito.given) Result(io.r2dbc.spi.Result) H2ConnectionFactory(io.r2dbc.h2.H2ConnectionFactory) ValidationDepth(io.r2dbc.spi.ValidationDepth) Collections(java.util.Collections) CloseableConnectionFactory(io.r2dbc.h2.CloseableConnectionFactory) Mockito.mock(org.mockito.Mockito.mock) CloseableConnectionFactory(io.r2dbc.h2.CloseableConnectionFactory) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 19 with StepVerifier

use of reactor.test.StepVerifier in project spring-boot by spring-projects.

the class ConnectionPoolMetricsTests method connectionFactoryIsInstrumented.

@Test
void connectionFactoryIsInstrumented() {
    SimpleMeterRegistry registry = new SimpleMeterRegistry();
    ConnectionPool connectionPool = new ConnectionPool(ConnectionPoolConfiguration.builder(this.connectionFactory).initialSize(3).maxSize(7).build());
    ConnectionPoolMetrics metrics = new ConnectionPoolMetrics(connectionPool, "test-pool", Tags.of(testTag, regionTag));
    metrics.bindTo(registry);
    // acquire two connections
    connectionPool.create().as(StepVerifier::create).expectNextCount(1).verifyComplete();
    connectionPool.create().as(StepVerifier::create).expectNextCount(1).verifyComplete();
    assertGauge(registry, "r2dbc.pool.acquired", 2);
    assertGauge(registry, "r2dbc.pool.allocated", 3);
    assertGauge(registry, "r2dbc.pool.idle", 1);
    assertGauge(registry, "r2dbc.pool.pending", 0);
    assertGauge(registry, "r2dbc.pool.max.allocated", 7);
    assertGauge(registry, "r2dbc.pool.max.pending", Integer.MAX_VALUE);
}
Also used : ConnectionPool(io.r2dbc.pool.ConnectionPool) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 20 with StepVerifier

use of reactor.test.StepVerifier in project spring-boot by spring-projects.

the class Neo4jReactiveHealthIndicatorTests method neo4jIsDown.

@Test
void neo4jIsDown() {
    Driver driver = mock(Driver.class);
    given(driver.rxSession(any(SessionConfig.class))).willThrow(ServiceUnavailableException.class);
    Neo4jReactiveHealthIndicator healthIndicator = new Neo4jReactiveHealthIndicator(driver);
    healthIndicator.health().as(StepVerifier::create).consumeNextWith((health) -> {
        assertThat(health.getStatus()).isEqualTo(Status.DOWN);
        assertThat(health.getDetails()).containsKeys("error");
    }).verifyComplete();
}
Also used : Status(org.springframework.boot.actuate.health.Status) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Driver(org.neo4j.driver.Driver) StepVerifier(reactor.test.StepVerifier) RxSession(org.neo4j.driver.reactive.RxSession) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SessionExpiredException(org.neo4j.driver.exceptions.SessionExpiredException) BDDMockito.then(org.mockito.BDDMockito.then) Mono(reactor.core.publisher.Mono) Mockito.times(org.mockito.Mockito.times) RxResult(org.neo4j.driver.reactive.RxResult) Test(org.junit.jupiter.api.Test) Values(org.neo4j.driver.Values) SessionConfig(org.neo4j.driver.SessionConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BDDMockito.given(org.mockito.BDDMockito.given) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) ResultSummary(org.neo4j.driver.summary.ResultSummary) Record(org.neo4j.driver.Record) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) Driver(org.neo4j.driver.Driver) SessionConfig(org.neo4j.driver.SessionConfig) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Aggregations

StepVerifier (reactor.test.StepVerifier)234 Test (org.junit.jupiter.api.Test)176 Mono (reactor.core.publisher.Mono)111 Flux (reactor.core.publisher.Flux)92 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)77 Duration (java.time.Duration)76 BeforeEach (org.junit.jupiter.api.BeforeEach)60 Test (org.junit.Test)55 Query (org.springframework.data.mongodb.core.query.Query)54 Assertions (org.assertj.core.api.Assertions)53 Document (org.bson.Document)49 Arrays (java.util.Arrays)46 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)44 Criteria (org.springframework.data.mongodb.core.query.Criteria)44 TimeUnit (java.util.concurrent.TimeUnit)42 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)42 Disposable (reactor.core.Disposable)39 List (java.util.List)38 ClassPathResource (org.springframework.core.io.ClassPathResource)38 AtomicReference (java.util.concurrent.atomic.AtomicReference)37