Add push unit test (#4939)

This commit is contained in:
杨翊 SionYang 2021-02-24 14:40:03 +08:00 committed by GitHub
parent 3550d8e5e4
commit 4e455680f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 39 additions and 24 deletions

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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();
}

View File

@ -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 {

View File

@ -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));
}
}

View File

@ -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