You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2023/05/19 08:49:28 UTC

[shardingsphere] branch master updated: Refactor : replace Thread.sleep with awaitility (#25794)

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

panjuan 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 b7ff0b87fc1 Refactor : replace Thread.sleep with awaitility (#25794)
b7ff0b87fc1 is described below

commit b7ff0b87fc15a32779f904379fbc5580bc29ae67
Author: 孙念君 Nianjun Sun <su...@apache.org>
AuthorDate: Fri May 19 16:49:13 2023 +0800

    Refactor : replace Thread.sleep with awaitility (#25794)
---
 features/sharding/core/pom.xml                             |  5 +++++
 .../keygen/SnowflakeKeyGenerateAlgorithmTest.java          | 12 +++++++-----
 .../sql/process/lock/ProcessOperationLockRegistryTest.java |  8 +++-----
 mode/type/cluster/core/pom.xml                             |  5 +++++
 .../subscriber/ProcessListChangedSubscriberTest.java       | 14 ++++----------
 mode/type/cluster/repository/provider/consul/pom.xml       |  4 ++++
 .../repository/cluster/consul/ConsulRepositoryTest.java    | 10 ++++++----
 proxy/backend/core/pom.xml                                 |  4 ++++
 .../connector/jdbc/connection/ResourceLockTest.java        |  8 +++-----
 9 files changed, 41 insertions(+), 29 deletions(-)

diff --git a/features/sharding/core/pom.xml b/features/sharding/core/pom.xml
index 86e8bffc766..f5937650185 100644
--- a/features/sharding/core/pom.xml
+++ b/features/sharding/core/pom.xml
@@ -144,5 +144,10 @@
             <version>${project.version}</version>
             <scope>test</scope>
         </dependency>
+        
+        <dependency>
+            <groupId>org.awaitility</groupId>
+            <artifactId>awaitility</artifactId>
+        </dependency>
     </dependencies>
 </project>
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/keygen/SnowflakeKeyGenerateAlgorithmTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/keygen/SnowflakeKeyGenerateAlgorithmTest.java
index 08d7e4e55f9..bc5a775aa5f 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/keygen/SnowflakeKeyGenerateAlgorithmTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/keygen/SnowflakeKeyGenerateAlgorithmTest.java
@@ -34,6 +34,7 @@ import org.apache.shardingsphere.sharding.exception.algorithm.keygen.SnowflakeCl
 import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
 import org.apache.shardingsphere.test.util.PropertiesBuilder;
 import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
+import org.awaitility.Awaitility;
 import org.junit.jupiter.api.Test;
 import org.mockito.internal.configuration.plugins.Plugins;
 
@@ -47,6 +48,7 @@ import java.util.concurrent.Callable;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicLong;
 
 import static org.hamcrest.CoreMatchers.is;
@@ -115,7 +117,7 @@ class SnowflakeKeyGenerateAlgorithmTest {
     }
     
     @Test
-    void assertLastDigitalOfGenerateKeyDifferentMillisecond() throws InterruptedException {
+    void assertLastDigitalOfGenerateKeyDifferentMillisecond() {
         SnowflakeKeyGenerateAlgorithm.setTimeService(new TimeService());
         KeyGenerateAlgorithm algorithm = TypedSPILoader.getService(KeyGenerateAlgorithm.class, "SNOWFLAKE", PropertiesBuilder.build(new Property("max-vibration-offset", "3")));
         if (algorithm instanceof InstanceContextAware) {
@@ -123,16 +125,16 @@ class SnowflakeKeyGenerateAlgorithmTest {
         }
         String actualGenerateKey0 = Long.toBinaryString(Long.parseLong(algorithm.generateKey().toString()));
         assertThat(Integer.parseInt(actualGenerateKey0.substring(actualGenerateKey0.length() - 3), 2), is(0));
-        Thread.sleep(2L);
+        Awaitility.await().pollDelay(2L, TimeUnit.MILLISECONDS).until(() -> true);
         String actualGenerateKey1 = Long.toBinaryString(Long.parseLong(algorithm.generateKey().toString()));
         assertThat(Integer.parseInt(actualGenerateKey1.substring(actualGenerateKey1.length() - 3), 2), is(1));
-        Thread.sleep(2L);
+        Awaitility.await().pollDelay(2L, TimeUnit.MILLISECONDS).until(() -> true);
         String actualGenerateKey2 = Long.toBinaryString(Long.parseLong(algorithm.generateKey().toString()));
         assertThat(Integer.parseInt(actualGenerateKey2.substring(actualGenerateKey2.length() - 3), 2), is(2));
-        Thread.sleep(2L);
+        Awaitility.await().pollDelay(2L, TimeUnit.MILLISECONDS).until(() -> true);
         String actualGenerateKey3 = Long.toBinaryString(Long.parseLong(algorithm.generateKey().toString()));
         assertThat(Integer.parseInt(actualGenerateKey3.substring(actualGenerateKey3.length() - 3), 2), is(3));
-        Thread.sleep(2L);
+        Awaitility.await().pollDelay(2L, TimeUnit.MILLISECONDS).until(() -> true);
         String actualGenerateKey4 = Long.toBinaryString(Long.parseLong(algorithm.generateKey().toString()));
         assertThat(Integer.parseInt(actualGenerateKey4.substring(actualGenerateKey4.length() - 3), 2), is(0));
     }
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
index fbcc62f08ff..90139a1b43f 100644
--- 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
@@ -17,9 +17,11 @@
 
 package org.apache.shardingsphere.infra.executor.sql.process.lock;
 
+import org.awaitility.Awaitility;
 import org.junit.jupiter.api.Test;
 
 import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import static org.hamcrest.MatcherAssert.assertThat;
@@ -33,11 +35,7 @@ class ProcessOperationLockRegistryTest {
         String lockId = "foo_id";
         long startMillis = System.currentTimeMillis();
         Executors.newFixedThreadPool(1).submit(() -> {
-            try {
-                Thread.sleep(50L);
-            } catch (final InterruptedException ignored) {
-                Thread.currentThread().interrupt();
-            }
+            Awaitility.await().pollDelay(50L, TimeUnit.MILLISECONDS).until(() -> true);
             ProcessOperationLockRegistry.getInstance().notify(lockId);
         });
         waitUntilReleaseReady(lockId);
diff --git a/mode/type/cluster/core/pom.xml b/mode/type/cluster/core/pom.xml
index 26833d6c6d4..9e70ebb7a9e 100644
--- a/mode/type/cluster/core/pom.xml
+++ b/mode/type/cluster/core/pom.xml
@@ -66,5 +66,10 @@
             <version>${project.version}</version>
             <scope>test</scope>
         </dependency>
+        
+        <dependency>
+            <groupId>org.awaitility</groupId>
+            <artifactId>awaitility</artifactId>
+        </dependency>
     </dependencies>
 </project>
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 fcc010ba02d..e247c102052 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
@@ -42,6 +42,7 @@ import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.statu
 import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
 import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
 import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration;
+import org.awaitility.Awaitility;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
@@ -56,6 +57,7 @@ import java.util.LinkedList;
 import java.util.Map;
 import java.util.Properties;
 import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import static org.hamcrest.MatcherAssert.assertThat;
@@ -123,11 +125,7 @@ class ProcessListChangedSubscriberTest {
         String taskId = "foo_id";
         long startMillis = System.currentTimeMillis();
         Executors.newFixedThreadPool(1).submit(() -> {
-            try {
-                Thread.sleep(50L);
-            } catch (final InterruptedException ignored) {
-                Thread.currentThread().interrupt();
-            }
+            Awaitility.await().pollDelay(50L, TimeUnit.MILLISECONDS).until(() -> true);
             subscriber.completeToReportLocalProcesses(new ReportLocalProcessesCompletedEvent(taskId));
         });
         waitUntilReleaseReady(taskId);
@@ -150,11 +148,7 @@ class ProcessListChangedSubscriberTest {
         String processId = "foo_id";
         long startMillis = System.currentTimeMillis();
         Executors.newFixedThreadPool(1).submit(() -> {
-            try {
-                Thread.sleep(50L);
-            } catch (final InterruptedException ignored) {
-                Thread.currentThread().interrupt();
-            }
+            Awaitility.await().pollDelay(50L, TimeUnit.MILLISECONDS).until(() -> true);
             subscriber.completeToKillLocalProcess(new KillLocalProcessCompletedEvent(processId));
         });
         waitUntilReleaseReady(processId);
diff --git a/mode/type/cluster/repository/provider/consul/pom.xml b/mode/type/cluster/repository/provider/consul/pom.xml
index 97573208150..9273cc57dd5 100644
--- a/mode/type/cluster/repository/provider/consul/pom.xml
+++ b/mode/type/cluster/repository/provider/consul/pom.xml
@@ -59,5 +59,9 @@
             <groupId>com.google.code.gson</groupId>
             <artifactId>gson</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.awaitility</groupId>
+            <artifactId>awaitility</artifactId>
+        </dependency>
     </dependencies>
 </project>
diff --git a/mode/type/cluster/repository/provider/consul/src/test/java/org/apache/shardingsphere/mode/repository/cluster/consul/ConsulRepositoryTest.java b/mode/type/cluster/repository/provider/consul/src/test/java/org/apache/shardingsphere/mode/repository/cluster/consul/ConsulRepositoryTest.java
index 80d020d7ea3..d16efd39b88 100644
--- a/mode/type/cluster/repository/provider/consul/src/test/java/org/apache/shardingsphere/mode/repository/cluster/consul/ConsulRepositoryTest.java
+++ b/mode/type/cluster/repository/provider/consul/src/test/java/org/apache/shardingsphere/mode/repository/cluster/consul/ConsulRepositoryTest.java
@@ -25,6 +25,7 @@ import com.ecwid.consul.v1.session.model.NewSession;
 import lombok.SneakyThrows;
 import org.apache.shardingsphere.mode.repository.cluster.consul.props.ConsulProperties;
 import org.apache.shardingsphere.mode.repository.cluster.lock.holder.DistributedLockHolder;
+import org.awaitility.Awaitility;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
@@ -42,6 +43,7 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Properties;
+import java.util.concurrent.TimeUnit;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
@@ -144,7 +146,7 @@ class ConsulRepositoryTest {
     }
     
     @Test
-    void assertWatchUpdate() throws InterruptedException {
+    void assertWatchUpdate() {
         final String key = "sharding/key";
         final String k1 = "sharding/key/key1";
         final String v1 = "value1";
@@ -157,7 +159,7 @@ class ConsulRepositoryTest {
         });
         client.setKVValue(k1, "value1-1");
         while (true) {
-            Thread.sleep(100L);
+            Awaitility.await().pollDelay(100L, TimeUnit.MILLISECONDS).until(() -> true);
             try {
                 verify(client, atLeastOnce()).getKVValues(any(String.class), any(QueryParams.class));
                 break;
@@ -167,7 +169,7 @@ class ConsulRepositoryTest {
     }
     
     @Test
-    void assertWatchDelete() throws InterruptedException {
+    void assertWatchDelete() {
         final String key = "sharding/key";
         final String k1 = "sharding/key/key1";
         final String v1 = "value1";
@@ -183,7 +185,7 @@ class ConsulRepositoryTest {
         });
         client.deleteKVValue(k2);
         while (true) {
-            Thread.sleep(100L);
+            Awaitility.await().pollDelay(100L, TimeUnit.MILLISECONDS).until(() -> true);
             try {
                 verify(client, atLeastOnce()).getKVValues(any(String.class), any(QueryParams.class));
                 break;
diff --git a/proxy/backend/core/pom.xml b/proxy/backend/core/pom.xml
index f196d8f95fc..fa21357627b 100644
--- a/proxy/backend/core/pom.xml
+++ b/proxy/backend/core/pom.xml
@@ -243,5 +243,9 @@
             <artifactId>HikariCP</artifactId>
             <scope>compile</scope>
         </dependency>
+        <dependency>
+            <groupId>org.awaitility</groupId>
+            <artifactId>awaitility</artifactId>
+        </dependency>
     </dependencies>
 </project>
diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/connection/ResourceLockTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/connection/ResourceLockTest.java
index 1c333020c87..19398f7e1e1 100644
--- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/connection/ResourceLockTest.java
+++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/connection/ResourceLockTest.java
@@ -17,10 +17,12 @@
 
 package org.apache.shardingsphere.proxy.backend.connector.jdbc.connection;
 
+import org.awaitility.Awaitility;
 import org.junit.jupiter.api.Test;
 
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
 
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
@@ -40,11 +42,7 @@ class ResourceLockTest {
         long startTime = System.currentTimeMillis();
         ExecutorService executorService = Executors.newFixedThreadPool(1);
         executorService.submit(() -> {
-            try {
-                Thread.sleep(50L);
-            } catch (final InterruptedException ignored) {
-                Thread.currentThread().interrupt();
-            }
+            Awaitility.await().pollDelay(50L, TimeUnit.MILLISECONDS).until(() -> true);
             resourceLock.doNotify();
         });
         resourceLock.doAwait();