You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2020/10/09 11:21:27 UTC
[shardingsphere] branch master updated: Optimize
HttpServerHandlerTest. (#7718)
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 478fcdd Optimize HttpServerHandlerTest. (#7718)
478fcdd is described below
commit 478fcdd9d5a1666f2aa41ec351629573a1384b4b
Author: 邱鹿 Lucas <lu...@163.com>
AuthorDate: Fri Oct 9 19:21:02 2020 +0800
Optimize HttpServerHandlerTest. (#7718)
Co-authored-by: qiulu3 <Lucas209910>
---
.../scaling/fixture/FixtureH2JDBCDumper.java | 38 ++++++++++++++++
.../scaling/fixture/FixtureH2ScalingEntry.java | 4 +-
.../scaling/fixture/FixtureNopImporter.java | 51 ++++++++++++++++++++++
.../scaling/web/HttpServerHandlerTest.java | 26 +++++++++++
.../src/test/resources/config.json | 2 +-
.../config_sharding_sphere_jdbc_target.json | 4 +-
6 files changed, 120 insertions(+), 5 deletions(-)
diff --git a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixtureH2JDBCDumper.java b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixtureH2JDBCDumper.java
new file mode 100644
index 0000000..09b736a
--- /dev/null
+++ b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixtureH2JDBCDumper.java
@@ -0,0 +1,38 @@
+/*
+ * 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.scaling.fixture;
+
+import org.apache.shardingsphere.scaling.core.config.InventoryDumperConfiguration;
+import org.apache.shardingsphere.scaling.core.datasource.DataSourceManager;
+import org.apache.shardingsphere.scaling.core.execute.executor.dumper.AbstractJDBCDumper;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+
+public final class FixtureH2JDBCDumper extends AbstractJDBCDumper {
+
+ public FixtureH2JDBCDumper(final InventoryDumperConfiguration dumperConfig, final DataSourceManager dataSourceManager) {
+ super(dumperConfig, dataSourceManager);
+ }
+
+ @Override
+ protected PreparedStatement createPreparedStatement(final Connection conn, final String sql) throws SQLException {
+ return conn.prepareStatement(sql);
+ }
+}
diff --git a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixtureH2ScalingEntry.java b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixtureH2ScalingEntry.java
index 8fc2971..bd31dc9 100644
--- a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixtureH2ScalingEntry.java
+++ b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixtureH2ScalingEntry.java
@@ -30,7 +30,7 @@ public final class FixtureH2ScalingEntry implements ScalingEntry {
@Override
public Class<? extends JDBCDumper> getJdbcDumperClass() {
- return null;
+ return FixtureH2JDBCDumper.class;
}
@Override
@@ -45,7 +45,7 @@ public final class FixtureH2ScalingEntry implements ScalingEntry {
@Override
public Class<? extends Importer> getImporterClass() {
- return null;
+ return FixtureNopImporter.class;
}
@Override
diff --git a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixtureNopImporter.java b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixtureNopImporter.java
new file mode 100644
index 0000000..4119c7d
--- /dev/null
+++ b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixtureNopImporter.java
@@ -0,0 +1,51 @@
+/*
+ * 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.scaling.fixture;
+
+import org.apache.shardingsphere.scaling.core.config.ImporterConfiguration;
+import org.apache.shardingsphere.scaling.core.datasource.DataSourceManager;
+import org.apache.shardingsphere.scaling.core.execute.executor.channel.Channel;
+import org.apache.shardingsphere.scaling.core.execute.executor.importer.Importer;
+
+public final class FixtureNopImporter implements Importer {
+
+ public FixtureNopImporter(final ImporterConfiguration importerConfig, final DataSourceManager dataSourceManager) {
+ }
+
+ @Override
+ public void setChannel(final Channel channel) {
+ }
+
+ @Override
+ public void write() {
+ }
+
+ @Override
+ public void start() {
+ }
+
+ @Override
+ public void stop() {
+ }
+
+ @Override
+ public void run() {
+ start();
+ write();
+ }
+}
diff --git a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/web/HttpServerHandlerTest.java b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/web/HttpServerHandlerTest.java
index 2613e74..1f44933 100644
--- a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/web/HttpServerHandlerTest.java
+++ b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/web/HttpServerHandlerTest.java
@@ -27,9 +27,14 @@ import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.CharsetUtil;
+import lombok.SneakyThrows;
import org.apache.shardingsphere.scaling.core.config.ScalingConfiguration;
import org.apache.shardingsphere.scaling.core.config.ScalingContext;
+import org.apache.shardingsphere.scaling.core.config.ScalingDataSourceConfiguration;
import org.apache.shardingsphere.scaling.core.config.ServerConfiguration;
+import org.apache.shardingsphere.scaling.core.config.SyncConfiguration;
+import org.apache.shardingsphere.scaling.core.datasource.DataSourceManager;
+import org.apache.shardingsphere.scaling.core.utils.SyncConfigurationUtil;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -37,8 +42,12 @@ import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
+import javax.sql.DataSource;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
@@ -61,11 +70,15 @@ public final class HttpServerHandlerTest {
private ScalingConfiguration scalingConfig;
+ private SyncConfiguration syncConfiguration;
+
@Before
public void setUp() {
initConfig("/config.json");
ScalingContext.getInstance().init(new ServerConfiguration());
httpServerHandler = new HttpServerHandler();
+ initTableData(syncConfiguration.getDumperConfiguration().getDataSourceConfiguration());
+ initTableData(syncConfiguration.getImporterConfiguration().getDataSourceConfiguration());
}
@Test
@@ -168,5 +181,18 @@ public final class HttpServerHandlerTest {
InputStream fileInputStream = HttpServerHandlerTest.class.getResourceAsStream(configFile);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
scalingConfig = GSON.fromJson(inputStreamReader, ScalingConfiguration.class);
+ syncConfiguration = SyncConfigurationUtil.toSyncConfigurations(scalingConfig).iterator().next();
+ }
+
+ @SneakyThrows(SQLException.class)
+ private void initTableData(final ScalingDataSourceConfiguration dataSourceConfig) {
+ DataSource dataSource = new DataSourceManager().getDataSource(dataSourceConfig);
+ try (Connection connection = dataSource.getConnection();
+ Statement statement = connection.createStatement()) {
+ statement.execute("DROP TABLE IF EXISTS t1");
+ statement.execute("CREATE TABLE t1 (id INT PRIMARY KEY, user_id VARCHAR(12))");
+ statement.execute("DROP TABLE IF EXISTS t2");
+ statement.execute("CREATE TABLE t2 (id INT PRIMARY KEY, user_id VARCHAR(12))");
+ }
}
}
diff --git a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/resources/config.json b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/resources/config.json
index 4eb01bd..28fcf97 100644
--- a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/resources/config.json
+++ b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/resources/config.json
@@ -20,7 +20,7 @@
"source": {
"type": "shardingSphereJdbc",
"parameter": {
- "dataSource": "dataSources:\n ds_0:\n dataSourceClassName: com.zaxxer.hikari.HikariDataSource\n props:\n jdbcUrl: jdbc:h2:mem:test_db_2;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL\n username: root\n password: '123456'\n connectionTimeout: 30000\n idleTimeout: 60000\n maxLifetime: 1800000\n maxPoolSize: 50\n minPoolSize: 1\n maintenanceIntervalMilliseconds: 30000\n readOnly: false\n",
+ "dataSource": "dataSources:\n ds_0:\n dataSourceClassName: com.zaxxer.hikari.HikariDataSource\n props:\n jdbcUrl: jdbc:h2:mem:test_db_2;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL\n username: root\n password: 'password'\n connectionTimeout: 30000\n idleTimeout: 60000\n maxLifetime: 1800000\n maxPoolSize: 50\n minPoolSize: 1\n maintenanceIntervalMilliseconds: 30000\n readOnly: false\n",
"rule": "rules:\n- !SHARDING\n defaultDatabaseStrategy:\n standard:\n shardingAlgorithmName: inline\n shardingColumn: user_id\n tables:\n t1:\n actualDataNodes: ds_0.t1\n keyGenerateStrategy:\n column: order_id\n logicTable: t1\n tableStrategy:\n standard:\n shardingAlgorithmName: inline\n shardingColumn: order_id\n t2:\n actualDataNodes: ds_0.t2\n keyGenerateStrategy:\n column: order_item_i [...]
}
},
diff --git a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/resources/config_sharding_sphere_jdbc_target.json b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/resources/config_sharding_sphere_jdbc_target.json
index 096ebf2..df589d9 100644
--- a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/resources/config_sharding_sphere_jdbc_target.json
+++ b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/resources/config_sharding_sphere_jdbc_target.json
@@ -20,14 +20,14 @@
"source": {
"type": "shardingSphereJdbc",
"parameter": {
- "dataSource": "dataSources:\n ds_0:\n dataSourceClassName: com.zaxxer.hikari.HikariDataSource\n props:\n jdbcUrl: jdbc:h2:mem:test_db_2;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL\n username: root\n password: '123456'\n connectionTimeout: 30000\n idleTimeout: 60000\n maxLifetime: 1800000\n maxPoolSize: 50\n minPoolSize: 1\n maintenanceIntervalMilliseconds: 30000\n readOnly: false\n",
+ "dataSource": "dataSources:\n ds_0:\n dataSourceClassName: com.zaxxer.hikari.HikariDataSource\n props:\n jdbcUrl: jdbc:h2:mem:test_db_2;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL\n username: root\n password: 'password'\n connectionTimeout: 30000\n idleTimeout: 60000\n maxLifetime: 1800000\n maxPoolSize: 50\n minPoolSize: 1\n maintenanceIntervalMilliseconds: 30000\n readOnly: false\n",
"rule": "rules:\n- !SHARDING\n defaultDatabaseStrategy:\n standard:\n shardingAlgorithmName: inline\n shardingColumn: user_id\n tables:\n t1:\n actualDataNodes: ds_0.t1\n keyGenerateStrategy:\n column: order_id\n logicTable: t1\n tableStrategy:\n standard:\n shardingAlgorithmName: inline\n shardingColumn: order_id\n t2:\n actualDataNodes: ds_0.t2\n keyGenerateStrategy:\n column: order_item_i [...]
}
},
"target": {
"type": "shardingSphereJdbc",
"parameter": {
- "dataSource": "dataSources:\n ds_0:\n dataSourceClassName: com.zaxxer.hikari.HikariDataSource\n props:\n jdbcUrl: jdbc:h2:mem:test_db_2;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL\n username: root\n password: '123456'\n connectionTimeout: 30000\n idleTimeout: 60000\n maxLifetime: 1800000\n maxPoolSize: 50\n minPoolSize: 1\n maintenanceIntervalMilliseconds: 30000\n readOnly: false\n",
+ "dataSource": "dataSources:\n ds_0:\n dataSourceClassName: com.zaxxer.hikari.HikariDataSource\n props:\n jdbcUrl: jdbc:h2:mem:test_db_2;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL\n username: root\n password: 'password'\n connectionTimeout: 30000\n idleTimeout: 60000\n maxLifetime: 1800000\n maxPoolSize: 50\n minPoolSize: 1\n maintenanceIntervalMilliseconds: 30000\n readOnly: false\n",
"rule": "rules:\n- !SHARDING\n defaultDatabaseStrategy:\n standard:\n shardingAlgorithmName: inline\n shardingColumn: user_id\n tables:\n t1:\n actualDataNodes: ds_0.t1\n keyGenerateStrategy:\n column: order_id\n logicTable: t1\n tableStrategy:\n standard:\n shardingAlgorithmName: inline\n shardingColumn: order_id\n t2:\n actualDataNodes: ds_0.t2\n keyGenerateStrategy:\n column: order_item_i [...]
}
}