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 2022/07/10 15:58:06 UTC
[shardingsphere] branch master updated: Add more assertion for ResourceSwitchManagerTest (#19006)
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 666ada29945 Add more assertion for ResourceSwitchManagerTest (#19006)
666ada29945 is described below
commit 666ada29945242aed994efca0001bdc4013226f1
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun Jul 10 23:58:00 2022 +0800
Add more assertion for ResourceSwitchManagerTest (#19006)
* Add more assertion for ResourceSwitchManagerTest
* Refactor MockedDataSource
* Fix test case
* Fix test case
* Fix test case
---
.../props/DataSourcePropertiesCreator.java | 2 +-
.../resource/ShardingSphereResourceTest.java | 5 +-
.../switcher/ResourceSwitchManagerTest.java | 72 +++++++++++++++++-----
.../fixture/MockedDataSourcePoolFieldMetaData.java | 39 ++++++------
.../fixture/MockedDataSourcePoolMetaData.java | 65 +++++++++++++++++++
...datasource.pool.metadata.DataSourcePoolMetaData | 18 ++++++
.../fixture/MockedDataSourcePoolFieldMetaData.java | 39 ++++++------
.../fixture/MockedDataSourcePoolMetaData.java | 65 +++++++++++++++++++
...datasource.pool.metadata.DataSourcePoolMetaData | 18 ++++++
.../shardingsphere/test/mock/MockedDataSource.java | 2 +-
10 files changed, 268 insertions(+), 57 deletions(-)
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesCreator.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesCreator.java
index f3e4cacd0f8..4952f04bdf8 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesCreator.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesCreator.java
@@ -81,7 +81,7 @@ public final class DataSourcePropertiesCreator {
for (Entry<String, Object> entry : new DataSourceReflection(dataSource).convertToProperties().entrySet()) {
String propertyName = entry.getKey();
Object propertyValue = entry.getValue();
- if (!poolMetaData.isPresent() || isValidProperty(propertyName, propertyValue, poolMetaData.get())) {
+ if (!poolMetaData.isPresent() || isValidProperty(propertyName, propertyValue, poolMetaData.get()) && !poolMetaData.get().getTransientFieldNames().contains(propertyName)) {
result.put(propertyName, propertyValue);
}
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResourceTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResourceTest.java
index 3b4bba1f031..44ec2b4668a 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResourceTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResourceTest.java
@@ -28,13 +28,14 @@ import static org.junit.Assert.assertTrue;
public final class ShardingSphereResourceTest {
+ @SuppressWarnings("BusyWait")
@Test
public void assertClose() throws SQLException, IOException, InterruptedException {
MockedDataSource dataSource = new MockedDataSource();
new ShardingSphereResource(Collections.singletonMap("foo_ds", dataSource)).close(dataSource);
- while (null == dataSource.getClosed() || !dataSource.getClosed()) {
+ while (!dataSource.isClosed()) {
Thread.sleep(10L);
}
- assertTrue(dataSource.getClosed());
+ assertTrue(dataSource.isClosed());
}
}
diff --git a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManagerTest.java b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManagerTest.java
index bf29b86f5c8..e4a4c3c6df4 100644
--- a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManagerTest.java
+++ b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManagerTest.java
@@ -17,32 +17,70 @@
package org.apache.shardingsphere.mode.manager.switcher;
-import java.util.Collections;
-import java.util.LinkedHashMap;
-import java.util.Map;
import org.apache.shardingsphere.infra.datasource.props.DataSourceProperties;
import org.apache.shardingsphere.infra.metadata.database.resource.ShardingSphereResource;
import org.apache.shardingsphere.test.mock.MockedDataSource;
import org.junit.Test;
-import static org.junit.Assert.assertTrue;
+import javax.sql.DataSource;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
-public class ResourceSwitchManagerTest {
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+public final class ResourceSwitchManagerTest {
+
@Test
- public void assertCreate() {
- MockedDataSource dataSource = new MockedDataSource();
- ShardingSphereResource resource = new ShardingSphereResource(Collections.singletonMap("foo_ds", dataSource));
- DataSourceProperties dataSourceProperties =
- new DataSourceProperties(MockedDataSource.class.getName(), createUserProperties("foo"));
- ResourceSwitchManager manager = new ResourceSwitchManager();
- SwitchingResource switchingResource = manager.create(resource, Collections.singletonMap("db", dataSourceProperties));
- assertTrue(switchingResource.getNewDataSources().containsKey("db"));
+ public void assertCreate() throws InterruptedException {
+ Map<String, DataSource> dataSourceMap = createDataSourceMap();
+ SwitchingResource actual = new ResourceSwitchManager().create(new ShardingSphereResource(dataSourceMap), createToBeChangedDataSourcePropsMap());
+ assertNewDataSources(actual);
+ actual.closeStaleDataSources();
+ assertStaleDataSources(dataSourceMap);
}
-
- private Map<String, Object> createUserProperties(final String username) {
- Map<String, Object> result = new LinkedHashMap<>(1, 1);
- result.put("username", username);
+
+ private Map<String, DataSource> createDataSourceMap() {
+ Map<String, DataSource> result = new HashMap<>(3, 1);
+ result.put("not_change", new MockedDataSource());
+ result.put("replace", new MockedDataSource());
+ result.put("delete", new MockedDataSource());
return result;
}
+
+ private Map<String, DataSourceProperties> createToBeChangedDataSourcePropsMap() {
+ Map<String, DataSourceProperties> result = new HashMap<>(3, 1);
+ result.put("new", new DataSourceProperties(MockedDataSource.class.getName(), Collections.emptyMap()));
+ result.put("not_change", new DataSourceProperties(MockedDataSource.class.getName(), Collections.emptyMap()));
+ result.put("replace", new DataSourceProperties(MockedDataSource.class.getName(), Collections.singletonMap("password", "new_pwd")));
+ return result;
+ }
+
+ private void assertNewDataSources(final SwitchingResource actual) {
+ assertThat(actual.getNewDataSources().size(), is(3));
+ assertTrue(actual.getNewDataSources().containsKey("not_change"));
+ assertTrue(actual.getNewDataSources().containsKey("new"));
+ assertTrue(actual.getNewDataSources().containsKey("replace"));
+ }
+
+ private void assertStaleDataSources(final Map<String, DataSource> originalDataSourceMap) throws InterruptedException {
+ assertStaleDataSource((MockedDataSource) originalDataSourceMap.get("delete"));
+ assertStaleDataSource((MockedDataSource) originalDataSourceMap.get("replace"));
+ assertNotStaleDataSource((MockedDataSource) originalDataSourceMap.get("not_change"));
+ }
+
+ @SuppressWarnings("BusyWait")
+ private void assertStaleDataSource(final MockedDataSource dataSource) throws InterruptedException {
+ while (!dataSource.isClosed()) {
+ Thread.sleep(10L);
+ }
+ assertTrue(dataSource.isClosed());
+ }
+
+ private void assertNotStaleDataSource(final MockedDataSource dataSource) {
+ assertFalse(dataSource.isClosed());
+ }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResourceTest.java b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/fixture/MockedDataSourcePoolFieldMetaData.java
similarity index 52%
copy from shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResourceTest.java
copy to shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/fixture/MockedDataSourcePoolFieldMetaData.java
index 3b4bba1f031..76bce640d74 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResourceTest.java
+++ b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/fixture/MockedDataSourcePoolFieldMetaData.java
@@ -15,26 +15,29 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.metadata.database.resource;
+package org.apache.shardingsphere.mode.metadata.persist.fixture;
-import org.apache.shardingsphere.test.mock.MockedDataSource;
-import org.junit.Test;
+import org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolFieldMetaData;
-import java.io.IOException;
-import java.sql.SQLException;
-import java.util.Collections;
-
-import static org.junit.Assert.assertTrue;
-
-public final class ShardingSphereResourceTest {
+public final class MockedDataSourcePoolFieldMetaData implements DataSourcePoolFieldMetaData {
+
+ @Override
+ public String getUsernameFieldName() {
+ return "username";
+ }
+
+ @Override
+ public String getPasswordFieldName() {
+ return "password";
+ }
+
+ @Override
+ public String getJdbcUrlFieldName() {
+ return "url";
+ }
- @Test
- public void assertClose() throws SQLException, IOException, InterruptedException {
- MockedDataSource dataSource = new MockedDataSource();
- new ShardingSphereResource(Collections.singletonMap("foo_ds", dataSource)).close(dataSource);
- while (null == dataSource.getClosed() || !dataSource.getClosed()) {
- Thread.sleep(10L);
- }
- assertTrue(dataSource.getClosed());
+ @Override
+ public String getJdbcUrlPropertiesFieldName() {
+ return "jdbcUrlProperties";
}
}
diff --git a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/fixture/MockedDataSourcePoolMetaData.java b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/fixture/MockedDataSourcePoolMetaData.java
new file mode 100644
index 00000000000..f4330b58ad9
--- /dev/null
+++ b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/fixture/MockedDataSourcePoolMetaData.java
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mode.metadata.persist.fixture;
+
+import org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolFieldMetaData;
+import org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaData;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+public final class MockedDataSourcePoolMetaData implements DataSourcePoolMetaData {
+
+ @Override
+ public Map<String, Object> getDefaultProperties() {
+ return Collections.singletonMap("maxPoolSize", 100);
+ }
+
+ @Override
+ public Map<String, Object> getInvalidProperties() {
+ Map<String, Object> result = new HashMap<>(2, 1);
+ result.put("maxPoolSize", -1);
+ result.put("minPoolSize", -1);
+ return result;
+ }
+
+ @Override
+ public Map<String, String> getPropertySynonyms() {
+ Map<String, String> result = new HashMap<>(2, 1);
+ result.put("maxPoolSize", "maxPoolSize");
+ result.put("minPoolSize", "minPoolSize");
+ return result;
+ }
+
+ @Override
+ public Collection<String> getTransientFieldNames() {
+ return Collections.singleton("closed");
+ }
+
+ @Override
+ public DataSourcePoolFieldMetaData getFieldMetaData() {
+ return new MockedDataSourcePoolFieldMetaData();
+ }
+
+ @Override
+ public String getType() {
+ return "org.apache.shardingsphere.test.mock.MockedDataSource";
+ }
+}
diff --git a/shardingsphere-mode/shardingsphere-mode-core/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaData b/shardingsphere-mode/shardingsphere-mode-core/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaData
new file mode 100644
index 00000000000..33af3f7897f
--- /dev/null
+++ b/shardingsphere-mode/shardingsphere-mode-core/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaData
@@ -0,0 +1,18 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+org.apache.shardingsphere.mode.metadata.persist.fixture.MockedDataSourcePoolMetaData
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResourceTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/fixture/MockedDataSourcePoolFieldMetaData.java
similarity index 52%
copy from shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResourceTest.java
copy to shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/fixture/MockedDataSourcePoolFieldMetaData.java
index 3b4bba1f031..40fea341a32 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResourceTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/fixture/MockedDataSourcePoolFieldMetaData.java
@@ -15,26 +15,29 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.metadata.database.resource;
+package org.apache.shardingsphere.proxy.backend.text.distsql.fixture;
-import org.apache.shardingsphere.test.mock.MockedDataSource;
-import org.junit.Test;
+import org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolFieldMetaData;
-import java.io.IOException;
-import java.sql.SQLException;
-import java.util.Collections;
-
-import static org.junit.Assert.assertTrue;
-
-public final class ShardingSphereResourceTest {
+public final class MockedDataSourcePoolFieldMetaData implements DataSourcePoolFieldMetaData {
+
+ @Override
+ public String getUsernameFieldName() {
+ return "username";
+ }
+
+ @Override
+ public String getPasswordFieldName() {
+ return "password";
+ }
+
+ @Override
+ public String getJdbcUrlFieldName() {
+ return "url";
+ }
- @Test
- public void assertClose() throws SQLException, IOException, InterruptedException {
- MockedDataSource dataSource = new MockedDataSource();
- new ShardingSphereResource(Collections.singletonMap("foo_ds", dataSource)).close(dataSource);
- while (null == dataSource.getClosed() || !dataSource.getClosed()) {
- Thread.sleep(10L);
- }
- assertTrue(dataSource.getClosed());
+ @Override
+ public String getJdbcUrlPropertiesFieldName() {
+ return "jdbcUrlProperties";
}
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/fixture/MockedDataSourcePoolMetaData.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/fixture/MockedDataSourcePoolMetaData.java
new file mode 100644
index 00000000000..05dcdc3dc2e
--- /dev/null
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/fixture/MockedDataSourcePoolMetaData.java
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.proxy.backend.text.distsql.fixture;
+
+import org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolFieldMetaData;
+import org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaData;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+public final class MockedDataSourcePoolMetaData implements DataSourcePoolMetaData {
+
+ @Override
+ public Map<String, Object> getDefaultProperties() {
+ return Collections.singletonMap("maxPoolSize", 100);
+ }
+
+ @Override
+ public Map<String, Object> getInvalidProperties() {
+ Map<String, Object> result = new HashMap<>(2, 1);
+ result.put("maxPoolSize", -1);
+ result.put("minPoolSize", -1);
+ return result;
+ }
+
+ @Override
+ public Map<String, String> getPropertySynonyms() {
+ Map<String, String> result = new HashMap<>(2, 1);
+ result.put("maxPoolSize", "maxPoolSize");
+ result.put("minPoolSize", "minPoolSize");
+ return result;
+ }
+
+ @Override
+ public Collection<String> getTransientFieldNames() {
+ return Collections.singleton("closed");
+ }
+
+ @Override
+ public DataSourcePoolFieldMetaData getFieldMetaData() {
+ return new MockedDataSourcePoolFieldMetaData();
+ }
+
+ @Override
+ public String getType() {
+ return "org.apache.shardingsphere.test.mock.MockedDataSource";
+ }
+}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaData b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaData
new file mode 100644
index 00000000000..7cad279d417
--- /dev/null
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaData
@@ -0,0 +1,18 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+org.apache.shardingsphere.proxy.backend.text.distsql.fixture.MockedDataSourcePoolMetaData
diff --git a/shardingsphere-test/shardingsphere-test-common/src/main/java/org/apache/shardingsphere/test/mock/MockedDataSource.java b/shardingsphere-test/shardingsphere-test-common/src/main/java/org/apache/shardingsphere/test/mock/MockedDataSource.java
index 66c8893dff0..9347514d085 100644
--- a/shardingsphere-test/shardingsphere-test-common/src/main/java/org/apache/shardingsphere/test/mock/MockedDataSource.java
+++ b/shardingsphere-test/shardingsphere-test-common/src/main/java/org/apache/shardingsphere/test/mock/MockedDataSource.java
@@ -64,7 +64,7 @@ public final class MockedDataSource implements DataSource, AutoCloseable {
private Connection connection;
@Setter(AccessLevel.NONE)
- private Boolean closed;
+ private boolean closed;
public MockedDataSource(final String url, final String username, final String password) {
this.url = url;