This is an automated email from the ASF dual-hosted git repository.
sunnianjun 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 71348ca9a95 Add test case for ProcessOperationLockRegistry (#25451)
71348ca9a95 is described below
commit 71348ca9a95b170f56e1338c29222d4444605626
Author: Liang Zhang <[email protected]>
AuthorDate: Thu May 4 20:54:06 2023 +0800
Add test case for ProcessOperationLockRegistry (#25451)
---
.../lock/ProcessOperationLockRegistryTest.java | 59 ++++++++++++++++++++++
.../ProcessListChangedSubscriberTest.java | 23 ++++-----
2 files changed, 70 insertions(+), 12 deletions(-)
diff --git
a/infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/process/lock/ProcessOperationLockRegistryTest.java
b/infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/process/lock/ProcessOperationLockRegistryTest.java
new file mode 100644
index 00000000000..056a660afae
--- /dev/null
+++
b/infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/process/lock/ProcessOperationLockRegistryTest.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.infra.executor.sql.process.lock;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.Executors;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.greaterThanOrEqualTo;
+import static org.hamcrest.Matchers.lessThanOrEqualTo;
+
+class ProcessOperationLockRegistryTest {
+
+ @Test
+ void assertLockAndNotify() {
+ String lockId = "foo_id";
+ long startMillis = System.currentTimeMillis();
+ Executors.newFixedThreadPool(1).submit(() -> {
+ try {
+ Thread.sleep(50L);
+ } catch (final InterruptedException ignored) {
+ }
+ ProcessOperationLockRegistry.getInstance().notify(lockId);
+ });
+ waitUntilReleaseReady(lockId);
+ long currentMillis = System.currentTimeMillis();
+ assertThat(currentMillis, greaterThanOrEqualTo(startMillis + 50L));
+ assertThat(currentMillis, lessThanOrEqualTo(startMillis + 5000L));
+ }
+
+ private static void waitUntilReleaseReady(final String lockId) {
+
ProcessOperationLockRegistry.getInstance().waitUntilReleaseReady(lockId, new
ProcessOperationLockReleaseStrategy() {
+
+ private final AtomicBoolean firstTime = new AtomicBoolean(true);
+
+ @Override
+ public boolean isReadyToRelease() {
+ return !firstTime.getAndSet(false);
+ }
+ });
+ }
+}
diff --git
a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/process/subscriber/ProcessListChangedSubscriberTest.java
b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/process/subscriber/ProcessListChangedSubscriberTest.java
index 224efdd21b1..2a295ff4316 100644
---
a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/process/subscriber/ProcessListChangedSubscriberTest.java
+++
b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/process/subscriber/ProcessListChangedSubscriberTest.java
@@ -56,11 +56,12 @@ import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Properties;
-import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
-import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.greaterThanOrEqualTo;
+import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -119,8 +120,7 @@ class ProcessListChangedSubscriberTest {
void assertCompleteToReportLocalProcesses() {
String taskId = "foo_id";
long startMillis = System.currentTimeMillis();
- ExecutorService executorService = Executors.newFixedThreadPool(1);
- executorService.submit(() -> {
+ Executors.newFixedThreadPool(1).submit(() -> {
try {
Thread.sleep(50L);
} catch (final InterruptedException ignored) {
@@ -128,9 +128,9 @@ class ProcessListChangedSubscriberTest {
subscriber.completeToReportLocalProcesses(new
ReportLocalProcessesCompletedEvent(taskId));
});
waitUntilReleaseReady(taskId);
- long currentTime = System.currentTimeMillis();
- assertTrue(currentTime >= startMillis + 50L);
- assertTrue(currentTime <= startMillis + 5000L);
+ long currentMillis = System.currentTimeMillis();
+ assertThat(currentMillis, greaterThanOrEqualTo(startMillis + 50L));
+ assertThat(currentMillis, lessThanOrEqualTo(startMillis + 5000L));
}
@Test
@@ -146,8 +146,7 @@ class ProcessListChangedSubscriberTest {
void assertCompleteToKillProcessInstance() {
String processId = "foo_id";
long startMillis = System.currentTimeMillis();
- ExecutorService executorService = Executors.newFixedThreadPool(1);
- executorService.submit(() -> {
+ Executors.newFixedThreadPool(1).submit(() -> {
try {
Thread.sleep(50L);
} catch (final InterruptedException ignored) {
@@ -155,9 +154,9 @@ class ProcessListChangedSubscriberTest {
subscriber.completeToKillProcessInstance(new
KillProcessInstanceCompleteEvent(processId));
});
waitUntilReleaseReady(processId);
- long currentTime = System.currentTimeMillis();
- assertTrue(currentTime >= startMillis + 50L);
- assertTrue(currentTime <= startMillis + 5000L);
+ long currentMillis = System.currentTimeMillis();
+ assertThat(currentMillis, greaterThanOrEqualTo(startMillis + 50L));
+ assertThat(currentMillis, lessThanOrEqualTo(startMillis + 5000L));
}
private static void waitUntilReleaseReady(final String lockId) {