Skip to content

Commit 5e6dbc7

Browse files
authored
Retry internal port checking in the same exec (#4460)
* Retry internal port checking in the same exec * Use just grep * Reduce the changeset
1 parent 6cde564 commit 5e6dbc7

File tree

2 files changed

+7
-15
lines changed

2 files changed

+7
-15
lines changed

core/src/main/java/org/testcontainers/containers/wait/internal/InternalCommandPortListeningCheck.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,19 @@ public class InternalCommandPortListeningCheck implements java.util.concurrent.C
2424

2525
@Override
2626
public Boolean call() {
27-
StringBuilder command = new StringBuilder("true");
27+
StringBuilder command = new StringBuilder("while true; do ( true ");
2828

2929
for (int internalPort : internalPorts) {
3030
command.append(" && ");
3131
command.append(" (");
32-
command.append(format("cat /proc/net/tcp* | awk '{print $2}' | grep -i ':0*%x'", internalPort));
32+
command.append(format("grep -i ':0*%x' /proc/net/tcp*", internalPort));
3333
command.append(" || ");
3434
command.append(format("nc -vz -w 1 localhost %d", internalPort));
3535
command.append(" || ");
3636
command.append(format("/bin/bash -c '</dev/tcp/localhost/%d'", internalPort));
3737
command.append(")");
3838
}
39+
command.append(" ) && exit 0 || sleep 0.1; done");
3940

4041
Instant before = Instant.now();
4142
try {

core/src/test/java/org/testcontainers/containers/wait/internal/InternalCommandPortListeningCheckTest.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
import org.junit.runners.Parameterized;
88
import org.rnorth.ducttape.TimeoutException;
99
import org.rnorth.ducttape.unreliables.Unreliables;
10+
import org.rnorth.visibleassertions.VisibleAssertions;
1011
import org.testcontainers.containers.GenericContainer;
1112
import org.testcontainers.images.builder.ImageFromDockerfile;
1213

1314
import java.util.concurrent.TimeUnit;
1415

1516
import static java.util.Arrays.asList;
16-
import static org.rnorth.visibleassertions.VisibleAssertions.assertFalse;
17-
import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue;
1817

1918
@RunWith(Parameterized.class)
2019
public class InternalCommandPortListeningCheckTest {
@@ -45,9 +44,7 @@ public void singleListening() {
4544

4645
Unreliables.retryUntilTrue(5, TimeUnit.SECONDS, check);
4746

48-
final Boolean result = check.call();
49-
50-
assertTrue("InternalCommandPortListeningCheck identifies a single listening port", result);
47+
VisibleAssertions.pass("InternalCommandPortListeningCheck identifies a single listening port");
5148
}
5249

5350
@Test
@@ -56,13 +53,9 @@ public void nonListening() {
5653

5754
try {
5855
Unreliables.retryUntilTrue(5, TimeUnit.SECONDS, check);
56+
VisibleAssertions.fail("expected to fail");
5957
} catch (TimeoutException e) {
60-
// we expect it to timeout
6158
}
62-
63-
final Boolean result = check.call();
64-
65-
assertFalse("InternalCommandPortListeningCheck detects a non-listening port among many", result);
6659
}
6760

6861
@Test
@@ -71,8 +64,6 @@ public void lowAndHighPortListening() {
7164

7265
Unreliables.retryUntilTrue(5, TimeUnit.SECONDS, check);
7366

74-
final Boolean result = check.call();
75-
76-
assertTrue("InternalCommandPortListeningCheck identifies a low and a high port", result);
67+
VisibleAssertions.pass("InternalCommandPortListeningCheck identifies a low and a high port");
7768
}
7869
}

0 commit comments

Comments
 (0)