
com.googlecode.openbox.testu.concurrent.ConcurrentTestRunner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of testu Show documentation
Show all versions of testu Show documentation
TestU is for common RESTful API test framework
package com.googlecode.openbox.testu.concurrent;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import com.googlecode.openbox.common.algorithm.Homogenizer;
import com.googlecode.openbox.common.algorithm.Homogenizer.Action;
import com.googlecode.openbox.common.context.BasicContext;
import com.googlecode.openbox.common.context.CommonContext;
import com.googlecode.openbox.http.Response;
import com.googlecode.openbox.http.monitors.CyclicBarrierMonitor;
import com.googlecode.openbox.testu.TestResult;
import com.googlecode.openbox.testu.TestUException;
public abstract class ConcurrentTestRunner {
private String runName;
public ConcurrentTestRunner(String runName) {
this.runName = runName;
}
public abstract void precondition(final CommonContext caseContext);
public abstract StepGroup[] getConcurrentStep(
final CommonContext caseContext);
public abstract ValidatorGroup[] getStepValidator(
CommonContext caseContext, Future[] results);
public abstract void validate(final CommonContext caseContext)
throws Exception;
public void start() throws Exception {
CommonContext caseContext = new BasicContext();
precondition(caseContext);
Action[] actions = getConcurrentStep(caseContext);
List tasks = Homogenizer.homogenize(actions);
if (null == tasks) {
throw TestUException
.create("concurrent test run "
+ runName
+ " error as getConcurrentStep is null, Please check related test case implentation ");
}
int threadNum = tasks.size();
if (threadNum <= 1) {
throw TestUException
.create("concurrent test "
+ runName
+ " case implentation invalidated as getConcurrentStep just return threadNum<=1");
}
ExecutorService executorService = Executors
.newFixedThreadPool(threadNum);
CyclicBarrierMonitor cyclicBarrierMonitor = CyclicBarrierMonitor
.create(threadNum);
@SuppressWarnings("unchecked")
Future[] results = new Future[threadNum];
for (int i = 0; i < threadNum; i++) {
ConcurrentStep task = tasks.get(i);
task.setCyclicBarrierMonitor(cyclicBarrierMonitor);
Future result = executorService.submit(task);
results[i] = result;
}
ValidatorGroup[] stepValidators = getStepValidator(caseContext, results);
List testResults = Homogenizer.homogenize(stepValidators);
TestResult.check(testResults);
validate(caseContext);
executorService.shutdown();
try {
executorService.awaitTermination(10, TimeUnit.HOURS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy