This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 55bd2b4eeb1 Add more test cases on mode module (#37637)
55bd2b4eeb1 is described below
commit 55bd2b4eeb14c25729caa2dc5630fdffe743e2f7
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Jan 4 21:31:09 2026 +0800
Add more test cases on mode module (#37637)
* Add more test cases on Doris SQL parser
* Add more test cases on mode module
---
.../ClusterExclusiveOperatorContextTest.java | 58 ++++++++++++++++
.../facade/ClusterPersistServiceFacadeTest.java | 66 ++++++++++++++++++
.../core/lock/DistributedLockHolderTest.java | 68 ++++++++++++++++++
.../lock/ZookeeperDistributedLockTest.java | 80 ++++++++++++++++++++++
4 files changed, 272 insertions(+)
diff --git
a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/exclusive/ClusterExclusiveOperatorContextTest.java
b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/exclusive/ClusterExclusiveOperatorContextTest.java
new file mode 100644
index 00000000000..77e96445bcd
--- /dev/null
+++
b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/exclusive/ClusterExclusiveOperatorContextTest.java
@@ -0,0 +1,58 @@
+/*
+ * 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.mode.manager.cluster.exclusive;
+
+import
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
+import
org.apache.shardingsphere.mode.repository.cluster.core.lock.DistributedLockHolder;
+import org.apache.shardingsphere.mode.repository.cluster.lock.DistributedLock;
+import
org.apache.shardingsphere.test.infra.framework.extension.mock.AutoMockExtension;
+import
org.apache.shardingsphere.test.infra.framework.extension.mock.StaticMockSettings;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(AutoMockExtension.class)
+@StaticMockSettings(DistributedLockHolder.class)
+class ClusterExclusiveOperatorContextTest {
+
+ @Mock
+ private ClusterPersistRepository repository;
+
+ @Mock
+ private DistributedLock distributedLock;
+
+ @Test
+ void assertStartTryLock() {
+ when(DistributedLockHolder.getDistributedLock("op",
repository)).thenReturn(distributedLock);
+ when(distributedLock.tryLock(50L)).thenReturn(true);
+ assertThat(new ClusterExclusiveOperatorContext(repository).start("op",
50L), is(true));
+ verify(distributedLock).tryLock(50L);
+ }
+
+ @Test
+ void assertStopUnlock() {
+ when(DistributedLockHolder.getDistributedLock("op",
repository)).thenReturn(distributedLock);
+ new ClusterExclusiveOperatorContext(repository).stop("op");
+ verify(distributedLock).unlock();
+ }
+}
diff --git
a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/persist/facade/ClusterPersistServiceFacadeTest.java
b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/persist/facade/ClusterPersistServiceFacadeTest.java
new file mode 100644
index 00000000000..8ce54430577
--- /dev/null
+++
b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/persist/facade/ClusterPersistServiceFacadeTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.mode.manager.cluster.persist.facade;
+
+import org.apache.shardingsphere.infra.instance.ComputeNodeInstance;
+import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
+import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
+import
org.apache.shardingsphere.mode.manager.cluster.persist.service.ClusterComputeNodePersistService;
+import
org.apache.shardingsphere.mode.manager.cluster.persist.service.ClusterMetaDataManagerPersistService;
+import
org.apache.shardingsphere.mode.manager.cluster.persist.service.ClusterProcessPersistService;
+import org.apache.shardingsphere.mode.metadata.manager.MetaDataContextManager;
+import
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.isA;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class ClusterPersistServiceFacadeTest {
+
+ @Mock
+ private ClusterPersistRepository repository;
+
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+ private MetaDataContextManager metaDataContextManager;
+
+ @Test
+ void assertConstructServices() {
+ ClusterPersistServiceFacade actual = new
ClusterPersistServiceFacade(metaDataContextManager, repository);
+ assertThat(actual.getMetaDataManagerService(),
isA(ClusterMetaDataManagerPersistService.class));
+ assertThat(actual.getComputeNodeService(),
isA(ClusterComputeNodePersistService.class));
+ assertThat(actual.getProcessService(),
isA(ClusterProcessPersistService.class));
+ }
+
+ @Test
+ void assertCloseOfflineInstance() {
+ InstanceMetaData instanceMetaData = mock(InstanceMetaData.class);
+ when(instanceMetaData.getId()).thenReturn("foo_instance");
+ when(instanceMetaData.getType()).thenReturn(InstanceType.PROXY);
+
when(metaDataContextManager.getComputeNodeInstanceContext().getInstance()).thenReturn(new
ComputeNodeInstance(instanceMetaData));
+ new ClusterPersistServiceFacade(metaDataContextManager,
repository).close();
+
verify(repository).delete("/nodes/compute_nodes/online/proxy/foo_instance");
+ }
+}
diff --git
a/mode/type/cluster/repository/core/src/test/java/org/apache/shardingsphere/mode/repository/cluster/core/lock/DistributedLockHolderTest.java
b/mode/type/cluster/repository/core/src/test/java/org/apache/shardingsphere/mode/repository/cluster/core/lock/DistributedLockHolderTest.java
new file mode 100644
index 00000000000..accc050daaf
--- /dev/null
+++
b/mode/type/cluster/repository/core/src/test/java/org/apache/shardingsphere/mode/repository/cluster/core/lock/DistributedLockHolderTest.java
@@ -0,0 +1,68 @@
+/*
+ * 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.mode.repository.cluster.core.lock;
+
+import lombok.SneakyThrows;
+import
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
+import
org.apache.shardingsphere.mode.repository.cluster.core.lock.type.DefaultDistributedLock;
+import org.apache.shardingsphere.mode.repository.cluster.lock.DistributedLock;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.internal.configuration.plugins.Plugins;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.Map;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.isA;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class DistributedLockHolderTest {
+
+ @Mock
+ private ClusterPersistRepository repository;
+
+ @BeforeEach
+ @AfterEach
+ @SuppressWarnings("unchecked")
+ @SneakyThrows(ReflectiveOperationException.class)
+ void clearLocks() {
+ ((Map<String, DistributedLock>)
Plugins.getMemberAccessor().get(DistributedLockHolder.class.getDeclaredField("LOCKS"),
null)).clear();
+ }
+
+ @Test
+ void assertGetDistributedLockReturnsRepositoryLock() {
+ DistributedLock distributedLock = mock(DistributedLock.class);
+
when(repository.getDistributedLock("lock-key")).thenReturn(Optional.of(distributedLock));
+ assertThat(DistributedLockHolder.getDistributedLock("lock-key",
repository), is(distributedLock));
+ }
+
+ @Test
+ void assertGetDistributedLockCreatesDefaultLock() {
+
when(repository.getDistributedLock("lock-key")).thenReturn(Optional.empty());
+ DistributedLock actual =
DistributedLockHolder.getDistributedLock("lock-key", repository);
+ assertThat(actual, isA(DefaultDistributedLock.class));
+ }
+}
diff --git
a/mode/type/cluster/repository/provider/zookeeper/src/test/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/lock/ZookeeperDistributedLockTest.java
b/mode/type/cluster/repository/provider/zookeeper/src/test/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/lock/ZookeeperDistributedLockTest.java
new file mode 100644
index 00000000000..0f548eb6d1c
--- /dev/null
+++
b/mode/type/cluster/repository/provider/zookeeper/src/test/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/lock/ZookeeperDistributedLockTest.java
@@ -0,0 +1,80 @@
+/*
+ * 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.mode.repository.cluster.zookeeper.lock;
+
+import org.apache.curator.framework.recipes.locks.InterProcessMutex;
+import
org.apache.shardingsphere.mode.repository.cluster.zookeeper.exception.ZookeeperExceptionHandler;
+import org.junit.jupiter.api.Test;
+import org.mockito.MockedConstruction;
+import org.mockito.MockedStatic;
+
+import java.util.concurrent.TimeUnit;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockConstruction;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+class ZookeeperDistributedLockTest {
+
+ @Test
+ void assertTryLockSuccess() {
+ try (
+ MockedConstruction<InterProcessMutex> mocked =
mockConstruction(InterProcessMutex.class,
+ (constructed, context) ->
when(constructed.acquire(200L, TimeUnit.MILLISECONDS)).thenReturn(true))) {
+ assertTrue(new ZookeeperDistributedLock("/lock",
mock()).tryLock(200L));
+ assertDoesNotThrow(() ->
verify(mocked.constructed().get(0)).acquire(200L, TimeUnit.MILLISECONDS));
+ }
+ }
+
+ @Test
+ void assertTryLockHandlesException() {
+ Exception ex = new Exception("failed");
+ try (
+ MockedStatic<ZookeeperExceptionHandler> mockedStatic =
mockStatic(ZookeeperExceptionHandler.class);
+ MockedConstruction<InterProcessMutex> ignored =
mockConstruction(InterProcessMutex.class,
+ (constructed, context) ->
when(constructed.acquire(100L, TimeUnit.MILLISECONDS)).thenThrow(ex))) {
+ assertFalse(new ZookeeperDistributedLock("/lock",
mock()).tryLock(100L));
+ mockedStatic.verify(() ->
ZookeeperExceptionHandler.handleException(ex));
+ }
+ }
+
+ @Test
+ void assertUnlockSuccess() {
+ try (MockedConstruction<InterProcessMutex> mocked =
mockConstruction(InterProcessMutex.class)) {
+ new ZookeeperDistributedLock("/lock", mock()).unlock();
+ assertDoesNotThrow(() ->
verify(mocked.constructed().get(0)).release());
+ }
+ }
+
+ @Test
+ void assertUnlockHandlesException() {
+ Exception ex = new Exception("release failed");
+ try (
+ MockedStatic<ZookeeperExceptionHandler> mockedStatic =
mockStatic(ZookeeperExceptionHandler.class);
+ MockedConstruction<InterProcessMutex> ignored =
mockConstruction(InterProcessMutex.class, (constructed, context) ->
doThrow(ex).when(constructed).release())) {
+ new ZookeeperDistributedLock("/lock", mock()).unlock();
+ assertDoesNotThrow(() -> mockedStatic.verify(() ->
ZookeeperExceptionHandler.handleException(ex)));
+ }
+ }
+}