-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
AbstractWaitStrategyTest.java
133 lines (117 loc) · 4.77 KB
/
AbstractWaitStrategyTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package org.testcontainers.junit.wait.strategy;
import org.jetbrains.annotations.NotNull;
import org.junit.Before;
import org.rnorth.ducttape.RetryCountExceededException;
import org.rnorth.visibleassertions.VisibleAssertions;
import org.testcontainers.TestImages;
import org.testcontainers.containers.ContainerLaunchException;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.WaitStrategy;
import java.time.Duration;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.junit.Assert.assertTrue;
/**
* Common test methods for {@link WaitStrategy} implementations.
*
* @author Pete Cornish {@literal <outofcoffee@gmail.com>}
*/
public abstract class AbstractWaitStrategyTest<W extends WaitStrategy> {
static final long WAIT_TIMEOUT_MILLIS = 3000;
/**
* Indicates that the WaitStrategy has completed waiting successfully.
*/
AtomicBoolean ready;
/**
* Subclasses should return an instance of {@link W} that sets {@code ready} to {@code true},
* if the wait was successful.
*
* @param ready the AtomicBoolean on which to indicate success
* @return WaitStrategy implementation
*/
@NotNull
protected abstract W buildWaitStrategy(final AtomicBoolean ready);
@Before
public void setUp() {
ready = new AtomicBoolean(false);
}
/**
* Starts a GenericContainer with the Alpine image, passing the given {@code shellCommand} as
* a parameter to {@literal sh -c} (the container CMD).
*
* @param shellCommand the shell command to execute
* @return the (unstarted) container
*/
private GenericContainer<?> startContainerWithCommand(String shellCommand) {
return startContainerWithCommand(shellCommand, buildWaitStrategy(ready));
}
/**
* Starts a GenericContainer with the Alpine image, passing the given {@code shellCommand} as
* a parameter to {@literal sh -c} (the container CMD) and apply a give wait strategy.
* Note that the timeout will be overwritten if any with {@link #WAIT_TIMEOUT_MILLIS}.
* @param shellCommand the shell command to execute
* @param waitStrategy The wait strategy to apply
* @return the (unstarted) container
*/
protected GenericContainer<?> startContainerWithCommand(String shellCommand, WaitStrategy waitStrategy) {
return startContainerWithCommand(shellCommand, waitStrategy, 8080);
}
protected GenericContainer<?> startContainerWithCommand(
String shellCommand,
WaitStrategy waitStrategy,
Integer... ports
) {
// apply WaitStrategy to container
return new GenericContainer<>(TestImages.ALPINE_IMAGE)
.withExposedPorts(ports)
.withCommand("sh", "-c", shellCommand)
.waitingFor(waitStrategy.withStartupTimeout(Duration.ofMillis(WAIT_TIMEOUT_MILLIS)));
}
/**
* Expects that the WaitStrategy returns successfully after connection to a container with a listening port.
*
* @param shellCommand the shell command to execute
*/
protected void waitUntilReadyAndSucceed(String shellCommand) {
try (GenericContainer<?> container = startContainerWithCommand(shellCommand)) {
waitUntilReadyAndSucceed(container);
}
}
/**
* Expects that the WaitStrategy throws a {@link RetryCountExceededException} after unsuccessful connection
* to a container with a listening port.
*
* @param shellCommand the shell command to execute
*/
protected void waitUntilReadyAndTimeout(String shellCommand) {
try (GenericContainer<?> container = startContainerWithCommand(shellCommand)) {
waitUntilReadyAndTimeout(container);
}
}
/**
* Expects that the WaitStrategy throws a {@link RetryCountExceededException} after unsuccessful connection
* to a container with a listening port.
*
* @param container the container to start
*/
protected void waitUntilReadyAndTimeout(GenericContainer<?> container) {
// start() blocks until successful or timeout
VisibleAssertions.assertThrows(
"an exception is thrown when timeout occurs (" + WAIT_TIMEOUT_MILLIS + "ms)",
ContainerLaunchException.class,
container::start
);
}
/**
* Expects that the WaitStrategy returns successfully after connection to a container with a listening port.
*
* @param container the container to start
*/
protected void waitUntilReadyAndSucceed(GenericContainer<?> container) {
// start() blocks until successful or timeout
container.start();
assertTrue(
String.format("Expected container to be ready after timeout of %sms", WAIT_TIMEOUT_MILLIS),
ready.get()
);
}
}