You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by xi...@apache.org on 2020/12/04 05:19:11 UTC

[shardingsphere] branch master updated: Add unit tests (#8500)

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

xiaoyu 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 3363a97  Add unit tests (#8500)
3363a97 is described below

commit 3363a976140889da693032e9b32f091bceef1949
Author: Haoran Meng <me...@gmail.com>
AuthorDate: Fri Dec 4 13:18:49 2020 +0800

    Add unit tests (#8500)
---
 .../governance/core/lock/LockCenter.java           |  6 --
 .../core/lock/GovernanceLockStrategyTest.java      | 59 +++++++++++++++
 .../governance/core/lock/LockCenterTest.java       | 83 ++++++++++++++++++++++
 .../shardingsphere/infra/lock/LockContext.java     |  2 +-
 .../shardingsphere/infra/lock/LockContextTest.java | 61 ++++++++++++++++
 .../infra/lock/StandardLockStrategyTest.java       | 46 ++++++++++++
 6 files changed, 250 insertions(+), 7 deletions(-)

diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/lock/LockCenter.java b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/lock/LockCenter.java
index 9dadeef..863883e 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/lock/LockCenter.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/lock/LockCenter.java
@@ -87,12 +87,6 @@ public final class LockCenter {
      */
     public void releaseGlobalLock() {
         registryRepository.releaseLock();
-    }
-    
-    /**
-     * Delete global lock.
-     */
-    public void deleteGlobalLock() {
         registryRepository.delete(lockNode.getGlobalLockNodePath());
     }
 }
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/lock/GovernanceLockStrategyTest.java b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/lock/GovernanceLockStrategyTest.java
new file mode 100644
index 0000000..e8cfed3
--- /dev/null
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/lock/GovernanceLockStrategyTest.java
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.shardingsphere.governance.core.lock;
+
+import org.apache.shardingsphere.governance.core.facade.GovernanceFacade;
+import org.apache.shardingsphere.infra.lock.LockStrategy;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class GovernanceLockStrategyTest {
+    
+    @Mock
+    private GovernanceFacade governanceFacade;
+    
+    @Mock
+    private LockCenter lockCenter;
+    
+    private LockStrategy lockStrategy;
+    
+    @Before
+    public void setUp() {
+        lockStrategy = new GovernanceLockStrategy(governanceFacade);
+        when(governanceFacade.getLockCenter()).thenReturn(lockCenter);
+    }
+    
+    @Test
+    public void assertTryLock() {
+        lockStrategy.tryLock();
+        verify(lockCenter).tryGlobalLock();
+    }
+    
+    @Test
+    public void assertReleaseLock() {
+        lockStrategy.releaseLock();
+        verify(lockCenter).releaseGlobalLock();
+    }
+}
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/lock/LockCenterTest.java b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/lock/LockCenterTest.java
new file mode 100644
index 0000000..d3841b9
--- /dev/null
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/lock/LockCenterTest.java
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.shardingsphere.governance.core.lock;
+
+import org.apache.shardingsphere.governance.core.event.model.lock.GlobalLockAddedEvent;
+import org.apache.shardingsphere.governance.core.lock.node.LockNode;
+import org.apache.shardingsphere.governance.core.registry.RegistryCenter;
+import org.apache.shardingsphere.governance.core.registry.RegistryCenterNodeStatus;
+import org.apache.shardingsphere.governance.repository.api.RegistryRepository;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.util.concurrent.TimeUnit;
+
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.verify;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class LockCenterTest {
+    
+    @Mock
+    private RegistryRepository registryRepository;
+    
+    @Mock
+    private RegistryCenter registryCenter;
+    
+    private LockCenter lockCenter;
+    
+    @Before
+    public void setUp() {
+        lockCenter = new LockCenter(registryRepository, registryCenter);
+    }
+    
+    @Test
+    public void assertLock() {
+        lockCenter.lock(new GlobalLockAddedEvent());
+        verify(registryCenter).persistInstanceData(RegistryCenterNodeStatus.LOCKED.toString());
+    }
+    
+    @Test
+    public void assertUnlock() {
+        lockCenter.lock(new GlobalLockAddedEvent());
+        lockCenter.unlock();
+        verify(registryCenter).persistInstanceData(RegistryCenterNodeStatus.OK.toString());
+    }
+    
+    @Test
+    public void assertTryGlobalLock() {
+        lockCenter.tryGlobalLock();
+        verify(registryRepository).tryLock(eq(5L), eq(TimeUnit.SECONDS));
+    }
+    
+    @Test
+    public void assertReleaseGlobalLock() {
+        lockCenter.releaseGlobalLock();
+        verify(registryRepository).releaseLock();
+        verify(registryRepository).delete(eq(new LockNode().getGlobalLockNodePath()));
+    }
+    
+    @After
+    public void tearDown() {
+        lockCenter.unlock();
+    }
+}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/lock/LockContext.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/lock/LockContext.java
index b5870a4..65987a3 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/lock/LockContext.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/lock/LockContext.java
@@ -38,7 +38,7 @@ public final class LockContext {
     
     private static final Condition CONDITION = LOCK.newCondition();
     
-    private static final long TIME_OUT_SECONDS = 60L;
+    private static final long TIME_OUT_SECONDS = 50L;
     
     /**
      * Init lock strategy.
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/lock/LockContextTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/lock/LockContextTest.java
new file mode 100644
index 0000000..2cb4ac2
--- /dev/null
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/lock/LockContextTest.java
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.shardingsphere.infra.lock;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.concurrent.TimeUnit;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+public final class LockContextTest {
+    
+    private final LockStrategy lockStrategy = new StandardLockStrategy();
+    
+    @Before
+    public void init() {
+        LockContext.init(lockStrategy);
+    }
+    
+    @Test
+    public void assertGetLockStrategy() {
+        assertNotNull(LockContext.getLockStrategy());
+        assertThat(LockContext.getLockStrategy(), is(lockStrategy));
+    }
+    
+    @Test
+    public void assetAwait() {
+        long startTime = System.currentTimeMillis();
+        new Thread(() -> {
+            try {
+                TimeUnit.MILLISECONDS.sleep(200L);
+                // CHECKSTYLE:OFF
+            } catch (final InterruptedException e) {
+                // CHECKSTYLE:ON
+            }
+            LockContext.signalAll();
+        }).start();
+        boolean result = LockContext.await();
+        assertTrue(result);
+        assertTrue(System.currentTimeMillis() - startTime >= 200L);
+    }
+}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/lock/StandardLockStrategyTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/lock/StandardLockStrategyTest.java
new file mode 100644
index 0000000..3f7fc91
--- /dev/null
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/lock/StandardLockStrategyTest.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.shardingsphere.infra.lock;
+
+import org.apache.shardingsphere.infra.state.StateContext;
+import org.apache.shardingsphere.infra.state.StateType;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+public final class StandardLockStrategyTest {
+    
+    private final StandardLockStrategy lockStrategy = new StandardLockStrategy();
+    
+    @Test
+    public void assertTryLock() {
+        assertTrue(lockStrategy.tryLock());
+        assertThat(StateContext.getCurrentState(), is(StateType.LOCK));
+        lockStrategy.releaseLock();
+    }
+    
+    @Test
+    public void assertReleaseLock() {
+        assertTrue(lockStrategy.tryLock());
+        assertThat(StateContext.getCurrentState(), is(StateType.LOCK));
+        lockStrategy.releaseLock();
+        assertThat(StateContext.getCurrentState(), is(StateType.OK));
+    }
+}