Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed: @Test(timeOut) was causing threadPoolSize to be ignored

  • Loading branch information...
commit 349a0501482e3fada78e74a70eea8b8585bca308 1 parent dd1fdbf
Cedric Beust authored
1  CHANGES.txt
View
@@ -5,6 +5,7 @@ Added: @Guice(module)
Added: timeOut for configuration methods
Added: -randomizesuites (Nalin Makar)
Added: IConfigurable
+Fixed: @Test(timeOut) was causing threadPoolSize to be ignored
Fixed: TESTNG-468: Listeners defined in suite XML file are ignored (Michael Benz)
Fixed: TESTNG-465: Guice modules are bound individually to an injector meaning that multiple modules can't be effectively used (Danny Thomas)
Fixed: Method selectors from suites were not properly initialized (toddq)
74 src/main/java/org/testng/internal/MethodInvocationHelper.java
View
@@ -205,36 +205,60 @@ public void runTestMethod(ITestResult tr) {
protected static void invokeWithTimeout(ITestNGMethod tm, Object instance, Object[] parameterValues,
ITestResult testResult)
throws InterruptedException, ThreadExecutionException {
- IExecutor exec= ThreadUtil.createExecutor(1, tm.getMethod().getName());
+ if (tm.getThreadPoolSize() == 0) {
+ invokeWithTimeoutWithNewExecutor(tm, instance, parameterValues, testResult);
+ } else {
+ invokeWithTimeoutWithNoExecutor(tm, instance, parameterValues, testResult);
+ }
+ }
- InvokeMethodRunnable imr = new InvokeMethodRunnable(tm, instance, parameterValues);
- IFutureResult future= exec.submitRunnable(imr);
- exec.shutdown();
- long realTimeOut = MethodHelper.calculateTimeOut(tm);
- boolean finished = exec.awaitTermination(realTimeOut);
+ private static void invokeWithTimeoutWithNoExecutor(ITestNGMethod tm,
+ Object instance, Object[] parameterValues, ITestResult testResult) {
- if (! finished) {
- exec.stopNow();
- ThreadTimeoutException exception = new ThreadTimeoutException("Method "
- + tm.getClass().getName() + "." + tm.getMethodName() + "()"
- + " didn't finish within the time-out "
- + realTimeOut);
- exception.setStackTrace(exec.getStackTraces()[0]);
- testResult.setThrowable(exception);
- testResult.setStatus(ITestResult.FAILURE);
- }
- else {
- Utils.log("Invoker " + Thread.currentThread().hashCode(), 3,
- "Method " + tm.getMethod() + " completed within the time-out " + tm.getTimeOut());
+ InvokeMethodRunnable imr = new InvokeMethodRunnable(tm, instance, parameterValues);
+ try {
+ imr.run();
+ testResult.setStatus(ITestResult.SUCCESS);
+ }
+ catch(Exception ex) {
+ testResult.setThrowable(ex.getCause());
+ testResult.setStatus(ITestResult.FAILURE);
+ }
+ }
- // We don't need the result from the future but invoking get() on it
- // will trigger the exception that was thrown, if any
- future.get();
- // done.await();
+ private static void invokeWithTimeoutWithNewExecutor(ITestNGMethod tm,
+ Object instance, Object[] parameterValues, ITestResult testResult)
+ throws InterruptedException, ThreadExecutionException {
+ IExecutor exec= ThreadUtil.createExecutor(1, tm.getMethod().getName());
- testResult.setStatus(ITestResult.SUCCESS); // if no exception till here than SUCCESS
- }
+ InvokeMethodRunnable imr = new InvokeMethodRunnable(tm, instance, parameterValues);
+ IFutureResult future = exec.submitRunnable(imr);
+ exec.shutdown();
+ long realTimeOut = MethodHelper.calculateTimeOut(tm);
+ boolean finished = exec.awaitTermination(realTimeOut);
+
+ if (! finished) {
+ exec.stopNow();
+ ThreadTimeoutException exception = new ThreadTimeoutException("Method "
+ + tm.getClass().getName() + "." + tm.getMethodName() + "()"
+ + " didn't finish within the time-out "
+ + realTimeOut);
+ exception.setStackTrace(exec.getStackTraces()[0]);
+ testResult.setThrowable(exception);
+ testResult.setStatus(ITestResult.FAILURE);
}
+ else {
+ Utils.log("Invoker " + Thread.currentThread().hashCode(), 3,
+ "Method " + tm.getMethod() + " completed within the time-out " + tm.getTimeOut());
+
+ // We don't need the result from the future but invoking get() on it
+ // will trigger the exception that was thrown, if any
+ future.get();
+// done.await();
+
+ testResult.setStatus(ITestResult.SUCCESS); // if no exception till here than SUCCESS
+ }
+ }
protected static void invokeConfigurable(final Object instance,
final Object[] parameters, Object configurableInstance,
2  src/test/java/test/thread/ThreadPoolSizeTest.java
View
@@ -11,7 +11,7 @@ public void setUp() {
initThreadLog();
}
- @Test(invocationCount = 20, threadPoolSize = 3)
+ @Test(invocationCount = 5, threadPoolSize = 3)
public void f1() {
long n = Thread.currentThread().getId();
log(getClass().getName(), "threadPoolSize:20");
2  src/test/java/test/thread/ThreadPoolSizeWithTimeOutTest.java
View
@@ -11,7 +11,7 @@ public void setUp() {
initThreadLog();
}
- @Test(invocationCount = 20, threadPoolSize = 3, timeOut = 1000)
+ @Test(invocationCount = 5, threadPoolSize = 3, timeOut = 1000)
public void f1() {
long n = Thread.currentThread().getId();
log(getClass().getName(), "threadPoolSize:20");
1  src/test/resources/testng.xml
View
@@ -80,6 +80,7 @@
<class name="test.mannotation.MAnnotationSampleTest" />
<class name="test.mannotation.MAnnotation2SampleTest" />
<class name="test.thread.ThreadPoolSizeTest" />
+ <class name="test.thread.ThreadPoolSizeWithTimeOutTest" />
<class name="test.thread.SequentialTest" />
<class name="test.thread.ParallelTestTest" />
<class name="test.thread.FactoryTest" />
Please sign in to comment.
Something went wrong with that request. Please try again.