22
33import com .github .dockerjava .api .DockerClient ;
44import com .github .dockerjava .api .command .LogContainerCmd ;
5+ import lombok .SneakyThrows ;
56import lombok .experimental .UtilityClass ;
67import org .testcontainers .containers .output .FrameConsumerResultCallback ;
78import org .testcontainers .containers .output .OutputFrame ;
89import org .testcontainers .containers .output .ToStringConsumer ;
910import org .testcontainers .containers .output .WaitingConsumer ;
1011
12+ import java .io .Closeable ;
13+ import java .io .IOException ;
1114import java .util .function .Consumer ;
1215
1316import static org .testcontainers .containers .output .OutputFrame .OutputType .STDERR ;
@@ -59,6 +62,7 @@ public void followOutput(DockerClient dockerClient,
5962 * @param types types of {@link OutputFrame} to receive
6063 * @return all previous output frames (stdout/stderr being separated by newline characters)
6164 */
65+ @ SneakyThrows (IOException .class )
6266 public String getOutput (DockerClient dockerClient ,
6367 String containerId ,
6468 OutputFrame .OutputType ... types ) {
@@ -73,17 +77,19 @@ public String getOutput(DockerClient dockerClient,
7377
7478 final ToStringConsumer consumer = new ToStringConsumer ();
7579 final WaitingConsumer wait = new WaitingConsumer ();
76- attachConsumer (dockerClient , containerId , consumer .andThen (wait ), false , types );
77-
78- wait . waitUntilEnd ();
79- return consumer . toUtf8String ();
80+ try ( Closeable closeable = attachConsumer (dockerClient , containerId , consumer .andThen (wait ), false , types )) {
81+ wait . waitUntilEnd ();
82+ return consumer . toUtf8String ();
83+ }
8084 }
8185
82- private static void attachConsumer (DockerClient dockerClient ,
83- String containerId ,
84- Consumer <OutputFrame > consumer ,
85- boolean followStream ,
86- OutputFrame .OutputType ... types ) {
86+ private static Closeable attachConsumer (
87+ DockerClient dockerClient ,
88+ String containerId ,
89+ Consumer <OutputFrame > consumer ,
90+ boolean followStream ,
91+ OutputFrame .OutputType ... types
92+ ) {
8793
8894 final LogContainerCmd cmd = dockerClient .logContainerCmd (containerId )
8995 .withFollowStream (followStream )
@@ -96,6 +102,6 @@ private static void attachConsumer(DockerClient dockerClient,
96102 if (type == STDERR ) cmd .withStdErr (true );
97103 }
98104
99- cmd .exec (callback );
105+ return cmd .exec (callback );
100106 }
101107}
0 commit comments