-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
KomachiSion
merged 6 commits into
alibaba:feature_support_grpc_core
from
li-xiao-shuang:feature/add-healthcheck-test
Mar 26, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f5c208d
add unit test for healthcheck
li-xiao-shuang 322ebe3
add newline
li-xiao-shuang 219ad92
delete javadoc
li-xiao-shuang 383e97d
add Licensed
li-xiao-shuang 54b9ac5
add verify
li-xiao-shuang 5d9ec19
normalize
li-xiao-shuang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
naming/src/test/java/com/alibaba/nacos/naming/healthcheck/v2/HealthCheckTaskV2Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
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(); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
.../java/com/alibaba/nacos/naming/healthcheck/v2/PersistentHealthStatusSynchronizerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
.../test/java/com/alibaba/nacos/naming/healthcheck/v2/processor/HealthCheckCommonV2Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
...com/alibaba/nacos/naming/healthcheck/v2/processor/HealthCheckProcessorV2DelegateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix