Skip to content

Commit e4612bf

Browse files
committed
Add AsserJ conditions for CountDownLatch
1 parent a9a05bf commit e4612bf

File tree

2 files changed

+77
-53
lines changed

2 files changed

+77
-53
lines changed

src/test/java/com/rabbitmq/stream/impl/ClientTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -742,8 +742,7 @@ void clientShouldContainServerAdvertisedHostAndPort() {
742742

743743
@ParameterizedTest
744744
@ValueSource(strings = {"", "some-publisher-reference"})
745-
void closingPublisherWhilePublishingShouldNotCloseConnection(String publisherReference)
746-
throws Exception {
745+
void closingPublisherWhilePublishingShouldNotCloseConnection(String publisherReference) {
747746
AtomicReference<CountDownLatch> confirmLatch =
748747
new AtomicReference<>(new CountDownLatch(500_000));
749748
CountDownLatch closedLatch = new CountDownLatch(1);

src/test/java/com/rabbitmq/stream/impl/TestUtils.java

Lines changed: 76 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,57 @@ static Map<String, StreamMetadata> metadata(Broker leader, List<Broker> replicas
362362
return metadata("stream", leader, replicas);
363363
}
364364

365+
private static String currentVersion(String currentVersion) {
366+
// versions built from source: 3.7.0+rc.1.4.gedc5d96
367+
if (currentVersion.contains("+")) {
368+
currentVersion = currentVersion.substring(0, currentVersion.indexOf("+"));
369+
}
370+
// alpha (snapshot) versions: 3.7.0~alpha.449-1
371+
if (currentVersion.contains("~")) {
372+
currentVersion = currentVersion.substring(0, currentVersion.indexOf("~"));
373+
}
374+
// alpha (snapshot) versions: 3.7.1-alpha.40
375+
if (currentVersion.contains("-")) {
376+
currentVersion = currentVersion.substring(0, currentVersion.indexOf("-"));
377+
}
378+
return currentVersion;
379+
}
380+
381+
static boolean atLeastVersion(String expectedVersion, String currentVersion) {
382+
if (currentVersion.contains("alpha-stream")) {
383+
return true;
384+
}
385+
try {
386+
currentVersion = currentVersion(currentVersion);
387+
return "0.0.0".equals(currentVersion) || versionCompare(currentVersion, expectedVersion) >= 0;
388+
} catch (RuntimeException e) {
389+
LoggerFactory.getLogger(TestUtils.class)
390+
.warn("Unable to parse broker version {}", currentVersion, e);
391+
throw e;
392+
}
393+
}
394+
395+
/**
396+
* https://stackoverflow.com/questions/6701948/efficient-way-to-compare-version-strings-in-java
397+
*/
398+
static int versionCompare(String str1, String str2) {
399+
String[] vals1 = str1.split("\\.");
400+
String[] vals2 = str2.split("\\.");
401+
int i = 0;
402+
// set index to first non-equal ordinal or length of shortest version string
403+
while (i < vals1.length && i < vals2.length && vals1[i].equals(vals2[i])) {
404+
i++;
405+
}
406+
// compare first non-equal ordinal number
407+
if (i < vals1.length && i < vals2.length) {
408+
int diff = Integer.valueOf(vals1[i]).compareTo(Integer.valueOf(vals2[i]));
409+
return Integer.signum(diff);
410+
}
411+
// the strings are equal or one string is a substring of the other
412+
// e.g. "1.2.3" = "1.2.3" or "1.2.3" < "1.2.3.4"
413+
return Integer.signum(vals1.length - vals2.length);
414+
}
415+
365416
@Target({ElementType.TYPE, ElementType.METHOD})
366417
@Retention(RetentionPolicy.RUNTIME)
367418
@Documented
@@ -420,6 +471,31 @@ interface RunnableWithException {
420471
void run() throws Exception;
421472
}
422473

474+
static class CountDownLatchConditions {
475+
476+
static Condition<CountDownLatch> completed() {
477+
return completed(Duration.ofSeconds(10));
478+
}
479+
480+
static Condition<CountDownLatch> completed(int timeoutInSeconds) {
481+
return completed(Duration.ofSeconds(timeoutInSeconds));
482+
}
483+
484+
static Condition<CountDownLatch> completed(Duration timeout) {
485+
return new Condition<>(
486+
latch -> {
487+
try {
488+
return latch.await(timeout.toMillis(), TimeUnit.MILLISECONDS);
489+
} catch (InterruptedException e) {
490+
Thread.interrupted();
491+
throw new RuntimeException(e);
492+
}
493+
},
494+
"completed in %d ms",
495+
timeout.toMillis());
496+
}
497+
}
498+
423499
public static class StreamTestInfrastructureExtension
424500
implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback {
425501

@@ -641,57 +717,6 @@ public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext con
641717
}
642718
}
643719

644-
private static String currentVersion(String currentVersion) {
645-
// versions built from source: 3.7.0+rc.1.4.gedc5d96
646-
if (currentVersion.contains("+")) {
647-
currentVersion = currentVersion.substring(0, currentVersion.indexOf("+"));
648-
}
649-
// alpha (snapshot) versions: 3.7.0~alpha.449-1
650-
if (currentVersion.contains("~")) {
651-
currentVersion = currentVersion.substring(0, currentVersion.indexOf("~"));
652-
}
653-
// alpha (snapshot) versions: 3.7.1-alpha.40
654-
if (currentVersion.contains("-")) {
655-
currentVersion = currentVersion.substring(0, currentVersion.indexOf("-"));
656-
}
657-
return currentVersion;
658-
}
659-
660-
static boolean atLeastVersion(String expectedVersion, String currentVersion) {
661-
if (currentVersion.contains("alpha-stream")) {
662-
return true;
663-
}
664-
try {
665-
currentVersion = currentVersion(currentVersion);
666-
return "0.0.0".equals(currentVersion) || versionCompare(currentVersion, expectedVersion) >= 0;
667-
} catch (RuntimeException e) {
668-
LoggerFactory.getLogger(TestUtils.class)
669-
.warn("Unable to parse broker version {}", currentVersion, e);
670-
throw e;
671-
}
672-
}
673-
674-
/**
675-
* https://stackoverflow.com/questions/6701948/efficient-way-to-compare-version-strings-in-java
676-
*/
677-
static int versionCompare(String str1, String str2) {
678-
String[] vals1 = str1.split("\\.");
679-
String[] vals2 = str2.split("\\.");
680-
int i = 0;
681-
// set index to first non-equal ordinal or length of shortest version string
682-
while (i < vals1.length && i < vals2.length && vals1[i].equals(vals2[i])) {
683-
i++;
684-
}
685-
// compare first non-equal ordinal number
686-
if (i < vals1.length && i < vals2.length) {
687-
int diff = Integer.valueOf(vals1[i]).compareTo(Integer.valueOf(vals2[i]));
688-
return Integer.signum(diff);
689-
}
690-
// the strings are equal or one string is a substring of the other
691-
// e.g. "1.2.3" = "1.2.3" or "1.2.3" < "1.2.3.4"
692-
return Integer.signum(vals1.length - vals2.length);
693-
}
694-
695720
static class CountDownLatchAssert implements AssertDelegateTarget {
696721

697722
private static final Duration TIMEOUT = Duration.ofSeconds(10);

0 commit comments

Comments
 (0)