/** * Convenience method to return a WaitStrategy for an exposed or mapped port. * * @return the WaitStrategy * @see HostPortWaitStrategy */ public static HostPortWaitStrategy forListeningPort() { return new HostPortWaitStrategy(); }
@Override public WaitStrategy withStartupTimeout(Duration startupTimeout) { delegateStrategy.withStartupTimeout(startupTimeout); return super.withStartupTimeout(startupTimeout); } }
@Override protected void waitUntilReady() { final Set<Integer> externalLivenessCheckPorts = getLivenessCheckPorts(); if (externalLivenessCheckPorts.isEmpty()) { 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.getContainerIpAddress() + " ports: " + externalLivenessCheckPorts + " should be listening)"); } }
private static SwarmGenericContainer fakeSmtpContainer() { return new SwarmGenericContainer(Images.FAKE_SMTP) .withAffinityToContainer() .waitingFor(new HostPortWaitStrategy() .withRateLimiter(RateLimiters.TWENTIES_PER_SECOND)); }
@Override protected void waitUntilReady() { delegateStrategy.waitUntilReady(this.waitStrategyTarget); }
private SwarmGenericContainer createContainer() { return new SwarmGenericContainer( new ImageFromDockerfile() .withFileFromClasspath("populate.ldif", "ldif-files/populate.ldif") .withFileFromClasspath("Dockerfile", "ldif-files/Dockerfile")) .withAffinityToContainer() .withEnv("SLAPD_DOMAIN", domain) .withEnv("SLAPD_PASSWORD", password) .withEnv("SLAPD_CONFIG_PASSWORD", password) .withExposedPorts(LdapGenericContainer.DEFAULT_LDAP_PORT) .waitingFor(new HostPortWaitStrategy().withRateLimiter(RateLimiters.TWENTIES_PER_SECOND)); } }
@Override protected void waitUntilReady() { delegateStrategy.waitUntilReady(this.waitStrategyTarget); }
/** * Convenience method to return a WaitStrategy for an exposed or mapped port. * * @return the WaitStrategy * @see HostPortWaitStrategy */ public static HostPortWaitStrategy forListeningPort() { return new HostPortWaitStrategy(); }
@Override protected void waitUntilReady() { final Set<Integer> externalLivenessCheckPorts = getLivenessCheckPorts(); if (externalLivenessCheckPorts.isEmpty()) { 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.getContainerIpAddress() + " ports: " + externalLivenessCheckPorts + " should be listening)"); } }
@Test public void testWaitOnOneOfMultipleStrategiesFailing() { final DockerComposeContainer environment = new DockerComposeContainer(new File("src/test/resources/compose-test.yml")) .withExposedService("redis_1", REDIS_PORT, Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(10))) .waitingFor("db_1", Wait.forLogMessage(".*test test test.*\\s", 1).withStartupTimeout(Duration.ofSeconds(10))) .withTailChildContainers(true); VisibleAssertions.assertThrows("waiting on one failing strategy to time out", RuntimeException.class, () -> environment.starting(Description.createTestDescription(Object.class, "name"))); }
private static WaitStrategy getCompositeWaitStrategy(ElasticSearchProperties properties) { WaitAllStrategy strategy = new WaitAllStrategy() .withStrategy(new HostPortWaitStrategy()); properties.indices.forEach(index -> strategy.withStrategy(new CreateIndex(properties, index))); return strategy .withStrategy(new WaitForGreenStatus(properties)) .withStartupTimeout(DEFAULT_CONTAINER_WAIT_DURATION); } }
@Override public WaitStrategy withStartupTimeout(Duration startupTimeout) { delegateStrategy.withStartupTimeout(startupTimeout); return super.withStartupTimeout(startupTimeout); } }
@Bean(name = DynamoDBProperties.BEAN_NAME_EMBEDDED_DYNAMODB, destroyMethod = "stop") public GenericContainer mariadb(ConfigurableEnvironment environment, DynamoDBProperties properties) throws Exception { log.info("Starting mariadb server. Docker image: {}", properties.dockerImage); GenericContainer mariadb = new GenericContainer(properties.dockerImage) .withLogConsumer(containerLogsConsumer(log)) .withExposedPorts(properties.port) .waitingFor(new HostPortWaitStrategy()) .withStartupTimeout(properties.getTimeoutDuration()); mariadb.start(); registerDynamodbEnvironment(mariadb, environment, properties); return mariadb; }
@Bean(name = AEROSPIKE_BEAN_NAME, destroyMethod = "stop") public GenericContainer aerospike(AerospikeWaitStrategy aerospikeWaitStrategy, ConfigurableEnvironment environment, AerospikeProperties properties) { log.info("Starting aerospike server. Docker image: {}", properties.dockerImage); WaitStrategy waitStrategy = new WaitAllStrategy() .withStrategy(aerospikeWaitStrategy) .withStrategy(new HostPortWaitStrategy()) .withStartupTimeout(Duration.of(60, SECONDS)); GenericContainer aerospike = new GenericContainer<>(properties.dockerImage) .withExposedPorts(properties.port) .withLogConsumer(containerLogsConsumer(log)) // see https://github.com/aerospike/aerospike-server.docker/blob/master/aerospike.template.conf .withEnv("NAMESPACE", properties.namespace) .withEnv("SERVICE_PORT", String.valueOf(properties.port)) .withEnv("MEM_GB", String.valueOf(1)) .withEnv("STORAGE_GB", String.valueOf(1)) .withCreateContainerCmdModifier(cmd -> cmd.withCapAdd(Capability.NET_ADMIN)) .waitingFor(waitStrategy) .withStartupTimeout(properties.getTimeoutDuration()); aerospike.start(); registerAerospikeEnvironment(aerospike, environment, properties); return aerospike; }