Skip to content

Commit

Permalink
[ISSUE #4983] Add unit tests to package com.alibaba.nacos.naming.core…
Browse files Browse the repository at this point in the history
….v2.service in nacos 2.0 (#5107)
  • Loading branch information
realJackSun authored Mar 16, 2021
1 parent 5b99b7d commit bb9e374
Show file tree
Hide file tree
Showing 3 changed files with 350 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.naming.core.v2.service;

import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.naming.core.v2.ServiceManager;
import com.alibaba.nacos.naming.core.v2.pojo.Service;
import com.alibaba.nacos.naming.core.v2.service.impl.EphemeralClientOperationServiceImpl;
import com.alibaba.nacos.naming.core.v2.service.impl.PersistentClientOperationServiceImpl;
import com.alibaba.nacos.naming.pojo.Subscriber;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class ClientOperationServiceProxyTest {
@Mock
private EphemeralClientOperationServiceImpl ephemeralClientOperationServiceImpl;

@Mock
private PersistentClientOperationServiceImpl persistentClientOperationServiceImpl;

private ClientOperationServiceProxy clientOperationServiceProxy;

@Mock
private Service service;

private final String ephemeralIpPortId = System.currentTimeMillis() + "127.0.0.1:80#true";

private final String persistentIpPortId = System.currentTimeMillis() + "127.0.0.1:80#false";

@Mock
private Instance ephemeralInstance;

@Mock
private Instance persistentInstance;

@Mock
private Subscriber subscriber;

@Before
public void setUp() throws Exception {
clientOperationServiceProxy = new ClientOperationServiceProxy(
ephemeralClientOperationServiceImpl, persistentClientOperationServiceImpl);
when(ephemeralInstance.isEphemeral()).thenReturn(true);
when(persistentInstance.isEphemeral()).thenReturn(false);
when(service.getNamespace()).thenReturn("public");
}

@Test
public void testChooseEphemeralClientOperationService() {
// Test register.
clientOperationServiceProxy.registerInstance(service, ephemeralInstance, ephemeralIpPortId);
verify(ephemeralClientOperationServiceImpl).registerInstance(service, ephemeralInstance, ephemeralIpPortId);
verify(persistentClientOperationServiceImpl, never())
.registerInstance(service, ephemeralInstance, ephemeralIpPortId);
// Before service is registered.
clientOperationServiceProxy.deregisterInstance(service, ephemeralInstance, ephemeralIpPortId);
verify(ephemeralClientOperationServiceImpl, never())
.deregisterInstance(service, ephemeralInstance, ephemeralIpPortId);
verify(persistentClientOperationServiceImpl, never())
.deregisterInstance(service, ephemeralInstance, ephemeralIpPortId);

ServiceManager.getInstance().getSingleton(service);
// Test deregister.
clientOperationServiceProxy.deregisterInstance(service, ephemeralInstance, ephemeralIpPortId);
verify(ephemeralClientOperationServiceImpl).deregisterInstance(service, ephemeralInstance, ephemeralIpPortId);
verify(persistentClientOperationServiceImpl, never())
.deregisterInstance(service, ephemeralInstance, ephemeralIpPortId);
}

@Test
public void testChoosePersistentClientOperationService() {
clientOperationServiceProxy.registerInstance(service, persistentInstance, persistentIpPortId);
verify(persistentClientOperationServiceImpl).registerInstance(service, persistentInstance, persistentIpPortId);
verify(ephemeralClientOperationServiceImpl, never())
.registerInstance(service, persistentInstance, persistentIpPortId);
ServiceManager.getInstance().getSingleton(service);
// Test deregister.
clientOperationServiceProxy.deregisterInstance(service, persistentInstance, persistentIpPortId);
verify(persistentClientOperationServiceImpl).deregisterInstance(service, persistentInstance, persistentIpPortId);
verify(ephemeralClientOperationServiceImpl, never())
.deregisterInstance(service, persistentInstance, persistentIpPortId);
}

@Test
public void testSubscribeService() {
clientOperationServiceProxy.subscribeService(service, subscriber, ephemeralIpPortId);
verify(ephemeralClientOperationServiceImpl).subscribeService(service, subscriber, ephemeralIpPortId);
verify(persistentClientOperationServiceImpl, never()).subscribeService(service, subscriber, ephemeralIpPortId);
}

@Test
public void testUnsubscribeService() {
clientOperationServiceProxy.unsubscribeService(service, subscriber, ephemeralIpPortId);
verify(ephemeralClientOperationServiceImpl).unsubscribeService(service, subscriber, ephemeralIpPortId);
verify(persistentClientOperationServiceImpl, never()).unsubscribeService(service, subscriber, ephemeralIpPortId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.naming.core.v2.service.impl;

import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.naming.core.DistroMapper;
import com.alibaba.nacos.naming.core.v2.client.Client;
import com.alibaba.nacos.naming.core.v2.client.ClientSyncAttributes;
import com.alibaba.nacos.naming.core.v2.client.manager.ClientManager;
import com.alibaba.nacos.naming.core.v2.client.manager.ClientManagerDelegate;
import com.alibaba.nacos.naming.core.v2.client.manager.impl.ConnectionBasedClientManager;
import com.alibaba.nacos.naming.core.v2.client.manager.impl.EphemeralIpPortClientManager;
import com.alibaba.nacos.naming.core.v2.client.manager.impl.PersistentIpPortClientManager;
import com.alibaba.nacos.naming.core.v2.pojo.Service;
import com.alibaba.nacos.naming.misc.SwitchDomain;
import com.alibaba.nacos.naming.pojo.Subscriber;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.lang.reflect.Field;
import java.util.Collection;

import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class EphemeralClientOperationServiceImplTest extends TestCase {

private EphemeralClientOperationServiceImpl ephemeralClientOperationServiceImpl;

@Mock
private ConnectionBasedClientManager connectionBasedClientManager;

@Mock
private PersistentIpPortClientManager persistentIpPortClientManager;

@Mock
private ClientSyncAttributes clientSyncAttributes;

@Mock
private SwitchDomain switchDomain;

@Mock
private DistroMapper distroMapper;

@Mock
private Service service;

@Mock
private Instance instance;

@Mock
private Subscriber subscriber;

private final String clientId = "1.1.1.1:80#true";

private final String ip = "1.1.1.1";

private final int port = 80;

@Before
public void setUp() throws Exception {
when(instance.getIp()).thenReturn(ip);
when(instance.getPort()).thenReturn(port);
when(service.getNamespace()).thenReturn("public");

EphemeralIpPortClientManager ephemeralIpPortClientManager = new EphemeralIpPortClientManager(distroMapper,
switchDomain);
ephemeralIpPortClientManager.syncClientConnected(clientId, clientSyncAttributes);
ClientManagerDelegate clientManagerDelegate = new ClientManagerDelegate(connectionBasedClientManager,
ephemeralIpPortClientManager, persistentIpPortClientManager);
ephemeralClientOperationServiceImpl = new EphemeralClientOperationServiceImpl(clientManagerDelegate);
}

@Test
public void testRegisterAndDeregisterInstance() throws Exception {
Field clientManagerField = EphemeralClientOperationServiceImpl.class.getDeclaredField("clientManager");
clientManagerField.setAccessible(true);
// Test register instance
ephemeralClientOperationServiceImpl.registerInstance(service, instance, clientId);
ClientManager innerClientManager = (ClientManager) clientManagerField.get(ephemeralClientOperationServiceImpl);
Client client = innerClientManager.getClient(clientId);
assertTrue(client.getAllPublishedService().contains(service));
assertEquals(client.getInstancePublishInfo(service).getIp(), ip);
assertEquals(client.getInstancePublishInfo(service).getPort(), port);
// Test deregister instance
ephemeralClientOperationServiceImpl.deregisterInstance(service, instance, clientId);
assertNull(innerClientManager.getClient(clientId).getInstancePublishInfo(service));
Collection<Service> allPublishService = client.getAllPublishedService();
assertFalse(allPublishService.contains(service));
}

@Test
public void testSubscribeAndUnsubscribeService() throws Exception {
Field clientManagerField = EphemeralClientOperationServiceImpl.class.getDeclaredField("clientManager");
clientManagerField.setAccessible(true);
ClientManager innerClientManager = (ClientManager) clientManagerField.get(ephemeralClientOperationServiceImpl);
// Test subscribe instance
ephemeralClientOperationServiceImpl.subscribeService(service, subscriber, clientId);
Client client = innerClientManager.getClient(clientId);
assertTrue(client.getAllSubscribeService().contains(service));
// Test unsubscribe instance
ephemeralClientOperationServiceImpl.unsubscribeService(service, subscriber, clientId);
client = innerClientManager.getClient(clientId);
assertFalse(client.getAllSubscribeService().contains(service));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.naming.core.v2.service.impl;

import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.consistency.Serializer;
import com.alibaba.nacos.consistency.cp.CPProtocol;
import com.alibaba.nacos.consistency.entity.WriteRequest;
import com.alibaba.nacos.core.distributed.ProtocolManager;
import com.alibaba.nacos.naming.core.v2.client.manager.impl.PersistentIpPortClientManager;
import com.alibaba.nacos.naming.core.v2.pojo.Service;
import com.alibaba.nacos.naming.pojo.Subscriber;
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.lang.reflect.Field;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class PersistentClientOperationServiceImplTest {

private PersistentClientOperationServiceImpl persistentClientOperationServiceImpl;

@Mock
private ConfigurableApplicationContext applicationContext;

@Mock
private PersistentIpPortClientManager clientManager;

@Mock
private Service service;

@Mock
private Subscriber subscriber;

@Mock
private Instance instance;

@Mock
private ProtocolManager protocolManager;

@Mock
private CPProtocol cpProtocol;

private final String clientId = "1.1.1.1:80#false";

@Mock
private Serializer serializer;

@Before
public void setUp() throws Exception {
when(applicationContext.getBean(ProtocolManager.class)).thenReturn(protocolManager);
when(protocolManager.getCpProtocol()).thenReturn(cpProtocol);
when(serializer.serialize(any(PersistentClientOperationServiceImpl.InstanceStoreRequest.class)))
.thenReturn(new byte[1]);
ApplicationUtils.injectContext(applicationContext);
Field serializerField = PersistentClientOperationServiceImpl.class.getDeclaredField("serializer");
serializerField.setAccessible(true);
clientManager = new PersistentIpPortClientManager();
persistentClientOperationServiceImpl = new PersistentClientOperationServiceImpl(clientManager);
serializerField.set(persistentClientOperationServiceImpl, serializer);
}

@Test
public void testRegisterAndDeregisterInstance() throws Exception {
Field clientManagerField = PersistentClientOperationServiceImpl.class.getDeclaredField("clientManager");
clientManagerField.setAccessible(true);
// Test register instance
persistentClientOperationServiceImpl.registerInstance(service, instance, clientId);
verify(cpProtocol).write(any(WriteRequest.class));
// Test deregister instance
persistentClientOperationServiceImpl.deregisterInstance(service, instance, clientId);
verify(cpProtocol, times(2)).write(any(WriteRequest.class));
}

@Test(expected = UnsupportedOperationException.class)
public void testSubscribeService() {
persistentClientOperationServiceImpl.subscribeService(service, subscriber, clientId);
}

@Test(expected = UnsupportedOperationException.class)
public void testUnsubscribeService() {
persistentClientOperationServiceImpl.unsubscribeService(service, subscriber, clientId);
}
}

0 comments on commit bb9e374

Please sign in to comment.