diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fdc09b1c430..368d2f0ad85 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -34,4 +34,4 @@ jobs: - name: Build with Maven run: mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U - name: Test With Maven - run: mvn -Prelease-nacos clean test -DforkCount=0 + run: mvn -Prelease-nacos clean test diff --git a/naming/pom.xml b/naming/pom.xml index d6ca6a8d3b7..e457a2710f3 100644 --- a/naming/pom.xml +++ b/naming/pom.xml @@ -157,14 +157,14 @@ - - org.apache.maven.plugins - maven-surefire-plugin - - true - -Dnacos.standalone=true - - + + + + + + + + org.apache.maven.plugins maven-assembly-plugin diff --git a/naming/src/test/java/com/alibaba/nacos/naming/BaseTest.java b/naming/src/test/java/com/alibaba/nacos/naming/BaseTest.java index 6507faed0b0..3d1ca30a821 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/BaseTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/BaseTest.java @@ -91,7 +91,7 @@ public abstract class BaseTest { protected UpgradeJudgement upgradeJudgement; @Spy - private MockEnvironment environment; + protected MockEnvironment environment; @Before public void before() { diff --git a/naming/src/test/java/com/alibaba/nacos/naming/consistency/persistent/PersistentConsistencyServiceDelegateImplTest.java b/naming/src/test/java/com/alibaba/nacos/naming/consistency/persistent/PersistentConsistencyServiceDelegateImplTest.java index 15e4af433c3..17d180e3232 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/consistency/persistent/PersistentConsistencyServiceDelegateImplTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/consistency/persistent/PersistentConsistencyServiceDelegateImplTest.java @@ -64,6 +64,7 @@ public void setUp() throws Exception { MockEnvironment environment = new MockEnvironment(); environment.setProperty(Constants.SUPPORT_UPGRADE_FROM_1X, "true"); EnvUtil.setEnvironment(environment); + EnvUtil.setIsStandalone(true); oldPersistentConsistencyServiceDelegate = new PersistentConsistencyServiceDelegateImpl(clusterVersionJudgement, raftConsistencyService, protocolManager); diff --git a/naming/src/test/java/com/alibaba/nacos/naming/controllers/CatalogControllerTest.java b/naming/src/test/java/com/alibaba/nacos/naming/controllers/CatalogControllerTest.java index 13ad94c47f7..9ff9ddbff1b 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/controllers/CatalogControllerTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/controllers/CatalogControllerTest.java @@ -97,7 +97,7 @@ public void testServiceDetail() throws Exception { assertTrue(actual.contains("\"groupName\":\"test-group-name\"")); assertTrue(actual.contains("\"metadata\":{}")); assertTrue(actual.contains("\"name\":\"test-service\"")); - assertTrue(actual.contains("\"selector\":{\"type\":\"none\"}")); + assertTrue(actual.contains("\"selector\":{\"type\":\"none\",\"contextType\":\"NONE\"}")); assertTrue(actual.contains("\"protectThreshold\":12.34")); assertTrue(actual.contains("\"clusters\":[{")); assertTrue(actual.contains("\"defaultCheckPort\":80")); diff --git a/naming/src/test/java/com/alibaba/nacos/naming/controllers/InstanceControllerV2Test.java b/naming/src/test/java/com/alibaba/nacos/naming/controllers/InstanceControllerV2Test.java index a73b12979f7..217c6c1b4c4 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/controllers/InstanceControllerV2Test.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/controllers/InstanceControllerV2Test.java @@ -51,7 +51,6 @@ public class InstanceControllerV2Test extends BaseTest { public void before() { super.before(); ReflectionTestUtils.setField(instanceControllerV2, "instanceServiceV2", instanceServiceV2); - mockInjectPushServer(); mockmvc = MockMvcBuilders.standaloneSetup(instanceControllerV2).build(); } diff --git a/naming/src/test/java/com/alibaba/nacos/naming/core/DistroMapperTest.java b/naming/src/test/java/com/alibaba/nacos/naming/core/DistroMapperTest.java index 5f2d1ba7458..cf9c7b2d860 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/core/DistroMapperTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/core/DistroMapperTest.java @@ -59,6 +59,7 @@ public class DistroMapperTest { public void setUp() { ConcurrentSkipListMap serverList = new ConcurrentSkipListMap<>(); EnvUtil.setEnvironment(new StandardEnvironment()); + EnvUtil.setIsStandalone(true); serverList.put(ip1, Member.builder().ip(ip1).port(port).build()); serverList.put(ip2, Member.builder().ip(ip2).port(port).build()); serverList.put(ip3, Member.builder().ip(ip3).port(port).build()); @@ -79,4 +80,4 @@ public void testMapSrv() { String server = distroMapper.mapSrv(serviceName); assertEquals(server, ip4); } -} \ No newline at end of file +} diff --git a/naming/src/test/java/com/alibaba/nacos/naming/core/DomainTest.java b/naming/src/test/java/com/alibaba/nacos/naming/core/DomainTest.java index 4f559b39491..31aa3c23778 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/core/DomainTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/core/DomainTest.java @@ -19,6 +19,7 @@ import com.alibaba.nacos.naming.BaseTest; import com.alibaba.nacos.naming.core.v2.upgrade.doublewrite.delay.DoubleWriteEventListener; import com.alibaba.nacos.naming.misc.UtilsAndCommons; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -48,6 +49,11 @@ public void before() { mockInjectPushServer(); } + @After + public void tearDown() throws Exception { + service.destroy(); + } + @Test public void updateDomain() { diff --git a/naming/src/test/java/com/alibaba/nacos/naming/core/InstanceOperatorClientImplTest.java b/naming/src/test/java/com/alibaba/nacos/naming/core/InstanceOperatorClientImplTest.java index 16f9eef4825..d31045bb092 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/core/InstanceOperatorClientImplTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/core/InstanceOperatorClientImplTest.java @@ -19,6 +19,7 @@ import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.NamingResponseCode; +import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.naming.core.v2.ServiceManager; import com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient; @@ -30,14 +31,15 @@ import com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata; import com.alibaba.nacos.naming.core.v2.pojo.Service; import com.alibaba.nacos.naming.core.v2.service.ClientOperationServiceProxy; -import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.naming.healthcheck.RsInfo; import com.alibaba.nacos.naming.misc.SwitchDomain; import com.alibaba.nacos.naming.pojo.InstanceOperationInfo; import com.alibaba.nacos.naming.pojo.Subscriber; import com.alibaba.nacos.naming.pojo.instance.BeatInfoInstanceBuilder; import com.alibaba.nacos.naming.push.UdpPushService; +import com.alibaba.nacos.naming.selector.SelectorManager; import com.alibaba.nacos.sys.env.EnvUtil; +import com.alibaba.nacos.sys.utils.ApplicationUtils; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -47,6 +49,8 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.stubbing.Answer; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.mock.env.MockEnvironment; import java.util.Collections; @@ -55,6 +59,9 @@ import java.util.Map; import java.util.Optional; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + /** * {@link InstanceOperatorClientImpl} unit tests. * @@ -88,11 +95,21 @@ public class InstanceOperatorClientImplTest { @Mock private UdpPushService pushService; + @Mock + private SelectorManager selectorManager; + + @Mock + private ConfigurableApplicationContext context; + @Before public void setUp() { Service service = Service.newService("A", "DEFAULT_GROUP", "C"); ServiceManager.getInstance().getSingleton(service); EnvUtil.setEnvironment(new MockEnvironment()); + ApplicationUtils.injectContext(context); + when(context.getBean(SelectorManager.class)).thenReturn(selectorManager); + when(selectorManager.select(any(), any(), any())) + .then((Answer>) invocationOnMock -> invocationOnMock.getArgument(2)); } @After @@ -104,13 +121,13 @@ public void tearDown() { @Test public void testRegisterInstance() { instanceOperatorClient.registerInstance("A", "B", new Instance()); - + Mockito.verify(clientOperationService).registerInstance(Mockito.any(), Mockito.any(), Mockito.anyString()); } @Test public void testRemoveInstance() { - Mockito.when(clientManager.contains(Mockito.anyString())).thenReturn(true); + when(clientManager.contains(Mockito.anyString())).thenReturn(true); instanceOperatorClient.removeInstance("A", "B", new Instance()); @@ -136,24 +153,25 @@ public void testPatchInstance() throws NacosException { ServiceInfo serviceInfo = new ServiceInfo(); serviceInfo.setHosts(instances); - Mockito.when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo); + when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo); instanceOperatorClient.patchInstance("A", "B", new InstancePatchObject("C", "1.1.1.1", 8848)); - Mockito.verify(metadataOperateService).updateInstanceMetadata(Mockito.any(), Mockito.anyString(), Mockito.any()); + Mockito.verify(metadataOperateService) + .updateInstanceMetadata(Mockito.any(), Mockito.anyString(), Mockito.any()); } @Test public void testListInstance() { - Mockito.when(pushService.canEnablePush(Mockito.anyString())).thenReturn(true); + when(pushService.canEnablePush(Mockito.anyString())).thenReturn(true); ServiceInfo serviceInfo = new ServiceInfo(); serviceInfo.setGroupName("DEFAULT_GROUP"); serviceInfo.setName("B"); - Mockito.when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo); - + when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo); + ServiceMetadata metadata = new ServiceMetadata(); - Mockito.when(metadataManager.getServiceMetadata(Mockito.any())).thenReturn(Optional.of(metadata)); + when(metadataManager.getServiceMetadata(Mockito.any())).thenReturn(Optional.of(metadata)); Subscriber subscriber = new Subscriber("2.2.2.2", "", "app", "1.1.1.1", "A", "B", 8848); instanceOperatorClient.listInstance("A", "B", subscriber, "C", true); @@ -164,13 +182,15 @@ public void testListInstance() { @Test public void testHandleBeat() throws NacosException { IpPortBasedClient ipPortBasedClient = Mockito.mock(IpPortBasedClient.class); - Mockito.when(clientManager.getClient(Mockito.anyString())).thenReturn(ipPortBasedClient); + when(clientManager.getClient(Mockito.anyString())).thenReturn(ipPortBasedClient); - Mockito.when(ipPortBasedClient.getAllPublishedService()).thenReturn(Collections.emptyList()); + when(ipPortBasedClient.getAllPublishedService()).thenReturn(Collections.emptyList()); + RsInfo rsInfo = new RsInfo(); + rsInfo.setMetadata(new HashMap<>(1)); int res = instanceOperatorClient - .handleBeat("A", "C", "1.1.1.1", 8848, "D", new RsInfo(), BeatInfoInstanceBuilder.newBuilder()); - + .handleBeat("A", "C", "1.1.1.1", 8848, "D", rsInfo, BeatInfoInstanceBuilder.newBuilder()); + Assert.assertEquals(NamingResponseCode.OK, res); } @@ -179,9 +199,10 @@ public void testGetHeartBeatInterval() { InstanceMetadata instanceMetadata = new InstanceMetadata(); Map map = new HashMap<>(2); instanceMetadata.setExtendData(map); - Mockito.when(metadataManager.getInstanceMetadata(Mockito.any(), Mockito.anyString())).thenReturn(Optional.of(instanceMetadata)); + when(metadataManager.getInstanceMetadata(Mockito.any(), Mockito.anyString())) + .thenReturn(Optional.of(instanceMetadata)); - Mockito.when(switchDomain.getClientBeatInterval()).thenReturn(100L); + when(switchDomain.getClientBeatInterval()).thenReturn(100L); long interval = instanceOperatorClient.getHeartBeatInterval("A", "C", "1.1.1.1", 8848, "D"); @@ -193,7 +214,7 @@ public void testListAllInstances() throws NacosException { ServiceInfo serviceInfo = new ServiceInfo(); serviceInfo.setHosts(Collections.emptyList()); - Mockito.when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo); + when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo); List instances = instanceOperatorClient.listAllInstances("A", "C"); @@ -208,7 +229,7 @@ public void testBatchUpdateMetadata() throws NacosException { instance.setPort(8848); ServiceInfo serviceInfo = new ServiceInfo(); serviceInfo.setHosts(Collections.singletonList(instance)); - Mockito.when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo); + when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo); InstanceOperationInfo instanceOperationInfo = new InstanceOperationInfo(); List res = instanceOperatorClient.batchUpdateMetadata("A", instanceOperationInfo, new HashMap<>()); @@ -224,9 +245,10 @@ public void testBatchDeleteMetadata() throws NacosException { instance.setPort(8848); ServiceInfo serviceInfo = new ServiceInfo(); serviceInfo.setHosts(Collections.singletonList(instance)); - Mockito.when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo); + when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo); - List res = instanceOperatorClient.batchDeleteMetadata("A", new InstanceOperationInfo(), new HashMap<>()); + List res = instanceOperatorClient + .batchDeleteMetadata("A", new InstanceOperationInfo(), new HashMap<>()); Assert.assertEquals(1, res.size()); } diff --git a/naming/src/test/java/com/alibaba/nacos/naming/core/ServiceManagerTest.java b/naming/src/test/java/com/alibaba/nacos/naming/core/ServiceManagerTest.java index d06b2903326..925aec34da2 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/core/ServiceManagerTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/core/ServiceManagerTest.java @@ -28,9 +28,11 @@ import com.alibaba.nacos.naming.consistency.Datum; import com.alibaba.nacos.naming.consistency.KeyBuilder; import com.alibaba.nacos.naming.core.ServiceManager.ServiceChecksum; +import com.alibaba.nacos.naming.core.v2.upgrade.doublewrite.delay.DoubleWriteEventListener; import com.alibaba.nacos.naming.misc.Message; import com.alibaba.nacos.naming.misc.Synchronizer; import com.alibaba.nacos.naming.misc.UtilsAndCommons; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -71,6 +73,9 @@ public class ServiceManagerTest extends BaseTest { @Mock private ServerMemberManager serverMemberManager; + @Mock + private DoubleWriteEventListener doubleWriteEventListener; + private Service service; private Cluster cluster; @@ -93,6 +98,12 @@ public void before() { mockCluster(); mockInstance(); mockServiceName(); + when(context.getBean(DoubleWriteEventListener.class)).thenReturn(doubleWriteEventListener); + } + + @After + public void tearDown() throws Exception { + service.destroy(); } private void mockService() { diff --git a/naming/src/test/java/com/alibaba/nacos/naming/core/ServiceOperatorV2ImplTest.java b/naming/src/test/java/com/alibaba/nacos/naming/core/ServiceOperatorV2ImplTest.java index 61840ef0801..550b578ebf6 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/core/ServiceOperatorV2ImplTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/core/ServiceOperatorV2ImplTest.java @@ -37,7 +37,9 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.util.ReflectionUtils; +import java.lang.reflect.Field; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -66,15 +68,24 @@ public class ServiceOperatorV2ImplTest { private ServiceStorage serviceStorage; @Before - public void setUp() { + public void setUp() throws IllegalAccessException { + cleanNamespace(); Service service = Service.newService("A", "B", "C"); ServiceManager.getInstance().getSingleton(service); } @After - public void tearDown() { + public void tearDown() throws IllegalAccessException { Service service = Service.newService("A", "B", "C"); ServiceManager.getInstance().removeSingleton(service); + cleanNamespace(); + } + + private void cleanNamespace() throws IllegalAccessException { + Field field = ReflectionUtils.findField(ServiceManager.class, "namespaceSingletonMaps"); + field.setAccessible(true); + Map map = (Map) field.get(ServiceManager.getInstance()); + map.clear(); } @Test @@ -97,7 +108,7 @@ public void testDelete() throws NacosException { serviceInfo.setHosts(Collections.emptyList()); Mockito.when(serviceStorage.getPushData(Mockito.any())).thenReturn(serviceInfo); - serviceOperatorV2.delete("A", "C"); + serviceOperatorV2.delete("A", "B@@C"); Mockito.verify(metadataOperateService).deleteServiceMetadata(Mockito.any()); } @@ -113,7 +124,7 @@ public void testQueryService() throws NacosException { Mockito.when(serviceStorage.getClusters(Mockito.any())).thenReturn(Collections.singleton("D")); - ObjectNode objectNode = serviceOperatorV2.queryService("A", "C"); + ObjectNode objectNode = serviceOperatorV2.queryService("A", "B@@C"); Assert.assertEquals("A", objectNode.get(FieldsConstants.NAME_SPACE_ID).asText()); Assert.assertEquals("C", objectNode.get(FieldsConstants.NAME).asText()); diff --git a/naming/src/test/java/com/alibaba/nacos/naming/core/ServiceTest.java b/naming/src/test/java/com/alibaba/nacos/naming/core/ServiceTest.java index dc38170c08b..0ce7efe3ea1 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/core/ServiceTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/core/ServiceTest.java @@ -16,11 +16,13 @@ package com.alibaba.nacos.naming.core; +import com.alibaba.nacos.api.selector.SelectorType; import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.naming.BaseTest; import com.alibaba.nacos.naming.core.v2.upgrade.doublewrite.delay.DoubleWriteEventListener; import com.alibaba.nacos.naming.selector.NoneSelector; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -54,6 +56,11 @@ public void before() { mockInjectSwitchDomain(); } + @After + public void tearDown() throws Exception { + service.destroy(); + } + @Test public void testUpdateIPs() { List instances = new ArrayList<>(); @@ -89,7 +96,6 @@ public void testSerialize() throws Exception { assertTrue(actual.contains("\"owners\":[]")); assertTrue(actual.contains("\"protectThreshold\":0.0")); assertTrue(actual.contains("\"resetWeight\":false")); - assertTrue(actual.contains("\"selector\":{\"type\":\"none\"}")); assertFalse(actual.contains("clientBeatCheckTask")); assertFalse(actual.contains("serviceString")); assertFalse(actual.contains("pushService")); @@ -98,6 +104,7 @@ public void testSerialize() throws Exception { @Test @SuppressWarnings("checkstyle:linelength") public void testDeserialize() throws Exception { + JacksonUtils.registerSubtype(NoneSelector.class, SelectorType.none.name()); String example = "{\"checksum\":\"394c845e1160bb880e7f26fb2149ed6d\",\"clusterMap\":{},\"empty\":true,\"enabled\":true,\"finalizeCount\":0,\"ipDeleteTimeout\":30000,\"lastModifiedMillis\":0,\"metadata\":{},\"name\":\"test-service\",\"owners\":[],\"protectThreshold\":0.0,\"resetWeight\":false,\"selector\":{\"type\":\"none\"}}"; Service actual = JacksonUtils.toObj(example, Service.class); assertEquals("394c845e1160bb880e7f26fb2149ed6d", actual.getChecksum()); diff --git a/naming/src/test/java/com/alibaba/nacos/naming/core/v2/event/publisher/NamingEventPublisherFactoryTest.java b/naming/src/test/java/com/alibaba/nacos/naming/core/v2/event/publisher/NamingEventPublisherFactoryTest.java index 04046330382..3e312629118 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/core/v2/event/publisher/NamingEventPublisherFactoryTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/core/v2/event/publisher/NamingEventPublisherFactoryTest.java @@ -21,7 +21,9 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.springframework.util.ReflectionUtils; +import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; @@ -36,6 +38,11 @@ public class NamingEventPublisherFactoryTest { public void setUp() throws Exception { originalEventPublisherMap = new HashMap<>(NotifyCenter.getPublisherMap()); NotifyCenter.getPublisherMap().clear(); + // Protect other unit test publisher affect this case. + Field field = ReflectionUtils.findField(NamingEventPublisherFactory.class, "publisher", Map.class); + field.setAccessible(true); + Map map = (Map) field.get(NamingEventPublisherFactory.getInstance()); + map.clear(); } @After diff --git a/naming/src/test/java/com/alibaba/nacos/naming/core/v2/upgrade/doublewrite/delay/DoubleWriteEventListenerTest.java b/naming/src/test/java/com/alibaba/nacos/naming/core/v2/upgrade/doublewrite/delay/DoubleWriteEventListenerTest.java index 98c8f88a9ed..a18f1c2e036 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/core/v2/upgrade/doublewrite/delay/DoubleWriteEventListenerTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/core/v2/upgrade/doublewrite/delay/DoubleWriteEventListenerTest.java @@ -19,6 +19,7 @@ import com.alibaba.nacos.naming.core.v2.event.service.ServiceEvent; import com.alibaba.nacos.naming.core.v2.pojo.Service; import com.alibaba.nacos.naming.core.v2.upgrade.UpgradeJudgement; +import com.alibaba.nacos.sys.env.Constants; import com.alibaba.nacos.sys.env.EnvUtil; import org.junit.After; import org.junit.Before; @@ -27,6 +28,7 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.mock.env.MockEnvironment; /** * {@link DoubleWriteEventListener} unit tests. @@ -47,6 +49,9 @@ public class DoubleWriteEventListenerTest { @Before public void setUp() { + MockEnvironment environment = new MockEnvironment(); + environment.setProperty(Constants.SUPPORT_UPGRADE_FROM_1X, "true"); + EnvUtil.setEnvironment(environment); EnvUtil.setIsStandalone(false); doubleWriteEventListener = new DoubleWriteEventListener(upgradeJudgement, doubleWriteDelayTaskEngine); } diff --git a/naming/src/test/java/com/alibaba/nacos/naming/monitor/MetricsMonitorTest.java b/naming/src/test/java/com/alibaba/nacos/naming/monitor/MetricsMonitorTest.java index d8612a88304..06d94b49046 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/monitor/MetricsMonitorTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/monitor/MetricsMonitorTest.java @@ -16,12 +16,18 @@ package com.alibaba.nacos.naming.monitor; +import org.junit.Before; import org.junit.Test; import static org.junit.Assert.assertEquals; public class MetricsMonitorTest { + @Before + public void setUp() { + MetricsMonitor.resetPush(); + } + @Test public void testGetTotalPush() { assertEquals(0, MetricsMonitor.getTotalPushMonitor().get()); diff --git a/naming/src/test/java/com/alibaba/nacos/naming/pojo/instance/BeatInfoInstanceBuilderTest.java b/naming/src/test/java/com/alibaba/nacos/naming/pojo/instance/BeatInfoInstanceBuilderTest.java index 704675fe3ce..b2c0fac59ee 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/pojo/instance/BeatInfoInstanceBuilderTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/pojo/instance/BeatInfoInstanceBuilderTest.java @@ -74,9 +74,8 @@ public void testBuild() { assertTrue(actual.isEnabled()); assertTrue(actual.isHealthy()); assertThat(actual.getInstanceId(), is("1.1.1.1#8848#c#g@@s")); - assertThat(actual.getMetadata().size(), is(2)); + assertThat(actual.getMetadata().size(), is(1)); assertThat(actual.getMetadata().get("mock"), is("mock")); - assertThat(actual.getMetadata().get("app"), is("DEFAULT")); verify(request).getParameter("mock"); } } diff --git a/naming/src/test/java/com/alibaba/nacos/naming/pojo/instance/HttpRequestInstanceBuilderTest.java b/naming/src/test/java/com/alibaba/nacos/naming/pojo/instance/HttpRequestInstanceBuilderTest.java index e2ade410e37..4d45a6da179 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/pojo/instance/HttpRequestInstanceBuilderTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/pojo/instance/HttpRequestInstanceBuilderTest.java @@ -80,8 +80,7 @@ public void testBuildSimple() throws NacosException { assertTrue(actual.isHealthy()); assertThat(actual.getInstanceId(), is(IP + "#" + PORT + "#" + UtilsAndCommons.DEFAULT_CLUSTER_NAME + "#" + SERVICE)); - assertThat(actual.getMetadata().size(), is(2)); - assertThat(actual.getMetadata().get("app"), is("DEFAULT")); + assertThat(actual.getMetadata().size(), is(1)); assertThat(actual.getMetadata().get("mock"), is("mock")); verify(request).getParameter("mock"); } @@ -112,9 +111,8 @@ public void testBuildFull() throws NacosException { assertFalse(actual.isEnabled()); assertFalse(actual.isHealthy()); assertThat(actual.getInstanceId(), is(IP + "#" + PORT + "#" + "cluster" + "#" + SERVICE)); - assertThat(actual.getMetadata().size(), is(3)); + assertThat(actual.getMetadata().size(), is(2)); assertThat(actual.getMetadata().get("mock"), is("mock")); - assertThat(actual.getMetadata().get("app"), is("DEFAULT")); assertThat(actual.getMetadata().get("a"), is("b")); verify(request).getParameter("mock"); } diff --git a/naming/src/test/java/com/alibaba/nacos/naming/push/v2/executor/PushExecutorRpcImplTest.java b/naming/src/test/java/com/alibaba/nacos/naming/push/v2/executor/PushExecutorRpcImplTest.java index 267e77bb56f..93aa957339f 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/push/v2/executor/PushExecutorRpcImplTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/push/v2/executor/PushExecutorRpcImplTest.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.naming.push.v2.executor; +import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.api.naming.remote.request.NotifySubscriberRequest; import com.alibaba.nacos.api.remote.PushCallBack; @@ -24,6 +25,9 @@ import com.alibaba.nacos.naming.misc.GlobalExecutor; import com.alibaba.nacos.naming.pojo.Subscriber; import com.alibaba.nacos.naming.push.v2.PushDataWrapper; +import com.alibaba.nacos.naming.selector.SelectorManager; +import com.alibaba.nacos.sys.env.EnvUtil; +import com.alibaba.nacos.sys.utils.ApplicationUtils; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -31,7 +35,10 @@ import org.mockito.invocation.InvocationOnMock; import org.mockito.junit.MockitoJUnitRunner; import org.mockito.stubbing.Answer; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.mock.env.MockEnvironment; +import java.util.List; import java.util.UUID; import static org.junit.Assert.assertEquals; @@ -39,6 +46,7 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class PushExecutorRpcImplTest { @@ -54,6 +62,12 @@ public class PushExecutorRpcImplTest { @Mock private PushCallBack pushCallBack; + @Mock + private SelectorManager selectorManager; + + @Mock + private ConfigurableApplicationContext context; + private PushDataWrapper pushData; private PushExecutorRpcImpl pushExecutor; @@ -65,9 +79,14 @@ public void setUp() throws Exception { serviceMetadata = new ServiceMetadata(); pushData = new PushDataWrapper(serviceMetadata, new ServiceInfo("G@@S")); pushExecutor = new PushExecutorRpcImpl(pushService); + EnvUtil.setEnvironment(new MockEnvironment()); doAnswer(new CallbackAnswer()).when(pushService) .pushWithCallback(eq(rpcClientId), any(NotifySubscriberRequest.class), eq(pushCallBack), eq(GlobalExecutor.getCallbackExecutor())); + ApplicationUtils.injectContext(context); + when(context.getBean(SelectorManager.class)).thenReturn(selectorManager); + when(selectorManager.select(any(), any(), any())).then( + (Answer>) invocationOnMock -> invocationOnMock.getArgument(2)); } @Test @@ -87,7 +106,7 @@ private class CallbackAnswer implements Answer { @Override public Void answer(InvocationOnMock invocationOnMock) throws Throwable { NotifySubscriberRequest pushRequest = invocationOnMock.getArgument(1); - assertEquals(pushData.getOriginalData(), pushRequest.getServiceInfo()); + assertEquals(pushData.getOriginalData().toString(), pushRequest.getServiceInfo().toString()); PushCallBack callBack = invocationOnMock.getArgument(2); callBack.onSuccess(); return null; diff --git a/naming/src/test/java/com/alibaba/nacos/naming/push/v2/executor/PushExecutorUdpImplTest.java b/naming/src/test/java/com/alibaba/nacos/naming/push/v2/executor/PushExecutorUdpImplTest.java index 6c2f9bef79f..fca833f4c46 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/push/v2/executor/PushExecutorUdpImplTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/push/v2/executor/PushExecutorUdpImplTest.java @@ -16,12 +16,15 @@ package com.alibaba.nacos.naming.push.v2.executor; +import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.api.remote.PushCallBack; import com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata; import com.alibaba.nacos.naming.pojo.Subscriber; import com.alibaba.nacos.naming.push.UdpPushService; import com.alibaba.nacos.naming.push.v2.PushDataWrapper; +import com.alibaba.nacos.naming.selector.SelectorManager; +import com.alibaba.nacos.sys.utils.ApplicationUtils; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -29,12 +32,16 @@ import org.mockito.invocation.InvocationOnMock; import org.mockito.junit.MockitoJUnitRunner; import org.mockito.stubbing.Answer; +import org.springframework.context.ConfigurableApplicationContext; + +import java.util.List; import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class PushExecutorUdpImplTest { @@ -50,6 +57,12 @@ public class PushExecutorUdpImplTest { @Mock private PushCallBack pushCallBack; + @Mock + private SelectorManager selectorManager; + + @Mock + private ConfigurableApplicationContext context; + private PushDataWrapper pushData; private PushExecutorUdpImpl pushExecutor; @@ -63,6 +76,10 @@ public void setUp() throws Exception { pushExecutor = new PushExecutorUdpImpl(pushService); doAnswer(new CallbackAnswer()).when(pushService) .pushDataWithCallback(eq(subscriber), any(ServiceInfo.class), eq(pushCallBack)); + ApplicationUtils.injectContext(context); + when(context.getBean(SelectorManager.class)).thenReturn(selectorManager); + when(selectorManager.select(any(), any(), any())).then( + (Answer>) invocationOnMock -> invocationOnMock.getArgument(2)); } @Test diff --git a/naming/src/test/java/com/alibaba/nacos/naming/raft/RaftStoreTest.java b/naming/src/test/java/com/alibaba/nacos/naming/raft/RaftStoreTest.java index a6ff01ed6a0..ef57578501a 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/raft/RaftStoreTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/raft/RaftStoreTest.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.naming.raft; +import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.naming.BaseTest; import com.alibaba.nacos.naming.consistency.Datum; import com.alibaba.nacos.naming.consistency.KeyBuilder; @@ -24,21 +25,17 @@ import com.alibaba.nacos.naming.consistency.persistent.raft.RaftStore; import com.alibaba.nacos.naming.core.Instance; import com.alibaba.nacos.naming.core.Instances; +import com.alibaba.nacos.sys.env.Constants; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.Spy; -import org.springframework.test.util.ReflectionTestUtils; public class RaftStoreTest extends BaseTest { - @InjectMocks - @Spy public RaftCore raftCore; - @Spy public RaftStore raftStore; @Mock @@ -46,7 +43,16 @@ public class RaftStoreTest extends BaseTest { @Before public void setUp() { - ReflectionTestUtils.setField(raftCore, "versionJudgement", versionJudgement); + super.before(); + environment.setProperty(Constants.SUPPORT_UPGRADE_FROM_1X, "true"); + raftStore = new RaftStore(); + raftCore = new RaftCore(peerSet, switchDomain, null, null, raftStore, versionJudgement, null); + } + + @After + public void tearDown() throws NacosException { + raftCore.shutdown(); + raftStore.shutdown(); } @Test diff --git a/naming/src/test/java/com/alibaba/nacos/naming/remote/udp/UdpConnectorTest.java b/naming/src/test/java/com/alibaba/nacos/naming/remote/udp/UdpConnectorTest.java index fba77e26a10..6659f1db76a 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/remote/udp/UdpConnectorTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/remote/udp/UdpConnectorTest.java @@ -36,6 +36,11 @@ import java.net.DatagramPacket; import java.net.DatagramSocket; import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.TimeUnit; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.when; /** * {@link UdpConnector} unit tests. @@ -64,21 +69,28 @@ public static void setEnv() { } @Before - public void setUp() { + public void setUp() throws IOException, InterruptedException { ReflectionTestUtils.setField(udpConnector, "ackMap", ackMap); ReflectionTestUtils.setField(udpConnector, "callbackMap", callbackMap); + DatagramSocket oldSocket = (DatagramSocket) ReflectionTestUtils.getField(udpConnector, "udpSocket"); ReflectionTestUtils.setField(udpConnector, "udpSocket", udpSocket); + doAnswer(invocationOnMock -> { + TimeUnit.MINUTES.sleep(1); + return null; + }).when(udpSocket).receive(any(DatagramPacket.class)); + oldSocket.close(); + TimeUnit.SECONDS.sleep(1); } @Test public void testContainAck() { - Mockito.when(ackMap.containsKey(Mockito.anyString())).thenReturn(true); + when(ackMap.containsKey(Mockito.anyString())).thenReturn(true); Assert.assertTrue(udpConnector.containAck("1111")); } @Test public void testSendData() throws NacosException, IOException { - Mockito.when(udpSocket.isClosed()).thenReturn(false); + when(udpSocket.isClosed()).thenReturn(false); AckEntry ackEntry = new AckEntry("A", new DatagramPacket(new byte[2], 2)); udpConnector.sendData(ackEntry); Mockito.verify(udpSocket).send(ackEntry.getOrigin()); @@ -86,19 +98,19 @@ public void testSendData() throws NacosException, IOException { @Test public void testSendDataWithCallback() throws IOException, InterruptedException { - Mockito.when(udpSocket.isClosed()).thenReturn(false); + when(udpSocket.isClosed()).thenReturn(false); AckEntry ackEntry = new AckEntry("A", new DatagramPacket(new byte[2], 2)); udpConnector.sendDataWithCallback(ackEntry, new PushCallBack() { @Override public long getTimeout() { return 0; } - + @Override public void onSuccess() { } - + @Override public void onFail(Throwable e) { Assert.fail(e.getMessage()); diff --git a/pom.xml b/pom.xml index 67c66d10fb5..25339b4a1c1 100644 --- a/pom.xml +++ b/pom.xml @@ -97,9 +97,7 @@ false true - jacoco - ${project.basedir}/../test/target/jacoco-it.exec file:**/generated-sources/**,**/test/** @@ -113,8 +111,7 @@ 3.8 0.12 3.0.2 - 4.3.0 - 0.7.8 + 0.8.7 2.20 3.0.4 3.0.2 @@ -368,48 +365,31 @@ ${project.build.sourceEncoding} - - org.eluder.coveralls - coveralls-maven-plugin - ${coveralls-maven-plugin.version} - org.jacoco jacoco-maven-plugin ${jacoco-maven-plugin.version} + + ${maven.test.skip} + ${project.build.directory}/coverage-reports/jacoco-unit.exec + ${project.build.directory}/coverage-reports/jacoco-unit.exec + file + true + default-prepare-agent prepare-agent - - ${project.build.directory}/jacoco.exec - - - - default-prepare-agent-integration - pre-integration-test - - prepare-agent-integration - - - ${project.build.directory}/jacoco-it.exec - failsafeArgLine - default-report + test report - - default-report-integration - - report-integration - -