-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
607b60f
commit c768f89
Showing
7 changed files
with
179 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
// [email protected]. | ||
package com.rabbitmq.client.amqp.impl; | ||
|
||
import static com.rabbitmq.client.amqp.Management.QueueType.QUORUM; | ||
import static com.rabbitmq.client.amqp.Management.QueueType.STREAM; | ||
import static com.rabbitmq.client.amqp.impl.AmqpConnection.enforceAffinity; | ||
import static org.assertj.core.api.Assertions.fail; | ||
|
@@ -48,6 +49,11 @@ public class AmqpConnectionAffinityUnitTest { | |
private static final String FOLLOWER2_NODENAME = "f2"; | ||
private static final Address FOLLOWER2_ADDRESS = new Address(FOLLOWER2_NODENAME, 5672); | ||
private static final String Q = "my-queue"; | ||
private static final Map<String, Address> NODES = | ||
Map.of( | ||
LEADER_NODENAME, LEADER_ADDRESS, | ||
FOLLOWER1_NODENAME, FOLLOWER1_ADDRESS, | ||
FOLLOWER2_NODENAME, FOLLOWER2_ADDRESS); | ||
|
||
AutoCloseable mocks; | ||
|
||
|
@@ -71,17 +77,39 @@ void tearDown() throws Exception { | |
} | ||
|
||
@Test | ||
void noInfoLookupIfAlreadyInCache() { | ||
void infoInCache_ShouldLookUpInfoAndCheckIt_ShouldUseConnectionIfMatch() { | ||
cache.queueInfo(info()); | ||
when(management.queueInfo(Q)).thenReturn(info()); | ||
when(cf.apply(anyList())).thenReturn(leaderConnection()); | ||
AmqpConnection.NativeConnectionWrapper w = enforceAffinity(cf, management, affinity(), cache); | ||
assertThat(w).isLeader(); | ||
verifyNoInteractions(management); | ||
verify(management, times(1)).queueInfo(Q); | ||
verify(cf, times(1)).apply(anyList()); | ||
verify(nativeConnection, never()).close(); | ||
assertThat(cache).contains(info()).hasMapping(LEADER_NODENAME, LEADER_ADDRESS); | ||
} | ||
|
||
@Test | ||
void infoInCache_ShouldLookUpInfoAndCheckIt_ShouldRetryIfConnectionDoesNotMatch() { | ||
String initialLeader = LEADER_NODENAME; | ||
when(management.queueInfo(Q)).thenReturn(info(initialLeader)); | ||
when(cf.apply(anyList())).thenReturn(leaderConnection()); | ||
AmqpConnection.NativeConnectionWrapper w = enforceAffinity(cf, management, affinity(), cache); | ||
assertThat(w).hasNodename(initialLeader); | ||
|
||
String newLeader = FOLLOWER1_NODENAME; | ||
// the QQ leader moves to another node for some reason | ||
// the cache is stale, the management is the authority | ||
when(management.queueInfo(Q)).thenReturn(info(newLeader)); | ||
when(cf.apply(anyList())).thenReturn(leaderConnection()).thenReturn(follower1Connection()); | ||
// we want the returned connection to be on the new leader | ||
w = enforceAffinity(cf, management, affinity(), cache); | ||
assertThat(w).hasNodename(newLeader); | ||
verify(management, times(2)).queueInfo(Q); | ||
verify(cf, times(3)).apply(anyList()); | ||
verify(nativeConnection, times(1)).close(); | ||
} | ||
|
||
@Test | ||
void infoLookupIfNotInCache() { | ||
when(cf.apply(anyList())).thenReturn(leaderConnection()); | ||
|
@@ -145,10 +173,20 @@ AmqpConnection.NativeConnectionWrapper follower2Connection() { | |
this.nativeConnection, FOLLOWER2_NODENAME, FOLLOWER2_ADDRESS); | ||
} | ||
|
||
AmqpConnection.NativeConnectionWrapper connection(String nodename) { | ||
return new AmqpConnection.NativeConnectionWrapper( | ||
this.nativeConnection, nodename, NODES.get(nodename)); | ||
} | ||
|
||
static ConnectionUtils.ConnectionAffinity affinity() { | ||
return new ConnectionUtils.ConnectionAffinity(Q, ConnectionSettings.Affinity.Operation.PUBLISH); | ||
} | ||
|
||
static Management.QueueInfo info(String leader) { | ||
return new TestQueueInfo( | ||
Q, QUORUM, leader, List.of(LEADER_NODENAME, FOLLOWER1_NODENAME, FOLLOWER2_NODENAME)); | ||
} | ||
|
||
static Management.QueueInfo info() { | ||
return info( | ||
Management.QueueType.QUORUM, LEADER_NODENAME, FOLLOWER1_NODENAME, FOLLOWER2_NODENAME); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters