add UT for control default plugin (#11572)

* add control default plugin Test

* Copyright fix
This commit is contained in:
hth 2024-01-05 17:48:41 +08:00 committed by GitHub
parent 8a69c9e8ac
commit 2296125d41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 254 additions and 0 deletions

View File

@ -0,0 +1,58 @@
/*
* Copyright 1999-2023 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.plugin.control.impl;
import com.alibaba.nacos.plugin.control.connection.request.ConnectionCheckRequest;
import com.alibaba.nacos.plugin.control.connection.response.ConnectionCheckResponse;
import com.alibaba.nacos.plugin.control.connection.rule.ConnectionControlRule;
import org.junit.Assert;
import org.junit.Test;
public class NacosConnectionControlManagerTest {
@Test
public void testApplyConnectionLimitRule() {
NacosConnectionControlManager nacosConnectionControlManager = new NacosConnectionControlManager();
ConnectionControlRule connectionControlRule = new ConnectionControlRule();
connectionControlRule.setCountLimit(10);
nacosConnectionControlManager.applyConnectionLimitRule(connectionControlRule);
ConnectionControlRule connectionLimitRule = nacosConnectionControlManager.getConnectionLimitRule();
Assert.assertEquals(connectionControlRule, connectionLimitRule);
}
@Test
public void testCheckLimit() {
NacosConnectionControlManager nacosConnectionControlManager = new NacosConnectionControlManager();
ConnectionControlRule connectionControlRule = new ConnectionControlRule();
connectionControlRule.setCountLimit(10);
nacosConnectionControlManager.applyConnectionLimitRule(connectionControlRule);
ConnectionCheckRequest connectionCheckRequest = new ConnectionCheckRequest("127.0.0.1", "test", "test");
ConnectionCheckResponse connectionCheckResponse = nacosConnectionControlManager.check(connectionCheckRequest);
Assert.assertFalse(connectionCheckResponse.isSuccess());
}
@Test
public void testCheckUnLimit() {
NacosConnectionControlManager nacosConnectionControlManager = new NacosConnectionControlManager();
ConnectionControlRule connectionControlRule = new ConnectionControlRule();
connectionControlRule.setCountLimit(30);
nacosConnectionControlManager.applyConnectionLimitRule(connectionControlRule);
ConnectionCheckRequest connectionCheckRequest = new ConnectionCheckRequest("127.0.0.1", "test", "test");
ConnectionCheckResponse connectionCheckResponse = nacosConnectionControlManager.check(connectionCheckRequest);
Assert.assertTrue(connectionCheckResponse.isSuccess());
}
}

View File

@ -0,0 +1,37 @@
/*
* Copyright 1999-2023 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.plugin.control.impl;
import com.alibaba.nacos.plugin.control.connection.ConnectionControlManager;
import com.alibaba.nacos.plugin.control.tps.TpsControlManager;
import org.junit.Assert;
import org.junit.Test;
public class NacosControlManagerBuilderTest {
@Test
public void test() {
NacosControlManagerBuilder nacosControlManagerBuilder = new NacosControlManagerBuilder();
ConnectionControlManager connectionControlManager = nacosControlManagerBuilder.buildConnectionControlManager();
TpsControlManager tpsControlManager = nacosControlManagerBuilder.buildTpsControlManager();
Assert.assertEquals("nacos", tpsControlManager.getName());
Assert.assertEquals("nacos", connectionControlManager.getName());
Assert.assertEquals("nacos", nacosControlManagerBuilder.getName());
}
}

View File

@ -0,0 +1,87 @@
/*
* Copyright 1999-2023 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.plugin.control.impl;
import com.alibaba.nacos.plugin.control.tps.MonitorType;
import com.alibaba.nacos.plugin.control.tps.request.TpsCheckRequest;
import com.alibaba.nacos.plugin.control.tps.response.TpsCheckResponse;
import com.alibaba.nacos.plugin.control.tps.rule.RuleDetail;
import com.alibaba.nacos.plugin.control.tps.rule.TpsControlRule;
import org.junit.Assert;
import org.junit.Test;
import java.util.concurrent.TimeUnit;
public class NacosTpsControlManagerTest {
@Test
public void testRegisterTpsPoint1() {
NacosTpsControlManager nacosTpsControlManager = new NacosTpsControlManager();
nacosTpsControlManager.registerTpsPoint("test");
Assert.assertTrue(nacosTpsControlManager.getPoints().containsKey("test"));
}
@Test
public void testRegisterTpsPoint2() {
NacosTpsControlManager nacosTpsControlManager = new NacosTpsControlManager();
TpsControlRule tpsLimitRule = new TpsControlRule();
nacosTpsControlManager.applyTpsRule("test", tpsLimitRule);
nacosTpsControlManager.registerTpsPoint("test");
Assert.assertTrue(nacosTpsControlManager.getPoints().containsKey("test"));
}
@Test
public void testApplyTpsRule1() {
NacosTpsControlManager nacosTpsControlManager = new NacosTpsControlManager();
TpsControlRule tpsLimitRule = new TpsControlRule();
nacosTpsControlManager.applyTpsRule("test", tpsLimitRule);
Assert.assertTrue(nacosTpsControlManager.getRules().containsKey("test"));
}
@Test
public void testApplyTpsRule2() {
NacosTpsControlManager nacosTpsControlManager = new NacosTpsControlManager();
nacosTpsControlManager.applyTpsRule("test", null);
Assert.assertFalse(nacosTpsControlManager.getRules().containsKey("test"));
}
@Test
public void testCheck() {
NacosTpsControlManager nacosTpsControlManager = new NacosTpsControlManager();
nacosTpsControlManager.registerTpsPoint("test");
final TpsControlRule tpsLimitRule = new TpsControlRule();
RuleDetail ruleDetail = new RuleDetail();
ruleDetail.setMaxCount(5);
ruleDetail.setMonitorType(MonitorType.INTERCEPT.getType());
ruleDetail.setPeriod(TimeUnit.SECONDS);
tpsLimitRule.setPointRule(ruleDetail);
tpsLimitRule.setPointName("test");
nacosTpsControlManager.applyTpsRule("test", tpsLimitRule);
long timeMillis = System.currentTimeMillis();
TpsCheckRequest tpsCheckRequest = new TpsCheckRequest();
tpsCheckRequest.setPointName("test");
tpsCheckRequest.setTimestamp(timeMillis);
TpsCheckResponse check = nacosTpsControlManager.check(tpsCheckRequest);
Assert.assertTrue(check.isSuccess());
}
}

View File

@ -0,0 +1,37 @@
/*
* Copyright 1999-2023 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.plugin.control.impl;
import com.alibaba.nacos.plugin.control.connection.ConnectionMetricsCollector;
public class TestConnectionMetricsCollector implements ConnectionMetricsCollector {
@Override
public String getName() {
return "test";
}
@Override
public int getTotalCount() {
return 20;
}
@Override
public int getCountForIp(String ip) {
return 10;
}
}

View File

@ -0,0 +1,35 @@
#
# Copyright 1999-2020 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.
#
#
#
# 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.
#
#
com.alibaba.nacos.plugin.control.impl.TestConnectionMetricsCollector