Add junit test
This commit is contained in:
parent
8143608e7d
commit
41d278d6f1
@ -0,0 +1,118 @@
|
||||
/*
|
||||
* Copyright 1999-2022 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.api.utils;
|
||||
|
||||
import com.alibaba.nacos.api.ability.constant.AbilityKey;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class AbilityTableUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testGetAbilityBitBy() {
|
||||
byte[] abilityBitBy = AbilityTableUtils.getAbilityBitBy(Arrays.asList(1, 8, 9, 17));
|
||||
Assert.assertEquals(abilityBitBy[0], -127);
|
||||
Assert.assertEquals(abilityBitBy[1], -128);
|
||||
Assert.assertEquals(abilityBitBy[2], -128);
|
||||
// clear
|
||||
byte[] abilityBits = AbilityTableUtils.getAbilityBitBy(Collections.emptyList());
|
||||
Assert.assertEquals(abilityBits.length , 1);
|
||||
Assert.assertEquals(abilityBits[0] , 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAbilityTableBy() {
|
||||
byte[] bytes = new byte[]{0};
|
||||
Map<AbilityKey, Boolean> abilityTableBy =
|
||||
AbilityTableUtils.getAbilityTableBy(bytes, AbilityKey.offset());
|
||||
Assert.assertEquals(abilityTableBy.getOrDefault(AbilityKey.TEST_1, false), false);
|
||||
Assert.assertEquals(abilityTableBy.getOrDefault(AbilityKey.TEST_2, false), false);
|
||||
|
||||
byte[] bytes1 = new byte[]{-64};
|
||||
Map<AbilityKey, Boolean> abilityTableBy1 =
|
||||
AbilityTableUtils.getAbilityTableBy(bytes1, AbilityKey.offset());
|
||||
Assert.assertEquals(abilityTableBy1.get(AbilityKey.TEST_1), true);
|
||||
Assert.assertEquals(abilityTableBy1.get(AbilityKey.TEST_2), true);
|
||||
|
||||
byte[] bytes2 = new byte[]{-128};
|
||||
Map<AbilityKey, Boolean> abilityTableBy2 =
|
||||
AbilityTableUtils.getAbilityTableBy(bytes2, AbilityKey.offset());
|
||||
Assert.assertEquals(abilityTableBy2.getOrDefault(AbilityKey.TEST_1, false), true);
|
||||
Assert.assertEquals(abilityTableBy2.getOrDefault(AbilityKey.TEST_2, false), false);
|
||||
|
||||
byte[] bytes3 = new byte[]{64};
|
||||
Map<AbilityKey, Boolean> abilityTableBy3 =
|
||||
AbilityTableUtils.getAbilityTableBy(bytes3, AbilityKey.offset());
|
||||
Assert.assertEquals(abilityTableBy3.getOrDefault(AbilityKey.TEST_1, false), false);
|
||||
Assert.assertEquals(abilityTableBy3.getOrDefault(AbilityKey.TEST_2, false), true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAbilityBiTableBy() {
|
||||
Map<AbilityKey, Boolean> map = new HashMap<>();
|
||||
byte[] bytes1 = AbilityTableUtils.getAbilityBiTableBy(AbilityKey.values(), map);
|
||||
Assert.assertEquals(1, bytes1.length);
|
||||
Assert.assertEquals(bytes1[0], 0);
|
||||
|
||||
map.put(AbilityKey.TEST_1, true);
|
||||
byte[] bytes2 = AbilityTableUtils.getAbilityBiTableBy(AbilityKey.values(), map);
|
||||
Assert.assertEquals(1, bytes1.length);
|
||||
Assert.assertEquals(bytes2[0], -128);
|
||||
|
||||
map.put(AbilityKey.TEST_1, false);
|
||||
map.put(AbilityKey.TEST_2, true);
|
||||
byte[] bytes3 = AbilityTableUtils.getAbilityBiTableBy(AbilityKey.values(), map);
|
||||
Assert.assertEquals(1, bytes3.length);
|
||||
Assert.assertEquals(bytes3[0], 64);
|
||||
|
||||
map.put(AbilityKey.TEST_1, true);
|
||||
byte[] bytes4 = AbilityTableUtils.getAbilityBiTableBy(AbilityKey.values(), map);
|
||||
Assert.assertEquals(1, bytes4.length);
|
||||
Assert.assertEquals(bytes4[0], -64);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAbilityBiTable() {
|
||||
Map<AbilityKey, Integer> offset = AbilityKey.offset();
|
||||
Map<AbilityKey, Boolean> abilities = new HashMap<>();
|
||||
byte[] bytes1 = AbilityTableUtils.getAbilityBiTableBy(offset, abilities);
|
||||
Assert.assertEquals(1, bytes1.length);
|
||||
Assert.assertEquals(bytes1[0], 0);
|
||||
|
||||
abilities.put(AbilityKey.TEST_1, true);
|
||||
byte[] bytes2 = AbilityTableUtils.getAbilityBiTableBy(offset, abilities);
|
||||
Assert.assertEquals(1, bytes2.length);
|
||||
Assert.assertEquals(bytes2[0], -128);
|
||||
|
||||
abilities.put(AbilityKey.TEST_2, true);
|
||||
byte[] bytes3 = AbilityTableUtils.getAbilityBiTableBy(offset, abilities);
|
||||
Assert.assertEquals(1, bytes3.length);
|
||||
Assert.assertEquals(bytes3[0], -64);
|
||||
|
||||
offset = new HashMap<>();
|
||||
offset.put(AbilityKey.TEST_1, 2);
|
||||
byte[] bytes4 = AbilityTableUtils.getAbilityBiTableBy(offset, abilities);
|
||||
Assert.assertEquals(1, bytes4.length);
|
||||
Assert.assertEquals(bytes4[0], 64);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,189 @@
|
||||
/*
|
||||
* Copyright 1999-2022 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.client.ability;
|
||||
|
||||
import com.alibaba.nacos.api.ability.constant.AbilityKey;
|
||||
import com.alibaba.nacos.api.ability.constant.AbilityStatus;
|
||||
import com.alibaba.nacos.api.ability.entity.AbilityTable;
|
||||
import com.alibaba.nacos.common.ability.handler.HandlerMapping;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Map;
|
||||
|
||||
public class AbilityControlManagerTest {
|
||||
|
||||
private TestClientAbilityControlManager clientAbilityControlManager = new TestClientAbilityControlManager();
|
||||
|
||||
private volatile int enabled = 0;
|
||||
|
||||
private volatile LinkedList<String> testPriority = new LinkedList<>();
|
||||
|
||||
@Before
|
||||
public void inject() {
|
||||
Map<AbilityKey, Boolean> newTable = new HashMap<>();
|
||||
newTable.put(AbilityKey.TEST_1, true);
|
||||
clientAbilityControlManager.setCurrentSupportingAbility(newTable);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClientAdd() {
|
||||
Map<AbilityKey, Boolean> newTable = new HashMap<>();
|
||||
newTable.put(AbilityKey.TEST_2, true);
|
||||
newTable.put(AbilityKey.TEST_1, true);
|
||||
AbilityTable table = new AbilityTable();
|
||||
table.setConnectionId("test-00001");
|
||||
table.setAbility(newTable);
|
||||
table.setServer(true);
|
||||
clientAbilityControlManager.addNewTable(table);
|
||||
Assert.assertEquals(AbilityStatus.NOT_SUPPORTED, clientAbilityControlManager.isSupport("test-00001", AbilityKey.TEST_2));
|
||||
Assert.assertEquals(AbilityStatus.SUPPORTED, clientAbilityControlManager.isSupport("test-00001", AbilityKey.TEST_1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClientRemove() {
|
||||
Map<AbilityKey, Boolean> clientTa = new HashMap<>();
|
||||
clientTa.put(AbilityKey.TEST_2, true);
|
||||
clientTa.put(AbilityKey.TEST_1, false);
|
||||
AbilityTable clientTable = new AbilityTable();
|
||||
clientTable.setConnectionId("test-01111");
|
||||
clientTable.setAbility(clientTa);
|
||||
clientTable.setServer(true);
|
||||
clientAbilityControlManager.addNewTable(clientTable);
|
||||
Assert.assertTrue(clientAbilityControlManager.contains(clientTable.getConnectionId()));
|
||||
clientAbilityControlManager.removeTable("test-01111");
|
||||
Assert.assertFalse(clientAbilityControlManager.contains(clientTable.getConnectionId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComponent() throws InterruptedException {
|
||||
enabled = 0;
|
||||
// invoke enable() or disable() when registering
|
||||
clientAbilityControlManager.registerComponent(AbilityKey.TEST_1, new TestHandlerMapping(), -1);
|
||||
Assert.assertEquals(1, clientAbilityControlManager.handlerMappingCount());
|
||||
|
||||
clientAbilityControlManager.enableCurrentNodeAbility(AbilityKey.TEST_1);
|
||||
// wait for invoking handler asyn
|
||||
Thread.sleep(200L);
|
||||
// nothing happens if it has enabled
|
||||
Assert.assertEquals(enabled, 1);
|
||||
Assert.assertTrue(clientAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.TEST_1));
|
||||
|
||||
// invoke disable()
|
||||
clientAbilityControlManager.disableCurrentNodeAbility(AbilityKey.TEST_1);
|
||||
// wait for invoking handler asyn
|
||||
Thread.sleep(200L);
|
||||
// disable will invoke handler
|
||||
Assert.assertEquals(enabled, 0);
|
||||
Assert.assertFalse(clientAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.TEST_1));
|
||||
|
||||
clientAbilityControlManager.disableCurrentNodeAbility(AbilityKey.TEST_1);
|
||||
// wait for invoking handler asyn
|
||||
Thread.sleep(200L);
|
||||
// nothing to do because it has disable
|
||||
Assert.assertEquals(enabled, 0);
|
||||
Assert.assertFalse(clientAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.TEST_1));
|
||||
|
||||
clientAbilityControlManager.enableCurrentNodeAbility(AbilityKey.TEST_1);
|
||||
// wait for invoking handler asyn
|
||||
Thread.sleep(200L);
|
||||
Assert.assertEquals(enabled, 1);
|
||||
Assert.assertTrue(clientAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.TEST_1));
|
||||
|
||||
clientAbilityControlManager.enableCurrentNodeAbility(AbilityKey.TEST_1);
|
||||
// wait for invoking handler asyn
|
||||
Thread.sleep(200L);
|
||||
Assert.assertEquals(enabled, 1);
|
||||
Assert.assertTrue(clientAbilityControlManager.isCurrentNodeAbilityRunning(AbilityKey.TEST_1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPriority() throws InterruptedException {
|
||||
TestClientAbilityControlManager testClientAbilityControlManager = new TestClientAbilityControlManager();
|
||||
AbilityKey key = AbilityKey.TEST_1;
|
||||
TestPriority clusterHandlerMapping1 = new TestPriority("1");
|
||||
TestPriority clusterHandlerMapping2 = new TestPriority("2");
|
||||
TestPriority clusterHandlerMapping3 = new TestPriority("3");
|
||||
// first one, invoke enable()
|
||||
testClientAbilityControlManager.registerComponent(key, clusterHandlerMapping2, 128);
|
||||
// last one, invoke enable()
|
||||
testClientAbilityControlManager.registerComponent(key, clusterHandlerMapping3);
|
||||
// second one, invoke enable()
|
||||
testClientAbilityControlManager.registerComponent(key, clusterHandlerMapping1, 12);
|
||||
// trigger cluster
|
||||
testClientAbilityControlManager.trigger(key);
|
||||
Assert.assertEquals(3, testClientAbilityControlManager.getHandlerMapping(key).size());
|
||||
// wait for invoking
|
||||
Thread.sleep(200L);
|
||||
Assert.assertEquals("2", testPriority.poll());
|
||||
Assert.assertEquals("3", testPriority.poll());
|
||||
Assert.assertEquals("1", testPriority.poll());
|
||||
// here are priority
|
||||
Assert.assertEquals("2", testPriority.poll());
|
||||
Assert.assertEquals("1", testPriority.poll());
|
||||
Assert.assertEquals("3", testPriority.poll());
|
||||
|
||||
// remove
|
||||
testClientAbilityControlManager.registerComponent(key, new TestHandlerMapping(), -1);
|
||||
Assert.assertEquals(4, testClientAbilityControlManager.getHandlerMapping(key).size());
|
||||
Assert.assertEquals(1, testClientAbilityControlManager.removeComponent(key, TestHandlerMapping.class));
|
||||
Assert.assertEquals(3, testClientAbilityControlManager.getHandlerMapping(key).size());
|
||||
testClientAbilityControlManager.removeAll(key);
|
||||
Assert.assertNull(testClientAbilityControlManager.getHandlerMapping(key));
|
||||
}
|
||||
|
||||
class TestPriority implements HandlerMapping {
|
||||
|
||||
String mark;
|
||||
|
||||
public TestPriority(String mark) {
|
||||
// unique one
|
||||
this.mark = mark.intern();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enable() {
|
||||
testPriority.offer(mark);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disable() {
|
||||
testPriority.offer(mark);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class TestHandlerMapping implements HandlerMapping {
|
||||
|
||||
@Override
|
||||
public void enable() {
|
||||
enabled++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disable() {
|
||||
enabled--;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -14,12 +14,12 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.nacos.test.ability;
|
||||
package com.alibaba.nacos.client.ability;
|
||||
|
||||
import com.alibaba.nacos.api.ability.constant.AbilityKey;
|
||||
import com.alibaba.nacos.client.ability.ClientAbilityControlManager;
|
||||
import com.alibaba.nacos.common.JustForTest;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class TestClientAbilityControlManager extends ClientAbilityControlManager {
|
||||
@ -28,4 +28,19 @@ public class TestClientAbilityControlManager extends ClientAbilityControlManager
|
||||
public void setCurrentSupportingAbility(Map<AbilityKey, Boolean> ability) {
|
||||
currentRunningAbility.putAll(ability);
|
||||
}
|
||||
|
||||
@JustForTest
|
||||
public int handlerMappingCount() {
|
||||
return super.handlerMapping().size();
|
||||
}
|
||||
|
||||
@JustForTest
|
||||
public List<HandlerWithPriority> getHandlerMapping(AbilityKey abilityKey) {
|
||||
return super.handlerMapping().get(abilityKey);
|
||||
}
|
||||
|
||||
@JustForTest
|
||||
public void trigger(AbilityKey abilityKey) {
|
||||
triggerHandlerMappingAsyn(abilityKey, true, handlerMapping());
|
||||
}
|
||||
}
|
@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.nacos.test.ability;
|
||||
package com.alibaba.nacos.core.ability;
|
||||
|
||||
import com.alibaba.nacos.api.ability.constant.AbilityKey;
|
||||
import com.alibaba.nacos.api.ability.constant.AbilityStatus;
|
||||
@ -33,8 +33,6 @@ import java.util.Set;
|
||||
@SpringBootTest
|
||||
public class AbilityControlManagerTest {
|
||||
|
||||
private TestClientAbilityControlManager clientAbilityControlManager = new TestClientAbilityControlManager();
|
||||
|
||||
private TestServerAbilityControlManager serverAbilityControlManager = new TestServerAbilityControlManager();
|
||||
|
||||
private volatile int clusterEnabled = 0;
|
||||
@ -47,7 +45,7 @@ public class AbilityControlManagerTest {
|
||||
public void inject() {
|
||||
Map<AbilityKey, Boolean> newTable = new HashMap<>();
|
||||
newTable.put(AbilityKey.TEST_1, true);
|
||||
clientAbilityControlManager.setCurrentSupportingAbility(newTable);
|
||||
serverAbilityControlManager.setCurrentSupportingAbility(newTable);
|
||||
|
||||
Map<AbilityKey, Boolean> table = new HashMap<>();
|
||||
table.put(AbilityKey.TEST_1, true);
|
||||
@ -68,9 +66,9 @@ public class AbilityControlManagerTest {
|
||||
table.setConnectionId("test-00001");
|
||||
table.setAbility(newTable);
|
||||
table.setServer(true);
|
||||
clientAbilityControlManager.addNewTable(table);
|
||||
Assert.assertEquals(AbilityStatus.NOT_SUPPORTED, clientAbilityControlManager.isSupport("test-00001", AbilityKey.TEST_2));
|
||||
Assert.assertEquals(AbilityStatus.SUPPORTED, clientAbilityControlManager.isSupport("test-00001", AbilityKey.TEST_1));
|
||||
serverAbilityControlManager.addNewTable(table);
|
||||
Assert.assertEquals(AbilityStatus.NOT_SUPPORTED, serverAbilityControlManager.isSupport("test-00001", AbilityKey.TEST_2));
|
||||
Assert.assertEquals(AbilityStatus.SUPPORTED, serverAbilityControlManager.isSupport("test-00001", AbilityKey.TEST_1));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -117,10 +115,10 @@ public class AbilityControlManagerTest {
|
||||
clientTable.setConnectionId("test-01111");
|
||||
clientTable.setAbility(clientTa);
|
||||
clientTable.setServer(true);
|
||||
clientAbilityControlManager.addNewTable(clientTable);
|
||||
Assert.assertTrue(clientAbilityControlManager.contains(clientTable.getConnectionId()));
|
||||
clientAbilityControlManager.removeTable("test-01111");
|
||||
Assert.assertFalse(clientAbilityControlManager.contains(clientTable.getConnectionId()));
|
||||
serverAbilityControlManager.addNewTable(clientTable);
|
||||
Assert.assertTrue(serverAbilityControlManager.contains(clientTable.getConnectionId()));
|
||||
serverAbilityControlManager.removeTable("test-01111");
|
||||
Assert.assertFalse(serverAbilityControlManager.contains(clientTable.getConnectionId()));
|
||||
}
|
||||
|
||||
@Test
|
@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.nacos.test.ability;
|
||||
package com.alibaba.nacos.core.ability;
|
||||
|
||||
import com.alibaba.nacos.api.ability.constant.AbilityKey;
|
||||
import com.alibaba.nacos.common.JustForTest;
|
Loading…
Reference in New Issue
Block a user