parent
263e223d94
commit
7fdbc63390
@ -27,4 +27,5 @@ import java.util.List;
|
||||
* @author lideyou
|
||||
*/
|
||||
public interface NamingResult extends SelectResult<List<Instance>> {
|
||||
|
||||
}
|
||||
|
@ -24,4 +24,5 @@ import com.alibaba.nacos.api.selector.client.Selector;
|
||||
* @author lideyou
|
||||
*/
|
||||
public interface NamingSelector extends Selector<NamingContext, NamingResult> {
|
||||
|
||||
}
|
||||
|
@ -23,6 +23,7 @@ package com.alibaba.nacos.api.selector.client;
|
||||
* @author lideyou
|
||||
*/
|
||||
public interface SelectResult<T> {
|
||||
|
||||
/**
|
||||
* Get select result.
|
||||
*
|
||||
|
@ -24,6 +24,7 @@ package com.alibaba.nacos.api.selector.client;
|
||||
* @author lideyou
|
||||
*/
|
||||
public interface Selector<C, E> {
|
||||
|
||||
/**
|
||||
* select the target result.
|
||||
*
|
||||
|
@ -29,6 +29,7 @@ import java.util.List;
|
||||
* @author lideyou
|
||||
*/
|
||||
public class InstancesDiff {
|
||||
|
||||
private final List<Instance> addedInstances = new ArrayList<>();
|
||||
|
||||
private final List<Instance> removedInstances = new ArrayList<>();
|
||||
@ -38,7 +39,8 @@ public class InstancesDiff {
|
||||
public InstancesDiff() {
|
||||
}
|
||||
|
||||
public InstancesDiff(List<Instance> addedInstances, List<Instance> removedInstances, List<Instance> modifiedInstances) {
|
||||
public InstancesDiff(List<Instance> addedInstances, List<Instance> removedInstances,
|
||||
List<Instance> modifiedInstances) {
|
||||
setAddedInstances(addedInstances);
|
||||
setRemovedInstances(removedInstances);
|
||||
setModifiedInstances(modifiedInstances);
|
||||
@ -79,6 +81,7 @@ public class InstancesDiff {
|
||||
|
||||
/**
|
||||
* Check if any instances have changed.
|
||||
*
|
||||
* @return true if there are instances that have changed
|
||||
*/
|
||||
public boolean hasDifferent() {
|
||||
@ -87,6 +90,7 @@ public class InstancesDiff {
|
||||
|
||||
/**
|
||||
* Check if any instances have been added.
|
||||
*
|
||||
* @return true if there are instances that have been added.
|
||||
*/
|
||||
public boolean isAdded() {
|
||||
@ -95,6 +99,7 @@ public class InstancesDiff {
|
||||
|
||||
/**
|
||||
* Check if any instances have been added.
|
||||
*
|
||||
* @return true if there are instances that have been added.
|
||||
*/
|
||||
public boolean isRemoved() {
|
||||
@ -103,6 +108,7 @@ public class InstancesDiff {
|
||||
|
||||
/**
|
||||
* Check if any instances have been added.
|
||||
*
|
||||
* @return true if there are instances that have been added.
|
||||
*/
|
||||
public boolean isModified() {
|
||||
|
@ -25,6 +25,7 @@ import com.alibaba.nacos.api.naming.listener.Event;
|
||||
* @author lideyou
|
||||
*/
|
||||
public abstract class AbstractNamingChangeListener extends AbstractEventListener {
|
||||
|
||||
@Override
|
||||
public final void onEvent(Event event) {
|
||||
if (event instanceof NamingChangeEvent) {
|
||||
@ -34,6 +35,7 @@ public abstract class AbstractNamingChangeListener extends AbstractEventListener
|
||||
|
||||
/**
|
||||
* Callback when instances have changed.
|
||||
*
|
||||
* @param event NamingChangeEvent
|
||||
*/
|
||||
public abstract void onChange(NamingChangeEvent event);
|
||||
|
@ -28,6 +28,7 @@ import java.util.List;
|
||||
* @author lideyou
|
||||
*/
|
||||
public class NamingChangeEvent extends NamingEvent {
|
||||
|
||||
private final InstancesDiff instancesDiff;
|
||||
|
||||
public NamingChangeEvent(String serviceName, List<Instance> instances, InstancesDiff instancesDiff) {
|
||||
@ -35,7 +36,8 @@ public class NamingChangeEvent extends NamingEvent {
|
||||
this.instancesDiff = instancesDiff;
|
||||
}
|
||||
|
||||
public NamingChangeEvent(String serviceName, String groupName, String clusters, List<Instance> instances, InstancesDiff instancesDiff) {
|
||||
public NamingChangeEvent(String serviceName, String groupName, String clusters, List<Instance> instances,
|
||||
InstancesDiff instancesDiff) {
|
||||
super(serviceName, groupName, clusters, instances);
|
||||
this.instancesDiff = instancesDiff;
|
||||
}
|
||||
|
@ -32,6 +32,7 @@ import java.util.stream.Collectors;
|
||||
* @author lideyou
|
||||
*/
|
||||
public class DefaultNamingSelector implements NamingSelector {
|
||||
|
||||
private final Predicate<Instance> filter;
|
||||
|
||||
public DefaultNamingSelector(Predicate<Instance> filter) {
|
||||
@ -45,10 +46,7 @@ public class DefaultNamingSelector implements NamingSelector {
|
||||
}
|
||||
|
||||
private List<Instance> doFilter(List<Instance> instances) {
|
||||
return instances == null ? Collections.emptyList() :
|
||||
instances
|
||||
.stream()
|
||||
.filter(filter)
|
||||
.collect(Collectors.toList());
|
||||
return instances == null ? Collections.emptyList()
|
||||
: instances.stream().filter(filter).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
@ -29,6 +29,7 @@ import java.util.Objects;
|
||||
* @author lideyou
|
||||
*/
|
||||
public class NamingListenerInvoker implements ListenerInvoker<NamingEvent> {
|
||||
|
||||
private final EventListener listener;
|
||||
|
||||
public NamingListenerInvoker(EventListener listener) {
|
||||
|
@ -30,6 +30,7 @@ import java.util.Objects;
|
||||
* @author lideyou
|
||||
*/
|
||||
public abstract class AbstractSelectorWrapper<S extends Selector<?, ?>, E, T extends Event> {
|
||||
|
||||
private final S selector;
|
||||
|
||||
private final ListenerInvoker<E> listener;
|
||||
@ -57,6 +58,7 @@ public abstract class AbstractSelectorWrapper<S extends Selector<?, ?>, E, T ext
|
||||
|
||||
/**
|
||||
* Build an event received by the listener.
|
||||
*
|
||||
* @param event original event
|
||||
* @return listener event
|
||||
*/
|
||||
|
@ -23,6 +23,7 @@ package com.alibaba.nacos.client.selector;
|
||||
* @author lideyou
|
||||
*/
|
||||
public interface ListenerInvoker<E> {
|
||||
|
||||
/**
|
||||
* Invoke inner listener.
|
||||
*
|
||||
|
@ -36,7 +36,8 @@ public class InstancesChangeEventTest {
|
||||
hosts.add(ins);
|
||||
InstancesDiff diff = new InstancesDiff();
|
||||
diff.setAddedInstances(hosts);
|
||||
InstancesChangeEvent event = new InstancesChangeEvent(eventScope, serviceName, groupName, clusters, hosts, diff);
|
||||
InstancesChangeEvent event = new InstancesChangeEvent(eventScope, serviceName, groupName, clusters, hosts,
|
||||
diff);
|
||||
Assert.assertEquals(eventScope, event.scope());
|
||||
Assert.assertEquals(serviceName, event.getServiceName());
|
||||
Assert.assertEquals(clusters, event.getClusters());
|
||||
|
@ -45,8 +45,7 @@ public class InstancesChangeNotifierTest {
|
||||
InstancesChangeNotifier instancesChangeNotifier = new InstancesChangeNotifier(eventScope);
|
||||
EventListener listener = Mockito.mock(EventListener.class);
|
||||
NamingSelector selector = NamingSelectorFactory.newClusterSelector(clusters);
|
||||
NamingSelectorWrapper wrapper = new NamingSelectorWrapper(name, group, clusterStr, selector,
|
||||
listener);
|
||||
NamingSelectorWrapper wrapper = new NamingSelectorWrapper(name, group, clusterStr, selector, listener);
|
||||
instancesChangeNotifier.registerListener(group, name, wrapper);
|
||||
List<ServiceInfo> subscribeServices = instancesChangeNotifier.getSubscribeServices();
|
||||
Assert.assertEquals(1, subscribeServices.size());
|
||||
@ -95,8 +94,7 @@ public class InstancesChangeNotifierTest {
|
||||
NamingSelector selector = NamingSelectorFactory.newClusterSelector(clusters);
|
||||
Assert.assertFalse(instancesChangeNotifier.isSubscribed(group, name));
|
||||
|
||||
NamingSelectorWrapper wrapper = new NamingSelectorWrapper(name, group, clusterStr, selector,
|
||||
listener);
|
||||
NamingSelectorWrapper wrapper = new NamingSelectorWrapper(name, group, clusterStr, selector, listener);
|
||||
instancesChangeNotifier.registerListener(group, name, wrapper);
|
||||
Assert.assertTrue(instancesChangeNotifier.isSubscribed(group, name));
|
||||
}
|
||||
@ -112,8 +110,7 @@ public class InstancesChangeNotifierTest {
|
||||
NamingSelector selector = NamingSelectorFactory.newClusterSelector(clusters);
|
||||
EventListener listener = Mockito.mock(EventListener.class);
|
||||
|
||||
NamingSelectorWrapper wrapper = new NamingSelectorWrapper(name, group, clusterStr, selector,
|
||||
listener);
|
||||
NamingSelectorWrapper wrapper = new NamingSelectorWrapper(name, group, clusterStr, selector, listener);
|
||||
instancesChangeNotifier.registerListener(group, name, wrapper);
|
||||
Instance instance = new Instance();
|
||||
InstancesDiff diff = new InstancesDiff(null, Collections.singletonList(instance), null);
|
||||
|
@ -27,18 +27,13 @@ import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class InstancesDiffTest {
|
||||
|
||||
@Test
|
||||
public void testGetDiff() {
|
||||
String serviceName = "testService";
|
||||
Instance addedIns = InstanceBuilder.newBuilder()
|
||||
.setServiceName(serviceName)
|
||||
.setClusterName("a").build();
|
||||
Instance removedIns = InstanceBuilder.newBuilder()
|
||||
.setServiceName(serviceName)
|
||||
.setClusterName("b").build();
|
||||
Instance modifiedIns = InstanceBuilder.newBuilder()
|
||||
.setServiceName(serviceName)
|
||||
.setClusterName("c").build();
|
||||
Instance addedIns = InstanceBuilder.newBuilder().setServiceName(serviceName).setClusterName("a").build();
|
||||
Instance removedIns = InstanceBuilder.newBuilder().setServiceName(serviceName).setClusterName("b").build();
|
||||
Instance modifiedIns = InstanceBuilder.newBuilder().setServiceName(serviceName).setClusterName("c").build();
|
||||
|
||||
InstancesDiff instancesDiff = new InstancesDiff();
|
||||
instancesDiff.setAddedInstances(Collections.singletonList(addedIns));
|
||||
@ -60,11 +55,8 @@ public class InstancesDiffTest {
|
||||
int addedCount = random.nextInt(32) + 1;
|
||||
int removedCount = random.nextInt(32) + 1;
|
||||
int modifiedCount = random.nextInt(32) + 1;
|
||||
InstancesDiff instancesDiff = new InstancesDiff(
|
||||
getInstanceList(addedCount),
|
||||
getInstanceList(removedCount),
|
||||
getInstanceList(modifiedCount)
|
||||
);
|
||||
InstancesDiff instancesDiff = new InstancesDiff(getInstanceList(addedCount), getInstanceList(removedCount),
|
||||
getInstanceList(modifiedCount));
|
||||
|
||||
Assert.assertTrue(instancesDiff.hasDifferent());
|
||||
Assert.assertTrue(instancesDiff.isAdded());
|
||||
|
@ -72,7 +72,8 @@ public class NamingChangeEventTest {
|
||||
|
||||
@Test
|
||||
public void testNamingChangeEventWithFullConstructor() {
|
||||
NamingChangeEvent event = new NamingChangeEvent("serviceName", "group", "clusters", Collections.EMPTY_LIST, instancesDiff);
|
||||
NamingChangeEvent event = new NamingChangeEvent("serviceName", "group", "clusters", Collections.EMPTY_LIST,
|
||||
instancesDiff);
|
||||
assertEquals("serviceName", event.getServiceName());
|
||||
assertEquals("group", event.getGroupName());
|
||||
assertEquals("clusters", event.getClusters());
|
||||
|
@ -33,6 +33,7 @@ import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
public class NamingListenerInvokerTest {
|
||||
|
||||
@Test
|
||||
public void testEventListener() {
|
||||
EventListener listener = mock(EventListener.class);
|
||||
|
Loading…
Reference in New Issue
Block a user