use of io.r2dbc.spi.Connection 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();
}
use of io.r2dbc.spi.Connection in project spring-framework by spring-projects.
the class R2dbcTransactionManagerUnitTests method testSimpleTransaction.
@Test
void testSimpleTransaction() {
TestTransactionSynchronization sync = new TestTransactionSynchronization(TransactionSynchronization.STATUS_COMMITTED);
AtomicInteger commits = new AtomicInteger();
when(connectionMock.commitTransaction()).thenReturn(Mono.fromRunnable(commits::incrementAndGet));
TransactionalOperator operator = TransactionalOperator.create(tm);
ConnectionFactoryUtils.getConnection(connectionFactoryMock).flatMap(connection -> TransactionSynchronizationManager.forCurrentTransaction().doOnNext(synchronizationManager -> synchronizationManager.registerSynchronization(sync))).as(operator::transactional).as(StepVerifier::create).expectNextCount(1).verifyComplete();
assertThat(commits).hasValue(1);
verify(connectionMock).isAutoCommit();
verify(connectionMock).beginTransaction();
verify(connectionMock).commitTransaction();
verify(connectionMock).close();
verifyNoMoreInteractions(connectionMock);
assertThat(sync.beforeCommitCalled).isTrue();
assertThat(sync.afterCommitCalled).isTrue();
assertThat(sync.beforeCompletionCalled).isTrue();
assertThat(sync.afterCompletionCalled).isTrue();
}
use of io.r2dbc.spi.Connection in project spring-framework by spring-projects.
the class SingleConnectionFactoryUnitTests method shouldAllocateSameConnection.
@Test
void shouldAllocateSameConnection() {
SingleConnectionFactory factory = new SingleConnectionFactory("r2dbc:h2:mem:///foo", false);
Mono<? extends Connection> cf1 = factory.create();
Mono<? extends Connection> cf2 = factory.create();
Connection c1 = cf1.block();
Connection c2 = cf2.block();
assertThat(c1).isSameAs(c2);
factory.destroy();
}
use of io.r2dbc.spi.Connection in project spring-framework by spring-projects.
the class SingleConnectionFactoryUnitTests method releaseConnectionShouldNotCloseConnection.
@Test
void releaseConnectionShouldNotCloseConnection() {
Connection connectionMock = mock(Connection.class);
ConnectionFactoryMetadata metadata = mock(ConnectionFactoryMetadata.class);
SingleConnectionFactory factory = new SingleConnectionFactory(connectionMock, metadata, true);
Connection connection = factory.create().block();
ConnectionFactoryUtils.releaseConnection(connection, factory).as(StepVerifier::create).verifyComplete();
verify(connectionMock, never()).close();
}
use of io.r2dbc.spi.Connection in project spring-framework by spring-projects.
the class SingleConnectionFactoryUnitTests method releaseConnectionShouldCloseUnrelatedConnection.
@Test
void releaseConnectionShouldCloseUnrelatedConnection() {
Connection connectionMock = mock(Connection.class);
Connection otherConnection = mock(Connection.class);
ConnectionFactoryMetadata metadata = mock(ConnectionFactoryMetadata.class);
when(otherConnection.close()).thenReturn(Mono.empty());
SingleConnectionFactory factory = new SingleConnectionFactory(connectionMock, metadata, false);
factory.create().as(StepVerifier::create).expectNextCount(1).verifyComplete();
ConnectionFactoryUtils.releaseConnection(otherConnection, factory).as(StepVerifier::create).verifyComplete();
verify(otherConnection).close();
}
Aggregations