Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #5096] Add unit tests to package com.alibaba.nacos.naming.healthcheck in nacos 2.0 #5170

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* 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.healthcheck.v2;

import com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient;
import com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataManager;
import com.alibaba.nacos.naming.core.v2.pojo.Service;
import com.alibaba.nacos.naming.misc.SwitchDomain;
import com.alibaba.nacos.sys.utils.ApplicationUtils;
import org.junit.Assert;
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;
import java.util.List;

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

@RunWith(MockitoJUnitRunner.class)
public class HealthCheckTaskV2Test {

private HealthCheckTaskV2 healthCheckTaskV2;

@Mock
private IpPortBasedClient ipPortBasedClient;

@Mock
private ConfigurableApplicationContext context;

@Mock
private SwitchDomain switchDomain;

@Mock
private Service service;

@Before
public void setUp() {
ApplicationUtils.injectContext(context);
when(ApplicationUtils.getBean(SwitchDomain.class)).thenReturn(switchDomain);
when(switchDomain.getTcpHealthParams()).thenReturn(new SwitchDomain.TcpHealthParams());
when(ApplicationUtils.getBean(NamingMetadataManager.class)).thenReturn(new NamingMetadataManager());
healthCheckTaskV2 = new HealthCheckTaskV2(ipPortBasedClient);
}

@Test
public void testDoHealthCheck() {
when(ipPortBasedClient.getAllPublishedService()).thenReturn(returnService());

healthCheckTaskV2.setCheckRtWorst(1);
healthCheckTaskV2.setCheckRtLastLast(1);
Assert.assertEquals(1, healthCheckTaskV2.getCheckRtWorst());
Assert.assertEquals(1, healthCheckTaskV2.getCheckRtLastLast());

healthCheckTaskV2.run();
healthCheckTaskV2.passIntercept();
healthCheckTaskV2.doHealthCheck();

verify(ipPortBasedClient, times(3)).getAllPublishedService();
verify(switchDomain, times(3)).isHealthCheckEnabled(service.getGroupedServiceName());
}

private List<Service> returnService() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can replace with Collections.singleList

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can replace with Collections.singleList

fix

return Collections.singletonList(service);
}

@Test
public void testGetClient() {
Assert.assertNotNull(healthCheckTaskV2.getClient());
}

@Test
public void testGetAndSet() {
healthCheckTaskV2.setCheckRtBest(1);
healthCheckTaskV2.setCheckRtNormalized(1);
healthCheckTaskV2.setCheckRtLast(1);
healthCheckTaskV2.setCancelled(true);
healthCheckTaskV2.setStartTime(1615796485783L);

Assert.assertEquals(1, healthCheckTaskV2.getCheckRtBest());
Assert.assertEquals(1, healthCheckTaskV2.getCheckRtNormalized());
Assert.assertEquals(1, healthCheckTaskV2.getCheckRtLast());
Assert.assertTrue(healthCheckTaskV2.isCancelled());
Assert.assertEquals(1615796485783L, healthCheckTaskV2.getStartTime());
}

