Skip to content

Commit 4592c22

Browse files
committed
Issue #127 - it is illegal to bind to a consistent hash exchange with a non integer binding key
1 parent 8930b81 commit 4592c22

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

src/main/java/com/github/fridujo/rabbitmq/mock/exchange/ConsistentHashExchange.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private int routingKeyToWeight(String routingKey) {
5555
try {
5656
return Integer.parseInt(routingKey);
5757
} catch (NumberFormatException e) {
58-
return routingKey.hashCode();
58+
throw new IllegalArgumentException("The binding key must be an integer");
5959
}
6060
}
6161

src/test/java/com/github/fridujo/rabbitmq/mock/exchange/ConsistentHashExchangeTests.java

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.github.fridujo.rabbitmq.mock.exchange;
22

3-
import static com.github.fridujo.rabbitmq.mock.AmqArguments.empty;
4-
import static com.github.fridujo.rabbitmq.mock.exchange.MockExchangeCreator.creatorWithExchangeType;
5-
import static java.util.Collections.emptyMap;
6-
import static org.assertj.core.api.Assertions.assertThat;
7-
import static org.assertj.core.api.Assertions.within;
8-
import static org.mockito.Mockito.mock;
3+
import com.github.fridujo.rabbitmq.mock.ReceiverPointer;
4+
import com.github.fridujo.rabbitmq.mock.ReceiverRegistry;
5+
import com.github.fridujo.rabbitmq.mock.configuration.Configuration;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.ValueSource;
99

1010
import java.util.Map;
1111
import java.util.Optional;
@@ -14,11 +14,13 @@
1414
import java.util.stream.Collectors;
1515
import java.util.stream.IntStream;
1616

17-
import org.junit.jupiter.api.Test;
18-
19-
import com.github.fridujo.rabbitmq.mock.ReceiverPointer;
20-
import com.github.fridujo.rabbitmq.mock.ReceiverRegistry;
21-
import com.github.fridujo.rabbitmq.mock.configuration.Configuration;
17+
import static com.github.fridujo.rabbitmq.mock.AmqArguments.empty;
18+
import static com.github.fridujo.rabbitmq.mock.exchange.MockExchangeCreator.creatorWithExchangeType;
19+
import static java.util.Collections.emptyMap;
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
22+
import static org.assertj.core.api.AssertionsForClassTypes.within;
23+
import static org.mockito.Mockito.mock;
2224

2325
class ConsistentHashExchangeTests {
2426

@@ -52,14 +54,11 @@ void dispatch_respects_queue_weight() {
5254
SingleReceiverExchange consistentHashEx = (SingleReceiverExchange) mockExchangeFactory.build("test", "x-consistent-hash", empty(), mock(ReceiverRegistry.class));
5355

5456
ReceiverPointer q1 = new ReceiverPointer(ReceiverPointer.Type.QUEUE, "Q1");
55-
consistentHashEx.bind(q1, "32", emptyMap());
57+
consistentHashEx.bind(q1, "10", emptyMap());
5658
ReceiverPointer q2 = new ReceiverPointer(ReceiverPointer.Type.QUEUE, "Q2");
57-
consistentHashEx.bind(q2, "64", emptyMap());
59+
consistentHashEx.bind(q2, "70", emptyMap());
5860
ReceiverPointer q3 = new ReceiverPointer(ReceiverPointer.Type.QUEUE, "Q3");
59-
consistentHashEx.bind(q3, " ", emptyMap());
60-
ReceiverPointer q4 = new ReceiverPointer(ReceiverPointer.Type.QUEUE, "Q4");
61-
consistentHashEx.bind(q4, "AA", emptyMap());
62-
consistentHashEx.unbind(q4, "AA");
61+
consistentHashEx.bind(q3, "20", emptyMap());
6362

6463
int messagesCount = 1_000_000;
6564
Map<ReceiverPointer, Long> deliveriesByReceiver = IntStream.range(0, messagesCount)
@@ -69,8 +68,21 @@ void dispatch_respects_queue_weight() {
6968

7069
assertThat(deliveriesByReceiver).containsOnlyKeys(q1, q2, q3);
7170

72-
assertThat(Long.valueOf(deliveriesByReceiver.get(q1)).doubleValue() / messagesCount).isCloseTo(0.25D, within(0.01));
73-
assertThat(Long.valueOf(deliveriesByReceiver.get(q2)).doubleValue() / messagesCount).isCloseTo(0.5D, within(0.01));
74-
assertThat(Long.valueOf(deliveriesByReceiver.get(q3)).doubleValue() / messagesCount).isCloseTo(0.25D, within(0.01));
71+
assertThat(Long.valueOf(deliveriesByReceiver.get(q1)).doubleValue() / messagesCount).isCloseTo(0.1D, within(0.01));
72+
assertThat(Long.valueOf(deliveriesByReceiver.get(q2)).doubleValue() / messagesCount).isCloseTo(0.7D, within(0.01));
73+
assertThat(Long.valueOf(deliveriesByReceiver.get(q3)).doubleValue() / messagesCount).isCloseTo(0.2D, within(0.01));
74+
}
75+
76+
77+
@ParameterizedTest(name = "Binding Consistent hash exchange with binding key \"{0}\" throws IllegalArgumentException")
78+
@ValueSource(strings = {"", "string", "#"})
79+
void binding_with_non_integer_key_throws_exception(String bindingKey) {
80+
SingleReceiverExchange consistentHashEx = (SingleReceiverExchange) mockExchangeFactory.build("test", "x-consistent-hash", empty(), mock(ReceiverRegistry.class));
81+
82+
ReceiverPointer q1 = new ReceiverPointer(ReceiverPointer.Type.QUEUE, "Q1");
83+
84+
assertThatThrownBy(() -> consistentHashEx.bind(q1, bindingKey, emptyMap()))
85+
.isInstanceOf(IllegalArgumentException.class)
86+
.hasMessage("The binding key must be an integer");
7587
}
7688
}

0 commit comments

Comments
 (0)