-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FOR 5094 remote] add unit tests in nacos-core. (#6262)
* [ISSUE #5904] add unit tests for ClientConnectionEventListenerRegistryTest and ConnectionManagerTest. * [ISSUE #5904] add unit tests for left class. * [nacos-core] [unit test] change the uuid. * [ISSUE #5904] fix the wrong assert.
- Loading branch information
1 parent
414e2fa
commit e2fc2e4
Showing
6 changed files
with
503 additions
and
0 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
...rc/test/java/com/alibaba/nacos/core/remote/ClientConnectionEventListenerRegistryTest.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,71 @@ | ||
/* | ||
* Copyright 1999-2021 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.core.remote; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* {@link ClientConnectionEventListenerRegistry} uint test. | ||
* | ||
* @author chenglu | ||
* @date 2021-07-02 14:43 | ||
*/ | ||
@RunWith(MockitoJUnitRunner.class) | ||
public class ClientConnectionEventListenerRegistryTest { | ||
|
||
@InjectMocks | ||
private ClientConnectionEventListenerRegistry registry; | ||
|
||
@Mock | ||
private Connection connection; | ||
|
||
@Test | ||
public void testRegistryMethods() { | ||
try { | ||
registry.registerClientConnectionEventListener(new MockClientConnectionEventListener()); | ||
|
||
registry.notifyClientConnected(connection); | ||
|
||
registry.notifyClientDisConnected(connection); | ||
|
||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
Assert.fail(e.getMessage()); | ||
} | ||
} | ||
|
||
class MockClientConnectionEventListener extends ClientConnectionEventListener { | ||
|
||
@Override | ||
public void clientConnected(Connection connect) { | ||
Assert.assertTrue(Objects.nonNull(connect)); | ||
} | ||
|
||
@Override | ||
public void clientDisConnected(Connection connect) { | ||
Assert.assertTrue(Objects.nonNull(connect)); | ||
} | ||
} | ||
} |
172 changes: 172 additions & 0 deletions
172
core/src/test/java/com/alibaba/nacos/core/remote/ConnectionManagerTest.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,172 @@ | ||
/* | ||
* Copyright 1999-2021 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.core.remote; | ||
|
||
import com.alibaba.nacos.api.exception.NacosException; | ||
import com.alibaba.nacos.api.remote.RemoteConstants; | ||
import com.alibaba.nacos.common.notify.NotifyCenter; | ||
import com.alibaba.nacos.core.remote.event.ConnectionLimitRuleChangeEvent; | ||
import com.alibaba.nacos.core.remote.grpc.GrpcConnection; | ||
import com.alibaba.nacos.sys.env.EnvUtil; | ||
import com.alibaba.nacos.sys.file.WatchFileCenter; | ||
import io.grpc.netty.shaded.io.netty.channel.Channel; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import java.nio.file.Paths; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
/** | ||
* {@link ConnectionManager} unit test. | ||
* | ||
* @author chenglu | ||
* @date 2021-07-02 14:57 | ||
*/ | ||
@RunWith(MockitoJUnitRunner.class) | ||
public class ConnectionManagerTest { | ||
|
||
@InjectMocks | ||
private ConnectionManager connectionManager; | ||
|
||
@Mock | ||
private ClientConnectionEventListenerRegistry registry; | ||
|
||
@InjectMocks | ||
private GrpcConnection connection; | ||
|
||
@Mock | ||
private Channel channel; | ||
|
||
@Mock | ||
private ConnectionMeta connectionMeta; | ||
|
||
private String connectId; | ||
|
||
private String clientIp = "1.1.1.1"; | ||
|
||
@Before | ||
public void setUp() { | ||
connectId = UUID.randomUUID().toString(); | ||
connectionManager.start(); | ||
connectionManager.initLimitRue(); | ||
Mockito.when(channel.isOpen()).thenReturn(true); | ||
Mockito.when(channel.isActive()).thenReturn(true); | ||
connectionMeta.clientIp = clientIp; | ||
Map<String, String> labels = new HashMap<>(); | ||
labels.put("key", "value"); | ||
labels.put(RemoteConstants.LABEL_SOURCE, RemoteConstants.LABEL_SOURCE_SDK); | ||
connectionMeta.labels = labels; | ||
connectionManager.loadCount(1, clientIp); | ||
|
||
connectionManager.register(connectId, connection); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
connectionManager.unregister(connectId); | ||
|
||
String tpsPath = Paths.get(EnvUtil.getNacosHome(), "data", "loader").toString(); | ||
WatchFileCenter.deregisterAllWatcher(tpsPath); | ||
|
||
NotifyCenter.deregisterSubscriber(connectionManager); | ||
NotifyCenter.deregisterPublisher(ConnectionLimitRuleChangeEvent.class); | ||
} | ||
|
||
@Test | ||
public void testCheckValid() { | ||
Assert.assertTrue(connectionManager.checkValid(connectId)); | ||
} | ||
|
||
@Test | ||
public void testTraced() { | ||
Assert.assertTrue(connectionManager.traced(clientIp)); | ||
} | ||
|
||
@Test | ||
public void testGetConnection() { | ||
Assert.assertEquals(connection, connectionManager.getConnection(connectId)); | ||
} | ||
|
||
@Test | ||
public void testGetConnectionsByClientIp() { | ||
Assert.assertEquals(1, connectionManager.getConnectionByIp(clientIp).size()); | ||
} | ||
|
||
@Test | ||
public void testGetCurrentConnectionCount() { | ||
Assert.assertEquals(1, connectionManager.getCurrentConnectionCount()); | ||
} | ||
|
||
@Test | ||
public void testRefreshActiveTime() { | ||
try { | ||
connectionManager.refreshActiveTime(connectId); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
Assert.fail(e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testLoadSingle() throws NacosException { | ||
Mockito.when(connectionMeta.isSdkSource()).thenReturn(true); | ||
connectionManager.loadSingle(connectId, clientIp); | ||
} | ||
|
||
@Test | ||
public void testCurrentClientsCount() { | ||
Map<String, String> labels = new HashMap<>(); | ||
labels.put("key", "value"); | ||
Assert.assertEquals(1, connectionManager.currentClientsCount(labels)); | ||
} | ||
|
||
@Test | ||
public void testCurrentSdkCount() { | ||
Assert.assertEquals(1, connectionManager.currentSdkClientCount()); | ||
} | ||
|
||
@Test | ||
public void testOnEvent() { | ||
try { | ||
String limitRule = "{\"monitorIpList\": [\"1.1.1.1\", \"2.2.2.2\"], \"countLimit\": 1}"; | ||
ConnectionLimitRuleChangeEvent limitRuleChangeEvent = new ConnectionLimitRuleChangeEvent(limitRule); | ||
connectionManager.onEvent(limitRuleChangeEvent); | ||
|
||
ConnectionManager.ConnectionLimitRule connectionLimitRule = connectionManager.getConnectionLimitRule(); | ||
Assert.assertEquals(1, connectionLimitRule.getCountLimit()); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
Assert.fail(e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testGetSubscribeType() { | ||
Assert.assertEquals(ConnectionLimitRuleChangeEvent.class, connectionManager.subscribeType()); | ||
} | ||
} | ||
|
38 changes: 38 additions & 0 deletions
38
core/src/test/java/com/alibaba/nacos/core/remote/HealthCheckRequestHandlerTest.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,38 @@ | ||
/* | ||
* Copyright 1999-2021 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.core.remote; | ||
|
||
import com.alibaba.nacos.api.remote.response.HealthCheckResponse; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* {@link HealthCheckRequestHandler} unit test. | ||
* | ||
* @author chenglu | ||
* @date 2021-07-02 19:17 | ||
*/ | ||
public class HealthCheckRequestHandlerTest { | ||
|
||
@Test | ||
public void testHandle() { | ||
HealthCheckRequestHandler handler = new HealthCheckRequestHandler(); | ||
HealthCheckResponse response = handler.handle(null, null); | ||
Assert.assertNotNull(response); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
core/src/test/java/com/alibaba/nacos/core/remote/RequestFiltersTest.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,47 @@ | ||
/* | ||
* Copyright 1999-2021 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.core.remote; | ||
|
||
import com.alibaba.nacos.api.exception.NacosException; | ||
import com.alibaba.nacos.api.remote.request.Request; | ||
import com.alibaba.nacos.api.remote.request.RequestMeta; | ||
import com.alibaba.nacos.api.remote.response.Response; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* {@link RequestFilters} unit test. | ||
* | ||
* @author chenglu | ||
* @date 2021-07-02 19:20 | ||
*/ | ||
public class RequestFiltersTest { | ||
|
||
@Test | ||
public void testRegisterFilter() { | ||
RequestFilters requestFilters = new RequestFilters(); | ||
requestFilters.registerFilter(new AbstractRequestFilter() { | ||
@Override | ||
protected Response filter(Request request, RequestMeta meta, Class handlerClazz) throws NacosException { | ||
return null; | ||
} | ||
}); | ||
|
||
Assert.assertEquals(1, requestFilters.filters.size()); | ||
} | ||
} |
Oops, something went wrong.