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

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

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

jianglongtao 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 bb33c650c72 Refactor : replace Thread.sleep() with Awaitility (#25798)
bb33c650c72 is described below

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

    Refactor : replace Thread.sleep() with Awaitility (#25798)
    
    * Refactor : replace Thread.sleep() with Awaitility
    
    * Refactor : remove useless import
    
    * Revert : rever the changes in ITContainers
---
 test/e2e/operation/pipeline/pom.xml                               | 5 +++++
 .../shardingsphere/test/e2e/data/pipeline/cases/cdc/CDCE2EIT.java | 4 +---
 .../e2e/data/pipeline/cases/migration/AbstractMigrationE2EIT.java | 8 ++++----
 .../cases/migration/general/MySQLMigrationGeneralE2EIT.java       | 4 +++-
 test/e2e/operation/showprocesslist/pom.xml                        | 5 +++++
 .../test/e2e/showprocesslist/engine/ShowProcessListE2EIT.java     | 4 +++-
 test/e2e/operation/transaction/pom.xml                            | 4 ++++
 .../e2e/transaction/cases/autocommit/MySQLAutoCommitTestCase.java | 8 +++++---
 .../cases/autocommit/PostgreSQLAutoCommitTestCase.java            | 8 +++++---
 9 files changed, 35 insertions(+), 15 deletions(-)

diff --git a/test/e2e/operation/pipeline/pom.xml b/test/e2e/operation/pipeline/pom.xml
index 0a287e7bfde..d95ace2d577 100644
--- a/test/e2e/operation/pipeline/pom.xml
+++ b/test/e2e/operation/pipeline/pom.xml
@@ -77,6 +77,11 @@
             <artifactId>commons-lang3</artifactId>
         </dependency>
         
+        <dependency>
+            <groupId>org.awaitility</groupId>
+            <artifactId>awaitility</artifactId>
+        </dependency>
+        
         <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
diff --git a/test/e2e/operation/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/data/pipeline/cases/cdc/CDCE2EIT.java b/test/e2e/operation/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/data/pipeline/cases/cdc/CDCE2EIT.java
index 1967d7d97be..98393d6cdfe 100644
--- a/test/e2e/operation/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/data/pipeline/cases/cdc/CDCE2EIT.java
+++ b/test/e2e/operation/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/data/pipeline/cases/cdc/CDCE2EIT.java
@@ -17,7 +17,6 @@
 
 package org.apache.shardingsphere.test.e2e.data.pipeline.cases.cdc;
 
-import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.tuple.Pair;
 import org.apache.shardingsphere.data.pipeline.api.check.consistency.DataConsistencyCheckResult;
@@ -158,14 +157,13 @@ class CDCE2EIT {
         containerComposer.proxyExecuteWithLog(CREATE_SHARDING_RULE_SQL, 2);
     }
     
-    @SneakyThrows(InterruptedException.class)
     private void initSchemaAndTable(final PipelineContainerComposer containerComposer, final Connection connection, final int sleepSeconds) throws SQLException {
         containerComposer.createSchema(connection, sleepSeconds);
         String sql = containerComposer.getExtraSQLCommand().getCreateTableOrder(SOURCE_TABLE_NAME);
         log.info("create table sql: {}", sql);
         connection.createStatement().execute(sql);
         if (sleepSeconds > 0) {
-            Thread.sleep(sleepSeconds * 1000L);
+            Awaitility.await().pollDelay(sleepSeconds, TimeUnit.SECONDS).until(() -> true);
         }
     }
     
diff --git a/test/e2e/operation/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/data/pipeline/cases/migration/AbstractMigrationE2EIT.java b/test/e2e/operation/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/data/pipeline/cases/migration/AbstractMigrationE2EIT.java
index ebfa6b4f2cd..c9741373b44 100644
--- a/test/e2e/operation/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/data/pipeline/cases/migration/AbstractMigrationE2EIT.java
+++ b/test/e2e/operation/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/data/pipeline/cases/migration/AbstractMigrationE2EIT.java
@@ -19,13 +19,13 @@ package org.apache.shardingsphere.test.e2e.data.pipeline.cases.migration;
 
 import com.google.common.base.Strings;
 import lombok.Getter;
-import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.shardingsphere.test.e2e.data.pipeline.cases.PipelineContainerComposer;
 import org.apache.shardingsphere.test.e2e.data.pipeline.command.MigrationDistSQLCommand;
 import org.apache.shardingsphere.test.e2e.data.pipeline.env.PipelineE2EEnvironment;
 import org.apache.shardingsphere.test.e2e.data.pipeline.env.enums.PipelineEnvTypeEnum;
 import org.apache.shardingsphere.test.e2e.env.container.atomic.util.DatabaseTypeUtils;
+import org.awaitility.Awaitility;
 import org.opengauss.util.PSQLException;
 
 import javax.xml.bind.JAXB;
@@ -34,6 +34,7 @@ import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 
 import static org.hamcrest.CoreMatchers.is;
@@ -141,7 +142,6 @@ public abstract class AbstractMigrationE2EIT {
         return jobList.stream().filter(a -> a.get("tables").toString().equals(tableName)).findFirst().orElseThrow(() -> new RuntimeException("not find " + tableName + " table")).get("id").toString();
     }
     
-    @SneakyThrows(InterruptedException.class)
     protected void assertCheckMigrationSuccess(final PipelineContainerComposer containerComposer, final String jobId, final String algorithmType) throws SQLException {
         containerComposer.proxyExecuteWithLog(String.format("CHECK MIGRATION '%s' BY TYPE (NAME='%s')", jobId, algorithmType), 0);
         // TODO Need to add after the stop then to start, can continue the consistency check from the previous progress
@@ -149,14 +149,14 @@ public abstract class AbstractMigrationE2EIT {
         for (int i = 0; i < 10; i++) {
             resultList = containerComposer.queryForListWithLog(String.format("SHOW MIGRATION CHECK STATUS '%s'", jobId));
             if (resultList.isEmpty()) {
-                Thread.sleep(3000L);
+                Awaitility.await().pollDelay(3L, TimeUnit.SECONDS).until(() -> true);
                 continue;
             }
             List<String> checkEndTimeList = resultList.stream().map(map -> map.get("check_end_time").toString()).filter(each -> !Strings.isNullOrEmpty(each)).collect(Collectors.toList());
             if (checkEndTimeList.size() == resultList.size()) {
                 break;
             } else {
-                Thread.sleep(3000L);
+                Awaitility.await().pollDelay(3L, TimeUnit.SECONDS).until(() -> true);
             }
         }
         log.info("check job results: {}", resultList);
diff --git a/test/e2e/operation/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/data/pipeline/cases/migration/general/MySQLMigrationGeneralE2EIT.java b/test/e2e/operation/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/data/pipeline/cases/migration/general/MySQLMigrationGeneralE2EIT.java
index 7899cb0f028..bddf7db7799 100644
--- a/test/e2e/operation/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/data/pipeline/cases/migration/general/MySQLMigrationGeneralE2EIT.java
+++ b/test/e2e/operation/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/data/pipeline/cases/migration/general/MySQLMigrationGeneralE2EIT.java
@@ -32,6 +32,7 @@ import org.apache.shardingsphere.test.e2e.data.pipeline.framework.param.Pipeline
 import org.apache.shardingsphere.test.e2e.data.pipeline.framework.param.PipelineE2ETestCaseArgumentsProvider;
 import org.apache.shardingsphere.test.e2e.data.pipeline.framework.param.PipelineTestParameter;
 import org.apache.shardingsphere.test.e2e.data.pipeline.util.DataSourceExecuteUtils;
+import org.awaitility.Awaitility;
 import org.junit.jupiter.api.condition.EnabledIf;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.ArgumentsSource;
@@ -40,6 +41,7 @@ import java.sql.SQLException;
 import java.time.LocalDateTime;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
@@ -80,7 +82,7 @@ class MySQLMigrationGeneralE2EIT extends AbstractMigrationE2EIT {
             assertMigrationSuccessById(containerComposer, orderJobId, "DATA_MATCH");
             String orderItemJobId = getJobIdByTableName(containerComposer, "ds_0.t_order_item");
             assertMigrationSuccessById(containerComposer, orderItemJobId, "DATA_MATCH");
-            Thread.sleep(2000L);
+            Awaitility.await().pollDelay(2L, TimeUnit.SECONDS).until(() -> true);
             assertMigrationSuccessById(containerComposer, orderItemJobId, "CRC32_MATCH");
             for (String each : listJobId(containerComposer)) {
                 commitMigrationByJobId(containerComposer, each);
diff --git a/test/e2e/operation/showprocesslist/pom.xml b/test/e2e/operation/showprocesslist/pom.xml
index 56b70bc31c5..f5a3dc1b4ff 100644
--- a/test/e2e/operation/showprocesslist/pom.xml
+++ b/test/e2e/operation/showprocesslist/pom.xml
@@ -50,5 +50,10 @@
             <groupId>com.zaxxer</groupId>
             <artifactId>HikariCP</artifactId>
         </dependency>
+        
+        <dependency>
+            <groupId>org.awaitility</groupId>
+            <artifactId>awaitility</artifactId>
+        </dependency>
     </dependencies>
 </project>
diff --git a/test/e2e/operation/showprocesslist/src/test/java/org/apache/shardingsphere/test/e2e/showprocesslist/engine/ShowProcessListE2EIT.java b/test/e2e/operation/showprocesslist/src/test/java/org/apache/shardingsphere/test/e2e/showprocesslist/engine/ShowProcessListE2EIT.java
index f20a1f4fdd1..c8ac638443c 100644
--- a/test/e2e/operation/showprocesslist/src/test/java/org/apache/shardingsphere/test/e2e/showprocesslist/engine/ShowProcessListE2EIT.java
+++ b/test/e2e/operation/showprocesslist/src/test/java/org/apache/shardingsphere/test/e2e/showprocesslist/engine/ShowProcessListE2EIT.java
@@ -22,6 +22,7 @@ import org.apache.shardingsphere.test.e2e.showprocesslist.container.composer.Clu
 import org.apache.shardingsphere.test.e2e.showprocesslist.env.ShowProcessListEnvironment;
 import org.apache.shardingsphere.test.e2e.showprocesslist.env.enums.ShowProcessListEnvTypeEnum;
 import org.apache.shardingsphere.test.e2e.showprocesslist.parameter.ShowProcessListTestParameter;
+import org.awaitility.Awaitility;
 import org.junit.jupiter.api.condition.EnabledIf;
 import org.junit.jupiter.api.extension.ExtensionContext;
 import org.junit.jupiter.params.ParameterizedTest;
@@ -37,6 +38,7 @@ import java.sql.Statement;
 import java.util.Collection;
 import java.util.LinkedList;
 import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
 import java.util.stream.Stream;
 
 import static org.hamcrest.CoreMatchers.is;
@@ -56,7 +58,7 @@ class ShowProcessListE2EIT {
         try (ClusterShowProcessListContainerComposer containerComposer = new ClusterShowProcessListContainerComposer(testParam)) {
             containerComposer.start();
             CompletableFuture<Void> executeSelectSleep = CompletableFuture.runAsync(getExecuteSleepThread("proxy", containerComposer));
-            Thread.sleep(5000L);
+            Awaitility.await().pollDelay(5L, TimeUnit.SECONDS).until(() -> true);
             try (
                     Connection connection = containerComposer.getProxyDataSource().getConnection();
                     Statement statement = connection.createStatement()) {
diff --git a/test/e2e/operation/transaction/pom.xml b/test/e2e/operation/transaction/pom.xml
index c9a2fc954cb..98a5bcfd4c4 100644
--- a/test/e2e/operation/transaction/pom.xml
+++ b/test/e2e/operation/transaction/pom.xml
@@ -134,5 +134,9 @@
             <artifactId>btm</artifactId>
             <version>${btm.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.awaitility</groupId>
+            <artifactId>awaitility</artifactId>
+        </dependency>
     </dependencies>
 </project>
diff --git a/test/e2e/operation/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/MySQLAutoCommitTestCase.java b/test/e2e/operation/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/MySQLAutoCommitTestCase.java
index a69c35e689e..2c725b4d7e3 100644
--- a/test/e2e/operation/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/MySQLAutoCommitTestCase.java
+++ b/test/e2e/operation/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/MySQLAutoCommitTestCase.java
@@ -17,17 +17,18 @@
 
 package org.apache.shardingsphere.test.e2e.transaction.cases.autocommit;
 
-import lombok.SneakyThrows;
 import org.apache.shardingsphere.test.e2e.transaction.cases.base.BaseTransactionTestCase;
 import org.apache.shardingsphere.test.e2e.transaction.engine.base.TransactionBaseE2EIT;
 import org.apache.shardingsphere.test.e2e.transaction.engine.base.TransactionContainerComposer;
 import org.apache.shardingsphere.test.e2e.transaction.engine.base.TransactionTestCase;
 import org.apache.shardingsphere.test.e2e.transaction.engine.constants.TransactionTestConstants;
 import org.apache.shardingsphere.transaction.api.TransactionType;
+import org.awaitility.Awaitility;
 
 import javax.sql.DataSource;
 import java.sql.Connection;
 import java.sql.SQLException;
+import java.util.concurrent.TimeUnit;
 
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -47,7 +48,6 @@ public final class MySQLAutoCommitTestCase extends BaseTransactionTestCase {
         assertAutoCommit();
     }
     
-    @SneakyThrows(InterruptedException.class)
     private void assertAutoCommit() throws SQLException {
         // TODO Currently XA transaction does not support two transactions in the same thread at the same time
         try (Connection connection1 = getDataSource().getConnection(); Connection connection2 = getDataSource().getConnection()) {
@@ -58,7 +58,9 @@ public final class MySQLAutoCommitTestCase extends BaseTransactionTestCase {
             executeWithLog(connection1, "insert into account(id, balance, transaction_id) values(1, 100, 1);");
             assertFalse(executeQueryWithLog(connection2, "select * from account;").next());
             connection1.commit();
-            Thread.sleep(1000L);
+            Awaitility.await().atMost(1L, TimeUnit.SECONDS).pollDelay(200L, TimeUnit.MILLISECONDS).until(
+                    () -> executeQueryWithLog(connection2, "select * from account;").next()
+            );
             assertTrue(executeQueryWithLog(connection2, "select * from account;").next());
         }
     }
diff --git a/test/e2e/operation/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/PostgreSQLAutoCommitTestCase.java b/test/e2e/operation/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/PostgreSQLAutoCommitTestCase.java
index accd89381f1..1e4debc7788 100644
--- a/test/e2e/operation/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/PostgreSQLAutoCommitTestCase.java
+++ b/test/e2e/operation/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/PostgreSQLAutoCommitTestCase.java
@@ -17,17 +17,18 @@
 
 package org.apache.shardingsphere.test.e2e.transaction.cases.autocommit;
 
-import lombok.SneakyThrows;
 import org.apache.shardingsphere.test.e2e.transaction.cases.base.BaseTransactionTestCase;
 import org.apache.shardingsphere.test.e2e.transaction.engine.base.TransactionBaseE2EIT;
 import org.apache.shardingsphere.test.e2e.transaction.engine.base.TransactionContainerComposer;
 import org.apache.shardingsphere.test.e2e.transaction.engine.base.TransactionTestCase;
 import org.apache.shardingsphere.test.e2e.transaction.engine.constants.TransactionTestConstants;
 import org.apache.shardingsphere.transaction.api.TransactionType;
+import org.awaitility.Awaitility;
 
 import javax.sql.DataSource;
 import java.sql.Connection;
 import java.sql.SQLException;
+import java.util.concurrent.TimeUnit;
 
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -47,7 +48,6 @@ public final class PostgreSQLAutoCommitTestCase extends BaseTransactionTestCase
         assertAutoCommit();
     }
     
-    @SneakyThrows(InterruptedException.class)
     private void assertAutoCommit() throws SQLException {
         try (Connection connection1 = getDataSource().getConnection(); Connection connection2 = getDataSource().getConnection()) {
             executeWithLog(connection1, "set transaction isolation level read committed;");
@@ -57,7 +57,9 @@ public final class PostgreSQLAutoCommitTestCase extends BaseTransactionTestCase
             executeWithLog(connection1, "insert into account(id, balance, transaction_id) values(1, 100, 1);");
             assertFalse(executeQueryWithLog(connection2, "select * from account;").next());
             connection1.commit();
-            Thread.sleep(1000L);
+            Awaitility.await().atMost(1L, TimeUnit.SECONDS).pollDelay(200L, TimeUnit.MILLISECONDS).until(
+                    () -> executeQueryWithLog(connection2, "select * from account;").next()
+            );
             assertTrue(executeQueryWithLog(connection2, "select * from account;").next());
         }
     }