forked from apache/shardingsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b7770e7
commit ac25cb0
Showing
10 changed files
with
384 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
...t-common/src/main/java/org/apache/shardingsphere/test/parallel/ParallelParameterized.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...-test-common/src/main/java/org/apache/shardingsphere/test/parallel/ParallelScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...n/java/org/apache/shardingsphere/test/runner/ShardingSphereParallelTestParameterized.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...t-common/src/main/java/org/apache/shardingsphere/test/runner/parallel/ParallelRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
.../src/main/java/org/apache/shardingsphere/test/runner/parallel/ParallelRunnerExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
43 changes: 43 additions & 0 deletions
43
...in/java/org/apache/shardingsphere/test/runner/parallel/ParallelRunnerExecutorFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
...src/main/java/org/apache/shardingsphere/test/runner/parallel/ParallelRunnerScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...src/main/java/org/apache/shardingsphere/test/runner/parallel/annotaion/ParallelLevel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
42 changes: 42 additions & 0 deletions
42
...ava/org/apache/shardingsphere/test/runner/parallel/annotaion/ParallelRuntimeStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
35 changes: 35 additions & 0 deletions
35
...va/org/apache/shardingsphere/test/runner/parallel/impl/DefaultParallelRunnerExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |