You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by me...@apache.org on 2021/11/22 11:44:10 UTC
[shardingsphere] branch master updated: Update MGRDatabaseDiscoveryTypeTest #13533 (#13676)
This is an automated email from the ASF dual-hosted git repository.
menghaoran 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 817967c Update MGRDatabaseDiscoveryTypeTest #13533 (#13676)
817967c is described below
commit 817967c02d3d6b9ffca6a8520cd6ab1f66c46965
Author: Simple <36...@users.noreply.github.com>
AuthorDate: Mon Nov 22 19:43:20 2021 +0800
Update MGRDatabaseDiscoveryTypeTest #13533 (#13676)
* updateMemberState单测
* updateMemberState单测
* update updateMemberState
* modify the way reflection modifies variables
* remove blank line
* Adjustment format
* Adjustment format
* Adjustment format
* Modify the reflection method
* Reduce the version of mockio and restore the previous changes
* add tab
Co-authored-by: cuishifeng <cu...@163.com>
---
pom.xml | 2 +-
.../mgr/MGRDatabaseDiscoveryTypeTest.java | 58 +++++++++++++++++++++-
2 files changed, 58 insertions(+), 2 deletions(-)
diff --git a/pom.xml b/pom.xml
index 7432b8d..8b87112 100644
--- a/pom.xml
+++ b/pom.xml
@@ -89,7 +89,7 @@
<junit.version>4.12</junit.version>
<hamcrest.version>1.3</hamcrest.version>
- <mockito.version>2.28.2</mockito.version>
+ <mockito.version>3.4.0</mockito.version>
<logback.version>1.2.0</logback.version>
<hikari-cp.version>3.4.2</hikari-cp.version>
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/src/test/java/org/apache/shardingsphere/dbdiscovery/mgr/MGRDatabaseDiscoveryTypeTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/src/test/java/org/apache/shardingsphere/dbdiscovery/mgr/MGRDatabaseDiscoveryTypeTest.java
index e47b7ad..e6105b4 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/src/test/java/org/apache/shardingsphere/dbdiscovery/mgr/MGRDatabaseDiscoveryTypeTest.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/src/test/java/org/apache/shardingsphere/dbdiscovery/mgr/MGRDatabaseDiscoveryTypeTest.java
@@ -17,25 +17,33 @@
package org.apache.shardingsphere.dbdiscovery.mgr;
+import com.google.common.eventbus.EventBus;
+import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
+import org.apache.shardingsphere.infra.rule.event.impl.DataSourceDisabledEvent;
import org.junit.Test;
+import org.mockito.Mockito;
import javax.sql.DataSource;
+import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
+import java.util.Map;
import java.util.LinkedList;
import java.util.List;
-import java.util.Map;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.verify;
public final class MGRDatabaseDiscoveryTypeTest {
@@ -117,4 +125,52 @@ public final class MGRDatabaseDiscoveryTypeTest {
mgrHaType.updatePrimaryDataSource("discovery_db", dataSourceMap, Collections.emptySet(), "group_name");
assertThat(mgrHaType.getPrimaryDataSource(), is("ds_2"));
}
+
+ @Test
+ public void updateMemberState() throws IllegalAccessException, NoSuchFieldException {
+ Field declaredField = MGRDatabaseDiscoveryType.class.getDeclaredField("oldPrimaryDataSource");
+ declaredField.setAccessible(true);
+ declaredField.set(mgrHaType, "ds_0");
+ EventBus eventBus = mock(EventBus.class);
+ mockStatic(ShardingSphereEventBus.class);
+ when(ShardingSphereEventBus.getInstance()).thenReturn(eventBus);
+ List<DataSource> dataSources = new LinkedList<>();
+ List<Connection> connections = new LinkedList<>();
+ List<Statement> statements = new LinkedList<>();
+ List<ResultSet> resultSets = new LinkedList<>();
+ List<DatabaseMetaData> databaseMetaData = new LinkedList<>();
+ for (int i = 0; i < 3; i++) {
+ dataSources.add(mock(DataSource.class));
+ connections.add(mock(Connection.class));
+ statements.add(mock(Statement.class));
+ resultSets.add(mock(ResultSet.class));
+ databaseMetaData.add(mock(DatabaseMetaData.class));
+ }
+ String sql = "SELECT MEMBER_HOST, MEMBER_PORT, MEMBER_STATE FROM performance_schema.replication_group_members";
+ try {
+ for (int i = 0; i < 3; i++) {
+ when(dataSources.get(i).getConnection()).thenReturn(connections.get(i));
+ when(connections.get(i).createStatement()).thenReturn(statements.get(i));
+ when(statements.get(i).executeQuery(sql)).thenReturn(resultSets.get(i));
+ when(resultSets.get(i).next()).thenReturn(true, false);
+ when(resultSets.get(i).getString("MEMBER_HOST")).thenReturn("127.0.0.1");
+ when(resultSets.get(i).getString("MEMBER_PORT")).thenReturn(Integer.toString(3306 + i));
+ when(resultSets.get(i).getString("MEMBER_STATE")).thenReturn("ONLINE");
+ when(connections.get(i).getMetaData()).thenReturn(databaseMetaData.get(i));
+ when(databaseMetaData.get(i).getURL()).thenReturn("jdbc:mysql://127.0.0.1:" + (3306 + i) + "/ds_0?serverTimezone=UTC&useSSL=false");
+ }
+ } catch (final SQLException ex) {
+ throw new ShardingSphereException(ex);
+ }
+ Map<String, DataSource> dataSourceMap = new HashMap<>(3, 1);
+ List<String> disabledDataSourceNames = new ArrayList<>();
+ for (int i = 0; i < 3; i++) {
+ dataSourceMap.put(String.format("ds_%s", i), dataSources.get(i));
+ if (disabledDataSourceNames.isEmpty()) {
+ disabledDataSourceNames.add(String.format("ds_1", i));
+ }
+ }
+ mgrHaType.updateMemberState("discovery_db", dataSourceMap, disabledDataSourceNames);
+ verify(eventBus).post(Mockito.refEq(new DataSourceDisabledEvent("discovery_db", "ds_2", true)));
+ }
}