|
1 | 1 | package org.testcontainers.containers.wait.strategy; |
2 | 2 |
|
3 | | -import org.rnorth.ducttape.timeouts.Timeouts; |
4 | | - |
5 | 3 | import java.time.Duration; |
6 | 4 | import java.util.ArrayList; |
7 | 5 | import java.util.List; |
8 | 6 | import java.util.concurrent.TimeUnit; |
9 | 7 |
|
| 8 | +import org.rnorth.ducttape.timeouts.Timeouts; |
| 9 | + |
10 | 10 | public class WaitAllStrategy implements WaitStrategy { |
11 | 11 |
|
| 12 | + public enum Mode { |
| 13 | + |
| 14 | + /** |
| 15 | + * This is the default mode: The timeout of the {@link WaitAllStrategy strategy} is applied to each individual |
| 16 | + * strategy, so that the container waits maximum for |
| 17 | + * {@link org.testcontainers.containers.wait.strategy.WaitAllStrategy#timeout}. |
| 18 | + */ |
| 19 | + WITH_OUTER_TIMEOUT, |
| 20 | + |
| 21 | + /** |
| 22 | + * Using this mode triggers the following behaviour: The outer timeout is disabled and the outer enclosing |
| 23 | + * strategy waits for all inner strategies according to their timeout. Once set, it disables |
| 24 | + * {@link org.testcontainers.containers.wait.strategy.WaitAllStrategy#withStartupTimeout(java.time.Duration)} method, |
| 25 | + * as it would overwrite inner timeouts. |
| 26 | + */ |
| 27 | + WITH_INDIVIDUAL_TIMEOUTS_ONLY, |
| 28 | + |
| 29 | + /** |
| 30 | + * This is the original mode of this strategy: The inner strategies wait with their preconfigured timeout |
| 31 | + * individually and the wait all strategy kills them, if the outer limit is reached. |
| 32 | + */ |
| 33 | + WITH_MAXIMUM_OUTER_TIMEOUT |
| 34 | + } |
| 35 | + |
| 36 | + private final Mode mode; |
12 | 37 | private final List<WaitStrategy> strategies = new ArrayList<>(); |
13 | 38 | private Duration timeout = Duration.ofSeconds(30); |
14 | 39 |
|
| 40 | + public WaitAllStrategy() { |
| 41 | + this(Mode.WITH_OUTER_TIMEOUT); |
| 42 | + } |
| 43 | + |
| 44 | + public WaitAllStrategy(Mode mode) { |
| 45 | + this.mode = mode; |
| 46 | + } |
| 47 | + |
15 | 48 | @Override |
16 | 49 | public void waitUntilReady(WaitStrategyTarget waitStrategyTarget) { |
17 | | - Timeouts.doWithTimeout((int) timeout.toMillis(), TimeUnit.MILLISECONDS, () -> { |
18 | | - for (WaitStrategy strategy : strategies) { |
19 | | - strategy.waitUntilReady(waitStrategyTarget); |
20 | | - } |
21 | | - }); |
| 50 | + if (mode == Mode.WITH_INDIVIDUAL_TIMEOUTS_ONLY) { |
| 51 | + waitUntilNestedStrategiesAreReady(waitStrategyTarget); |
| 52 | + } else { |
| 53 | + Timeouts.doWithTimeout((int) timeout.toMillis(), TimeUnit.MILLISECONDS, () -> { |
| 54 | + waitUntilNestedStrategiesAreReady(waitStrategyTarget); |
| 55 | + }); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private void waitUntilNestedStrategiesAreReady(WaitStrategyTarget waitStrategyTarget) { |
| 60 | + for (WaitStrategy strategy : strategies) { |
| 61 | + strategy.waitUntilReady(waitStrategyTarget); |
| 62 | + } |
22 | 63 | } |
23 | 64 |
|
24 | 65 | public WaitAllStrategy withStrategy(WaitStrategy strategy) { |
| 66 | + |
| 67 | + if (mode == Mode.WITH_OUTER_TIMEOUT) { |
| 68 | + applyStartupTimeout(strategy); |
| 69 | + } |
| 70 | + |
25 | 71 | this.strategies.add(strategy); |
26 | 72 | return this; |
27 | 73 | } |
28 | 74 |
|
29 | 75 | @Override |
30 | | - public WaitStrategy withStartupTimeout(Duration startupTimeout) { |
| 76 | + public WaitAllStrategy withStartupTimeout(Duration startupTimeout) { |
| 77 | + |
| 78 | + if (mode == Mode.WITH_INDIVIDUAL_TIMEOUTS_ONLY) { |
| 79 | + throw new IllegalStateException(String.format( |
| 80 | + "Changing startup timeout is not supported with mode %s", Mode.WITH_INDIVIDUAL_TIMEOUTS_ONLY)); |
| 81 | + } |
| 82 | + |
31 | 83 | this.timeout = startupTimeout; |
| 84 | + strategies.forEach(this::applyStartupTimeout); |
32 | 85 | return this; |
33 | 86 | } |
| 87 | + |
| 88 | + private void applyStartupTimeout(WaitStrategy childStrategy) { |
| 89 | + childStrategy.withStartupTimeout(this.timeout); |
| 90 | + } |
34 | 91 | } |
0 commit comments