@Test
public void testAfterIntercept() {
healthCheckTaskV2.afterIntercept();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.healthcheck.v2;

import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.naming.core.v2.client.Client;
import com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo;
import com.alibaba.nacos.naming.core.v2.pojo.Service;
import com.alibaba.nacos.naming.core.v2.service.impl.PersistentClientOperationServiceImpl;
import com.alibaba.nacos.naming.utils.InstanceUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.mockito.Mockito.verify;

@RunWith(MockitoJUnitRunner.class)
public class PersistentHealthStatusSynchronizerTest {

@Mock
private PersistentClientOperationServiceImpl persistentClientOperationService;

@Mock
private Client client;

@Test
public void testInstanceHealthStatusChange() {
Service service = Service.newService("public", "DEFAULT", "nacos", true);
InstancePublishInfo instancePublishInfo = new InstancePublishInfo("127.0.0.1", 8080);
PersistentHealthStatusSynchronizer persistentHealthStatusSynchronizer = new PersistentHealthStatusSynchronizer(
persistentClientOperationService);
persistentHealthStatusSynchronizer.instanceHealthStatusChange(true, client, service, instancePublishInfo);

Instance updateInstance = InstanceUtil.parseToApiInstance(service, instancePublishInfo);
updateInstance.setHealthy(true);

verify(client).getClientId();
verify(persistentClientOperationService).registerInstance(service, updateInstance, client.getClientId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* 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.healthcheck.v2.processor;

import com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient;
import com.alibaba.nacos.naming.core.v2.pojo.HealthCheckInstancePublishInfo;
import com.alibaba.nacos.naming.core.v2.pojo.Service;
import com.alibaba.nacos.naming.healthcheck.v2.HealthCheckTaskV2;
import com.alibaba.nacos.naming.misc.SwitchDomain;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.concurrent.atomic.AtomicInteger;

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

@RunWith(MockitoJUnitRunner.class)
public class HealthCheckCommonV2Test {

@Mock
private SwitchDomain.HealthParams healthParams;

@Mock
private HealthCheckTaskV2 healthCheckTaskV2;

@Mock
private Service service;

@Mock
private IpPortBasedClient ipPortBasedClient;

@Mock
private HealthCheckInstancePublishInfo healthCheckInstancePublishInfo;

private HealthCheckCommonV2 healthCheckCommonV2;

@Before
public void setUp() {
healthCheckCommonV2 = new HealthCheckCommonV2();
when(healthCheckTaskV2.getClient()).thenReturn(ipPortBasedClient);
when(ipPortBasedClient.getInstancePublishInfo(service)).thenReturn(healthCheckInstancePublishInfo);
when(healthCheckInstancePublishInfo.getFailCount()).thenReturn(new AtomicInteger());
}

@Test
public void testReEvaluateCheckRT() {
healthCheckCommonV2.reEvaluateCheckRT(1, healthCheckTaskV2, healthParams);

verify(healthParams, times(2)).getMax();
verify(healthParams, times(1)).getMin();
verify(healthParams, times(2)).getFactor();

verify(healthCheckTaskV2).getCheckRtWorst();
verify(healthCheckTaskV2).getCheckRtBest();
verify(healthCheckTaskV2).getCheckRtNormalized();
}

@Test
public void testCheckOk() {
healthCheckCommonV2.checkOk(healthCheckTaskV2, service, "test checkOk");

verify(healthCheckTaskV2).getClient();
verify(service).getGroupedServiceName();
verify(ipPortBasedClient).getInstancePublishInfo(service);
verify(healthCheckInstancePublishInfo).isHealthy();
verify(healthCheckInstancePublishInfo).getCluster();
verify(healthCheckInstancePublishInfo).resetFailCount();
verify(healthCheckInstancePublishInfo).finishCheck();

}

@Test
public void testCheckFail() {
when(healthCheckInstancePublishInfo.isHealthy()).thenReturn(true);
healthCheckCommonV2.checkFail(healthCheckTaskV2, service, "test checkFail");

verify(healthCheckTaskV2).getClient();
verify(service).getGroupedServiceName();
verify(ipPortBasedClient).getInstancePublishInfo(service);
verify(healthCheckInstancePublishInfo).isHealthy();
verify(healthCheckInstancePublishInfo).getCluster();
verify(healthCheckInstancePublishInfo).resetOkCount();
verify(healthCheckInstancePublishInfo).finishCheck();
}

@Test
public void testCheckFailNow() {
when(healthCheckInstancePublishInfo.isHealthy()).thenReturn(true);
healthCheckCommonV2.checkFailNow(healthCheckTaskV2, service, "test checkFailNow");

verify(healthCheckTaskV2).getClient();
verify(service).getGroupedServiceName();
verify(ipPortBasedClient).getInstancePublishInfo(service);
verify(healthCheckInstancePublishInfo).isHealthy();
verify(healthCheckInstancePublishInfo).getCluster();
verify(healthCheckInstancePublishInfo).resetOkCount();
verify(healthCheckInstancePublishInfo).finishCheck();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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.healthcheck.v2.processor;

import com.alibaba.nacos.api.naming.pojo.healthcheck.HealthCheckType;
import com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient;
import com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata;
import com.alibaba.nacos.naming.core.v2.pojo.Service;
import com.alibaba.nacos.naming.healthcheck.extend.HealthCheckExtendProvider;
import com.alibaba.nacos.naming.healthcheck.v2.HealthCheckTaskV2;
import org.junit.Assert;
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.ArrayList;
import java.util.List;
import java.util.Map;

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

@RunWith(MockitoJUnitRunner.class)
public class HealthCheckProcessorV2DelegateTest {

@Mock
private HealthCheckExtendProvider healthCheckExtendProvider;

@Mock
private HealthCheckTaskV2 healthCheckTaskV2;

@Mock
private Service service;

@Mock
private ClusterMetadata clusterMetadata;

private HealthCheckProcessorV2Delegate healthCheckProcessorV2Delegate;

@Before
public void setUp() {
healthCheckProcessorV2Delegate = new HealthCheckProcessorV2Delegate(healthCheckExtendProvider);
verify(healthCheckExtendProvider).init();
}

@Test
public void testAddProcessor() throws NoSuchFieldException, IllegalAccessException {
List<HealthCheckProcessorV2> list = new ArrayList<>();
list.add(new TcpHealthCheckProcessor(null, null));
healthCheckProcessorV2Delegate.addProcessor(list);

Class<HealthCheckProcessorV2Delegate> healthCheckProcessorV2DelegateClass = HealthCheckProcessorV2Delegate.class;
Field field = healthCheckProcessorV2DelegateClass.getDeclaredField("healthCheckProcessorMap");
field.setAccessible(true);
Map<String, HealthCheckProcessorV2> map = (Map<String, HealthCheckProcessorV2>) field
.get(healthCheckProcessorV2Delegate);
HealthCheckProcessorV2 healthCheckProcessorV2 = map.get(HealthCheckType.TCP.name());
Assert.assertNotNull(healthCheckProcessorV2);
}

@Test
public void testProcess() throws NoSuchFieldException, IllegalAccessException {
testAddProcessor();
when(clusterMetadata.getHealthyCheckType()).thenReturn(HealthCheckType.TCP.name());
when(healthCheckTaskV2.getClient()).thenReturn(new IpPortBasedClient("127.0.0.1:80#true", true));

healthCheckProcessorV2Delegate.process(healthCheckTaskV2, service, clusterMetadata);

verify(clusterMetadata).getHealthyCheckType();
verify(healthCheckTaskV2).getClient();
}

@Test
public void testGetType() {
Assert.assertNull(healthCheckProcessorV2Delegate.getType());
}
}
Loading