Skip to content

Commit

Permalink
Issue fridujo#127 - it is illegal to bind to a consistent hash exchan…
Browse files Browse the repository at this point in the history
…ge with a non integer binding key
  • Loading branch information
RedMu committed Jul 1, 2020
1 parent 8930b81 commit a3343ac
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<ReceiverPointer, Long> deliveriesByReceiver = IntStream.range(0, messagesCount)
Expand All @@ -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");
}
}

0 comments on commit a3343ac

Please sign in to comment.