From 7fdbc63390288b598723eb6cbbe7065023fd4be2 Mon Sep 17 00:00:00 2001 From: Dale Lee <112548822+ldyedu@users.noreply.github.com> Date: Tue, 28 Nov 2023 10:19:42 +0800 Subject: [PATCH] For #10374, format code using Nacos Style (#11438) --- .../api/naming/selector/NamingContext.java | 8 ++--- .../api/naming/selector/NamingResult.java | 1 + .../api/naming/selector/NamingSelector.java | 1 + .../api/selector/client/SelectResult.java | 1 + .../nacos/api/selector/client/Selector.java | 1 + .../client/naming/event/InstancesDiff.java | 36 +++++++++++-------- .../AbstractNamingChangeListener.java | 4 ++- .../naming/listener/NamingChangeEvent.java | 20 ++++++----- .../selector/DefaultNamingSelector.java | 14 ++++---- .../selector/NamingListenerInvoker.java | 13 +++---- .../selector/AbstractSelectorWrapper.java | 22 ++++++------ .../client/selector/ListenerInvoker.java | 1 + .../event/InstancesChangeEventTest.java | 3 +- .../event/InstancesChangeNotifierTest.java | 13 +++---- .../naming/event/InstancesDiffTest.java | 34 +++++++----------- .../listener/NamingChangeEventTest.java | 23 ++++++------ .../selector/DefaultNamingSelectorTest.java | 14 ++++---- .../selector/NamingListenerInvokerTest.java | 7 ++-- .../client/selector/SelectorManagerTest.java | 18 +++++----- 19 files changed, 121 insertions(+), 113 deletions(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/selector/NamingContext.java b/api/src/main/java/com/alibaba/nacos/api/naming/selector/NamingContext.java index 04b340a6f..ae94a8902 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/selector/NamingContext.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/selector/NamingContext.java @@ -26,28 +26,28 @@ import java.util.List; * @author lideyou */ public interface NamingContext { - + /** * Get service name. * * @return service name */ String getServiceName(); - + /** * Get group name. * * @return group name */ String getGroupName(); - + /** * Get clusters. * * @return clusters */ String getClusters(); - + /** * Get current instances. * diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/selector/NamingResult.java b/api/src/main/java/com/alibaba/nacos/api/naming/selector/NamingResult.java index a3173af9c..370616775 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/selector/NamingResult.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/selector/NamingResult.java @@ -27,4 +27,5 @@ import java.util.List; * @author lideyou */ public interface NamingResult extends SelectResult> { + } diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/selector/NamingSelector.java b/api/src/main/java/com/alibaba/nacos/api/naming/selector/NamingSelector.java index 48edd76d5..252a56977 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/selector/NamingSelector.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/selector/NamingSelector.java @@ -24,4 +24,5 @@ import com.alibaba.nacos.api.selector.client.Selector; * @author lideyou */ public interface NamingSelector extends Selector { + } diff --git a/api/src/main/java/com/alibaba/nacos/api/selector/client/SelectResult.java b/api/src/main/java/com/alibaba/nacos/api/selector/client/SelectResult.java index 3c5979d9e..951f69bd6 100644 --- a/api/src/main/java/com/alibaba/nacos/api/selector/client/SelectResult.java +++ b/api/src/main/java/com/alibaba/nacos/api/selector/client/SelectResult.java @@ -23,6 +23,7 @@ package com.alibaba.nacos.api.selector.client; * @author lideyou */ public interface SelectResult { + /** * Get select result. * diff --git a/api/src/main/java/com/alibaba/nacos/api/selector/client/Selector.java b/api/src/main/java/com/alibaba/nacos/api/selector/client/Selector.java index 523b426d6..131e777b0 100644 --- a/api/src/main/java/com/alibaba/nacos/api/selector/client/Selector.java +++ b/api/src/main/java/com/alibaba/nacos/api/selector/client/Selector.java @@ -24,6 +24,7 @@ package com.alibaba.nacos.api.selector.client; * @author lideyou */ public interface Selector { + /** * select the target result. * diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/event/InstancesDiff.java b/client/src/main/java/com/alibaba/nacos/client/naming/event/InstancesDiff.java index c374331a7..5026499f6 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/event/InstancesDiff.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/event/InstancesDiff.java @@ -29,80 +29,86 @@ import java.util.List; * @author lideyou */ public class InstancesDiff { + private final List addedInstances = new ArrayList<>(); - + private final List removedInstances = new ArrayList<>(); - + private final List modifiedInstances = new ArrayList<>(); - + public InstancesDiff() { } - - public InstancesDiff(List addedInstances, List removedInstances, List modifiedInstances) { + + public InstancesDiff(List addedInstances, List removedInstances, + List modifiedInstances) { setAddedInstances(addedInstances); setRemovedInstances(removedInstances); setModifiedInstances(modifiedInstances); } - + public List getAddedInstances() { return addedInstances; } - + public void setAddedInstances(Collection addedInstances) { this.addedInstances.clear(); if (CollectionUtils.isNotEmpty(addedInstances)) { this.addedInstances.addAll(addedInstances); } } - + public List getRemovedInstances() { return removedInstances; } - + public void setRemovedInstances(Collection removedInstances) { this.removedInstances.clear(); if (CollectionUtils.isNotEmpty(removedInstances)) { this.removedInstances.addAll(removedInstances); } } - + public List getModifiedInstances() { return modifiedInstances; } - + public void setModifiedInstances(Collection modifiedInstances) { this.modifiedInstances.clear(); if (CollectionUtils.isNotEmpty(modifiedInstances)) { this.modifiedInstances.addAll(modifiedInstances); } } - + /** * Check if any instances have changed. + * * @return true if there are instances that have changed */ public boolean hasDifferent() { return isAdded() || isRemoved() || isModified(); } - + /** * Check if any instances have been added. + * * @return true if there are instances that have been added. */ public boolean isAdded() { return CollectionUtils.isNotEmpty(this.addedInstances); } - + /** * Check if any instances have been added. + * * @return true if there are instances that have been added. */ public boolean isRemoved() { return CollectionUtils.isNotEmpty(this.removedInstances); } - + /** * Check if any instances have been added. + * * @return true if there are instances that have been added. */ public boolean isModified() { diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/listener/AbstractNamingChangeListener.java b/client/src/main/java/com/alibaba/nacos/client/naming/listener/AbstractNamingChangeListener.java index cb54c4356..932147359 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/listener/AbstractNamingChangeListener.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/listener/AbstractNamingChangeListener.java @@ -25,15 +25,17 @@ 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) { onChange((NamingChangeEvent) event); } } - + /** * Callback when instances have changed. + * * @param event NamingChangeEvent */ public abstract void onChange(NamingChangeEvent event); diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/listener/NamingChangeEvent.java b/client/src/main/java/com/alibaba/nacos/client/naming/listener/NamingChangeEvent.java index 764afd0a2..62b4dbf33 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/listener/NamingChangeEvent.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/listener/NamingChangeEvent.java @@ -28,38 +28,40 @@ import java.util.List; * @author lideyou */ public class NamingChangeEvent extends NamingEvent { + private final InstancesDiff instancesDiff; - + public NamingChangeEvent(String serviceName, List instances, InstancesDiff instancesDiff) { super(serviceName, instances); this.instancesDiff = instancesDiff; } - - public NamingChangeEvent(String serviceName, String groupName, String clusters, List instances, InstancesDiff instancesDiff) { + + public NamingChangeEvent(String serviceName, String groupName, String clusters, List instances, + InstancesDiff instancesDiff) { super(serviceName, groupName, clusters, instances); this.instancesDiff = instancesDiff; } - + public boolean isAdded() { return this.instancesDiff.isAdded(); } - + public boolean isRemoved() { return this.instancesDiff.isRemoved(); } - + public boolean isModified() { return this.instancesDiff.isModified(); } - + public List getAddedInstances() { return this.instancesDiff.getAddedInstances(); } - + public List getRemovedInstances() { return this.instancesDiff.getRemovedInstances(); } - + public List getModifiedInstances() { return this.instancesDiff.getModifiedInstances(); } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/selector/DefaultNamingSelector.java b/client/src/main/java/com/alibaba/nacos/client/naming/selector/DefaultNamingSelector.java index fdd274d33..ad15508df 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/selector/DefaultNamingSelector.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/selector/DefaultNamingSelector.java @@ -32,23 +32,21 @@ import java.util.stream.Collectors; * @author lideyou */ public class DefaultNamingSelector implements NamingSelector { + private final Predicate filter; - + public DefaultNamingSelector(Predicate filter) { this.filter = filter; } - + @Override public NamingResult select(NamingContext context) { List instances = doFilter(context.getInstances()); return () -> instances; } - + private List doFilter(List 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()); } } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/selector/NamingListenerInvoker.java b/client/src/main/java/com/alibaba/nacos/client/naming/selector/NamingListenerInvoker.java index 377f1c1b5..88279bf4e 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/selector/NamingListenerInvoker.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/selector/NamingListenerInvoker.java @@ -29,12 +29,13 @@ import java.util.Objects; * @author lideyou */ public class NamingListenerInvoker implements ListenerInvoker { + private final EventListener listener; - + public NamingListenerInvoker(EventListener listener) { this.listener = listener; } - + @Override public void invoke(NamingEvent event) { if (listener instanceof AbstractEventListener && ((AbstractEventListener) listener).getExecutor() != null) { @@ -43,21 +44,21 @@ public class NamingListenerInvoker implements ListenerInvoker { listener.onEvent(event); } } - + @Override public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) { return false; } - + if (this == o) { return true; } - + NamingListenerInvoker that = (NamingListenerInvoker) o; return Objects.equals(listener, that.listener); } - + @Override public int hashCode() { return Objects.hashCode(listener); diff --git a/client/src/main/java/com/alibaba/nacos/client/selector/AbstractSelectorWrapper.java b/client/src/main/java/com/alibaba/nacos/client/selector/AbstractSelectorWrapper.java index 1a7820ff3..edbab0810 100644 --- a/client/src/main/java/com/alibaba/nacos/client/selector/AbstractSelectorWrapper.java +++ b/client/src/main/java/com/alibaba/nacos/client/selector/AbstractSelectorWrapper.java @@ -30,15 +30,16 @@ import java.util.Objects; * @author lideyou */ public abstract class AbstractSelectorWrapper, E, T extends Event> { + private final S selector; - + private final ListenerInvoker listener; - + public AbstractSelectorWrapper(S selector, ListenerInvoker listener) { this.selector = selector; this.listener = listener; } - + /** * Check whether the event can be callback. * @@ -46,7 +47,7 @@ public abstract class AbstractSelectorWrapper, E, T ext * @return true if the event can be callback */ protected abstract boolean isSelectable(T event); - + /** * Check whether the result can be callback. * @@ -54,14 +55,15 @@ public abstract class AbstractSelectorWrapper, E, T ext * @return true if the result can be callback */ protected abstract boolean isCallable(E event); - + /** * Build an event received by the listener. + * * @param event original event * @return listener event */ protected abstract E buildListenerEvent(T event); - + /** * Notify listener. * @@ -76,15 +78,15 @@ public abstract class AbstractSelectorWrapper, E, T ext listener.invoke(newEvent); } } - + public ListenerInvoker getListener() { return this.listener; } - + public S getSelector() { return this.selector; } - + @Override public boolean equals(Object o) { if (this == o) { @@ -96,7 +98,7 @@ public abstract class AbstractSelectorWrapper, E, T ext AbstractSelectorWrapper that = (AbstractSelectorWrapper) o; return Objects.equals(selector, that.selector) && Objects.equals(listener, that.listener); } - + @Override public int hashCode() { return Objects.hash(selector, listener); diff --git a/client/src/main/java/com/alibaba/nacos/client/selector/ListenerInvoker.java b/client/src/main/java/com/alibaba/nacos/client/selector/ListenerInvoker.java index 42104762a..81996eea7 100644 --- a/client/src/main/java/com/alibaba/nacos/client/selector/ListenerInvoker.java +++ b/client/src/main/java/com/alibaba/nacos/client/selector/ListenerInvoker.java @@ -23,6 +23,7 @@ package com.alibaba.nacos.client.selector; * @author lideyou */ public interface ListenerInvoker { + /** * Invoke inner listener. * diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/event/InstancesChangeEventTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/event/InstancesChangeEventTest.java index 742573b82..9392ce17e 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/event/InstancesChangeEventTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/event/InstancesChangeEventTest.java @@ -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()); diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/event/InstancesChangeNotifierTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/event/InstancesChangeNotifierTest.java index 728657cc9..3c6cd63f9 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/event/InstancesChangeNotifierTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/event/InstancesChangeNotifierTest.java @@ -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 subscribeServices = instancesChangeNotifier.getSubscribeServices(); Assert.assertEquals(1, subscribeServices.size()); @@ -94,9 +93,8 @@ public class InstancesChangeNotifierTest { EventListener listener = Mockito.mock(EventListener.class); 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)); } @@ -111,9 +109,8 @@ public class InstancesChangeNotifierTest { InstancesChangeNotifier instancesChangeNotifier = new InstancesChangeNotifier(eventScope); 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); diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/event/InstancesDiffTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/event/InstancesDiffTest.java index cc62821d5..0842558a8 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/event/InstancesDiffTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/event/InstancesDiffTest.java @@ -27,24 +27,19 @@ 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)); instancesDiff.setRemovedInstances(Collections.singletonList(removedIns)); instancesDiff.setModifiedInstances(Collections.singletonList(modifiedIns)); - + Assert.assertTrue(instancesDiff.hasDifferent()); Assert.assertTrue(instancesDiff.isAdded()); Assert.assertTrue(instancesDiff.isRemoved()); @@ -53,19 +48,16 @@ public class InstancesDiffTest { Assert.assertEquals(removedIns, instancesDiff.getRemovedInstances().get(0)); Assert.assertEquals(modifiedIns, instancesDiff.getModifiedInstances().get(0)); } - + @Test public void testWithFullConstructor() { Random random = new Random(); 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()); Assert.assertTrue(instancesDiff.isRemoved()); @@ -82,7 +74,7 @@ public class InstancesDiffTest { Assert.assertFalse(instancesDiff.isRemoved()); Assert.assertFalse(instancesDiff.isModified()); } - + @Test public void testWithNoConstructor() { Random random = new Random(); @@ -93,7 +85,7 @@ public class InstancesDiffTest { instancesDiff.setAddedInstances(getInstanceList(addedCount)); instancesDiff.setRemovedInstances(getInstanceList(removedCount)); instancesDiff.setModifiedInstances(getInstanceList(modifiedCount)); - + Assert.assertTrue(instancesDiff.hasDifferent()); Assert.assertEquals(addedCount, instancesDiff.getAddedInstances().size()); Assert.assertEquals(removedCount, instancesDiff.getRemovedInstances().size()); @@ -106,7 +98,7 @@ public class InstancesDiffTest { Assert.assertFalse(instancesDiff.isRemoved()); Assert.assertFalse(instancesDiff.isModified()); } - + private static List getInstanceList(int count) { ArrayList list = new ArrayList<>(count); for (int i = 0; i < count; i++) { diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/listener/NamingChangeEventTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/listener/NamingChangeEventTest.java index 90c90bfe9..66278c13a 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/listener/NamingChangeEventTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/listener/NamingChangeEventTest.java @@ -30,11 +30,11 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertFalse; public class NamingChangeEventTest { - + private MockNamingEventListener eventListener; - + private InstancesDiff instancesDiff; - + @Before public void setUp() throws Exception { eventListener = new MockNamingEventListener(); @@ -43,7 +43,7 @@ public class NamingChangeEventTest { instancesDiff.setRemovedInstances(Arrays.asList(new Instance(), new Instance())); instancesDiff.setModifiedInstances(Arrays.asList(new Instance())); } - + @Test public void testNamingChangeEventWithSimpleConstructor() { NamingChangeEvent event = new NamingChangeEvent("serviceName", Collections.EMPTY_LIST, instancesDiff); @@ -69,10 +69,11 @@ public class NamingChangeEventTest { assertFalse(event.isModified()); assertEquals(0, event.getRemovedInstances().size()); } - + @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()); @@ -95,7 +96,7 @@ public class NamingChangeEventTest { assertFalse(event.isModified()); assertEquals(0, event.getRemovedInstances().size()); } - + @Test public void testGetChanges() { NamingChangeEvent event = new NamingChangeEvent("serviceName", Collections.EMPTY_LIST, instancesDiff); @@ -104,22 +105,22 @@ public class NamingChangeEventTest { event.getAddedInstances().clear(); assertFalse(event.isAdded()); assertEquals(0, event.getAddedInstances().size()); - + assertTrue(event.isRemoved()); assertEquals(2, event.getRemovedInstances().size()); event.getRemovedInstances().clear(); assertFalse(event.isRemoved()); assertEquals(0, event.getRemovedInstances().size()); - + assertTrue(event.isModified()); assertEquals(1, event.getModifiedInstances().size()); event.getModifiedInstances().clear(); assertFalse(event.isModified()); assertEquals(0, event.getRemovedInstances().size()); } - + private static class MockNamingEventListener extends AbstractNamingChangeListener { - + @Override public void onChange(NamingChangeEvent event) { assertNull(getExecutor()); diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/selector/DefaultNamingSelectorTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/selector/DefaultNamingSelectorTest.java index 4915af36d..7860d5112 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/selector/DefaultNamingSelectorTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/selector/DefaultNamingSelectorTest.java @@ -31,27 +31,27 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public class DefaultNamingSelectorTest { - + @Test public void testSelect() { DefaultNamingSelector namingSelector = new DefaultNamingSelector(Instance::isHealthy); Random random = new Random(); int total = random.nextInt(32) + 1; int health = random.nextInt(total); - + NamingContext namingContext = getMockNamingContext(total, health); NamingResult result = namingSelector.select(namingContext); - + assertEquals(health, result.getResult().size()); result.getResult().forEach(ins -> assertTrue(ins.isHealthy())); } - + private NamingContext getMockNamingContext(int total, int health) { NamingContext namingContext = mock(NamingContext.class); when(namingContext.getInstances()).thenReturn(getInstance(total, health)); return namingContext; } - + private List getInstance(int total, int health) { List list = new ArrayList<>(total); for (int i = 0; i < total; i++) { @@ -59,11 +59,11 @@ public class DefaultNamingSelectorTest { instance.setHealthy(false); list.add(instance); } - + for (int i = 0; i < health; i++) { list.get(i).setHealthy(true); } - + return list; } } diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/selector/NamingListenerInvokerTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/selector/NamingListenerInvokerTest.java index 5f49d6044..0614bca0c 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/selector/NamingListenerInvokerTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/selector/NamingListenerInvokerTest.java @@ -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); @@ -41,7 +42,7 @@ public class NamingListenerInvokerTest { listenerInvoker.invoke(event); verify(listener).onEvent(event); } - + @Test public void testAbstractEventListener() { AbstractEventListener listener = mock(AbstractEventListener.class); @@ -50,7 +51,7 @@ public class NamingListenerInvokerTest { listenerInvoker.invoke(event); verify(listener).getExecutor(); } - + @Test public void testAbstractNamingChaneEventListener() { AbstractNamingChangeListener listener = spy(AbstractNamingChangeListener.class); @@ -59,7 +60,7 @@ public class NamingListenerInvokerTest { listenerInvoker.invoke(event); verify(listener).onChange(event); } - + @Test public void testEquals() { EventListener listener1 = mock(EventListener.class); diff --git a/client/src/test/java/com/alibaba/nacos/client/selector/SelectorManagerTest.java b/client/src/test/java/com/alibaba/nacos/client/selector/SelectorManagerTest.java index 31b3b6a31..b3cbace49 100644 --- a/client/src/test/java/com/alibaba/nacos/client/selector/SelectorManagerTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/selector/SelectorManagerTest.java @@ -30,7 +30,7 @@ import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; public class SelectorManagerTest { - + @Test public void testCurd() { SelectorManager selectorManager = new SelectorManager<>(); @@ -41,7 +41,7 @@ public class SelectorManagerTest { selectorManager.removeSelectorWrapper(subId, sw); assertNull(selectorManager.getSelectorWrappers(subId)); } - + @Test public void testSubInfo() { SelectorManager selectorManager = new SelectorManager<>(); @@ -49,36 +49,36 @@ public class SelectorManagerTest { for (int i = 0; i < 64; i++) { list.add(generateRandomString(2, 32)); } - + for (String subId : list) { selectorManager.addSelectorWrapper(subId, mock(NamingSelectorWrapper.class)); assertTrue(selectorManager.isSubscribed(subId)); } - + Set subsSet = selectorManager.getSubscriptions(); for (String subId : subsSet) { assertTrue(list.contains(subId)); } - + for (String subId : list) { selectorManager.removeSubscription(subId); assertFalse(selectorManager.isSubscribed(subId)); } } - + private static String generateRandomString(int minLength, int maxLength) { String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; - + Random random = new Random(); int length = random.nextInt(maxLength - minLength + 1) + minLength; StringBuilder sb = new StringBuilder(); - + for (int i = 0; i < length; i++) { int index = random.nextInt(characters.length()); char randomChar = characters.charAt(index); sb.append(randomChar); } - + return sb.toString(); } }