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 2021/02/19 11:15:39 UTC
[shardingsphere] branch master updated: Refactor junit.parallel
package for IT (#9439)
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 c4ccbc1 Refactor junit.parallel package for IT (#9439)
c4ccbc1 is described below
commit c4ccbc1bc857516040aa64f913fc41c7245a0766
Author: Liang Zhang <te...@163.com>
AuthorDate: Fri Feb 19 19:15:22 2021 +0800
Refactor junit.parallel package for IT (#9439)
* Refactor ITRunnerScenariosExecutor
* Remove ITRunnerSerialExecutor
* Rename ITRunnerParallelExecutor
* Rename ITRunnerParallelExecutor
* Rename ParallelParameterized
* Rename ParallelRunnerScheduler
* Add junit.parallel package
* Refactor ParallelRunnerScheduler
* Refactor ParallelRunnerScheduler
---
.../test/integration/engine/it/BaseIT.java | 4 +-
.../engine/junit/ITRunnerScheduler.java | 114 ---------------------
.../engine/junit/impl/ITRunnerSerialExecutor.java | 36 -------
.../ParallelParameterized.java} | 10 +-
.../ParallelRunnerExecutor.java} | 6 +-
.../junit/parallel/ParallelRunnerScheduler.java | 98 ++++++++++++++++++
.../impl/CaseParallelRunnerExecutor.java} | 8 +-
.../impl/ScenarioParallelRunnerExecutor.java} | 90 ++++++++--------
8 files changed, 154 insertions(+), 212 deletions(-)
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/BaseIT.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/BaseIT.java
index 6e07d6c..a202296 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/BaseIT.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/BaseIT.java
@@ -22,7 +22,7 @@ import lombok.Getter;
import org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory;
import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
-import org.apache.shardingsphere.test.integration.engine.junit.ITParameterized;
+import org.apache.shardingsphere.test.integration.engine.junit.parallel.ParallelParameterized;
import org.apache.shardingsphere.test.integration.env.EnvironmentPath;
import org.apache.shardingsphere.test.integration.env.EnvironmentType;
import org.apache.shardingsphere.test.integration.env.IntegrationTestEnvironment;
@@ -41,7 +41,7 @@ import java.sql.SQLException;
import java.util.Map;
import java.util.TimeZone;
-@RunWith(ITParameterized.class)
+@RunWith(ParallelParameterized.class)
@Getter(AccessLevel.PROTECTED)
public abstract class BaseIT {
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/ITRunnerScheduler.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/ITRunnerScheduler.java
deleted file mode 100644
index 363e478..0000000
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/ITRunnerScheduler.java
+++ /dev/null
@@ -1,114 +0,0 @@
-/*
- * 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.test.integration.engine.junit;
-
-import lombok.SneakyThrows;
-import org.apache.shardingsphere.infra.database.type.DatabaseType;
-import org.apache.shardingsphere.infra.database.type.dialect.PostgreSQLDatabaseType;
-import org.apache.shardingsphere.test.integration.cases.SQLCommandType;
-import org.apache.shardingsphere.test.integration.engine.junit.impl.ITRunnerParallelExecutor;
-import org.apache.shardingsphere.test.integration.engine.junit.impl.ITRunnerScenariosExecutor;
-import org.apache.shardingsphere.test.integration.engine.junit.impl.ITRunnerSerialExecutor;
-import org.apache.shardingsphere.test.integration.engine.param.model.ParameterizedArray;
-import org.apache.shardingsphere.test.integration.env.IntegrationTestEnvironment;
-import org.junit.runners.model.RunnerScheduler;
-import org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters;
-
-import java.lang.reflect.Field;
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * Represents a strategy for scheduling when individual test methods should be run (in serial or parallel).
- *
- * <p>
- * WARNING: still experimental, may go away.
- * </p>
- */
-public final class ITRunnerScheduler implements RunnerScheduler {
-
- private final Field parametersField;
-
- private final Map<String, ITRunnerExecutor> runnerExecutors;
-
- private volatile Field runnerField;
-
- public ITRunnerScheduler() {
- parametersField = getParametersField();
- runnerExecutors = getITRunnerExecutors();
- }
-
- @SneakyThrows(NoSuchFieldException.class)
- private Field getParametersField() {
- Field result = BlockJUnit4ClassRunnerWithParameters.class.getDeclaredField("parameters");
- result.setAccessible(true);
- return result;
- }
-
- private Map<String, ITRunnerExecutor> getITRunnerExecutors() {
- Map<String, ITRunnerExecutor> result = new HashMap<>(IntegrationTestEnvironment.getInstance().getDataSourceEnvironments().size() * 3, 1);
- for (DatabaseType each : IntegrationTestEnvironment.getInstance().getDataSourceEnvironments().keySet()) {
- result.put(getITRunnerExecutorKey(each.getName(), SQLCommandType.DQL.name()), new ITRunnerParallelExecutor());
- if (each instanceof PostgreSQLDatabaseType) {
- result.put(getITRunnerExecutorKey(each.getName(), SQLCommandType.DDL.name()), new ITRunnerSerialExecutor());
- } else {
- result.put(getITRunnerExecutorKey(each.getName(), SQLCommandType.DDL.name()), new ITRunnerScenariosExecutor());
- }
- result.put(getITRunnerExecutorKey(each.getName(), ""), new ITRunnerScenariosExecutor());
- }
- return result;
- }
-
- private String getITRunnerExecutorKey(final String databaseType, final String sqlCommandType) {
- return String.join("_", databaseType, sqlCommandType);
- }
-
- @Override
- public void schedule(final Runnable childStatement) {
- Object[] parameters = getITParameters(childStatement);
- ParameterizedArray parameterizedArray = (ParameterizedArray) parameters[0];
- getITRunnerExecutor(parameterizedArray).execute(parameterizedArray, childStatement);
- }
-
- @SneakyThrows(ReflectiveOperationException.class)
- private Object[] getITParameters(final Runnable childStatement) {
- if (null == runnerField) {
- runnerField = childStatement.getClass().getDeclaredField("val$each");
- runnerField.setAccessible(true);
- }
- return (Object[]) parametersField.get(runnerField.get(childStatement));
- }
-
- private ITRunnerExecutor getITRunnerExecutor(final ParameterizedArray parameterizedArray) {
- switch (parameterizedArray.getSqlCommandType()) {
- case DQL:
- return runnerExecutors.get(getITRunnerExecutorKey(parameterizedArray.getDatabaseType().getName(), SQLCommandType.DQL.name()));
- case DDL:
- return runnerExecutors.get(getITRunnerExecutorKey(parameterizedArray.getDatabaseType().getName(), SQLCommandType.DDL.name()));
- default:
- return runnerExecutors.get(getITRunnerExecutorKey(parameterizedArray.getDatabaseType().getName(), ""));
- }
- }
-
- @Override
- public void finished() {
- if (null != runnerExecutors) {
- runnerExecutors.values().forEach(ITRunnerExecutor::finished);
- }
- }
-}
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/impl/ITRunnerSerialExecutor.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/impl/ITRunnerSerialExecutor.java
deleted file mode 100644
index 86e5584..0000000
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/impl/ITRunnerSerialExecutor.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * 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.test.integration.engine.junit.impl;
-
-import org.apache.shardingsphere.test.integration.engine.junit.ITRunnerExecutor;
-import org.apache.shardingsphere.test.integration.engine.param.model.ParameterizedArray;
-
-/**
- * IT runner serial executor.
- */
-public final class ITRunnerSerialExecutor implements ITRunnerExecutor {
-
- @Override
- public void execute(final ParameterizedArray parameterizedArray, final Runnable childStatement) {
- childStatement.run();
- }
-
- @Override
- public void finished() {
- }
-}
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/ITParameterized.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/parallel/ParallelParameterized.java
similarity index 76%
rename from shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/ITParameterized.java
rename to shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/parallel/ParallelParameterized.java
index d0b3cf9..acd5729 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/ITParameterized.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/parallel/ParallelParameterized.java
@@ -15,19 +15,19 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.test.integration.engine.junit;
+package org.apache.shardingsphere.test.integration.engine.junit.parallel;
import org.junit.runners.Parameterized;
/**
- * Integration test parameterized.
+ * Parallel parameterized.
*/
-public final class ITParameterized extends Parameterized {
+public final class ParallelParameterized extends Parameterized {
//CHECKSTYLE:OFF
- public ITParameterized(final Class<?> klass) throws Throwable {
+ public ParallelParameterized(final Class<?> klass) throws Throwable {
//CHECKSTYLE:ON
super(klass);
- setScheduler(new ITRunnerScheduler());
+ setScheduler(new ParallelRunnerScheduler());
}
}
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/ITRunnerExecutor.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/parallel/ParallelRunnerExecutor.java
similarity index 90%
rename from shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/ITRunnerExecutor.java
rename to shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/parallel/ParallelRunnerExecutor.java
index 73a94f9..c5d8577 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/ITRunnerExecutor.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/parallel/ParallelRunnerExecutor.java
@@ -15,14 +15,14 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.test.integration.engine.junit;
+package org.apache.shardingsphere.test.integration.engine.junit.parallel;
import org.apache.shardingsphere.test.integration.engine.param.model.ParameterizedArray;
/**
- * IT runner executor.
+ * Parallel runner executor.
*/
-public interface ITRunnerExecutor {
+public interface ParallelRunnerExecutor {
/**
* Execute child statement.
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/parallel/ParallelRunnerScheduler.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/parallel/ParallelRunnerScheduler.java
new file mode 100644
index 0000000..1523987
--- /dev/null
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/parallel/ParallelRunnerScheduler.java
@@ -0,0 +1,98 @@
+/*
+ * 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.test.integration.engine.junit.parallel;
+
+import lombok.SneakyThrows;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import org.apache.shardingsphere.test.integration.cases.SQLCommandType;
+import org.apache.shardingsphere.test.integration.engine.junit.parallel.impl.CaseParallelRunnerExecutor;
+import org.apache.shardingsphere.test.integration.engine.junit.parallel.impl.ScenarioParallelRunnerExecutor;
+import org.apache.shardingsphere.test.integration.engine.param.model.ParameterizedArray;
+import org.apache.shardingsphere.test.integration.env.IntegrationTestEnvironment;
+import org.junit.runners.model.RunnerScheduler;
+import org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters;
+
+import java.lang.reflect.Field;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Parallel runner scheduler.
+ */
+public final class ParallelRunnerScheduler implements RunnerScheduler {
+
+ private final Field parametersField;
+
+ private final Map<String, ParallelRunnerExecutor> runnerExecutors;
+
+ private volatile Field runnerField;
+
+ public ParallelRunnerScheduler() {
+ parametersField = getParametersField();
+ runnerExecutors = getRunnerExecutors();
+ }
+
+ @SneakyThrows(NoSuchFieldException.class)
+ private Field getParametersField() {
+ Field result = BlockJUnit4ClassRunnerWithParameters.class.getDeclaredField("parameters");
+ result.setAccessible(true);
+ return result;
+ }
+
+ private Map<String, ParallelRunnerExecutor> getRunnerExecutors() {
+ Map<String, ParallelRunnerExecutor> result = new HashMap<>(IntegrationTestEnvironment.getInstance().getDataSourceEnvironments().size() * 2, 1);
+ for (DatabaseType each : IntegrationTestEnvironment.getInstance().getDataSourceEnvironments().keySet()) {
+ result.put(getRunnerExecutorKey(each.getName(), SQLCommandType.DQL.name()), new CaseParallelRunnerExecutor());
+ result.put(getRunnerExecutorKey(each.getName(), ""), new ScenarioParallelRunnerExecutor());
+ }
+ return result;
+ }
+
+ private String getRunnerExecutorKey(final String databaseType, final String sqlCommandType) {
+ return String.join("_", databaseType, sqlCommandType);
+ }
+
+ @Override
+ public void schedule(final Runnable childStatement) {
+ Object[] parameters = getParameters(childStatement);
+ ParameterizedArray parameterizedArray = (ParameterizedArray) parameters[0];
+ getRunnerExecutor(parameterizedArray).execute(parameterizedArray, childStatement);
+ }
+
+ @SneakyThrows(ReflectiveOperationException.class)
+ private Object[] getParameters(final Runnable childStatement) {
+ if (null == runnerField) {
+ runnerField = childStatement.getClass().getDeclaredField("val$each");
+ runnerField.setAccessible(true);
+ }
+ return (Object[]) parametersField.get(runnerField.get(childStatement));
+ }
+
+ private ParallelRunnerExecutor getRunnerExecutor(final ParameterizedArray parameterizedArray) {
+ return SQLCommandType.DQL == parameterizedArray.getSqlCommandType()
+ ? runnerExecutors.get(getRunnerExecutorKey(parameterizedArray.getDatabaseType().getName(), SQLCommandType.DQL.name()))
+ : runnerExecutors.get(getRunnerExecutorKey(parameterizedArray.getDatabaseType().getName(), ""));
+ }
+
+ @Override
+ public void finished() {
+ if (null != runnerExecutors) {
+ runnerExecutors.values().forEach(ParallelRunnerExecutor::finished);
+ }
+ }
+}
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/impl/ITRunnerParallelExecutor.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/parallel/impl/CaseParallelRunnerExecutor.java
similarity index 88%
rename from shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/impl/ITRunnerParallelExecutor.java
rename to shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/parallel/impl/CaseParallelRunnerExecutor.java
index 246ba61..7b0eb9d 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/impl/ITRunnerParallelExecutor.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/parallel/impl/CaseParallelRunnerExecutor.java
@@ -15,20 +15,20 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.test.integration.engine.junit.impl;
+package org.apache.shardingsphere.test.integration.engine.junit.parallel.impl;
import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.executor.kernel.thread.ExecutorServiceManager;
-import org.apache.shardingsphere.test.integration.engine.junit.ITRunnerExecutor;
+import org.apache.shardingsphere.test.integration.engine.junit.parallel.ParallelRunnerExecutor;
import org.apache.shardingsphere.test.integration.engine.param.model.ParameterizedArray;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
/**
- * IT runner parallel executor.
+ * Parallel runner executor with case.
*/
-public final class ITRunnerParallelExecutor implements ITRunnerExecutor {
+public final class CaseParallelRunnerExecutor implements ParallelRunnerExecutor {
private final ExecutorServiceManager executorServiceManager = new ExecutorServiceManager(Runtime.getRuntime().availableProcessors() * 2 - 1);
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/impl/ITRunnerScenariosExecutor.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/parallel/impl/ScenarioParallelRunnerExecutor.java
similarity index 62%
rename from shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/impl/ITRunnerScenariosExecutor.java
rename to shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/parallel/impl/ScenarioParallelRunnerExecutor.java
index ae7922d..5ed950b 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/impl/ITRunnerScenariosExecutor.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/junit/parallel/impl/ScenarioParallelRunnerExecutor.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.test.integration.engine.junit.impl;
+package org.apache.shardingsphere.test.integration.engine.junit.parallel.impl;
import com.google.common.base.Charsets;
import com.google.common.hash.Hasher;
@@ -31,73 +31,67 @@ import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
-import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
-import org.apache.shardingsphere.test.integration.engine.junit.ITRunnerExecutor;
+import org.apache.shardingsphere.test.integration.engine.junit.parallel.ParallelRunnerExecutor;
import org.apache.shardingsphere.test.integration.engine.param.model.ParameterizedArray;
import org.apache.shardingsphere.test.integration.env.IntegrationTestEnvironment;
import java.util.Collection;
import java.util.LinkedList;
-import java.util.Set;
import java.util.concurrent.ThreadFactory;
+import java.util.stream.Collectors;
/**
- * IT runner scenarios executor.
+ * Parallel runner executor with scenario.
*/
@Slf4j
-public final class ITRunnerScenariosExecutor implements ITRunnerExecutor {
+public final class ScenarioParallelRunnerExecutor implements ParallelRunnerExecutor {
private final Disruptor<CaseEntryEvent> disruptor;
private final RingBuffer<CaseEntryEvent> ringBuffer;
- private final Collection<CaseEventHandler> caseEventHandlers;
-
- @SneakyThrows
- public ITRunnerScenariosExecutor() {
- EventFactory<CaseEntryEvent> eventFactory = CaseEntryEvent::new;
- ThreadFactory threadFactory = new ThreadFactoryBuilder().setDaemon(false).setNameFormat("disruptor-processors-%d")
- .setUncaughtExceptionHandler((t, e) -> log.error("disruptor handler thread exception", e)).build();
- disruptor = new Disruptor<>(eventFactory, 16384, threadFactory, ProducerType.SINGLE, new BlockingWaitStrategy());
- IntegrationTestEnvironment integrationTestEnvironment = IntegrationTestEnvironment.getInstance();
- Collection<String> adapters = integrationTestEnvironment.getAdapters();
- Collection<String> scenarios = integrationTestEnvironment.getScenarios();
- Set<DatabaseType> databaseTypes = integrationTestEnvironment.getDataSourceEnvironments().keySet();
- caseEventHandlers = new LinkedList<>();
- initCaseEventHandlers(adapters, scenarios, databaseTypes);
- CaseEventHandler[] caseEventHandlerArray = new CaseEventHandler[caseEventHandlers.size()];
- caseEventHandlers.toArray(caseEventHandlerArray);
- disruptor.handleEventsWith(caseEventHandlerArray).then(new CleanupEventHandler());
+ public ScenarioParallelRunnerExecutor() {
+ disruptor = createDisruptor();
+ disruptor.handleEventsWith(createEventHandlers()).then(new CleanupEventHandler());
disruptor.start();
ringBuffer = disruptor.getRingBuffer();
}
- private void initCaseEventHandlers(final Collection<String> adapters, final Collection<String> scenarios, final Set<DatabaseType> databaseTypes) {
- for (String each : adapters) {
- initCaseEventHandlers(each, scenarios, databaseTypes);
+ private Disruptor<CaseEntryEvent> createDisruptor() {
+ EventFactory<CaseEntryEvent> eventFactory = CaseEntryEvent::new;
+ ThreadFactory threadFactory = new ThreadFactoryBuilder()
+ .setDaemon(false).setNameFormat("disruptor-processors-%d").setUncaughtExceptionHandler((thread, ex) -> log.error("Disruptor handler thread exception", ex)).build();
+ return new Disruptor<>(eventFactory, 16384, threadFactory, ProducerType.SINGLE, new BlockingWaitStrategy());
+ }
+
+ private CaseEventHandler[] createEventHandlers() {
+ Collection<CaseEventHandler> result = new LinkedList<>();
+ for (String each : IntegrationTestEnvironment.getInstance().getAdapters()) {
+ result.addAll(createEventHandlers(each, IntegrationTestEnvironment.getInstance().getScenarios(), IntegrationTestEnvironment.getInstance().getDataSourceEnvironments().keySet()));
}
+ return result.toArray(new CaseEventHandler[0]);
}
- private void initCaseEventHandlers(final String adapter, final Collection<String> scenarios, final Set<DatabaseType> databaseTypes) {
+ private Collection<CaseEventHandler> createEventHandlers(final String adapter, final Collection<String> scenarios, final Collection<DatabaseType> databaseTypes) {
+ Collection<CaseEventHandler> result = new LinkedList<>();
for (String each : scenarios) {
- initCaseEventHandlers(adapter, each, databaseTypes);
+ result.addAll(createEventHandlers(adapter, each, databaseTypes));
}
+ return result;
}
- private void initCaseEventHandlers(final String adapter, final String scenario, final Collection<DatabaseType> databaseTypes) {
- for (DatabaseType each : databaseTypes) {
- caseEventHandlers.add(new CaseEventHandler(new CaseKey(adapter, scenario, each.getName()).hashCode()));
- }
+ private Collection<CaseEventHandler> createEventHandlers(final String adapter, final String scenario, final Collection<DatabaseType> databaseTypes) {
+ return databaseTypes.stream().map(each -> new CaseEventHandler(new CaseKey(adapter, scenario, each.getName()).hashCode())).collect(Collectors.toList());
}
@Override
public void execute(final ParameterizedArray parameterizedArray, final Runnable childStatement) {
- ringBuffer.publishEvent((e, seq) -> {
- e.reset();
- e.setCaseKey(new CaseKey(parameterizedArray.getAdapter(), parameterizedArray.getScenario(), parameterizedArray.getDatabaseType().getName()));
- e.setChildStatement(childStatement);
+ ringBuffer.publishEvent((event, sequence) -> {
+ event.reset();
+ event.setCaseKey(new CaseKey(parameterizedArray.getAdapter(), parameterizedArray.getScenario(), parameterizedArray.getDatabaseType().getName()));
+ event.setChildStatement(childStatement);
});
}
@@ -109,14 +103,14 @@ public final class ITRunnerScenariosExecutor implements ITRunnerExecutor {
}
@RequiredArgsConstructor
- public static final class CaseKey {
-
+ private static final class CaseKey {
+
private final String adapter;
-
+
private final String scenario;
-
+
private final String databaseTypeName;
-
+
@Override
public int hashCode() {
Hasher hasher = Hashing.murmur3_32().newHasher();
@@ -134,9 +128,9 @@ public final class ITRunnerScenariosExecutor implements ITRunnerExecutor {
private static final class CaseEntryEvent {
private CaseKey caseKey;
-
+
private Runnable childStatement;
-
+
@Override
public int hashCode() {
return caseKey.hashCode();
@@ -153,22 +147,22 @@ public final class ITRunnerScenariosExecutor implements ITRunnerExecutor {
private final int hashCode;
- private Sequence reportingSeq;
-
+ private Sequence reportingSequence;
+
@Override
public void onEvent(final CaseEntryEvent event, final long sequence, final boolean endOfBatch) {
if (null == event.caseKey || event.hashCode() != hashCode) {
return;
}
event.childStatement.run();
- if (null != reportingSeq) {
- reportingSeq.set(sequence);
+ if (null != reportingSequence) {
+ reportingSequence.set(sequence);
}
}
@Override
public void setSequenceCallback(final Sequence sequenceCallback) {
- this.reportingSeq = sequenceCallback;
+ reportingSequence = sequenceCallback;
}
}