From a3343ac7da3dfe5aafa00210f6f9ca58da80283c Mon Sep 17 00:00:00 2001 From: Dan Hall Date: Thu, 2 Jul 2020 00:22:35 +0100 Subject: [PATCH] Issue #127 - it is illegal to bind to a consistent hash exchange with a non integer binding key --- .../mock/exchange/ConsistentHashExchange.java | 2 +- .../exchange/ConsistentHashExchangeTests.java | 32 +++++++++++++------ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/github/fridujo/rabbitmq/mock/exchange/ConsistentHashExchange.java b/src/main/java/com/github/fridujo/rabbitmq/mock/exchange/ConsistentHashExchange.java index d979b659..d7398fa1 100644 --- a/src/main/java/com/github/fridujo/rabbitmq/mock/exchange/ConsistentHashExchange.java +++ b/src/main/java/com/github/fridujo/rabbitmq/mock/exchange/ConsistentHashExchange.java @@ -55,7 +55,7 @@ private int routingKeyToWeight(String routingKey) { try { return Integer.parseInt(routingKey); } catch (NumberFormatException e) { - return routingKey.hashCode(); + throw new IllegalArgumentException("The binding key must be an integer"); } } diff --git a/src/test/java/com/github/fridujo/rabbitmq/mock/exchange/ConsistentHashExchangeTests.java b/src/test/java/com/github/fridujo/rabbitmq/mock/exchange/ConsistentHashExchangeTests.java index eec29ab3..b3130a58 100644 --- a/src/test/java/com/github/fridujo/rabbitmq/mock/exchange/ConsistentHashExchangeTests.java +++ b/src/test/java/com/github/fridujo/rabbitmq/mock/exchange/ConsistentHashExchangeTests.java @@ -4,6 +4,7 @@ import static com.github.fridujo.rabbitmq.mock.exchange.MockExchangeCreator.creatorWithExchangeType; import static java.util.Collections.emptyMap; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.within; import static org.mockito.Mockito.mock; @@ -52,14 +53,11 @@ void dispatch_respects_queue_weight() { SingleReceiverExchange consistentHashEx = (SingleReceiverExchange) mockExchangeFactory.build("test", "x-consistent-hash", empty(), mock(ReceiverRegistry.class)); ReceiverPointer q1 = new ReceiverPointer(ReceiverPointer.Type.QUEUE, "Q1"); - consistentHashEx.bind(q1, "32", emptyMap()); + consistentHashEx.bind(q1, "10", emptyMap()); ReceiverPointer q2 = new ReceiverPointer(ReceiverPointer.Type.QUEUE, "Q2"); - consistentHashEx.bind(q2, "64", emptyMap()); + consistentHashEx.bind(q2, "70", emptyMap()); ReceiverPointer q3 = new ReceiverPointer(ReceiverPointer.Type.QUEUE, "Q3"); - consistentHashEx.bind(q3, " ", emptyMap()); - ReceiverPointer q4 = new ReceiverPointer(ReceiverPointer.Type.QUEUE, "Q4"); - consistentHashEx.bind(q4, "AA", emptyMap()); - consistentHashEx.unbind(q4, "AA"); + consistentHashEx.bind(q3, "20", emptyMap()); int messagesCount = 1_000_000; Map deliveriesByReceiver = IntStream.range(0, messagesCount) @@ -69,8 +67,24 @@ void dispatch_respects_queue_weight() { assertThat(deliveriesByReceiver).containsOnlyKeys(q1, q2, q3); - assertThat(Long.valueOf(deliveriesByReceiver.get(q1)).doubleValue() / messagesCount).isCloseTo(0.25D, within(0.01)); - assertThat(Long.valueOf(deliveriesByReceiver.get(q2)).doubleValue() / messagesCount).isCloseTo(0.5D, within(0.01)); - assertThat(Long.valueOf(deliveriesByReceiver.get(q3)).doubleValue() / messagesCount).isCloseTo(0.25D, within(0.01)); + assertThat(Long.valueOf(deliveriesByReceiver.get(q1)).doubleValue() / messagesCount).isCloseTo(0.1D, within(0.01)); + assertThat(Long.valueOf(deliveriesByReceiver.get(q2)).doubleValue() / messagesCount).isCloseTo(0.7D, within(0.01)); + assertThat(Long.valueOf(deliveriesByReceiver.get(q3)).doubleValue() / messagesCount).isCloseTo(0.2D, within(0.01)); + } + + + @Test + void binding_with_non_integer_key_throws_exception() { + SingleReceiverExchange consistentHashEx = (SingleReceiverExchange) mockExchangeFactory.build("test", "x-consistent-hash", empty(), mock(ReceiverRegistry.class)); + + ReceiverPointer q1 = new ReceiverPointer(ReceiverPointer.Type.QUEUE, "Q1"); + + assertThatThrownBy(() -> {consistentHashEx.bind(q1, "", emptyMap());}) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("The binding key must be an integer"); + + assertThatThrownBy(() -> {consistentHashEx.bind(q1, "string", emptyMap());}) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("The binding key must be an integer"); } }