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

add UT for control default plugin #11572

Merged
merged 2 commits into from
Jan 5, 2024
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,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());
}
}
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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
Loading