Skip to content

Commit

Permalink
add support for test parallel run
Browse files Browse the repository at this point in the history
  • Loading branch information
tianhao960 committed Sep 9, 2022
1 parent b7770e7 commit ac25cb0
Show file tree
Hide file tree
Showing 10 changed files with 384 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.parallel;

import org.junit.runners.Parameterized;

/**
* parallel runner for parameterize test suite.
*/
public class ParallelParameterized extends Parameterized {

// CHECKSTYLE:OFF
public ParallelParameterized(final Class<?> klass) throws Throwable {
// CHECKSTYLE:ON
super(klass);
setScheduler(new ParallelScheduler());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.parallel;

import org.junit.runners.model.RunnerScheduler;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
* parallel scheduler for junit.
*/
public class ParallelScheduler implements RunnerScheduler {

private ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

@Override
public void schedule(final Runnable childStatement) {
executorService.submit(childStatement);
}

@Override
public void finished() {
executorService.shutdown();
try {
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.runner;


import org.apache.shardingsphere.test.runner.parallel.ParallelRunnerScheduler;
import org.apache.shardingsphere.test.runner.parallel.annotaion.ParallelRuntimeStrategy;
import org.junit.runners.Parameterized;

/**
* ShardingSphere integration test parameterized.
*/
public final class ShardingSphereParallelTestParameterized extends Parameterized {

// CHECKSTYLE:OFF
public ShardingSphereParallelTestParameterized(final Class<?> clazz) throws Throwable {
// CHECKSTYLE:ON
super(clazz);
ParallelRuntimeStrategy parallelRuntimeStrategy = clazz.getAnnotation(ParallelRuntimeStrategy.class);
if (null != parallelRuntimeStrategy) {
setScheduler(new ParallelRunnerScheduler(parallelRuntimeStrategy.value()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.runner.parallel;

import org.apache.shardingsphere.test.parallel.ParallelScheduler;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;

/**
* parallel runner for junit.
*/
public class ParallelRunner extends BlockJUnit4ClassRunner {

/**
* Creates a ParallelRunner to run {@code klass}.
* If you now annotate a test-class with @RunWith(ParallelRunner.class) each method will run within its own thread.
*
* @param klass test class
* @throws InitializationError if the test class is malformed.
*/
public ParallelRunner(final Class<?> klass) throws InitializationError {
super(klass);
setScheduler(new ParallelScheduler());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.runner.parallel;

/**
* Parallel runner executor.
*/
public interface ParallelRunnerExecutor {

/**
* Execute child statement.
*
* @param childStatement child statement
*/
void execute(Runnable childStatement);

/**
* Override to implement any behavior that must occur after all children have been scheduled (for example, waiting for them all to finish).
*/
void finished();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.runner.parallel;

import org.apache.shardingsphere.test.runner.parallel.annotaion.ParallelLevel;
import org.apache.shardingsphere.test.runner.parallel.impl.DefaultParallelRunnerExecutor;

/**
* Parallel runner executor factory.
*/
public final class ParallelRunnerExecutorFactory {

/**
* Get parallel runner executor.
*
* @param parallelLevel parallel level
* @return parallel runner executor
*/
public ParallelRunnerExecutor getExecutor(final ParallelLevel parallelLevel) {
switch (parallelLevel) {
case DEFAULT:
return new DefaultParallelRunnerExecutor();
default:
throw new UnsupportedOperationException("Unsupported runtime strategy.");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.runner.parallel;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.test.runner.parallel.annotaion.ParallelLevel;
import org.junit.runners.model.RunnerScheduler;

/**
* Parallel runner scheduler.
*/
@RequiredArgsConstructor
public final class ParallelRunnerScheduler implements RunnerScheduler {

private final ParallelLevel parallelLevel;

private final ParallelRunnerExecutorFactory executorFactory = new ParallelRunnerExecutorFactory();

@Override
public void schedule(final Runnable childStatement) {
executorFactory.getExecutor(parallelLevel).execute(childStatement);
}

@Override
public void finished() {
executorFactory.getAllExecutors().forEach(ParallelRunnerExecutor::finished);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.runner.parallel.annotaion;

/**
* Parallel level.
*/
public enum ParallelLevel {

CASE, SCENARIO, DEFAULT
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.runner.parallel.annotaion;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Parallel runtime strategy.
*/
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface ParallelRuntimeStrategy {

/**
* Get parallel level.
*
* @return value parallel level
*/
ParallelLevel value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.apache.shardingsphere.test.runner.parallel.impl;


import org.apache.shardingsphere.test.runner.parallel.ParallelRunnerExecutor;
import java.util.Collection;
import java.util.LinkedList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class DefaultParallelRunnerExecutor implements ParallelRunnerExecutor {

private final Collection<Future<?>> taskFeatures = new LinkedList<>();

public ExecutorService getExecuteService(){
return Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
}

@Override
public void execute(final Runnable childStatement) {
taskFeatures.add(getExecuteService().submit(childStatement));
}

@Override
public void finished() {
taskFeatures.forEach(each -> {
try {
each.get();
} catch (final InterruptedException | ExecutionException ignored) {
}
});
getExecuteService().shutdownNow();
}
}

0 comments on commit ac25cb0

Please sign in to comment.