Skip to content

Commit

Permalink
Adjust SpringCloudCommandRouter tests accordingly
Browse files Browse the repository at this point in the history
-Deviate between asserting a local and a remote member
-Use the localServiceInstance correctly

#488
  • Loading branch information
smcvb committed Mar 6, 2018
1 parent 89a17c2 commit e3efd7b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public class SpringCloudCommandRouterTest {
private static final String SERVICE_INSTANCE_ID = "SERVICEID";
private static final URI SERVICE_INSTANCE_URI = URI.create("endpoint");
private static final Predicate<? super CommandMessage<?>> COMMAND_NAME_FILTER = c -> true;
private static final boolean LOCAL_MEMBER = true;
private static final boolean REMOTE_MEMBER = false;

private SpringCloudCommandRouter testSubject;

Expand All @@ -75,8 +77,6 @@ public class SpringCloudCommandRouterTest {
private String serializedCommandFilterClassName;
private HashMap<String, String> serviceInstanceMetadata;
@Mock
private ServiceInstance serviceInstance;
@Mock
private ConsistentHashChangeListener consistentHashChangeListener;

@Before
Expand All @@ -88,16 +88,13 @@ public void setUp() throws Exception {
atomicConsistentHashField = SpringCloudCommandRouter.class.getDeclaredField(atomicConsistentHashFieldName);

serviceInstanceMetadata = new HashMap<>();
when(serviceInstance.getServiceId()).thenReturn(SERVICE_INSTANCE_ID);
when(serviceInstance.getUri()).thenReturn(SERVICE_INSTANCE_URI);
when(serviceInstance.getMetadata()).thenReturn(serviceInstanceMetadata);

when(localServiceInstance.getServiceId()).thenReturn(SERVICE_INSTANCE_ID);
when(localServiceInstance.getUri()).thenReturn(SERVICE_INSTANCE_URI);
when(localServiceInstance.getMetadata()).thenReturn(serviceInstanceMetadata);

when(discoveryClient.getServices()).thenReturn(Collections.singletonList(SERVICE_INSTANCE_ID));
when(discoveryClient.getInstances(SERVICE_INSTANCE_ID)).thenReturn(Collections.singletonList(serviceInstance));
when(discoveryClient.getInstances(SERVICE_INSTANCE_ID))
.thenReturn(Collections.singletonList(localServiceInstance));

when(routingStrategy.getRoutingKey(any())).thenReturn(ROUTING_KEY);

Expand Down Expand Up @@ -130,7 +127,7 @@ public void testFindDestinationReturnsMemberForCommandMessage() {
assertTrue(resultOptional.isPresent());
Member resultMember = resultOptional.orElseThrow(IllegalStateException::new);

assertMember(SERVICE_INSTANCE_ID, SERVICE_INSTANCE_URI, resultMember);
assertMember(SERVICE_INSTANCE_ID, SERVICE_INSTANCE_URI, REMOTE_MEMBER, resultMember);

verify(routingStrategy).getRoutingKey(TEST_COMMAND);
}
Expand All @@ -152,7 +149,7 @@ protected boolean matchesSafely(ConsistentHash item) {
return item.getMembers()
.stream()
.map(Member::name)
.anyMatch(i -> i.equals(SERVICE_INSTANCE_ID + "[" + SERVICE_INSTANCE_URI + "]"));
.anyMatch(memberName -> memberName.contains(SERVICE_INSTANCE_ID));
}

@Override
Expand All @@ -172,7 +169,7 @@ public void testUpdateMemberShipUpdatesConsistentHash() {
Set<Member> resultMemberSet = resultAtomicConsistentHash.get().getMembers();
assertFalse(resultMemberSet.isEmpty());

assertMember(SERVICE_INSTANCE_ID, SERVICE_INSTANCE_URI, resultMemberSet.iterator().next());
assertMember(SERVICE_INSTANCE_ID, SERVICE_INSTANCE_URI, LOCAL_MEMBER, resultMemberSet.iterator().next());
}

@Test
Expand All @@ -189,7 +186,7 @@ public void testUpdateMembershipsOnHeartbeatEventUpdatesConsistentHash() {
Set<Member> resultMemberSet = resultAtomicConsistentHash.get().getMembers();
assertFalse(resultMemberSet.isEmpty());

assertMember(SERVICE_INSTANCE_ID, SERVICE_INSTANCE_URI, resultMemberSet.iterator().next());
assertMember(SERVICE_INSTANCE_ID, SERVICE_INSTANCE_URI, LOCAL_MEMBER, resultMemberSet.iterator().next());

verify(discoveryClient).getServices();
verify(discoveryClient).getInstances(SERVICE_INSTANCE_ID);
Expand Down Expand Up @@ -259,7 +256,10 @@ public void testUpdateMembershipsWithVanishedMemberOnHeartbeatEventRemoveMember(

Set<Member> resultMemberSetAfterVanish = resultAtomicConsistentHashAfterVanish.get().getMembers();
assertEquals(1, resultMemberSetAfterVanish.size());
assertMember(SERVICE_INSTANCE_ID, SERVICE_INSTANCE_URI, resultMemberSetAfterVanish.iterator().next());
assertMember(SERVICE_INSTANCE_ID,
SERVICE_INSTANCE_URI,
LOCAL_MEMBER,
resultMemberSetAfterVanish.iterator().next());
}

@Test
Expand All @@ -277,7 +277,7 @@ public void testUpdateMembershipsOnHeartbeatEventFiltersInstancesWithoutCommandR
when(discoveryClient.getServices())
.thenReturn(ImmutableList.of(SERVICE_INSTANCE_ID, expectedServiceInstanceId));
when(discoveryClient.getInstances(SERVICE_INSTANCE_ID))
.thenReturn(ImmutableList.of(serviceInstance, nonCommandRouterServiceInstance));
.thenReturn(ImmutableList.of(localServiceInstance, nonCommandRouterServiceInstance));

testSubject.updateMemberships(mock(HeartbeatEvent.class));

Expand Down Expand Up @@ -375,7 +375,7 @@ public void testUpdateMembershipsOnHeartbeatEventTwoInstancesOnSameServiceIdUpda

when(discoveryClient.getServices()).thenReturn(ImmutableList.of(SERVICE_INSTANCE_ID));
when(discoveryClient.getInstances(SERVICE_INSTANCE_ID))
.thenReturn(ImmutableList.of(serviceInstance, remoteInstance));
.thenReturn(ImmutableList.of(localServiceInstance, remoteInstance));

testSubject.updateMemberships(mock(HeartbeatEvent.class));

Expand All @@ -386,15 +386,24 @@ public void testUpdateMembershipsOnHeartbeatEventTwoInstancesOnSameServiceIdUpda
assertEquals(expectedMemberSetSize, resultMemberSet.size());
}

private void assertMember(String expectedMemberName, URI expectedEndpoint, Member resultMember) {
private void assertMember(String expectedMemberName, URI expectedEndpoint, boolean localMember,
Member resultMember) {
assertEquals(resultMember.getClass(), ConsistentHash.ConsistentHashMember.class);
ConsistentHash.ConsistentHashMember result = (ConsistentHash.ConsistentHashMember) resultMember;
assertEquals(expectedMemberName + "[" + expectedEndpoint + "]", result.name());
if (localMember) {
assertTrue(result.name().contains(expectedMemberName));
} else {
assertEquals(expectedMemberName + "[" + expectedEndpoint + "]", result.name());
}
assertEquals(LOAD_FACTOR, result.segmentCount());

Optional<URI> connectionEndpointOptional = result.getConnectionEndpoint(URI.class);
assertTrue(connectionEndpointOptional.isPresent());
URI resultEndpoint = connectionEndpointOptional.orElseThrow(IllegalStateException::new);
assertEquals(resultEndpoint, expectedEndpoint);
if (localMember) {
assertFalse(connectionEndpointOptional.isPresent());
} else {
assertTrue(connectionEndpointOptional.isPresent());
URI resultEndpoint = connectionEndpointOptional.orElseThrow(IllegalStateException::new);
assertEquals(resultEndpoint, expectedEndpoint);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.collect.ImmutableList;
import org.axonframework.commandhandling.CommandMessage;
import org.axonframework.commandhandling.distributed.RoutingStrategy;
import org.axonframework.commandhandling.distributed.commandfilter.AcceptAll;
import org.axonframework.commandhandling.distributed.commandfilter.DenyAll;
import org.axonframework.serialization.xml.XStreamSerializer;
import org.junit.Before;
Expand Down Expand Up @@ -54,7 +55,7 @@ public class SpringCloudHttpBackupCommandRouterTest {
private static final int LOAD_FACTOR = 1;
private static final String SERVICE_INSTANCE_ID = "SERVICE_ID";
private static final URI SERVICE_INSTANCE_URI = URI.create("endpoint");
private static final Predicate<? super CommandMessage<?>> COMMAND_NAME_FILTER = c -> true;
private static final Predicate<? super CommandMessage<?>> COMMAND_NAME_FILTER = AcceptAll.INSTANCE;

private SpringCloudHttpBackupCommandRouter testSubject;

Expand All @@ -67,8 +68,6 @@ public class SpringCloudHttpBackupCommandRouterTest {
@Mock
private RestTemplate restTemplate;
private String messageRoutingInformationEndpoint = "/message-routing-information";
@Mock
private ServiceInstance serviceInstance;

private URI testRemoteUri = URI.create("http://remote");
private MessageRoutingInformation expectedMessageRoutingInfo;
Expand All @@ -78,16 +77,13 @@ public class SpringCloudHttpBackupCommandRouterTest {
@SuppressWarnings("unchecked")
@Before
public void setUp() {
when(serviceInstance.getServiceId()).thenReturn(SERVICE_INSTANCE_ID);
when(serviceInstance.getUri()).thenReturn(SERVICE_INSTANCE_URI);
when(serviceInstance.getMetadata()).thenReturn(new HashMap<>());

when(localServiceInstance.getServiceId()).thenReturn(SERVICE_INSTANCE_ID);
when(localServiceInstance.getUri()).thenReturn(SERVICE_INSTANCE_URI);
when(localServiceInstance.getMetadata()).thenReturn(new HashMap<>());

when(discoveryClient.getServices()).thenReturn(Collections.singletonList(SERVICE_INSTANCE_ID));
when(discoveryClient.getInstances(SERVICE_INSTANCE_ID)).thenReturn(Collections.singletonList(serviceInstance));
when(discoveryClient.getInstances(SERVICE_INSTANCE_ID))
.thenReturn(Collections.singletonList(localServiceInstance));

expectedMessageRoutingInfo =
new MessageRoutingInformation(LOAD_FACTOR, COMMAND_NAME_FILTER, new XStreamSerializer());
Expand Down Expand Up @@ -123,14 +119,9 @@ public void testGetLocalMessageRoutingInformationReturnsMessageRoutingInformatio

@Test
public void testGetMessageRoutingInformationReturnsLocalMessageRoutingInformationIfSimpleMemberIsLocal() {
ServiceInstance serviceInstance = mock(ServiceInstance.class);
when(serviceInstance.getServiceId()).thenReturn(SERVICE_INSTANCE_ID);
when(serviceInstance.getUri()).thenReturn(SERVICE_INSTANCE_URI);
when(serviceInstance.getMetadata()).thenReturn(new HashMap<>());

testSubject.updateMembership(LOAD_FACTOR, COMMAND_NAME_FILTER);

Optional<MessageRoutingInformation> result = testSubject.getMessageRoutingInformation(serviceInstance);
Optional<MessageRoutingInformation> result = testSubject.getMessageRoutingInformation(localServiceInstance);

assertTrue(result.isPresent());
assertEquals(expectedMessageRoutingInfo, result.get());
Expand Down Expand Up @@ -261,7 +252,7 @@ public void testUpdateMembershipsOnHeartbeatEventRequestsMessageRoutingInformati

when(discoveryClient.getServices()).thenReturn(ImmutableList.of(SERVICE_INSTANCE_ID));
when(discoveryClient.getInstances(SERVICE_INSTANCE_ID))
.thenReturn(ImmutableList.of(serviceInstance, remoteInstance));
.thenReturn(ImmutableList.of(localServiceInstance, remoteInstance));

testSubject.updateMemberships(mock(HeartbeatEvent.class));

Expand Down

0 comments on commit e3efd7b

Please sign in to comment.