### Question:
RetryConnectionAcquiringStrategy extends AbstractConnectionAcquiringStrategy { @Override public Connection getConnection(ConnectionRequestContext requestContext) throws SQLException { int remainingAttempts = retryAttempts; try { do { try { return getConnectionFactory().getConnection(requestContext); } catch (AcquireTimeoutException e) { requestContext.incrementAttempts(); remainingAttempts--; LOGGER.warn("Can't acquireConnection connection, remaining retry attempts {}", remainingAttempts); if (remainingAttempts <= 0) { throw e; } } } while (true); } finally { int attemptedRetries = requestContext.getRetryAttempts(); if (attemptedRetries > 0) { retryAttemptsHistogram.update(attemptedRetries); } } } private  RetryConnectionAcquiringStrategy(ConfigurationProperties<? extends DataSource, Metrics, PoolAdapter> configurationProperties, int retryAttempts); @Override Connection getConnection(ConnectionRequestContext requestContext); @Override String toString(); static final String RETRY_ATTEMPTS_HISTOGRAM; }### Answer:
@Test public void testConnectionAcquiredInOneAttempt() throws SQLException { when(poolAdapter.getConnection(same(connectionRequestContext))).thenReturn(connection); RetryConnectionAcquiringStrategy retryConnectionAcquiringStrategy = new RetryConnectionAcquiringStrategy.Factory<DataSource>(5).newInstance(configuration); assertEquals(0, connectionRequestContext.getRetryAttempts()); assertSame(connection, retryConnectionAcquiringStrategy.getConnection(connectionRequestContext)); assertEquals(0, connectionRequestContext.getRetryAttempts()); verify(histogram, never()).update(anyInt()); }
@Test public void testConnectionAcquiredInTwoAttempts() throws SQLException { when(poolAdapter.getConnection(same(connectionRequestContext))) .thenThrow(new AcquireTimeoutException(new Exception())) .thenReturn(connection); RetryConnectionAcquiringStrategy retryConnectionAcquiringStrategy = new RetryConnectionAcquiringStrategy.Factory<DataSource>(5).newInstance(configuration); assertEquals(0, connectionRequestContext.getRetryAttempts()); assertSame(connection, retryConnectionAcquiringStrategy.getConnection(connectionRequestContext)); assertEquals(1, connectionRequestContext.getRetryAttempts()); verify(histogram, times(1)).update(1); }
@Test public void testConnectionNotAcquiredAfterAllAttempts() throws SQLException { Exception rootException = new Exception(); when(poolAdapter.getConnection(same(connectionRequestContext))) .thenThrow(new AcquireTimeoutException(rootException)); RetryConnectionAcquiringStrategy retryConnectionAcquiringStrategy = new RetryConnectionAcquiringStrategy.Factory<DataSource>(2).newInstance(configuration); assertEquals(0, connectionRequestContext.getRetryAttempts()); try { retryConnectionAcquiringStrategy.getConnection(connectionRequestContext); } catch (AcquireTimeoutException e) { assertSame(rootException, e.getCause()); } assertEquals(2, connectionRequestContext.getRetryAttempts()); verify(histogram, times(1)).update(2); }