Java Code Examples for org.rnorth.ducttape.unreliables.Unreliables#retryUntilSuccess()

The following examples show how to use org.rnorth.ducttape.unreliables.Unreliables#retryUntilSuccess() . 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: CassandraStorageExtension.java    From zipkin-dependencies with Apache License 2.0 6 votes vote down vote up
@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 2
Source File: CassandraStorageExtension.java    From zipkin-dependencies with Apache License 2.0 6 votes vote down vote up
@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: GenericContainer.java    From testcontainers-java with MIT License 6 votes vote down vote up
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 4
Source File: CassandraContainer.java    From spark-dependencies with Apache License 2.0 5 votes vote down vote up
@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 5
Source File: BrowserWebDriverContainer.java    From testcontainers-java with MIT License 5 votes vote down vote up
@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 6
Source File: DockerClientProviderStrategy.java    From testcontainers-java with MIT License 5 votes vote down vote up
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 7
Source File: GenericContainerRuleTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
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 8
Source File: DockerComposeContainerWithBuildTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@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 9
Source File: FixedHostPortContainerTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
/**
 * 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 10
Source File: DockerComposeOverridesTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@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 11
Source File: BaseClickHouseTest.java    From beam with Apache License 2.0 4 votes vote down vote up
@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 12
Source File: Inconsistents.java    From duct-tape with MIT License 4 votes vote down vote up
/**
 * 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);
        }
    });
}