You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/06/14 04:15:54 UTC

[shardingsphere] branch master updated: add unit test assertLoadReplicaStatus (#18346)

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

zhangliang 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 fd7c4bd888f add unit test assertLoadReplicaStatus (#18346)
fd7c4bd888f is described below

commit fd7c4bd888ff83e4d893a3bb67af0c32ee6e9034
Author: natehuang <na...@tencent.com>
AuthorDate: Tue Jun 14 12:15:48 2022 +0800

    add unit test assertLoadReplicaStatus (#18346)
---
 ...tionDatabaseDiscoveryProviderAlgorithmTest.java | 32 ++++++++++++++++++++--
 1 file changed, 29 insertions(+), 3 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithmTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MySQLNormalReplicationDatabaseDiscoveryP [...]
index dda9663a113..555d4cd486b 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithmTest.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithmTest.java
@@ -17,13 +17,18 @@
 
 package org.apache.shardingsphere.dbdiscovery.mysql.type;
 
+import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryProviderAlgorithm;
+import org.apache.shardingsphere.dbdiscovery.spi.ReplicaDataSourceStatus;
 import org.junit.Test;
 
 import javax.sql.DataSource;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.Collections;
+import java.util.Properties;
 
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
@@ -33,15 +38,15 @@ public final class MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithmTest
     
     @Test
     public void assertCheckEnvironment() throws SQLException {
-        new MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithm().checkEnvironment("foo_db", Collections.singletonList(mockDataSource()));
+        new MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithm().checkEnvironment("foo_db", Collections.singletonList(mockDataSourceForReplicationInstances()));
     }
     
     @Test
     public void assertIsPrimaryInstance() throws SQLException {
-        assertTrue(new MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithm().isPrimaryInstance(mockDataSource()));
+        assertTrue(new MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithm().isPrimaryInstance(mockDataSourceForReplicationInstances()));
     }
     
-    private DataSource mockDataSource() throws SQLException {
+    private DataSource mockDataSourceForReplicationInstances() throws SQLException {
         DataSource result = mock(DataSource.class, RETURNS_DEEP_STUBS);
         ResultSet resultSet = mock(ResultSet.class);
         when(result.getConnection().createStatement().executeQuery("SHOW SLAVE HOSTS")).thenReturn(resultSet);
@@ -51,4 +56,25 @@ public final class MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithmTest
         when(result.getConnection().getMetaData().getURL()).thenReturn("jdbc:mysql://127.0.0.1:3306/foo_ds");
         return result;
     }
+
+    private DataSource mockDataSourceForReplicaStatus() throws SQLException {
+        DataSource result = mock(DataSource.class, RETURNS_DEEP_STUBS);
+        ResultSet resultSet = mock(ResultSet.class);
+        when(result.getConnection().createStatement().executeQuery("SHOW SLAVE STATUS")).thenReturn(resultSet);
+        when(resultSet.next()).thenReturn(true, false);
+        when(resultSet.getLong("Seconds_Behind_Master")).thenReturn(10L);
+        return result;
+    }
+
+    @Test
+    public void assertLoadReplicaStatus() throws SQLException {
+        Properties props = new Properties();
+        props.setProperty("delay-milliseconds-threshold", "15000");
+        DatabaseDiscoveryProviderAlgorithm algorithm = new MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithm();
+        algorithm.init(props);
+        DataSource dataSource = mockDataSourceForReplicaStatus();
+        ReplicaDataSourceStatus actual = algorithm.loadReplicaStatus(dataSource);
+        assertTrue(actual.isOnline());
+        assertThat(actual.getReplicationDelayMilliseconds(), is(10000L));
+    }
 }