2
2
3
3
import com .github .dockerjava .api .DockerClient ;
4
4
import com .github .dockerjava .api .command .LogContainerCmd ;
5
+ import lombok .SneakyThrows ;
5
6
import lombok .experimental .UtilityClass ;
6
7
import org .testcontainers .containers .output .FrameConsumerResultCallback ;
7
8
import org .testcontainers .containers .output .OutputFrame ;
8
9
import org .testcontainers .containers .output .ToStringConsumer ;
9
10
import org .testcontainers .containers .output .WaitingConsumer ;
10
11
12
+ import java .io .Closeable ;
13
+ import java .io .IOException ;
11
14
import java .util .function .Consumer ;
12
15
13
16
import static org .testcontainers .containers .output .OutputFrame .OutputType .STDERR ;
@@ -59,6 +62,7 @@ public void followOutput(DockerClient dockerClient,
59
62
* @param types types of {@link OutputFrame} to receive
60
63
* @return all previous output frames (stdout/stderr being separated by newline characters)
61
64
*/
65
+ @ SneakyThrows (IOException .class )
62
66
public String getOutput (DockerClient dockerClient ,
63
67
String containerId ,
64
68
OutputFrame .OutputType ... types ) {
@@ -73,17 +77,19 @@ public String getOutput(DockerClient dockerClient,
73
77
74
78
final ToStringConsumer consumer = new ToStringConsumer ();
75
79
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
+ }
80
84
}
81
85
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
+ ) {
87
93
88
94
final LogContainerCmd cmd = dockerClient .logContainerCmd (containerId )
89
95
.withFollowStream (followStream )
@@ -96,6 +102,6 @@ private static void attachConsumer(DockerClient dockerClient,
96
102
if (type == STDERR ) cmd .withStdErr (true );
97
103
}
98
104
99
- cmd .exec (callback );
105
+ return cmd .exec (callback );
100
106
}
101
107
}
0 commit comments