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 9aa426d024a Add more test cases on UnlockClusterExecutorTest (#38035)
9aa426d024a is described below

commit 9aa426d024a3c4a1bb8dcfb0ede0a7676ec96ace
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Feb 14 00:52:56 2026 +0800

    Add more test cases on UnlockClusterExecutorTest (#38035)
---
 .../ral/updatable/lock/UnlockClusterExecutor.java  |  3 +-
 .../updatable/lock/UnlockClusterExecutorTest.java  | 71 +++++++++++++++++-----
 2 files changed, 58 insertions(+), 16 deletions(-)

diff --git 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/lock/UnlockClusterExecutor.java
 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/lock/UnlockClusterExecutor.java
index ecfed775b11..b3ffb6eefc5 100644
--- 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/lock/UnlockClusterExecutor.java
+++ 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/lock/UnlockClusterExecutor.java
@@ -36,8 +36,7 @@ public final class UnlockClusterExecutor implements 
DistSQLUpdateExecutor<Unlock
     @Override
     public void executeUpdate(final UnlockClusterStatement sqlStatement, final 
ContextManager contextManager) throws SQLException {
         checkState(contextManager);
-        long timeoutMillis = sqlStatement.getTimeoutMillis();
-        contextManager.getExclusiveOperatorEngine().operate(new 
LockClusterOperation(), timeoutMillis, () -> {
+        contextManager.getExclusiveOperatorEngine().operate(new 
LockClusterOperation(), sqlStatement.getTimeoutMillis(), () -> {
             checkState(contextManager);
             
contextManager.getPersistServiceFacade().getStateService().update(ShardingSphereState.OK);
             // TODO unlock snapshot info if locked
diff --git 
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/lock/UnlockClusterExecutorTest.java
 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/lock/UnlockClusterExecutorTest.java
index 3eea0a400c2..62edcfb6ded 100644
--- 
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/lock/UnlockClusterExecutorTest.java
+++ 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/lock/UnlockClusterExecutorTest.java
@@ -17,36 +17,79 @@
 
 package 
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable.lock;
 
+import 
org.apache.shardingsphere.distsql.handler.engine.update.DistSQLUpdateExecutor;
 import 
org.apache.shardingsphere.distsql.statement.type.ral.updatable.UnlockClusterStatement;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.mode.exclusive.ExclusiveOperatorEngine;
+import 
org.apache.shardingsphere.mode.exclusive.callback.ExclusiveOperationVoidCallback;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 import 
org.apache.shardingsphere.mode.manager.cluster.lock.exception.NotLockedClusterException;
 import org.apache.shardingsphere.mode.state.ShardingSphereState;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockito.junit.jupiter.MockitoExtension;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.sql.SQLException;
+import java.util.stream.Stream;
 
 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.anyLong;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.eq;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
-@ExtendWith(MockitoExtension.class)
 class UnlockClusterExecutorTest {
     
-    private final UnlockClusterExecutor executor = new UnlockClusterExecutor();
+    private final UnlockClusterExecutor executor = (UnlockClusterExecutor) 
TypedSPILoader.getService(DistSQLUpdateExecutor.class, 
UnlockClusterStatement.class);
     
-    @Test
-    void assertExecuteUpdateWithNotLockedCluster() {
+    @ParameterizedTest(name = "{0}")
+    @MethodSource("provideExecuteUpdateScenarios")
+    void assertExecuteUpdate(final String name, final UnlockClusterStatement 
sqlStatement, final ShardingSphereState initialState, final ShardingSphereState 
callbackState,
+                             final Class<? extends Throwable> 
expectedException, final long expectedTimeoutMillis,
+                             final boolean expectedOperateInvoked, final 
boolean expectedStateUpdated) throws SQLException {
         ContextManager contextManager = mock(ContextManager.class, 
RETURNS_DEEP_STUBS);
-        
when(contextManager.getStateContext().getState()).thenReturn(ShardingSphereState.OK);
-        assertThrows(NotLockedClusterException.class, () -> 
executor.executeUpdate(new UnlockClusterStatement(), contextManager));
+        ExclusiveOperatorEngine exclusiveOperatorEngine = 
mock(ExclusiveOperatorEngine.class);
+        
when(contextManager.getExclusiveOperatorEngine()).thenReturn(exclusiveOperatorEngine);
+        
when(contextManager.getStateContext().getState()).thenReturn(initialState, 
callbackState);
+        if (expectedOperateInvoked) {
+            doAnswer(invocation -> {
+                ExclusiveOperationVoidCallback callback = 
invocation.getArgument(2);
+                callback.execute();
+                return null;
+            }).when(exclusiveOperatorEngine).operate(any(), anyLong(), 
any(ExclusiveOperationVoidCallback.class));
+        }
+        if (null == expectedException) {
+            assertDoesNotThrow(() -> executor.executeUpdate(sqlStatement, 
contextManager));
+        } else {
+            assertThrows(expectedException, () -> 
executor.executeUpdate(sqlStatement, contextManager));
+        }
+        if (expectedOperateInvoked) {
+            verify(exclusiveOperatorEngine).operate(any(), 
eq(expectedTimeoutMillis), any(ExclusiveOperationVoidCallback.class));
+        } else {
+            verify(exclusiveOperatorEngine, never()).operate(any(), anyLong(), 
any(ExclusiveOperationVoidCallback.class));
+        }
+        if (expectedStateUpdated) {
+            
verify(contextManager.getPersistServiceFacade().getStateService()).update(ShardingSphereState.OK);
+        } else {
+            verify(contextManager.getPersistServiceFacade().getStateService(), 
never()).update(ShardingSphereState.OK);
+        }
     }
     
-    @Test
-    void assertExecuteUpdateWithUsingTimeout() {
-        ContextManager contextManager = mock(ContextManager.class, 
RETURNS_DEEP_STUBS);
-        
when(contextManager.getStateContext().getState()).thenReturn(ShardingSphereState.UNAVAILABLE);
-        assertDoesNotThrow(() -> executor.executeUpdate(new 
UnlockClusterStatement(2000L), contextManager));
+    private static Stream<Arguments> provideExecuteUpdateScenarios() {
+        return Stream.of(
+                Arguments.of("throw when cluster is not locked", new 
UnlockClusterStatement(), ShardingSphereState.OK, ShardingSphereState.OK,
+                        NotLockedClusterException.class, 3000L, false, false),
+                Arguments.of("unlock with explicit timeout", new 
UnlockClusterStatement(2000L), ShardingSphereState.UNAVAILABLE, 
ShardingSphereState.UNAVAILABLE,
+                        null, 2000L, true, true),
+                Arguments.of("unlock with default timeout", new 
UnlockClusterStatement(), ShardingSphereState.UNAVAILABLE, 
ShardingSphereState.UNAVAILABLE,
+                        null, 3000L, true, true),
+                Arguments.of("throw when callback state changes", new 
UnlockClusterStatement(2000L), ShardingSphereState.UNAVAILABLE, 
ShardingSphereState.OK,
+                        NotLockedClusterException.class, 2000L, true, false));
     }
 }

Reply via email to