11package 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
1010import java .util .Map ;
1111import java .util .Optional ;
1414import java .util .stream .Collectors ;
1515import 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
2325class 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