sandynz commented on a change in pull request #16011:
URL: https://github.com/apache/shardingsphere/pull/16011#discussion_r825453669



##########
File path: 
shardingsphere-test/shardingsphere-pipeline-test/src/test/java/org/apache/shardingsphere/data/pipeline/core/lock/PipelineSimpleLockTest.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.data.pipeline.core.lock;
+
+import org.apache.shardingsphere.data.pipeline.core.context.PipelineContext;
+import org.apache.shardingsphere.infra.instance.InstanceContext;
+import org.apache.shardingsphere.mode.manager.ContextManager;
+import 
org.apache.shardingsphere.mode.manager.cluster.coordinator.lock.service.LockNode;
+import 
org.apache.shardingsphere.mode.manager.cluster.coordinator.lock.service.LockRegistryService;
+import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
+import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
+import 
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
+import org.apache.shardingsphere.transaction.context.TransactionContexts;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.lang.reflect.Field;
+import java.util.concurrent.TimeUnit;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class PipelineSimpleLockTest {
+
+    @Mock
+    private ClusterPersistRepository clusterPersistRepository;
+
+    @Mock
+    private MetaDataPersistService metaDataPersistService;
+
+    @Mock
+    private MetaDataContexts metaDataContexts;
+
+    private PipelineSimpleLock pipelineSimpleLock;
+
+    @Before
+    public void setUp() throws ReflectiveOperationException {
+        metaDataPersistService = new 
MetaDataPersistService(clusterPersistRepository);
+        metaDataContexts = new MetaDataContexts(metaDataPersistService);
+        ContextManager contextManager = new ContextManager();
+        contextManager.init(metaDataContexts, mock(TransactionContexts.class), 
mock(InstanceContext.class));
+        PipelineContext.initContextManager(contextManager);
+        LockRegistryService lockService = new 
LockRegistryService(clusterPersistRepository);
+        Field field = lockService.getClass().getDeclaredField("repository");
+        field.setAccessible(true);
+        field.set(lockService, clusterPersistRepository);

Review comment:
       These lines of code is not necessary, since `lockService` will be 
initialized when creating `PipelineSimpleLock`.

##########
File path: 
shardingsphere-test/shardingsphere-pipeline-test/src/test/java/org/apache/shardingsphere/data/pipeline/core/lock/PipelineSimpleLockTest.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.data.pipeline.core.lock;
+
+import org.apache.shardingsphere.data.pipeline.core.context.PipelineContext;
+import org.apache.shardingsphere.infra.instance.InstanceContext;
+import org.apache.shardingsphere.mode.manager.ContextManager;
+import 
org.apache.shardingsphere.mode.manager.cluster.coordinator.lock.service.LockNode;
+import 
org.apache.shardingsphere.mode.manager.cluster.coordinator.lock.service.LockRegistryService;
+import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
+import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
+import 
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
+import org.apache.shardingsphere.transaction.context.TransactionContexts;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.lang.reflect.Field;
+import java.util.concurrent.TimeUnit;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class PipelineSimpleLockTest {
+
+    @Mock
+    private ClusterPersistRepository clusterPersistRepository;
+
+    @Mock
+    private MetaDataPersistService metaDataPersistService;
+
+    @Mock
+    private MetaDataContexts metaDataContexts;
+
+    private PipelineSimpleLock pipelineSimpleLock;
+
+    @Before
+    public void setUp() throws ReflectiveOperationException {
+        metaDataPersistService = new 
MetaDataPersistService(clusterPersistRepository);
+        metaDataContexts = new MetaDataContexts(metaDataPersistService);
+        ContextManager contextManager = new ContextManager();
+        contextManager.init(metaDataContexts, mock(TransactionContexts.class), 
mock(InstanceContext.class));
+        PipelineContext.initContextManager(contextManager);
+        LockRegistryService lockService = new 
LockRegistryService(clusterPersistRepository);
+        Field field = lockService.getClass().getDeclaredField("repository");
+        field.setAccessible(true);
+        field.set(lockService, clusterPersistRepository);
+        pipelineSimpleLock = PipelineSimpleLock.getInstance();
+    }
+
+    @Test
+    public void assertTryLock() {
+        pipelineSimpleLock.tryLock("test", 50L);
+        
verify(clusterPersistRepository).tryLock(LockNode.getLockNodePath("test"), 50L, 
TimeUnit.MILLISECONDS);
+    }
+
+    @Test
+    public void assertReleaseLock() {
+        pipelineSimpleLock.releaseLock("test");
+        
verify(clusterPersistRepository).releaseLock(LockNode.getLockNodePath("test"));
+    }

Review comment:
       1, These 2 methods could be merged into 1 method, since `releaseLock` 
depends on non-empty `lockNameLockedMap`.
   
   2, `LockNode.getLockNodePath("test")` is not enough, since there's 
`decorateLockName` in `PipelineSimpleLock`. A field variable named 
`decorateLockName` could be added, it'll be initialized in `setUp` method.
   
   3, In order to make sure `lockNameLockedMap` is not empty, 
`lockRegistryService.tryLock` must return true, and it depends on 
`repository.tryLock` return true. So we could add 
`when(clusterPersistRepository.tryLock(LockNode.getLockNodePath(decoratedLockName),
 50L, TimeUnit.MILLISECONDS)).thenReturn(true);` in `setUp` method.
   




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to