private void sweepUntilConditionSatisfied(BooleanSupplier predicate) { Awaitility.waitAtMost(30, TimeUnit.SECONDS) .pollInterval(1, TimeUnit.SECONDS) .until(() -> { todoClient.runIterationOfTargetedSweep(); return predicate.getAsBoolean(); }); }
@Test public void shutdownBlocksWhenTaskIsRunningUntilCleanupIsDone() { SlowCallback slowCallback = new SlowCallback(); long start = System.currentTimeMillis(); AtomicBoolean started = new AtomicBoolean(false); PTExecutors.newSingleThreadScheduledExecutor().submit(() -> slowCallback.runWithRetry(started)); Awaitility.waitAtMost(500L, TimeUnit.MILLISECONDS).until(started::get); slowCallback.blockUntilSafeToShutdown(); assertThat(System.currentTimeMillis()).isGreaterThanOrEqualTo(start + 2000L); }
@Test public void callbackCanCallTmMethodsEvenThoughTmStillThrows() { everythingInitialized(); ClusterAvailabilityStatusBlockingCallback blockingCallback = new ClusterAvailabilityStatusBlockingCallback(); manager = getManagerWithCallback(true, blockingCallback, executorService); ExecutorService tickerThread = PTExecutors.newSingleThreadExecutor(true); tickerThread.submit(() -> executorService.tick(1000, TimeUnit.MILLISECONDS)); Awaitility.waitAtMost(THREE, TimeUnit.SECONDS).until(blockingCallback::wasInvoked); verify(mockKvs, atLeast(1)).getClusterAvailabilityStatus(); assertThatThrownBy(manager::getKeyValueServiceStatus).isInstanceOf(NotInitializedException.class); blockingCallback.stopBlocking(); Awaitility.waitAtMost(THREE, TimeUnit.SECONDS).until(manager::isInitialized); assertThat(manager.getKeyValueServiceStatus()).isEqualTo(KeyValueServiceStatus.HEALTHY_ALL_OPERATIONS); tickerThread.shutdown(); }
@Test public void backgroundThoroughSweepDeletesOldVersion() throws InterruptedException { long ts = todoClient.addTodoWithIdAndReturnTimestamp(100L, TODO); assertThat(todoClient.doesNotExistBeforeTimestamp(100L, ts)).isFalse(); todoClient.addTodoWithIdAndReturnTimestamp(100L, TODO); Awaitility.waitAtMost(2, TimeUnit.MINUTES).pollInterval(2, TimeUnit.SECONDS) .until(() -> todoClient.doesNotExistBeforeTimestamp(100L, ts)); }
@Test public void callbackBlocksInitializationUntilDone() { everythingInitialized(); ClusterAvailabilityStatusBlockingCallback blockingCallback = new ClusterAvailabilityStatusBlockingCallback(); manager = getManagerWithCallback(true, blockingCallback, executorService); ExecutorService tickerThread = PTExecutors.newSingleThreadExecutor(true); tickerThread.submit(() -> executorService.tick(1000, TimeUnit.MILLISECONDS)); Awaitility.waitAtMost(THREE, TimeUnit.SECONDS).until(blockingCallback::wasInvoked); assertFalse(manager.isInitialized()); blockingCallback.stopBlocking(); Awaitility.waitAtMost(THREE, TimeUnit.SECONDS).until(manager::isInitialized); tickerThread.shutdown(); }
private static void killCassandraContainer(String containerName) throws IOException, InterruptedException { CONTAINERS.getContainer(containerName).kill(); DockerPort containerPort = new DockerPort(containerName, CASSANDRA_THRIFT_PORT, CASSANDRA_THRIFT_PORT); Awaitility.waitAtMost(10, TimeUnit.SECONDS).pollInterval(2, TimeUnit.SECONDS).until( () -> !containerPort.isListeningNow()); } }
default void waitForPortsFile(final Path dataDir) { final File file = new File(dataDir.toFile(), "pantheon.ports"); Awaitility.waitAtMost(30, TimeUnit.SECONDS) .until( () -> { if (file.exists()) { try (final Stream<String> s = Files.lines(file.toPath())) { return s.count() > 0; } } else { return false; } }); } }
private void killPantheonProcess(final String name, final Process process) { LOG.info("Killing " + name + " process"); Awaitility.waitAtMost(30, TimeUnit.SECONDS) .until( () -> { if (process.isAlive()) { process.destroy(); pantheonProcesses.remove(name); return false; } else { pantheonProcesses.remove(name); return true; } }); } }
private boolean tryReadFromSocket() throws IOException, InterruptedException { final MutableInt status = new MutableInt(0); Awaitility .waitAtMost(Duration.ONE_MINUTE) .pollDelay(new Duration(10, TimeUnit.MILLISECONDS)) .until(() -> { int read = socket.read(readBuffer); status.setValue(read); return read != 0; }); if (status.intValue() == -1) { monitor.debug("Error reading, got -1"); return false; } return true; }
private void tryPublishWithRetry(byte[] bytes) { Awaitility.waitAtMost(Duration.ONE_MINUTE).pollInterval(Duration.ONE_SECOND).until(() -> tryPublish(bytes)); }
@Test public void shouldRunReactively() { AtomicInteger counter = new AtomicInteger(); for (int i = 0; i < CALLS_NUMBER; i++) { client.mirrorBodyWithDelay("testBody") .doOnNext(order -> counter.incrementAndGet()) .subscribe(); } waitAtMost(new Duration(timeToCompleteReactively(), TimeUnit.MILLISECONDS)) .until(() -> counter.get() == CALLS_NUMBER); }
@Test public void shouldRunReactively() throws JsonProcessingException { IceCreamOrder orderGenerated = new OrderGenerator().generate(1); String orderStr = TestUtils.MAPPER.writeValueAsString(orderGenerated); wireMockRule.stubFor(get(urlEqualTo("/icecream/orders/1")) .withHeader("Accept", equalTo("application/json")) .willReturn(aResponse().withStatus(200) .withHeader("Content-Type", "application/json") .withBody(orderStr) .withFixedDelay(DELAY_IN_MILLIS))); IcecreamServiceApi client = builder() .target(IcecreamServiceApi.class, "http://localhost:" + wireMockRule.port()); AtomicInteger counter = new AtomicInteger(); new Thread(() -> { for (int i = 0; i < CALLS_NUMBER; i++) { client.findFirstOrder() .doOnNext(order -> counter.incrementAndGet()) .subscribe(); } }).start(); waitAtMost(new Duration(timeToCompleteReactively(), TimeUnit.MILLISECONDS)) .until(() -> counter.get() == CALLS_NUMBER); }