Skip to content

Commit 103745d

Browse files
authored
Merge pull request #959 from rabbitmq/improve-broker-version-detection
Improve broker version detection
2 parents b44dd60 + e3ba0f5 commit 103745d

2 files changed

Lines changed: 40 additions & 16 deletions

File tree

src/main/java/com/rabbitmq/stream/impl/Utils.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020-2025 Broadcom. All Rights Reserved.
1+
// Copyright (c) 2020-2026 Broadcom. All Rights Reserved.
22
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
33
//
44
// This software, the RabbitMQ Stream Java client library, is dual-licensed under the
@@ -64,6 +64,8 @@
6464
import java.util.function.LongSupplier;
6565
import java.util.function.Predicate;
6666
import java.util.function.Supplier;
67+
import java.util.regex.Matcher;
68+
import java.util.regex.Pattern;
6769
import javax.net.ssl.X509TrustManager;
6870
import org.slf4j.Logger;
6971
import org.slf4j.LoggerFactory;
@@ -496,20 +498,14 @@ static boolean offsetBefore(long x, long y) {
496498
return Long.compareUnsigned(x, y) < 0;
497499
}
498500

499-
private static String currentVersion(String currentVersion) {
500-
// versions built from source: 3.7.0+rc.1.4.gedc5d96
501-
if (currentVersion.contains("+")) {
502-
currentVersion = currentVersion.substring(0, currentVersion.indexOf("+"));
503-
}
504-
// alpha (snapshot) versions: 3.7.0~alpha.449-1
505-
if (currentVersion.contains("~")) {
506-
currentVersion = currentVersion.substring(0, currentVersion.indexOf("~"));
507-
}
508-
// alpha (snapshot) versions: 3.7.1-alpha.40
509-
if (currentVersion.contains("-")) {
510-
currentVersion = currentVersion.substring(0, currentVersion.indexOf("-"));
501+
private static final Pattern SEMVER_PATTERN = Pattern.compile("(\\d+\\.\\d+\\.\\d+)");
502+
503+
static String currentVersion(String currentVersion) {
504+
Matcher matcher = SEMVER_PATTERN.matcher(currentVersion);
505+
if (matcher.find()) {
506+
return matcher.group(1);
511507
}
512-
return currentVersion;
508+
throw new IllegalArgumentException("No semver pattern found in: " + currentVersion);
513509
}
514510

515511
/**
@@ -534,7 +530,12 @@ static int versionCompare(String str1, String str2) {
534530
}
535531

536532
static boolean is3_11_OrMore(String brokerVersion) {
537-
return versionCompare(currentVersion(brokerVersion), "3.11.0") >= 0;
533+
try {
534+
return versionCompare(currentVersion(brokerVersion), "3.11.0") >= 0;
535+
} catch (Exception e) {
536+
LOGGER.debug("Unable to parse broker version {}", brokerVersion, e);
537+
return true;
538+
}
538539
}
539540

540541
static Client.ClientParameters maybeSetUpClientParametersFromUris(

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020-2025 Broadcom. All Rights Reserved.
1+
// Copyright (c) 2020-2026 Broadcom. All Rights Reserved.
22
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
33
//
44
// This software, the RabbitMQ Stream Java client library, is dual-licensed under the
@@ -118,6 +118,7 @@ void testOffsetBefore() {
118118
"3.9.21,false",
119119
"3.9.22-alpha.13,false",
120120
"3.10.6,false",
121+
"tanzu+rabbitmq.v3.10.6.dev.1.179.g335e26b,false",
121122
"3.11.0-alpha.15,true",
122123
"3.11.0,true",
123124
"3.11.1,true",
@@ -127,11 +128,33 @@ void testOffsetBefore() {
127128
"4.1.0-alpha.15,true",
128129
"4.1.0,true",
129130
"4.1.1,true",
131+
"tanzu+rabbitmq.v4.3.0.dev.1.179.g335e26b,true",
130132
})
131133
void is_3_11_OrMore(String input, boolean expected) {
132134
assertThat(Utils.is3_11_OrMore(input)).isEqualTo(expected);
133135
}
134136

137+
@ParameterizedTest
138+
@CsvSource({
139+
"4.3.0,4.3.0",
140+
"3.13.6,3.13.6",
141+
"3.13.6.2,3.13.6",
142+
"3.13.6-alpha.0,3.13.6",
143+
"3.13.6~beta-1,3.13.6",
144+
"3.13.6+funky-metadata-1,3.13.6",
145+
"3.7.0+rc.1.4.gedc5d96,3.7.0",
146+
"tanzu+rabbitmq.v4.3.0.dev.1.179.g335e26b,4.3.0"
147+
})
148+
void currentVersionExtraction(String input, String expected) {
149+
assertThat(Utils.currentVersion(input)).isEqualTo(expected);
150+
}
151+
152+
@ParameterizedTest
153+
@CsvSource({"tanzu+rabbitmq", "not-a-version", "abc"})
154+
void versionCheckReturnsTrueWhenVersionCannotBeParsed(String brokerVersion) {
155+
assertThat(Utils.is3_11_OrMore(brokerVersion)).isTrue();
156+
}
157+
135158
@Test
136159
void maybeSetUpClientParametersFromUrisShouldKeepCredentialsIfNotInUri() {
137160
String u = UUID.randomUUID().toString();

0 commit comments

Comments
 (0)