@Override public void afterPropertiesSet() throws Exception { List<Consumer<Builder>> customizers = ImmutableList.<Consumer<Builder>>builder() .add(DEFAULT_DATABASE_CONFIGURATION) .addAll(databaseCustomizers) .add(FORCED_DATABASE_CONFIGURATION) .build(); PreparedDbProvider provider = PreparedDbProvider.forPreparer(EmptyDatabasePreparer.INSTANCE, customizers); PGSimpleDataSource dataSource = provider.createDataSource().unwrap(PGSimpleDataSource.class); EmbeddedDatabaseReporter.reportDataSource(dataSource); Semaphore semaphore = CONNECTION_SEMAPHORES.get(dataSource.getPortNumber()); this.dataSource = new BlockingDataSourceWrapper(dataSource, semaphore); }
@Test public void primaryDataSourceShouldBeReplaced() throws Exception { assertThat(dataSource.unwrap(PGSimpleDataSource.class).getPortNumber()).isEqualTo(33333); } }
@Override public synchronized ListenableFuture<DataSource> reload(Flyway flyway) { Executor executor = bootstrapExecutor != null ? bootstrapExecutor : Runnable::run; List<Consumer<Builder>> customizers = ImmutableList.<Consumer<Builder>>builder() .add(DEFAULT_DATABASE_CONFIGURATION) .addAll(databaseCustomizers) .add(FORCED_DATABASE_CONFIGURATION) .build(); CompletableFuture<DataSource> reloadFuture = dataSourceFuture.thenApplyAsync(x -> { for (int current = 1; current <= maxAttempts; current++) { try { FlywayDatabasePreparer preparer = new FlywayDatabasePreparer(flyway); PreparedDbProvider provider = PreparedDbProvider.forPreparer(preparer, customizers); PGSimpleDataSource dataSource = provider.createDataSource().unwrap(PGSimpleDataSource.class); Semaphore semaphore = CONNECTION_SEMAPHORES.get(dataSource.getPortNumber()); return new BlockingDataSourceWrapper(dataSource, semaphore); } catch (Exception e) { if (ExceptionUtils.indexOfType(e, IOException.class) == -1 || current == maxAttempts) { throw new CompletionException(e); } } } throw new IllegalStateException("maxAttempts parameter must be greater or equal to 1"); }, executor); // main data source future must never fail, otherwise all following tests will fail dataSourceFuture = reloadFuture.exceptionally(throwable -> null); return new CompletableToListenableFutureAdapter<>(reloadFuture); }