You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/04/08 01:13:26 UTC

[shardingsphere] branch master updated: Add Lock event unit test. (#16644)

This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new fa6ddcdb169 Add Lock event unit test. (#16644)
fa6ddcdb169 is described below

commit fa6ddcdb169b20ff94eba8dfc222ea0734f9f104
Author: zhaojinchao <zh...@apache.org>
AuthorDate: Fri Apr 8 09:13:19 2022 +0800

    Add Lock event unit test. (#16644)
---
 .../lock/DistributeLockContextTest.java            | 36 ++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/lock/DistributeLockContextTest.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/lock/DistributeLockContextTest.java
index d06dd7164be..a9b994d5a03 100644
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/lock/DistributeLockContextTest.java
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/lock/DistributeLockContextTest.java
@@ -23,11 +23,18 @@ import org.apache.shardingsphere.infra.instance.InstanceContext;
 import org.apache.shardingsphere.infra.instance.definition.InstanceDefinition;
 import org.apache.shardingsphere.infra.instance.definition.InstanceType;
 import org.apache.shardingsphere.infra.instance.workerid.WorkerIdGenerator;
+import org.apache.shardingsphere.infra.lock.ShardingSphereGlobalLock;
 import org.apache.shardingsphere.infra.lock.ShardingSphereLock;
+import org.apache.shardingsphere.mode.manager.cluster.coordinator.lock.event.AckLockReleasedEvent;
+import org.apache.shardingsphere.mode.manager.cluster.coordinator.lock.event.LockedEvent;
 import org.apache.shardingsphere.mode.manager.cluster.coordinator.lock.service.LockRegistryService;
 import org.junit.Test;
 
+import java.lang.reflect.Field;
+import java.util.Arrays;
+import java.util.Map;
 import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
 
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
@@ -67,4 +74,33 @@ public final class DistributeLockContextTest {
         distributeLockContext.getOrCreateSchemaLock("schema");
         assertFalse(distributeLockContext.isLockedSchema("schema"));
     }
+    
+    @Test
+    public void assertAddGlobalLock() throws IllegalAccessException, NoSuchFieldException {
+        DistributeLockContext distributeLockContext = new DistributeLockContext(mock(LockRegistryService.class));
+        Field declaredField = DistributeLockContext.class.getDeclaredField("currentInstance");
+        declaredField.setAccessible(true);
+        declaredField.set(distributeLockContext, new ComputeNodeInstance(new InstanceDefinition(InstanceType.PROXY, "127.0.0.1@3307")));
+        Field computeNodeInstancesDeclaredField = DistributeLockContext.class.getDeclaredField("computeNodeInstances");
+        computeNodeInstancesDeclaredField.setAccessible(true);
+        computeNodeInstancesDeclaredField.set(distributeLockContext, Arrays.asList(new ComputeNodeInstance(new InstanceDefinition(InstanceType.PROXY, "127.0.0.1@3307"))));
+        distributeLockContext.renew(new LockedEvent("schema1-127.0.0.1@3308"));
+        assertTrue(distributeLockContext.getSchemaLock("schema1").isPresent());
+    }
+    
+    @Test
+    public void assertRemoveGlobalLock() throws IllegalAccessException, NoSuchFieldException {
+        DistributeLockContext distributeLockContext = new DistributeLockContext(mock(LockRegistryService.class));
+        Field declaredField = DistributeLockContext.class.getDeclaredField("currentInstance");
+        declaredField.setAccessible(true);
+        declaredField.set(distributeLockContext, new ComputeNodeInstance(new InstanceDefinition(InstanceType.PROXY, "127.0.0.1@3307")));
+        Map<String, ShardingSphereGlobalLock> globalLocks = new ConcurrentHashMap<>();
+        globalLocks.put("schema", mock(ShardingSphereGlobalLock.class));
+        Field globalLocksDeclaredField = DistributeLockContext.class.getDeclaredField("globalLocks");
+        globalLocksDeclaredField.setAccessible(true);
+        globalLocksDeclaredField.set(distributeLockContext, globalLocks);
+        assertTrue(distributeLockContext.getSchemaLock("schema").isPresent());
+        distributeLockContext.renew(new AckLockReleasedEvent("schema-127.0.0.1@3307"));
+        assertFalse(distributeLockContext.getSchemaLock("schema").isPresent());
+    }
 }