Skip to content

Commit f2ed1dc

Browse files
committed
Add broker version condition when checking x-death headers
Those headers have been introduced in RabbitMQ 3.7, so checking them against earlier versions doesn't make sense. References rabbitmq/rabbitmq-server#1332
1 parent 61f047d commit f2ed1dc

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/test/java/com/rabbitmq/client/test/TestUtils.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,34 @@ public static void close(Connection connection) {
4444
}
4545
}
4646

47+
public static boolean isVersion37orLater(Connection connection) {
48+
String currentVersion = connection.getServerProperties().get("version").toString();
49+
if (currentVersion.contains("+")) {
50+
currentVersion = currentVersion.substring(0, currentVersion.indexOf("+"));
51+
}
52+
return "0.0.0".equals(currentVersion) ? true : versionCompare(currentVersion, "3.7.0") >= 0;
53+
}
54+
55+
/**
56+
* http://stackoverflow.com/questions/6701948/efficient-way-to-compare-version-strings-in-java
57+
*
58+
*/
59+
static int versionCompare(String str1, String str2) {
60+
String[] vals1 = str1.split("\\.");
61+
String[] vals2 = str2.split("\\.");
62+
int i = 0;
63+
// set index to first non-equal ordinal or length of shortest version string
64+
while (i < vals1.length && i < vals2.length && vals1[i].equals(vals2[i])) {
65+
i++;
66+
}
67+
// compare first non-equal ordinal number
68+
if (i < vals1.length && i < vals2.length) {
69+
int diff = Integer.valueOf(vals1[i]).compareTo(Integer.valueOf(vals2[i]));
70+
return Integer.signum(diff);
71+
}
72+
// the strings are equal or one string is a substring of the other
73+
// e.g. "1.2.3" = "1.2.3" or "1.2.3" < "1.2.3.4"
74+
return Integer.signum(vals1.length - vals2.length);
75+
}
76+
4777
}

src/test/java/com/rabbitmq/client/test/functional/DeadLetterExchange.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.rabbitmq.client.*;
1919
import com.rabbitmq.client.AMQP.BasicProperties;
2020
import com.rabbitmq.client.test.BrokerTestCase;
21+
import com.rabbitmq.client.test.TestUtils;
2122
import org.junit.Test;
2223

2324
import java.io.IOException;
@@ -536,9 +537,14 @@ public void process(GetResponse getResponse) {
536537
assertNotNull(headers);
537538
ArrayList<Object> death = (ArrayList<Object>) headers.get("x-death");
538539
assertNotNull(death);
539-
assertNotNull(headers.get("x-first-death-queue"));
540-
assertNotNull(headers.get("x-first-death-reason"));
541-
assertNotNull(headers.get("x-first-death-exchange"));
540+
// the following assertions shouldn't be checked on version lower than 3.7
541+
// as the headers are new in 3.7
542+
// see https://github.com/rabbitmq/rabbitmq-server/issues/1332
543+
if(TestUtils.isVersion37orLater(channel.getConnection())) {
544+
assertNotNull(headers.get("x-first-death-queue"));
545+
assertNotNull(headers.get("x-first-death-reason"));
546+
assertNotNull(headers.get("x-first-death-exchange"));
547+
}
542548
assertEquals(1, death.size());
543549
assertDeathReason(death, 0, TEST_QUEUE_NAME, reason,
544550
"amq.direct",

0 commit comments

Comments
 (0)