org.rnorth.ducttape.unreliables.Unreliables Java Examples
The following examples show how to use
org.rnorth.ducttape.unreliables.Unreliables.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CompositeTest.java From duct-tape with MIT License | 6 votes |
private String execute(ExampleService exampleService) throws Exception { return circuitBreaker.tryGet(() -> { return Unreliables.retryUntilSuccess(2, TimeUnit.SECONDS, () -> { return rateLimiter.getWhenReady(() -> { return Timeouts.getWithTimeout(100, TimeUnit.MILLISECONDS, () -> { return exampleService.getValue("Hello World"); }); }); }); }, () -> { LOGGER.error("Circuit breaker was tripped"); }, () -> { return "default value"; }); }
Example #2
Source File: CassandraStorageExtension.java From zipkin-dependencies with Apache License 2.0 | 6 votes |
@Override protected void waitUntilContainerStarted() { Unreliables.retryUntilSuccess(2, TimeUnit.MINUTES, () -> { if (!isRunning()) { throw new ContainerLaunchException("Container failed to start"); } InetSocketAddress address = new InetSocketAddress(getContainerIpAddress(), getMappedPort(9042)); try (Cluster cluster = getCluster(address); Session session = cluster.newSession()) { session.execute("SELECT now() FROM system.local"); logger().info("Obtained a connection to container ({})", cluster.getClusterName()); return null; // unused value } }); }
Example #3
Source File: CassandraStorageExtension.java From zipkin-dependencies with Apache License 2.0 | 6 votes |
@Override protected void waitUntilContainerStarted() { Unreliables.retryUntilSuccess(2, TimeUnit.MINUTES, () -> { if (!isRunning()) { throw new ContainerLaunchException("Container failed to start"); } InetSocketAddress address = new InetSocketAddress(getContainerIpAddress(), getMappedPort(9042)); try (Cluster cluster = getCluster(address); Session session = cluster.newSession()) { session.execute("SELECT now() FROM system.local"); logger().info("Obtained a connection to container ({})", cluster.getClusterName()); return null; // unused value } }); }
Example #4
Source File: GenericContainerTest.java From testcontainers-java with MIT License | 6 votes |
@Override @SneakyThrows protected void waitUntilReady() { Unreliables.retryUntilTrue(5, TimeUnit.SECONDS, () -> { ContainerState state = waitStrategyTarget.getCurrentContainerInfo().getState(); log.debug("Current state: {}", state); if (!"exited".equalsIgnoreCase(state.getStatus())) { Thread.sleep(100); return false; } return predicate.test(state); }); throw new IllegalStateException("Nope!"); }
Example #5
Source File: StartupCheckStrategy.java From testcontainers-java with MIT License | 6 votes |
public boolean waitUntilStartupSuccessful(DockerClient dockerClient, String containerId) { final Boolean[] startedOK = {null}; Unreliables.retryUntilTrue((int) timeout.toMillis(), TimeUnit.MILLISECONDS, () -> { //noinspection CodeBlock2Expr return DOCKER_CLIENT_RATE_LIMITER.getWhenReady(() -> { StartupStatus state = checkStartupState(dockerClient, containerId); switch (state) { case SUCCESSFUL: startedOK[0] = true; return true; case FAILED: startedOK[0] = false; return true; default: return false; } }); }); return startedOK[0]; }
Example #6
Source File: GenericContainer.java From testcontainers-java with MIT License | 6 votes |
protected void doStart() { try { configure(); Instant startedAt = Instant.now(); logger().debug("Starting container: {}", getDockerImageName()); AtomicInteger attempt = new AtomicInteger(0); Unreliables.retryUntilSuccess(startupAttempts, () -> { logger().debug("Trying to start container: {} (attempt {}/{})", getDockerImageName(), attempt.incrementAndGet(), startupAttempts); tryStart(startedAt); return true; }); } catch (Exception e) { throw new ContainerLaunchException("Container startup failed", e); } }
Example #7
Source File: GenericContainerRuleTest.java From testcontainers-java with MIT License | 5 votes |
@Test public void simpleRabbitMqTest() throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(rabbitMq.getHost()); factory.setPort(rabbitMq.getMappedPort(RABBITMQ_PORT)); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(RABBIQMQ_TEST_EXCHANGE, "direct", true); String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, RABBIQMQ_TEST_EXCHANGE, RABBITMQ_TEST_ROUTING_KEY); // Set up a consumer on the queue final boolean[] messageWasReceived = new boolean[1]; channel.basicConsume(queueName, false, new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { messageWasReceived[0] = Arrays.equals(body, RABBITMQ_TEST_MESSAGE.getBytes()); } }); // post a message channel.basicPublish(RABBIQMQ_TEST_EXCHANGE, RABBITMQ_TEST_ROUTING_KEY, null, RABBITMQ_TEST_MESSAGE.getBytes()); // check the message was received assertTrue("The message was received", Unreliables.retryUntilSuccess(5, TimeUnit.SECONDS, () -> { if (!messageWasReceived[0]) { throw new IllegalStateException("Message not received yet"); } return true; })); }
Example #8
Source File: SpamAssassinWaitStrategy.java From james-project with Apache License 2.0 | 5 votes |
@Override public void waitUntilReady(WaitStrategyTarget waitStrategyTarget) { Unreliables.retryUntilTrue(Ints.checkedCast(timeout.getSeconds()), TimeUnit.SECONDS, () -> { try { return spamAssassinContainer .execInContainer("spamassassin", "-V") .getStdout() .contains("SpamAssassin version 3.4.2"); } catch (IOException | InterruptedException e) { return false; } } ); }
Example #9
Source File: CassandraWaitStrategy.java From james-project with Apache License 2.0 | 5 votes |
@Override public void waitUntilReady(WaitStrategyTarget waitStrategyTarget) { Unreliables.retryUntilTrue(Ints.checkedCast(timeout.getSeconds()), TimeUnit.SECONDS, () -> { try { return cassandraContainer .execInContainer("cqlsh", "-u", "cassandra", "-p", "cassandra", "-e", "show host") .getStdout() .contains("Connected to Test Cluster"); } catch (IOException | InterruptedException e) { return false; } } ); }
Example #10
Source File: InternalCommandPortListeningCheckTest.java From testcontainers-java with MIT License | 5 votes |
@Test public void nonListening() { final InternalCommandPortListeningCheck check = new InternalCommandPortListeningCheck(container, ImmutableSet.of(8080, 1234)); try { Unreliables.retryUntilTrue(5, TimeUnit.SECONDS, check); } catch (TimeoutException e) { // we expect it to timeout } final Boolean result = check.call(); assertFalse("InternalCommandPortListeningCheck detects a non-listening port among many", result); }
Example #11
Source File: DockerComposeOverridesTest.java From testcontainers-java with MIT License | 5 votes |
@Test public void test() { try (DockerComposeContainer compose = new DockerComposeContainer(composeFiles) .withLocalCompose(localMode) .withExposedService(SERVICE_NAME, SERVICE_PORT)) { compose.start(); BufferedReader br = Unreliables.retryUntilSuccess(10, TimeUnit.SECONDS, () -> { Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); Socket socket = new Socket(compose.getServiceHost(SERVICE_NAME, SERVICE_PORT), compose.getServicePort(SERVICE_NAME, SERVICE_PORT)); return new BufferedReader(new InputStreamReader(socket.getInputStream())); }); Unreliables.retryUntilTrue(10, TimeUnit.SECONDS, () -> { while (br.ready()) { String line = br.readLine(); if (line.contains(expectedEnvVar)) { pass("Mapped environment variable was found"); return true; } } info("Mapped environment variable was not found yet - process probably not ready"); Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS); return false; }); } }
Example #12
Source File: FixedHostPortContainerTest.java From testcontainers-java with MIT License | 5 votes |
/** * Simple socket content reader from given container:port * * @param container to query * @param port to send request to * @return socket reader content * @throws IOException if any */ private String readResponse(GenericContainer container, Integer port) throws IOException { try ( final BufferedReader reader = Unreliables.retryUntilSuccess(10, TimeUnit.SECONDS, () -> { Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); final Socket socket = new Socket(container.getHost(), port); return new BufferedReader(new InputStreamReader(socket.getInputStream())); } ) ) { return reader.readLine(); } }
Example #13
Source File: DockerComposeContainerWithBuildTest.java From testcontainers-java with MIT License | 5 votes |
@Test public void performTest() { final File composeFile = new File("src/test/resources/compose-build-test/docker-compose.yml"); final AtomicReference<String> builtImageName = new AtomicReference<>(""); final AtomicReference<String> pulledImageName = new AtomicReference<>(""); try (DockerComposeContainer environment = new DockerComposeContainer<>(composeFile) .withExposedService("customredis", 6379) .withBuild(true) .withRemoveImages(removeMode)) { environment.start(); builtImageName.set(imageNameForRunningContainer("_customredis_1")); final boolean isBuiltImagePresentWhileRunning = isImagePresent(builtImageName.get()); assertEquals("the built image is present while running", true, isBuiltImagePresentWhileRunning); pulledImageName.set(imageNameForRunningContainer("_normalredis_1")); final boolean isPulledImagePresentWhileRunning = isImagePresent(pulledImageName.get()); assertEquals("the pulled image is present while running", true, isPulledImagePresentWhileRunning); } Unreliables.retryUntilSuccess(10, TimeUnit.SECONDS, () -> { final boolean isBuiltImagePresentAfterRunning = isImagePresent(builtImageName.get()); assertEquals("the built image is not present after running", shouldBuiltImageBePresentAfterRunning, isBuiltImagePresentAfterRunning); return null; }); Unreliables.retryUntilSuccess(10, TimeUnit.SECONDS, () -> { final boolean isPulledImagePresentAfterRunning = isImagePresent(pulledImageName.get()); assertEquals("the pulled image is present after running", shouldPulledImageBePresentAfterRunning, isPulledImagePresentAfterRunning); return null; }); }
Example #14
Source File: GenericContainerRuleTest.java From testcontainers-java with MIT License | 5 votes |
private BufferedReader getReaderForContainerPort80(GenericContainer container) { return Unreliables.retryUntilSuccess(10, TimeUnit.SECONDS, () -> { Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); Socket socket = new Socket(container.getHost(), container.getFirstMappedPort()); return new BufferedReader(new InputStreamReader(socket.getInputStream())); }); }
Example #15
Source File: KafkaConnectConverterIT.java From apicurio-registry with Apache License 2.0 | 5 votes |
private <T> List<ConsumerRecord<T, T>> drain(KafkaConsumer<T, T> consumer, int expectedRecordCount) { LOGGER.info("Waiting for consumer to receive {} records", expectedRecordCount); List<ConsumerRecord<T, T>> allRecords = new ArrayList<>(); Unreliables.retryUntilTrue(20, TimeUnit.SECONDS, () -> { consumer.poll(Duration.ofMillis(50)) .iterator() .forEachRemaining(allRecords::add); return allRecords.size() == expectedRecordCount; }); LOGGER.info("All {} records received", expectedRecordCount); return allRecords; }
Example #16
Source File: DockerClientProviderStrategy.java From testcontainers-java with MIT License | 5 votes |
protected void ping(DockerClient client, int timeoutInSeconds) { try { Unreliables.retryUntilSuccess(timeoutInSeconds, TimeUnit.SECONDS, () -> { return PING_RATE_LIMITER.getWhenReady(() -> { LOGGER.debug("Pinging docker daemon..."); client.pingCmd().exec(); return true; }); }); } catch (TimeoutException e) { IOUtils.closeQuietly(client); throw e; } }
Example #17
Source File: DockerHealthcheckWaitStrategy.java From testcontainers-java with MIT License | 5 votes |
@Override protected void waitUntilReady() { try { Unreliables.retryUntilTrue((int) startupTimeout.getSeconds(), TimeUnit.SECONDS, waitStrategyTarget::isHealthy); } catch (TimeoutException e) { throw new ContainerLaunchException("Timed out waiting for container to become healthy"); } }
Example #18
Source File: HostPortWaitStrategy.java From testcontainers-java with MIT License | 5 votes |
@Override protected void waitUntilReady() { final Set<Integer> externalLivenessCheckPorts = getLivenessCheckPorts(); if (externalLivenessCheckPorts.isEmpty()) { if (log.isDebugEnabled()) { log.debug("Liveness check ports of {} is empty. Not waiting.", waitStrategyTarget.getContainerInfo().getName()); } return; } @SuppressWarnings("unchecked") List<Integer> exposedPorts = waitStrategyTarget.getExposedPorts(); final Set<Integer> internalPorts = getInternalPorts(externalLivenessCheckPorts, exposedPorts); Callable<Boolean> internalCheck = new InternalCommandPortListeningCheck(waitStrategyTarget, internalPorts); Callable<Boolean> externalCheck = new ExternalPortListeningCheck(waitStrategyTarget, externalLivenessCheckPorts); try { Unreliables.retryUntilTrue((int) startupTimeout.getSeconds(), TimeUnit.SECONDS, () -> getRateLimiter().getWhenReady(() -> internalCheck.call() && externalCheck.call())); } catch (TimeoutException e) { throw new ContainerLaunchException("Timed out waiting for container port to open (" + waitStrategyTarget.getHost() + " ports: " + externalLivenessCheckPorts + " should be listening)"); } }
Example #19
Source File: BrowserWebDriverContainer.java From testcontainers-java with MIT License | 5 votes |
@Override protected void containerIsStarted(InspectContainerResponse containerInfo) { driver = Unreliables.retryUntilSuccess(30, TimeUnit.SECONDS, Timeouts.getWithTimeout(10, TimeUnit.SECONDS, () -> () -> new RemoteWebDriver(getSeleniumAddress(), capabilities))); if (vncRecordingContainer != null) { LOGGER.debug("Starting VNC recording"); vncRecordingContainer.start(); } }
Example #20
Source File: CassandraContainer.java From spark-dependencies with Apache License 2.0 | 5 votes |
@Override protected void waitUntilContainerStarted() { Unreliables.retryUntilSuccess(120, TimeUnit.SECONDS, () -> { if (!isRunning()) { throw new ContainerLaunchException("Container failed to start"); } try (Cluster cluster = getCluster(); Session session = cluster.newSession()) { session.execute("SELECT now() FROM system.local"); logger().info("Obtained a connection to container ({})", cluster.getClusterName()); return null; // unused value } }); }
Example #21
Source File: KafkaContainerTest.java From testcontainers-java with MIT License | 4 votes |
protected void testKafkaFunctionality(String bootstrapServers) throws Exception { try ( KafkaProducer<String, String> producer = new KafkaProducer<>( ImmutableMap.of( ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers, ProducerConfig.CLIENT_ID_CONFIG, UUID.randomUUID().toString() ), new StringSerializer(), new StringSerializer() ); KafkaConsumer<String, String> consumer = new KafkaConsumer<>( ImmutableMap.of( ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers, ConsumerConfig.GROUP_ID_CONFIG, "tc-" + UUID.randomUUID(), ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest" ), new StringDeserializer(), new StringDeserializer() ); ) { String topicName = "messages"; consumer.subscribe(Arrays.asList(topicName)); producer.send(new ProducerRecord<>(topicName, "testcontainers", "rulezzz")).get(); Unreliables.retryUntilTrue(10, TimeUnit.SECONDS, () -> { ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100)); if (records.isEmpty()) { return false; } assertThat(records) .hasSize(1) .extracting(ConsumerRecord::topic, ConsumerRecord::key, ConsumerRecord::value) .containsExactly(tuple(topicName, "testcontainers", "rulezzz")); return true; }); consumer.unsubscribe(); } }
Example #22
Source File: BaseClickHouseTest.java From beam with Apache License 2.0 | 4 votes |
@BeforeClass public static void setup() throws IOException, InterruptedException { // network sharing doesn't work with ClassRule network = Network.newNetwork(); zookeeper = new GenericContainer<>("zookeeper:3.4.13") .withStartupAttempts(10) .withExposedPorts(2181) .withNetwork(network) .withNetworkAliases("zookeeper"); // so far zookeeper container always starts successfully, so no extra retries zookeeper.start(); clickHouse = (ClickHouseContainer) new ClickHouseContainer(CLICKHOUSE_IMAGE) .withStartupAttempts(10) .withCreateContainerCmdModifier( // type inference for `(CreateContainerCmd) -> cmd.` doesn't work cmd -> ((CreateContainerCmd) cmd) .withMemory(256 * 1024 * 1024L) .withMemorySwap(4L * 1024 * 1024 * 1024L)) .withNetwork(network) .withClasspathResourceMapping( "config.d/zookeeper_default.xml", "/etc/clickhouse-server/config.d/zookeeper_default.xml", BindMode.READ_ONLY); BackOff backOff = FluentBackoff.DEFAULT .withMaxRetries(3) .withInitialBackoff(Duration.standardSeconds(15)) .backoff(); // try to start clickhouse-server a couple of times, see BEAM-6639 while (true) { try { Unreliables.retryUntilSuccess( 10, () -> { DockerClientFactory.instance() .checkAndPullImage(DockerClientFactory.instance().client(), CLICKHOUSE_IMAGE); return null; }); clickHouse.start(); break; } catch (Exception e) { if (!BackOffUtils.next(Sleeper.DEFAULT, backOff)) { throw e; } else { List<Image> images = DockerClientFactory.instance().client().listImagesCmd().withShowAll(true).exec(); String listImagesOutput = "listImagesCmd:\n" + Joiner.on('\n').join(images) + "\n"; LOG.warn("failed to start clickhouse-server\n\n" + listImagesOutput, e); } } } }
Example #23
Source File: RabbitMQWaitStrategy.java From james-project with Apache License 2.0 | 4 votes |
@Override public void waitUntilReady(WaitStrategyTarget waitStrategyTarget) { int seconds = Ints.checkedCast(this.timeout.getSeconds()); Unreliables.retryUntilTrue(seconds, TimeUnit.SECONDS, this::isConnected); }
Example #24
Source File: Inconsistents.java From duct-tape with MIT License | 4 votes |
/** * Retry invocation of a supplier repeatedly until it returns a consistent result for a sufficient time period. * * This is intended for calls to components that take an unknown amount of time to stabilise, and where * repeated checks are the only way to detect that a stable state has been reached. * * @param consistentTime how long the result should be consistent for before it is returned * @param totalTimeout how long in total to wait for stabilisation to occur * @param timeUnit time unit for time intervals * @param lambda an UnreliableSupplier which should be called * @param <T> the return type of the UnreliableSupplier * @return the result of the supplier if it returned a consistent result for the specified interval */ public static <T> T retryUntilConsistent(final int consistentTime, final int totalTimeout, @NotNull final TimeUnit timeUnit, @NotNull final Callable<T> lambda) { check("consistent time must be greater than 0", consistentTime > 0); check("total timeout must be greater than 0", totalTimeout > 0); long start = System.currentTimeMillis(); Object[] recentValue = {null}; long[] firstRecentValueTime = {0}; long[] bestRun = {0}; Object[] bestRunValue = {null}; long consistentTimeInMillis = TimeUnit.MILLISECONDS.convert(consistentTime, timeUnit); return Unreliables.retryUntilSuccess(totalTimeout, timeUnit, () -> { T value = lambda.call(); boolean valueIsSame = value == recentValue[0] || (value != null && value.equals(recentValue[0])); if (valueIsSame) { long now = System.currentTimeMillis(); long timeSinceFirstValue = now - firstRecentValueTime[0]; if (timeSinceFirstValue > bestRun[0]) { bestRun[0] = timeSinceFirstValue; bestRunValue[0] = value; } if (timeSinceFirstValue > consistentTimeInMillis) { return value; } } else { // Reset everything and see if the next call yields the same result as this time recentValue[0] = value; firstRecentValueTime[0] = System.currentTimeMillis(); } long timeSinceStart = System.currentTimeMillis() - start; if (bestRun[0] > 0) { throw new InconsistentResultsException(timeSinceStart, bestRunValue[0], bestRun[0]); } else { throw new ResultsNeverConsistentException(timeSinceStart); } }); }
Example #25
Source File: JaegerTestDriverContainer.java From spark-dependencies with Apache License 2.0 | 4 votes |
@Override protected void waitUntilContainerStarted() { String statusUrl = String.format("http://localhost:%d/", this.getMappedPort(8080)); Unreliables.retryUntilTrue((int)waitUntilReady.toMillis(), TimeUnit.MILLISECONDS, containerStartedCondition(statusUrl)); }
Example #26
Source File: InternalCommandPortListeningCheckTest.java From testcontainers-java with MIT License | 3 votes |
@Test public void singleListening() { final InternalCommandPortListeningCheck check = new InternalCommandPortListeningCheck(container, ImmutableSet.of(8080)); Unreliables.retryUntilTrue(5, TimeUnit.SECONDS, check); final Boolean result = check.call(); assertTrue("InternalCommandPortListeningCheck identifies a single listening port", result); }
Example #27
Source File: InternalCommandPortListeningCheckTest.java From testcontainers-java with MIT License | 3 votes |
@Test public void lowAndHighPortListening() { final InternalCommandPortListeningCheck check = new InternalCommandPortListeningCheck(container, ImmutableSet.of(100, 8080)); Unreliables.retryUntilTrue(5, TimeUnit.SECONDS, check); final Boolean result = check.call(); assertTrue("InternalCommandPortListeningCheck identifies a low and a high port", result); }