Best Testcontainers-java code snippet using org.testcontainers.containers.output.FrameConsumerResultCallback
Source:DockerTestServerController.java
...5import org.slf4j.Logger;6import org.slf4j.LoggerFactory;7import org.testcontainers.containers.Container;8import org.testcontainers.containers.GenericContainer;9import org.testcontainers.containers.output.FrameConsumerResultCallback;10import org.testcontainers.containers.output.OutputFrame;11import org.testcontainers.containers.output.Slf4jLogConsumer;12import org.testcontainers.containers.wait.strategy.Wait;13import org.testcontainers.images.builder.ImageFromDockerfile;14import java.io.IOException;15import java.time.LocalDateTime;16import java.util.Arrays;17import java.util.List;18import java.util.concurrent.TimeUnit;19public class DockerTestServerController extends TestServerController {20 private static final Logger logger = LoggerFactory.getLogger(DockerTestServerController.class);21 private static final String TEST_SERVER_IMAGE_NAME = "dedis/conode-test:latest";22 private static final String TEMPORARY_DOCKER_IMAGE = "conode-test-run";23 private final GenericContainer<?> blockchainContainer;24 DockerTestServerController() {25 super();26 logger.warn("local docker will be started for tests.");27 logger.info("This test run assumes that image " + TEST_SERVER_IMAGE_NAME + " is available in your system.");28 logger.info("To build this image you should run `make docker` - this will create the test image with test " +29 "keys.");30 logger.info("For a test run this code will create additional docker image with name " + TEMPORARY_DOCKER_IMAGE +31 ", at the end this additional image will be automatically deleted");32 try {33 blockchainContainer = new GenericContainer<>(34 new ImageFromDockerfile(TEMPORARY_DOCKER_IMAGE, true)35 .withDockerfileFromBuilder(builder -> builder36 .from(TEST_SERVER_IMAGE_NAME)37 .expose(7770, 7771, 7772, 7773, 7774, 7775, 7776, 7777, 7778, 7779, 7780, 7781, 7782, 7783))38 );39 blockchainContainer.setPortBindings(Arrays.asList(40 "7770:7770", "7771:7771",41 "7772:7772", "7773:7773",42 "7774:7774", "7775:7775",43 "7776:7776", "7777:7777",44 "7778:7778", "7779:7779",45 "7780:7780", "7781:7781",46 "7782:7782", "7783:7783"));47 blockchainContainer.withExposedPorts(7770, 7771, 7772, 7773, 7774, 7775, 7776, 7777);48 blockchainContainer.waitingFor(Wait.forListeningPort());49 blockchainContainer.start();50 Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(logger);51 blockchainContainer.withLogConsumer(logConsumer);52 blockchainContainer.followOutput(logConsumer);53 logger.info("Started at {}", LocalDateTime.now());54 } catch (Exception e) {55 logger.info("Exception at {}", LocalDateTime.now());56 throw new IllegalStateException("Cannot start docker image with test server. Please ensure that local conodes are not running.", e);57 }58 }59 @Override60 public void startConode(int nodeNumber) throws InterruptedException {61 if (nodeNumber <= 0) {62 throw new InterruptedException("Node numbering starts at 1!");63 }64 logger.info("Starting container co{}/private.toml", nodeNumber);65 runCmdInBackgroundStd(blockchainContainer, "env", "COTHORITY_ALLOW_INSECURE_ADMIN=1", "DEBUG_TIME=true", "CONODE_SERVICE_PATH=.",66 "conode", "-d", "1", "-c", "co" + nodeNumber + "/private.toml", "server");67 // Wait a bit for the server to actually start.68 Thread.sleep(1000);69 }70 @Override71 public void killConode(int nodeNumber) throws IOException, InterruptedException {72 if (nodeNumber <= 0) {73 throw new InterruptedException("Node numbering starts at 1!");74 }75 logger.info("Killing container co{}/private.toml", nodeNumber);76 Container.ExecResult psResults = blockchainContainer.execInContainer("ps", "-o", "pid=,command=", "-C", "conode");77 for (String psLine : psResults.getStdout().split("\\n")) {78 if (psLine.contains("co" + nodeNumber + "/private.toml")) {79 String pid = psLine.trim().split("\\s")[0];80 blockchainContainer.execInContainer("kill", pid);81 break;82 }83 }84 }85 @Override86 public void cleanDBs() throws IOException, InterruptedException {87 logger.info("Removing all accumulated databases");88 // cannot use `rm *.db` here because there is no shell expansion done.89 Container.ExecResult psResults = blockchainContainer.execInContainer("find", ".", "-name", "*.db", "-exec", "rm", "-vf", "{}", ";");90 logger.info("DBs removed: {}", psResults.getStdout());91 }92 /**93 * We only get 4 conodes because the run_conode.sh file (from the Dockerfile) only starts 4 conodes.94 * The other conodes (5 to 7) are used for testing roster changes.95 */96 @Override97 public List<ServerIdentity> getConodes() {98 return getIdentities().subList(0, 4);99 }100 private void runCmdInBackground(GenericContainer container, String... cmd) throws InterruptedException {101 DockerClient dockerClient = container.getDockerClient();102 ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(container.getContainerId())103 .withAttachStdout(true)104 .withAttachStderr(true)105 .withAttachStdin(false)106 .withCmd(cmd)107 .exec();108 FrameConsumerResultCallback fc = new FrameConsumerResultCallback();109 Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(logger);110 fc.addConsumer(OutputFrame.OutputType.STDOUT, logConsumer);111 fc.addConsumer(OutputFrame.OutputType.STDERR, logConsumer);112 dockerClient.execStartCmd(execCreateCmdResponse.getId())113 .exec(fc).awaitStarted();114 }115 private void runCmdInBackgroundStd(GenericContainer container, String... cmd) throws InterruptedException {116 DockerClient dockerClient = container.getDockerClient();117 ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(container.getContainerId())118 .withAttachStdout(true)119 .withAttachStderr(true)120 .withAttachStdin(false)121 .withCmd(cmd)122 .exec();123 FrameConsumerResultCallback fc = new FrameConsumerResultCallback();124 Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(logger);125 fc.addConsumer(OutputFrame.OutputType.STDOUT, logConsumer);126 fc.addConsumer(OutputFrame.OutputType.STDERR, logConsumer);127 boolean started = dockerClient.execStartCmd(execCreateCmdResponse.getId())128 .exec(fc).awaitStarted(10, TimeUnit.SECONDS);129 if (!started){130 logger.error("Couldn't start node in time: {}", String.join(" ", cmd));131 }132 }133}...
Source:ContainerLogForwarder.java
...6import java.util.concurrent.atomic.AtomicBoolean;7import java.util.concurrent.atomic.AtomicLong;8import org.jboss.logging.Logger;9import org.testcontainers.DockerClientFactory;10import org.testcontainers.containers.output.FrameConsumerResultCallback;11import org.testcontainers.containers.output.OutputFrame;12import io.quarkus.deployment.dev.devservices.DevServiceDescriptionBuildItem;13public class ContainerLogForwarder implements Closeable {14 private final DevServiceDescriptionBuildItem devService;15 private final AtomicLong timestamp = new AtomicLong(0L);16 private final Logger logger;17 private final String shortId;18 private FrameConsumerResultCallback resultCallback;19 private final AtomicBoolean running = new AtomicBoolean(false);20 public ContainerLogForwarder(DevServiceDescriptionBuildItem devService) {21 this.devService = devService;22 this.logger = Logger.getLogger(devService.getName());23 this.shortId = devService.getContainerInfo().getShortId();24 }25 public DevServiceDescriptionBuildItem getDevService() {26 return devService;27 }28 public boolean isRunning() {29 return running.get();30 }31 public void start() {32 if (running.compareAndSet(false, true)) {33 this.resultCallback = new FrameConsumerResultCallback();34 this.resultCallback.addConsumer(STDOUT, frame -> {35 if (running.get())36 logger.infof("[%s] %s", shortId, updateTimestamp(frame));37 });38 this.resultCallback.addConsumer(STDERR, frame -> {39 if (running.get())40 logger.errorf("[%s] %s", shortId, updateTimestamp(frame));41 });42 DockerClientFactory.lazyClient().logContainerCmd(devService.getContainerInfo().getId())43 .withFollowStream(true)44 .withStdErr(true)45 .withStdOut(true)46 .withSince(timestamp.intValue())47 .exec(resultCallback);...
FrameConsumerResultCallback
Using AI Code Generation
1import org.testcontainers.containers.output.FrameConsumerResultCallback;2import org.testcontainers.containers.output.OutputFrame;3import org.testcontainers.containers.output.ToStringConsumer;4import org.testcontainers.containers.output.WaitingConsumer;5import org.testcontainers.containers.output.OutputFrame.OutputType;6public class TestcontainersConsumer {7 public static void main(String[] args) throws Exception {8 DockerContainer dc = new DockerContainer();9 dc.withCommand("sh", "-c", "echo 'hello'; echo 'world' >&2; sleep 1; echo 'bye'; exit 2");10 dc.start();11 FrameConsumerResultCallback callback = new FrameConsumerResultCallback();12 dc.followOutput(callback);13 callback.awaitCompletion();14 System.out.println(callback.toString());15 System.out.println(callback.getStdout());16 System.out.println(callback.getStderr());17 System.out.println(callback.getCombinedOutput());18 System.out.println(callback.getExitCode());19 }20}21import org.testcontainers.containers.output.WaitingConsumer;22import org.testcontainers.containers.output.OutputFrame.OutputType;23public class TestcontainersConsumer {24 public static void main(String[] args) throws Exception {25 DockerContainer dc = new DockerContainer();26 dc.withCommand("sh", "-c", "echo 'hello'; echo 'world' >&2; sleep 1; echo 'bye'; exit 2");27 dc.start();28 WaitingConsumer consumer = new WaitingConsumer();29 dc.followOutput(consumer);30 OutputFrame frame = consumer.waitUntil(frame -> frame.getUtf8String().contains("bye"));31 System.out.println(frame.getUtf8String());32 System.out.println(frame.getStreamType());33 System.out.println(frame.getTimestamp());34 System.out.println(frame.getType());35 }36}37import org.testcontainers.containers.output.ToStringConsumer;38import org.testcontainers.containers.output.OutputFrame.OutputType;39public class TestcontainersConsumer {40 public static void main(String[] args) throws Exception {41 DockerContainer dc = new DockerContainer();42 dc.withCommand("sh", "-c", "echo 'hello'; echo 'world' >&2; sleep 1; echo '
FrameConsumerResultCallback
Using AI Code Generation
1import org.testcontainers.containers.output.FrameConsumerResultCallback;2import org.testcontainers.containers.output.OutputFrame;3import org.testcontainers.containers.output.ToStringConsumer;4import org.testcontainers.containers.output.WaitingConsumer;5import java.util.concurrent.TimeUnit;6public class FrameConsumerResultCallbackDemo {7 public static void main(String[] args) throws InterruptedException {8 FrameConsumerResultCallback resultCallback = new FrameConsumerResultCallback();9 ToStringConsumer toStringConsumer = new ToStringConsumer();10 WaitingConsumer waitingConsumer = new WaitingConsumer();11 resultCallback.addConsumer(toStringConsumer);12 resultCallback.addConsumer(waitingConsumer);13 resultCallback.accept(new OutputFrame() {14 public Type getType() {15 return Type.STDOUT;16 }17 public String getUtf8String() {18 return "Hello World";19 }20 });21 resultCallback.accept(new OutputFrame() {22 public Type getType() {23 return Type.STDOUT;24 }25 public String getUtf8String() {26 return "Hello World";27 }28 });29 resultCallback.accept(new OutputFrame() {30 public Type getType() {31 return Type.STDOUT;32 }33 public String getUtf8String() {34 return "Hello World";35 }36 });37 resultCallback.accept(new OutputFrame() {
FrameConsumerResultCallback
Using AI Code Generation
1package org.testcontainers.containers.output;2import org.testcontainers.containers.GenericContainer;3import org.testcontainers.containers.output.FrameConsumerResultCallback;4import java.util.concurrent.TimeUnit;5public class FrameConsumerResultCallbackDemo {6 public static void main(String[] args) throws Exception {7 try (GenericContainer container = new GenericContainer("alpine:3.8")) {8 container.start();9 FrameConsumerResultCallback resultCallback = new FrameConsumerResultCallback();10 container.followOutput(resultCallback);11 resultCallback.awaitCompletion(5, TimeUnit.SECONDS);12 System.out.println(resultCallback.getStdout());13 }14 }15}16[alpine:3.8] 2019-01-20 07:37:18,342 INFO success: getty entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)17[alpine:3.8] 2019-01-20 07:37:18,342 INFO success: dbus entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)18[alpine:3.8] 2019-01-20 07:37:18,342 INFO success: crond entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)19[alpine:3.8] 2019-01-20 07:37:18,342 INFO success: rsyslog entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)20[alpine:3.8] 2019-01-20 07:37:18,342 INFO success: udevd entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
FrameConsumerResultCallback
Using AI Code Generation
1package org.testcontainers.containers.output;2import java.io.IOException;3import java.nio.charset.Charset;4import org.testcontainers.containers.output.BaseConsumerResultCallback;5import org.testcontainers.containers.output.FrameConsumerResultCallback;6import org.testcontainers.containers.output.OutputFrame;7import com.github.dockerjava.api.model.Frame;8public class FrameConsumerResultCallbackTest {9 public static void main(String[] args) throws IOException {10 FrameConsumerResultCallback callback = new FrameConsumerResultCallback();11 callback.accept(new Frame() {12 public String toString() {13 return "Frame [streamType=STDOUT, payload=hello world]";14 }15 });16 System.out.println(callback.toString());17 }18}19package org.testcontainers.containers.output;20import java.io.IOException;21import java.nio.charset.Charset;22import org.testcontainers.containers.output.BaseConsumerResultCallback;23import org.testcontainers.containers.output.FrameConsumerResultCallback;24import org.testcontainers.containers.output.OutputFrame;25import com.github.dockerjava.api.model.Frame;26public class FrameConsumerResultCallbackTest {27 public static void main(String[] args) throws IOException {28 FrameConsumerResultCallback callback = new FrameConsumerResultCallback();29 callback.accept(new Frame() {30 public String toString() {31 return "Frame [streamType=STDOUT, payload=hello world]";32 }33 });34 callback.addConsumer(outputFrame -> {35 System.out.println(outputFrame.getUtf8String());36 });37 }38}39package org.testcontainers.containers.output;40import java.io.IOException;41import java.nio.charset.Charset;42import org.testcontainers.containers.output.BaseConsumerResultCallback;43import org.testcontainers.containers.output.FrameConsumerResultCallback;44import org.testcontainers.containers.output.OutputFrame;45import com.github.dockerjava.api.model.Frame;46public class FrameConsumerResultCallbackTest {47 public static void main(String[] args) throws IOException {
FrameConsumerResultCallback
Using AI Code Generation
1import org.testcontainers.containers.output.FrameConsumerResultCallback;2import org.testcontainers.containers.output.OutputFrame;3import org.testcontainers.containers.output.ToStringConsumer;4import org.testcontainers.containers.output.WaitingConsumer;5import org.testcontainers.containers.output.ToStringConsumer;6import org.testcontainers.containers.output.WaitingConsumer;7import org.testcontainers.containers.output.OutputFrame;8import org.testcontainers.containers.output.FrameConsumerResultCallback;9import org.testcontainers.containers.output.ToStringConsumer;10import org.testcontainers.containers.output.WaitingConsumer;11import org.testcontainers.containers.output.OutputFrame;12import org.testcontainers.containers.output.FrameConsumerResultCallback;13import org.testcontainers.containers.output.ToStringConsumer;14import org.testcontainers.containers.output.WaitingConsumer;15import org.testcontainers.containers.output.OutputFrame;16import org.testcontainers.containers.output.FrameConsumerResultCallback;17import org.testcontainers.containers.output.ToStringConsumer;18import org.testcontainers.containers.output.WaitingConsumer;19import org.testcontainers.containers.output.OutputFrame;20import org.testcontainers.containers.output.FrameConsumerResultCallback;21import org.testcontainers.containers.output.ToStringConsumer;22import org.testcontainers.containers.output.WaitingConsumer;23import org.testcontainers.containers.output.OutputFrame;24import org.testcontainers.containers.output.FrameConsumerResultCallback;25import org.testcontainers.containers.output.ToStringConsumer;26import org.testcontainers.containers.output.WaitingConsumer;27import org.testcontainers.containers.output.OutputFrame;28import org.testcontainers.containers.output.FrameConsumerResultCallback;29import org.testcontainers.containers.output.ToStringConsumer;30import org.testcontainers.containers.output.WaitingConsumer;31import org.testcontainers.containers.output.OutputFrame;32import org.testcontainers.containers.output.FrameConsumerResultCallback;33import org.testcontainers.containers.output.ToStringConsumer;34import org.testcontainers.containers.output.WaitingConsumer;35import org.testcontainers.containers.output.OutputFrame;36import org.testcontainers.containers.output.FrameConsumerResultCallback;37import org.testcontainers.containers.output.ToStringConsumer;38import org.testcontainers.containers.output.WaitingConsumer;39import org.testcontainers.containers.output.OutputFrame;40import org.testcontainers.containers.output.FrameConsumerResultCallback;41import org.testcontainers.containers.output.ToStringConsumer;42import org.testcontainers.containers.output.WaitingConsumer;43import org.testcontainers.containers.output.OutputFrame;44import org.testcontainers.containers.output.FrameConsumerResultCallback;45import org.testcontainers.containers.output.ToStringConsumer;46import org.testcontainers.containers
FrameConsumerResultCallback
Using AI Code Generation
1package org.testcontainers.containers.output;2import org.testcontainers.containers.Container;3import org.testcontainers.containers.output.OutputFrame.OutputType;4import org.testcontainers.containers.output.ToStringConsumer;5import java.util.concurrent.TimeUnit;6import java.util.concurrent.TimeoutException;7import java.util.function.Consumer;8public class FrameConsumerResultCallback extends ToStringConsumer implements Consumer<OutputFrame> {9 private final Consumer<OutputFrame> consumer;10 private final long timeout;11 private final TimeUnit timeUnit;12 public FrameConsumerResultCallback() {13 this(1L, TimeUnit.MINUTES);14 }15 public FrameConsumerResultCallback(long timeout, TimeUnit timeUnit) {16 this((Consumer)null, timeout, timeUnit);17 }18 public FrameConsumerResultCallback(Consumer<OutputFrame> consumer, long timeout, TimeUnit timeUnit) {19 this.consumer = consumer;20 this.timeout = timeout;21 this.timeUnit = timeUnit;22 }23 public void accept(OutputFrame outputFrame) {24 if (this.consumer != null) {25 this.consumer.accept(outputFrame);26 }27 if (outputFrame.getType() == OutputType.STDOUT || outputFrame.getType() == OutputType.STDERR) {28 this.builder.append(outputFrame.getUtf8String());29 }30 }31 public String getTargetId() {32 return "ResultCallback";33 }34 public void onStart(Container container) {35 }36 public void onCompleted() {37 }38 public void onThrowable(Throwable throwable) {39 }40 public String toString() {41 return this.builder.toString();42 }43 public String awaitCompletion() throws TimeoutException, InterruptedException {44 return this.awaitCompletion(this.timeout, this.timeUnit);45 }46 public String awaitCompletion(long timeout, TimeUnit timeUnit) throws TimeoutException, InterruptedException {47 return this.builder.toString();48 }49}50package org.testcontainers.containers.output;51import java.io.IOException;52import java.util.concurrent.TimeUnit;53import java.util.concurrent.TimeoutException;54import java.util.function.Consumer;55import org.testcontainers.containers.Container;56import org.testcontainers.containers.output.OutputFrame.OutputType;57import org.testcontainers.containers.output.ToStringConsumer;
FrameConsumerResultCallback
Using AI Code Generation
1package org.testcontainers.containers.output;2import org.testcontainers.containers.Container;3import org.testcontainers.containers.Container.ExecResult;4import org.testcontainers.containers.Container.ExecResultCallback;5import org.testcontainers.containers.Container.ExecResultCallback.ResultCallback;6import java.io.IOException;7import java.util.concurrent.TimeUnit;8public class FrameConsumerResultCallback extends ExecResultCallback {9 public FrameConsumerResultCallback(FrameConsumer frameConsumer) {10 super(frameConsumer);11 }12 public static ResultCallback<ExecResult> newInstance(FrameConsumer frameConsumer) {13 return new FrameConsumerResultCallback(frameConsumer);14 }15 public void awaitCompletion() throws InterruptedException {16 try {17 super.awaitCompletion();18 } catch (IOException e) {19 throw new RuntimeException(e);20 }21 }22 public void awaitCompletion(long timeout, TimeUnit unit) throws InterruptedException {23 try {24 super.awaitCompletion(timeout, unit);25 } catch (IOException e) {26 throw new RuntimeException(e);27 }28 }29 public ExecResult awaitSuccess() {30 try {31 return super.awaitSuccess();32 } catch (IOException e) {33 throw new RuntimeException(e);34 }35 }36 public void close() throws IOException {37 try {38 super.close();39 } catch (InterruptedException e) {40 throw new RuntimeException(e);41 }42 }43 public void onStart(Cmd cmd) {44 ((FrameConsumer) delegate).onStart(cmd);45 }46 public void onNext(Frame item) {47 ((FrameConsumer) delegate).onNext(item);48 }49 public void onError(Throwable throwable) {50 ((FrameConsumer) delegate).onError(throwable);51 }52 public void onComplete() {53 ((FrameConsumer) delegate).onComplete();54 }55}56package org.testcontainers.containers.output;57import org.testcontainers.containers.Container;58import org.testcontainers.containers.Container.ExecResult;59import org.testcontainers.containers.Container.ExecResultCallback;60import org.testcontainers.containers.Container.ExecResultCallback.ResultCallback;61import java.io.IOException;62import java.util.concurrent.TimeUnit;63public class FrameConsumerResultCallback extends ExecResultCallback {
FrameConsumerResultCallback
Using AI Code Generation
1package org.testcontainers.containers;2import org.testcontainers.containers.output.FrameConsumerResultCallback;3import org.testcontainers.containers.output.OutputFrame;4import java.io.IOException;5import java.util.concurrent.TimeUnit;6public class ContainerOutputConsumer {7 public static void main(String[] args) throws IOException, InterruptedException {8 GenericContainer container = new GenericContainer("alpine:3.8")9 .withCommand("sh", "-c", "echo hello world; sleep 5")10 .withExposedPorts(80);11 container.start();12 FrameConsumerResultCallback callback = new FrameConsumerResultCallback();13 container.followOutput(callback);14 OutputFrame frame = callback.get(5, TimeUnit.SECONDS);15 System.out.println(frame.getUtf8String());16 callback.close();17 }18}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!