From 4e455680f3e5c406c3154b87677473b58fd2d135 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E7=BF=8A=20SionYang?= <263976490@qq.com> Date: Wed, 24 Feb 2021 14:40:03 +0800 Subject: [PATCH] Add push unit test (#4939) --- .../v2/executor/PushExecutorDelegate.java | 1 - .../v2/executor/PushExecutorDelegateTest.java | 23 ++++++++++--------- .../v2/executor/PushExecutorRpcImplTest.java | 11 +++++---- .../v2/executor/PushExecutorUdpImplTest.java | 9 ++++---- .../push/v2/task/FixturePushExecutor.java | 5 ++-- .../task/PushDelayTaskExecuteEngineTest.java | 3 ++- .../push/v2/task/PushExecuteTaskTest.java | 11 +++++++++ 7 files changed, 39 insertions(+), 24 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/push/v2/executor/PushExecutorDelegate.java b/naming/src/main/java/com/alibaba/nacos/naming/push/v2/executor/PushExecutorDelegate.java index 593673020..4626cc31c 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/push/v2/executor/PushExecutorDelegate.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/push/v2/executor/PushExecutorDelegate.java @@ -16,7 +16,6 @@ package com.alibaba.nacos.naming.push.v2.executor; -import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.api.remote.PushCallBack; import com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient; import com.alibaba.nacos.naming.pojo.Subscriber; diff --git a/naming/src/test/java/com/alibaba/nacos/naming/push/v2/executor/PushExecutorDelegateTest.java b/naming/src/test/java/com/alibaba/nacos/naming/push/v2/executor/PushExecutorDelegateTest.java index f88de5f91..b31e29e1d 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/push/v2/executor/PushExecutorDelegateTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/push/v2/executor/PushExecutorDelegateTest.java @@ -19,6 +19,7 @@ package com.alibaba.nacos.naming.push.v2.executor; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.api.remote.PushCallBack; import com.alibaba.nacos.naming.pojo.Subscriber; +import com.alibaba.nacos.naming.push.v2.PushDataWrapper; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -32,7 +33,7 @@ import static org.mockito.Mockito.verify; @RunWith(MockitoJUnitRunner.class) public class PushExecutorDelegateTest { - private final String udpClientId = "1.1.1.1:60000"; + private final String udpClientId = "1.1.1.1:60000#true"; private final String rpcClientId = UUID.randomUUID().toString(); @@ -48,37 +49,37 @@ public class PushExecutorDelegateTest { @Mock private PushCallBack pushCallBack; - private ServiceInfo serviceInfo; + private PushDataWrapper pushdata; private PushExecutorDelegate delegate; @Before public void setUp() throws Exception { - serviceInfo = new ServiceInfo("G@@S"); + pushdata = new PushDataWrapper(new ServiceInfo("G@@S")); delegate = new PushExecutorDelegate(pushExecutorRpc, pushExecutorUdp); } @Test public void testDoPushForUdp() { - delegate.doPush(udpClientId, subscriber, serviceInfo); - verify(pushExecutorUdp).doPush(udpClientId, subscriber, serviceInfo); + delegate.doPush(udpClientId, subscriber, pushdata); + verify(pushExecutorUdp).doPush(udpClientId, subscriber, pushdata); } @Test public void testDoPushForRpc() { - delegate.doPush(rpcClientId, subscriber, serviceInfo); - verify(pushExecutorRpc).doPush(rpcClientId, subscriber, serviceInfo); + delegate.doPush(rpcClientId, subscriber, pushdata); + verify(pushExecutorRpc).doPush(rpcClientId, subscriber, pushdata); } @Test public void doPushWithCallbackForUdp() { - delegate.doPushWithCallback(udpClientId, subscriber, serviceInfo, pushCallBack); - verify(pushExecutorUdp).doPushWithCallback(udpClientId, subscriber, serviceInfo, pushCallBack); + delegate.doPushWithCallback(udpClientId, subscriber, pushdata, pushCallBack); + verify(pushExecutorUdp).doPushWithCallback(udpClientId, subscriber, pushdata, pushCallBack); } @Test public void doPushWithCallbackForRpc() { - delegate.doPushWithCallback(rpcClientId, subscriber, serviceInfo, pushCallBack); - verify(pushExecutorRpc).doPushWithCallback(rpcClientId, subscriber, serviceInfo, pushCallBack); + delegate.doPushWithCallback(rpcClientId, subscriber, pushdata, pushCallBack); + verify(pushExecutorRpc).doPushWithCallback(rpcClientId, subscriber, pushdata, pushCallBack); } } 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 bdff03078..8117b9f6e 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 @@ -22,6 +22,7 @@ import com.alibaba.nacos.api.remote.PushCallBack; import com.alibaba.nacos.core.remote.RpcPushService; import com.alibaba.nacos.naming.misc.GlobalExecutor; import com.alibaba.nacos.naming.pojo.Subscriber; +import com.alibaba.nacos.naming.push.v2.PushDataWrapper; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -52,13 +53,13 @@ public class PushExecutorRpcImplTest { @Mock private PushCallBack pushCallBack; - private ServiceInfo serviceInfo; + private PushDataWrapper pushData; private PushExecutorRpcImpl pushExecutor; @Before public void setUp() throws Exception { - serviceInfo = new ServiceInfo("G@@S"); + pushData = new PushDataWrapper(new ServiceInfo("G@@S")); pushExecutor = new PushExecutorRpcImpl(pushService); doAnswer(new CallbackAnswer()).when(pushService) .pushWithCallback(eq(rpcClientId), any(NotifySubscriberRequest.class), eq(pushCallBack), @@ -67,13 +68,13 @@ public class PushExecutorRpcImplTest { @Test public void testDoPush() { - pushExecutor.doPush(rpcClientId, subscriber, serviceInfo); + pushExecutor.doPush(rpcClientId, subscriber, pushData); verify(pushService).pushWithoutAck(eq(rpcClientId), any(NotifySubscriberRequest.class)); } @Test public void testDoPushWithCallback() { - pushExecutor.doPushWithCallback(rpcClientId, subscriber, serviceInfo, pushCallBack); + pushExecutor.doPushWithCallback(rpcClientId, subscriber, pushData, pushCallBack); verify(pushCallBack).onSuccess(); } @@ -82,7 +83,7 @@ public class PushExecutorRpcImplTest { @Override public Void answer(InvocationOnMock invocationOnMock) throws Throwable { NotifySubscriberRequest pushRequest = invocationOnMock.getArgument(1); - assertEquals(serviceInfo, pushRequest.getServiceInfo()); + assertEquals(pushData.getOriginalData(), pushRequest.getServiceInfo()); 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 be10ffbdd..5d35de81e 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 @@ -20,6 +20,7 @@ import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.api.remote.PushCallBack; import com.alibaba.nacos.naming.pojo.Subscriber; import com.alibaba.nacos.naming.push.UdpPushService; +import com.alibaba.nacos.naming.push.v2.PushDataWrapper; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -48,13 +49,13 @@ public class PushExecutorUdpImplTest { @Mock private PushCallBack pushCallBack; - private ServiceInfo serviceInfo; + private PushDataWrapper pushData; private PushExecutorUdpImpl pushExecutor; @Before public void setUp() throws Exception { - serviceInfo = new ServiceInfo("G@@S"); + pushData = new PushDataWrapper(new ServiceInfo("G@@S")); pushExecutor = new PushExecutorUdpImpl(pushService); doAnswer(new CallbackAnswer()).when(pushService) .pushDataWithCallback(eq(subscriber), any(ServiceInfo.class), eq(pushCallBack)); @@ -62,13 +63,13 @@ public class PushExecutorUdpImplTest { @Test public void testDoPush() { - pushExecutor.doPush(rpcClientId, subscriber, serviceInfo); + pushExecutor.doPush(rpcClientId, subscriber, pushData); verify(pushService).pushDataWithoutCallback(eq(subscriber), any(ServiceInfo.class)); } @Test public void testDoPushWithCallback() { - pushExecutor.doPushWithCallback(rpcClientId, subscriber, serviceInfo, pushCallBack); + pushExecutor.doPushWithCallback(rpcClientId, subscriber, pushData, pushCallBack); verify(pushCallBack).onSuccess(); } diff --git a/naming/src/test/java/com/alibaba/nacos/naming/push/v2/task/FixturePushExecutor.java b/naming/src/test/java/com/alibaba/nacos/naming/push/v2/task/FixturePushExecutor.java index 4e012bd92..66d18adcf 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/push/v2/task/FixturePushExecutor.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/push/v2/task/FixturePushExecutor.java @@ -19,6 +19,7 @@ package com.alibaba.nacos.naming.push.v2.task; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.api.remote.PushCallBack; import com.alibaba.nacos.naming.pojo.Subscriber; +import com.alibaba.nacos.naming.push.v2.PushDataWrapper; import com.alibaba.nacos.naming.push.v2.executor.PushExecutor; public class FixturePushExecutor implements PushExecutor { @@ -28,11 +29,11 @@ public class FixturePushExecutor implements PushExecutor { private Throwable failedException; @Override - public void doPush(String clientId, Subscriber subscriber, ServiceInfo data) { + public void doPush(String clientId, Subscriber subscriber, PushDataWrapper data) { } @Override - public void doPushWithCallback(String clientId, Subscriber subscriber, ServiceInfo data, PushCallBack callBack) { + public void doPushWithCallback(String clientId, Subscriber subscriber, PushDataWrapper data, PushCallBack callBack) { if (shouldSuccess) { callBack.onSuccess(); } else { diff --git a/naming/src/test/java/com/alibaba/nacos/naming/push/v2/task/PushDelayTaskExecuteEngineTest.java b/naming/src/test/java/com/alibaba/nacos/naming/push/v2/task/PushDelayTaskExecuteEngineTest.java index 4b556e0ab..1646f17a1 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/push/v2/task/PushDelayTaskExecuteEngineTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/push/v2/task/PushDelayTaskExecuteEngineTest.java @@ -24,6 +24,7 @@ import com.alibaba.nacos.naming.core.v2.index.ClientServiceIndexesManager; import com.alibaba.nacos.naming.core.v2.index.ServiceStorage; import com.alibaba.nacos.naming.core.v2.pojo.Service; import com.alibaba.nacos.naming.pojo.Subscriber; +import com.alibaba.nacos.naming.push.v2.PushDataWrapper; import com.alibaba.nacos.naming.push.v2.executor.PushExecutor; import org.junit.After; import org.junit.Before; @@ -86,7 +87,7 @@ public class PushDelayTaskExecuteEngineTest { PushDelayTask pushDelayTask = new PushDelayTask(service, 0L); executeEngine.addTask(service, pushDelayTask); TimeUnit.MILLISECONDS.sleep(200L); - verify(pushExecutor).doPushWithCallback(anyString(), any(Subscriber.class), any(ServiceInfo.class), + verify(pushExecutor).doPushWithCallback(anyString(), any(Subscriber.class), any(PushDataWrapper.class), any(PushCallBack.class)); } } diff --git a/naming/src/test/java/com/alibaba/nacos/naming/push/v2/task/PushExecuteTaskTest.java b/naming/src/test/java/com/alibaba/nacos/naming/push/v2/task/PushExecuteTaskTest.java index 6077c51e3..b08702778 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/push/v2/task/PushExecuteTaskTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/push/v2/task/PushExecuteTaskTest.java @@ -17,6 +17,7 @@ package com.alibaba.nacos.naming.push.v2.task; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; +import com.alibaba.nacos.core.remote.control.TpsMonitorManager; import com.alibaba.nacos.naming.core.v2.client.Client; import com.alibaba.nacos.naming.core.v2.client.manager.ClientManager; import com.alibaba.nacos.naming.core.v2.index.ClientServiceIndexesManager; @@ -25,11 +26,13 @@ import com.alibaba.nacos.naming.core.v2.pojo.Service; import com.alibaba.nacos.naming.monitor.MetricsMonitor; import com.alibaba.nacos.naming.pojo.Subscriber; import com.alibaba.nacos.naming.push.v2.NoRequiredRetryException; +import com.alibaba.nacos.sys.utils.ApplicationUtils; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.context.ConfigurableApplicationContext; import java.util.Collections; @@ -61,6 +64,12 @@ public class PushExecuteTaskTest { @Mock private ServiceStorage serviceStorage; + @Mock + private TpsMonitorManager tpsMonitorManager; + + @Mock + private ConfigurableApplicationContext context; + @Mock private Client client; @@ -78,6 +87,8 @@ public class PushExecuteTaskTest { when(delayTaskExecuteEngine.getIndexesManager()).thenReturn(indexesManager); when(delayTaskExecuteEngine.getPushExecutor()).thenReturn(pushExecutor); when(delayTaskExecuteEngine.getServiceStorage()).thenReturn(serviceStorage); + ApplicationUtils.injectContext(context); + when(context.getBean(TpsMonitorManager.class)).thenReturn(tpsMonitorManager); } @Test