11package com .example .kafkacluster ;
22
3- import com .github .dockerjava .api .DockerClient ;
4- import com .github .dockerjava .core .command .ExecStartResultCallback ;
53import lombok .SneakyThrows ;
64import org .rnorth .ducttape .unreliables .Unreliables ;
7- import org .testcontainers .DockerClientFactory ;
5+ import org .testcontainers .containers . Container ;
86import org .testcontainers .containers .GenericContainer ;
97import org .testcontainers .containers .Network ;
108import org .testcontainers .lifecycle .Startable ;
119import org .testcontainers .lifecycle .Startables ;
1210import org .testcontainers .containers .KafkaContainer ;
1311
14- import java .io .ByteArrayOutputStream ;
1512import java .util .Collection ;
1613import java .util .concurrent .TimeUnit ;
1714import java .util .stream .Collectors ;
@@ -30,7 +27,6 @@ public class KafkaContainerCluster implements Startable {
3027 private final Network network ;
3128 private final GenericContainer zookeeper ;
3229 private final Collection <KafkaContainer > brokers ;
33- private final DockerClient dockerClient = DockerClientFactory .instance ().client ();
3430
3531 public KafkaContainerCluster (int brokersNum , int internalTopicsRf ) {
3632 this (CONFLUENT_PLATFORM_VERSION , brokersNum , internalTopicsRf );
@@ -96,25 +92,18 @@ private Stream<GenericContainer> allContainers() {
9692 @ Override
9793 @ SneakyThrows
9894 public void start () {
99- Stream <Startable > startables = this .brokers .stream ().map (b -> ( Startable ) b );
95+ Stream <Startable > startables = this .brokers .stream ().map (Startable . class :: cast );
10096 Startables .deepStart (startables ).get (60 , SECONDS );
10197
102- Unreliables .retryUntilTrue (30 , TimeUnit .SECONDS , () -> Stream .of (this .zookeeper )
103- .map (this ::clusterBrokers )
104- .anyMatch (brokers -> brokers .split ("," ).length == this .brokersNum ));
105- }
98+ Unreliables .retryUntilTrue (30 , TimeUnit .SECONDS , () -> {
99+ Container .ExecResult result = this .zookeeper .execInContainer (
100+ "sh" , "-c" ,
101+ "zookeeper-shell zookeeper:" + KafkaContainer .ZOOKEEPER_PORT + " ls /brokers/ids | tail -n 1"
102+ );
103+ String brokers = result .getStdout ();
106104
107- @ SneakyThrows
108- private String clusterBrokers (GenericContainer c ) {
109- ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
110- dockerClient
111- .execStartCmd (
112- dockerClient .execCreateCmd (c .getContainerId ()).withAttachStdout (true )
113- .withCmd ("sh" , "-c" , "zookeeper-shell zookeeper:" + KafkaContainer .ZOOKEEPER_PORT + " ls /brokers/ids | tail -n 1" ).exec ().getId ()
114- )
115- .exec (new ExecStartResultCallback (outputStream , null ))
116- .awaitCompletion ();
117- return outputStream .toString ();
105+ return brokers != null && brokers .split ("," ).length == this .brokersNum ;
106+ });
118107 }
119108
120109 @ Override
0 commit comments