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 3c01961d10b Enhance discovery scenes it (#24340)
3c01961d10b is described below

commit 3c01961d10be81f49a7eebf5b895575fff3143b9
Author: zhaojinchao <[email protected]>
AuthorDate: Fri Feb 24 22:40:12 2023 +0800

    Enhance discovery scenes it (#24340)
---
 .../e2e/discovery/build/DiscoveryRuleBuilder.java  | 12 +---
 .../discovery/cases/base/BaseDiscoveryE2EIT.java   | 77 +++++++++++++++++++---
 .../cases/mysql/MGRGeneralDiscoveryE2EIT.java      |  4 +-
 .../discovery/command/DiscoveryDistSQLCommand.java | 12 ++++
 .../resources/env/common/discovery-command.xml     | 34 ++++++++++
 5 files changed, 121 insertions(+), 18 deletions(-)

diff --git 
a/test/e2e/discovery/src/test/java/org/apache/shardingsphere/test/e2e/discovery/build/DiscoveryRuleBuilder.java
 
b/test/e2e/discovery/src/test/java/org/apache/shardingsphere/test/e2e/discovery/build/DiscoveryRuleBuilder.java
index 6bc68591474..0a264e07992 100644
--- 
a/test/e2e/discovery/src/test/java/org/apache/shardingsphere/test/e2e/discovery/build/DiscoveryRuleBuilder.java
+++ 
b/test/e2e/discovery/src/test/java/org/apache/shardingsphere/test/e2e/discovery/build/DiscoveryRuleBuilder.java
@@ -17,33 +17,27 @@
 
 package org.apache.shardingsphere.test.e2e.discovery.build;
 
-import 
org.apache.shardingsphere.test.e2e.discovery.cases.base.BaseDiscoveryE2EIT;
+import lombok.RequiredArgsConstructor;
 import 
org.apache.shardingsphere.test.e2e.discovery.command.DiscoveryDistSQLCommand;
 import org.awaitility.Awaitility;
 import org.awaitility.Durations;
 
 import javax.sql.DataSource;
-import javax.xml.bind.JAXB;
 import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
-import java.util.Objects;
 
 /**
  * Build discovery rule.
  */
+@RequiredArgsConstructor
 public final class DiscoveryRuleBuilder {
     
     private final DiscoveryDistSQLCommand discoveryDistSQLCommand;
     
     private final DataSource proxyDataSource;
     
-    public DiscoveryRuleBuilder(final DataSource proxyDataSource) {
-        this.proxyDataSource = proxyDataSource;
-        discoveryDistSQLCommand = 
JAXB.unmarshal(Objects.requireNonNull(BaseDiscoveryE2EIT.class.getClassLoader().getResource("env/common/discovery-command.xml")),
 DiscoveryDistSQLCommand.class);
-    }
-    
     /**
      *  build Discovery Environment.
      *
@@ -85,7 +79,7 @@ public final class DiscoveryRuleBuilder {
     
     private void createReadwriteSplittingRule(final Statement statement) 
throws SQLException {
         
statement.execute(discoveryDistSQLCommand.getCreateReadwriteSplittingRule().getExecuteSQL());
-        Awaitility.await().atMost(Durations.TWO_SECONDS).until(() -> 
assertResult0(statement, 
discoveryDistSQLCommand.getCreateReadwriteSplittingRule().getAssertionSQL()));
+        Awaitility.await().atMost(Durations.FIVE_SECONDS).until(() -> 
assertResult0(statement, 
discoveryDistSQLCommand.getCreateReadwriteSplittingRule().getAssertionSQL()));
     }
     
     private boolean assertResult0(final Statement statement, final String 
assertionSQL) {
diff --git 
a/test/e2e/discovery/src/test/java/org/apache/shardingsphere/test/e2e/discovery/cases/base/BaseDiscoveryE2EIT.java
 
b/test/e2e/discovery/src/test/java/org/apache/shardingsphere/test/e2e/discovery/cases/base/BaseDiscoveryE2EIT.java
index eb901a19f38..4e5d5c62a71 100644
--- 
a/test/e2e/discovery/src/test/java/org/apache/shardingsphere/test/e2e/discovery/cases/base/BaseDiscoveryE2EIT.java
+++ 
b/test/e2e/discovery/src/test/java/org/apache/shardingsphere/test/e2e/discovery/cases/base/BaseDiscoveryE2EIT.java
@@ -25,6 +25,7 @@ import org.apache.commons.lang.StringUtils;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.test.e2e.discovery.build.DiscoveryRuleBuilder;
 import 
org.apache.shardingsphere.test.e2e.discovery.cases.DatabaseClusterEnvironment;
+import 
org.apache.shardingsphere.test.e2e.discovery.command.DiscoveryDistSQLCommand;
 import 
org.apache.shardingsphere.test.e2e.discovery.env.DiscoveryE2ETestEnvironment;
 import 
org.apache.shardingsphere.test.e2e.discovery.framework.container.compose.BaseContainerComposer;
 import 
org.apache.shardingsphere.test.e2e.discovery.framework.container.compose.DockerContainerComposer;
@@ -33,6 +34,7 @@ import org.awaitility.Awaitility;
 import org.awaitility.Durations;
 
 import javax.sql.DataSource;
+import javax.xml.bind.JAXB;
 import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.SQLException;
@@ -58,12 +60,15 @@ public abstract class BaseDiscoveryE2EIT {
     
     private final DataSource proxyDataSource;
     
+    private final DiscoveryDistSQLCommand discoveryDistSQLCommand;
+    
     public BaseDiscoveryE2EIT(final DiscoveryTestParameter testParam) {
         databaseType = testParam.getDatabaseType();
         containerComposer = new 
DockerContainerComposer(testParam.getScenario(), testParam.getDatabaseType(), 
testParam.getStorageContainerImage());
         containerComposer.start();
         mappedDataSources = containerComposer.getMappedDatasource();
         proxyDataSource = containerComposer.getProxyDatasource();
+        discoveryDistSQLCommand = 
JAXB.unmarshal(Objects.requireNonNull(BaseDiscoveryE2EIT.class.getClassLoader().getResource("env/common/discovery-command.xml")),
 DiscoveryDistSQLCommand.class);
     }
     
     /**
@@ -72,7 +77,7 @@ public abstract class BaseDiscoveryE2EIT {
      * @throws SQLException SQL exception
      */
     public void initDiscoveryEnvironment() throws SQLException {
-        new DiscoveryRuleBuilder(proxyDataSource).buildDiscoveryEnvironment();
+        new DiscoveryRuleBuilder(discoveryDistSQLCommand, 
proxyDataSource).buildDiscoveryEnvironment();
     }
     
     /**
@@ -131,7 +136,7 @@ public abstract class BaseDiscoveryE2EIT {
         mgrEnvironment.getDataSources().remove(getPrimaryDataSourceName());
         String closedRoutingDataSourceName = 
getCloseReplicationDataSourceName(mgrEnvironment);
         mgrEnvironment.getDataSources().remove(closedRoutingDataSourceName);
-        Awaitility.await().atMost(Durations.ONE_MINUTE)
+        Awaitility.await().atMost(Durations.TWO_MINUTES)
                 .until(() -> 
getRouteDataSourceName().equals(Objects.requireNonNull(mgrEnvironment.getDataSources().entrySet().stream().findFirst().orElse(null)).getKey()));
     }
     
@@ -158,12 +163,68 @@ public abstract class BaseDiscoveryE2EIT {
     }
     
     /**
-     * Assert close all replication data source.
-     * @param mgrEnvironment mgr environment
-     * @throws SQLException SQL Exception
+     * Drop database discovery database.
+     *
+     * @throws SQLException sql exception
+     */
+    public void dropDatabaseDiscoveryDatabase() throws SQLException {
+        try (
+                Connection connection = getProxyDataSource().getConnection();
+                Statement statement = connection.createStatement()) {
+            
statement.execute(discoveryDistSQLCommand.getDropDatabase().getExecuteSQL());
+            Awaitility.await().atMost(Durations.FIVE_SECONDS).until(() -> 
assertDropSQL(statement, 
discoveryDistSQLCommand.getDropDatabase().getAssertionSQL()));
+        }
+    }
+    
+    private boolean assertDropSQL(final Statement statement, final String 
assertionSQL) {
+        try (ResultSet resultSet = statement.executeQuery(assertionSQL)) {
+            return false;
+        } catch (final SQLException ignored) {
+            return true;
+        }
+    }
+    
+    /**
+     * Create readwrite-splitting database.
+     *
+     * @throws SQLException sql exception
+     */
+    public void createReadWriteSplittingDatabase() throws SQLException {
+        try (
+                Connection connection = getProxyDataSource().getConnection();
+                Statement statement = connection.createStatement()) {
+            
statement.execute(discoveryDistSQLCommand.getCreateReadwriteSplittingDatabase().getExecuteSQL());
+            Awaitility.await().atMost(Durations.FIVE_SECONDS).until(() -> 
assertCreateSQL(statement, 
discoveryDistSQLCommand.getCreateReadwriteSplittingDatabase().getAssertionSQL()));
+        }
+    }
+    
+    private boolean assertCreateSQL(final Statement statement, final String 
assertionSQL) {
+        try (ResultSet resultSet = statement.executeQuery(assertionSQL)) {
+            return true;
+        } catch (final SQLException ignored) {
+            return false;
+        }
+    }
+    
+    /**
+     * Register single storage units.
+     *
+     * @throws SQLException sql exception
      */
-    public void assertCloseAllReplicationDataSource(final 
DatabaseClusterEnvironment mgrEnvironment) throws SQLException {
-        
closeDataSource(Objects.requireNonNull(mgrEnvironment.getDataSources().values().stream().findFirst().orElse(null)));
-        Awaitility.await().atMost(Durations.ONE_MINUTE).until(() -> 
getRouteDataSourceName().equals(getPrimaryDataSourceName()));
+    public void registerSingleStorageUnit() throws SQLException {
+        try (
+                Connection connection = getProxyDataSource().getConnection();
+                Statement statement = connection.createStatement()) {
+            
statement.execute(discoveryDistSQLCommand.getRegisterSingleStorageUnit().getExecuteSQL());
+            Awaitility.await().atMost(Durations.FIVE_SECONDS).until(() -> 
assertRDLDistSQL(statement, 
discoveryDistSQLCommand.getRegisterSingleStorageUnit().getAssertionSQL()));
+        }
+    }
+    
+    private boolean assertRDLDistSQL(final Statement statement, final String 
assertionSQL) {
+        try (ResultSet resultSet = statement.executeQuery(assertionSQL)) {
+            return resultSet.next();
+        } catch (final SQLException ignored) {
+            return false;
+        }
     }
 }
diff --git 
a/test/e2e/discovery/src/test/java/org/apache/shardingsphere/test/e2e/discovery/cases/mysql/MGRGeneralDiscoveryE2EIT.java
 
b/test/e2e/discovery/src/test/java/org/apache/shardingsphere/test/e2e/discovery/cases/mysql/MGRGeneralDiscoveryE2EIT.java
index 8eb49935385..92c93e5bdad 100644
--- 
a/test/e2e/discovery/src/test/java/org/apache/shardingsphere/test/e2e/discovery/cases/mysql/MGRGeneralDiscoveryE2EIT.java
+++ 
b/test/e2e/discovery/src/test/java/org/apache/shardingsphere/test/e2e/discovery/cases/mysql/MGRGeneralDiscoveryE2EIT.java
@@ -54,6 +54,8 @@ public final class MGRGeneralDiscoveryE2EIT extends 
BaseDiscoveryE2EIT {
         initDiscoveryEnvironment();
         assertClosePrimaryDataSource(mgrEnvironment);
         assertCloseReplicationDataSource(mgrEnvironment);
-        assertCloseAllReplicationDataSource(mgrEnvironment);
+        dropDatabaseDiscoveryDatabase();
+        createReadWriteSplittingDatabase();
+        registerSingleStorageUnit();
     }
 }
diff --git 
a/test/e2e/discovery/src/test/java/org/apache/shardingsphere/test/e2e/discovery/command/DiscoveryDistSQLCommand.java
 
b/test/e2e/discovery/src/test/java/org/apache/shardingsphere/test/e2e/discovery/command/DiscoveryDistSQLCommand.java
index ffea573a48c..3754b9aae39 100644
--- 
a/test/e2e/discovery/src/test/java/org/apache/shardingsphere/test/e2e/discovery/command/DiscoveryDistSQLCommand.java
+++ 
b/test/e2e/discovery/src/test/java/org/apache/shardingsphere/test/e2e/discovery/command/DiscoveryDistSQLCommand.java
@@ -46,4 +46,16 @@ public final class DiscoveryDistSQLCommand {
     @XmlElement(name = "create-readwrite-splitting-rule")
     @Getter
     private DistSQLCommandPOJO createReadwriteSplittingRule;
+    
+    @XmlElement(name = "drop-database")
+    @Getter
+    private DistSQLCommandPOJO dropDatabase;
+    
+    @XmlElement(name = "create-readwrite-splitting-database")
+    @Getter
+    private DistSQLCommandPOJO createReadwriteSplittingDatabase;
+    
+    @XmlElement(name = "register-single-storage-unit")
+    @Getter
+    private DistSQLCommandPOJO registerSingleStorageUnit;
 }
diff --git 
a/test/e2e/discovery/src/test/resources/env/common/discovery-command.xml 
b/test/e2e/discovery/src/test/resources/env/common/discovery-command.xml
index 6c8fd58b3f2..d12fab61427 100644
--- a/test/e2e/discovery/src/test/resources/env/common/discovery-command.xml
+++ b/test/e2e/discovery/src/test/resources/env/common/discovery-command.xml
@@ -84,4 +84,38 @@
             SHOW READWRITE_SPLITTING RULES replica_ds;
         </assertion-sql>
     </create-readwrite-splitting-rule>
+
+    <drop-database>
+        <execute-sql>
+            DROP DATABASE db_discovery;
+        </execute-sql>
+        <assertion-sql>
+            USE db_discovery;
+        </assertion-sql>
+    </drop-database>
+
+    <create-readwrite-splitting-database>
+        <execute-sql>
+            CREATE DATABASE readwrite_splitting_db;
+        </execute-sql>
+        <assertion-sql>
+            USE readwrite_splitting_db;
+        </assertion-sql>
+    </create-readwrite-splitting-database>
+
+    <register-single-storage-unit>
+        <execute-sql>
+            REGISTER STORAGE UNIT ds_3 (
+            HOST="mysql_4",
+            PORT=3306,
+            DB="it_discovery_test",
+            USER="test_user",
+            PASSWORD="Test@123",
+            PROPERTIES("connectionTimeout"="5000")
+            );
+        </execute-sql>
+        <assertion-sql>
+            SHOW STORAGE UNITS;
+        </assertion-sql>
+    </register-single-storage-unit>
 </command>

Reply via email to