Skip to content

Commit

Permalink
Using LogMessageWaitStrategy for VncRecordingContainer (testcontainer…
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Rempfer committed Apr 11, 2020
1 parent 21ba448 commit e911925
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 40 deletions.
@@ -1,24 +1,18 @@
package org.testcontainers.containers;

import com.github.dockerjava.api.model.Frame;
import lombok.Getter;
import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.ToString;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.rnorth.ducttape.TimeoutException;
import org.rnorth.ducttape.unreliables.Unreliables;
import org.testcontainers.containers.output.FrameConsumerResultCallback;
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;
import org.testcontainers.utility.TestcontainersConfiguration;

import java.io.Closeable;
import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Base64;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
* 'Sidekick container' with the sole purpose of recording the VNC screen output from another container.
Expand Down Expand Up @@ -59,39 +53,7 @@ public VncRecordingContainer(@NonNull Network network, @NonNull String targetNet

this.targetNetworkAlias = targetNetworkAlias;
withNetwork(network);

waitingFor(new AbstractWaitStrategy() {

@Override
protected void waitUntilReady() {
try {
Unreliables.retryUntilTrue((int) startupTimeout.toMillis(), TimeUnit.MILLISECONDS, () -> {
CountDownLatch latch = new CountDownLatch(1);

FrameConsumerResultCallback callback = new FrameConsumerResultCallback() {
@Override
public void onNext(Frame frame) {
if (frame != null && new String(frame.getPayload()).contains("Connected")) {
latch.countDown();
}
}
};

try (
Closeable __ = dockerClient.logContainerCmd(getContainerId())
.withFollowStream(true)
.withSince(0)
.withStdErr(true)
.exec(callback)
) {
return latch.await(1, TimeUnit.SECONDS);
}
});
} catch (TimeoutException e) {
throw new ContainerLaunchException("Timed out waiting for log output", e);
}
}
});
waitingFor(new LogMessageWaitStrategy().withRegEx(".*Connected.*"));
}

public VncRecordingContainer withVncPassword(@NonNull String vncPassword) {
Expand Down
@@ -0,0 +1,33 @@
package org.testcontainers.containers;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

public class VncRecordingContainerTest {

private GenericContainer target;

@Before
public void initialize() {
target = new GenericContainer("selenium/standalone-chrome-debug").
withNetwork(Network.SHARED).
withExposedPorts(5900);
target.start();
}

@Test
public void verifyStartup() {
try (VncRecordingContainer container = new VncRecordingContainer(target)) {
container.start();
assertThat(container.isRunning()).isTrue();
}
}

@After
public void tearDown() {
target.stop();
}
}

0 comments on commit e911925

Please sign in to comment.