Question about timeOut parameter inside a a @Test

393 views
Skip to first unread message

Tsiakos, Panagiotis

unread,
Jan 27, 2012, 6:59:37 AM1/27/12
to testng...@googlegroups.com, Botsis, Minas, Sidereas, Sokratis

Hi ,

I have a guestion about the timeOut parameter inside a @Test method  (

timeOut The maximum number of milliseconds this test should take.
)

saw that when the timeoout expires the test fails throwing a timeout exception and the main thread goes

to the next @Test (that is what I expected)

The problem is that I observed also that a second threard is spawned in parallel, that tries to execute the @Test with the expired timeout,

from the point that the timeout occurred. That really messes the execution of the @Test methods inside a test class.

Is this a bug or for some reason it works as designed?

Regards

Panagiotis

 

Panagiotis Tsiakos

Siemens Enterprise Communications S.A.

Enterprise Product Development
15 Andrea Metaxa str., Room 1.14

GR 145 64, Nea Kifisia
Athens, Greece
Tel: +30 210 8189609

Fax: +30 210 8189761
mailto: panagiotis.tsiakos@siemens-enterprise.com

 

Krishnan Mahadevan

unread,
Jan 27, 2012, 7:08:27 AM1/27/12
to testng...@googlegroups.com
Am not quite sure if I got this question right, but I ran a quick check and dont see a problem. Perhaps you can elaborate more with a demo class and provide the expected results Vs actual results that you are seeing along with the testng version that you are working with.

As expected only method1 failed for me and method2 passed.
Am working with TestNG eclipse plug-in version 6.3.0


Here's my sample test :
public class TestNGTimeOutDemo {
@Test(timeOut=1500)
public void method1() throws InterruptedException {
Thread.sleep(2000);
}

@Test
public void method2() throws InterruptedException {
Thread.sleep(2000);
Assert.assertTrue(true);
}
}

PASSED: method2

FAILED: method1

org.testng.internal.thread.ThreadTimeoutException: Method org.testng.internal.TestNGMethod.method1() didn't finish within the time-out 1500



Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.

Tsiakos, Panagiotis

unread,
Jan 27, 2012, 9:10:05 AM1/27/12
to testng...@googlegroups.com
Hi Krishnan after investigation we concluded that the problem occurs when the TestNG timeOut takes place in a called method inside the @Test method
See below code for example. I would expect that the aMethod with arg b waits for ..   should not printed since it occurs after the timeout.
If aMethod does not catch the InterruptedException then everything works fine.
 
 
 
package com.testall;
 
import org.testng.annotations.Test;
 
public class A01_test1{
 
   @Test(priority = 1 , timeOut=5000)
   public void test1() throws InterruptedException {
    aMethod("a");
    aMethod("b");
    System.out.println("If you see this then is an ERROR");
   }
  
 @Test(priority = 2)
 public void test2() throws InterruptedException {
   System.out.println("Test2 start");
   Thread.sleep(10000);
   System.out.println("Test2 finsish");
 }
 
 public void aMethod(String argument){
  System.out.println("Method called with "+argument);
  int i=0;
   while(i<5){
     System.out.println("aMethod with arg "+ argument +" waits for "+i);
      try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
      i++;
    } 
 }
}
 
 
 
The output is as follows: (see red coloured lines)
 

[TestNG] [DEBUG] suiteXmlPath: "C:\DOCUME~1\grmssfq0\LOCALS~1\Temp\testng-eclipse--1900961393\testng-customsuite.xml"

[TestNG] Running:

C:\Documents and Settings\grmssfq0\Local Settings\Temp\testng-eclipse--1900961393\testng-customsuite.xml

Method called with a

aMethod with arg a waits for 0

aMethod with arg a waits for 1

aMethod with arg a waits for 2

aMethod with arg a waits for 3

aMethod with arg a waits for 4

Method called with b

aMethod with arg b waits for 0

java.lang.InterruptedException: sleep interrupted

Test2 start

at java.lang.Thread.sleep(Native Method)

at com.testall.A01_test1.aMethod(A01_test1.java:27)

at com.testall.A01_test1.test1(A01_test1.java:10)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)

at org.testng.internal.InvokeMethodRunnable.runOne(InvokeMethodRunnable.java:46)

at org.testng.internal.InvokeMethodRunnable.run(InvokeMethodRunnable.java:37)

at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)

at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)

at java.util.concurrent.FutureTask.run(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

aMethod with arg b waits for 1

aMethod with arg b waits for 2

aMethod with arg b waits for 3

aMethod with arg b waits for 4

If you see this then is an ERROR

Test2 finsish

PASSED: test2

FAILED: test1

org.testng.internal.thread.ThreadTimeoutException: Method org.testng.internal.TestNGMethod.test1() didn't finish within the time-out 5000

at java.lang.Throwable.getStackTraceElement(Native Method)

at java.lang.Throwable.getOurStackTrace(Throwable.java:591)

at java.lang.Throwable.printStackTrace(Throwable.java:462)

at java.lang.Throwable.printStackTrace(Throwable.java:451)

at com.testall.A01_test1.aMethod(A01_test1.java:29)

at com.testall.A01_test1.test1(A01_test1.java:10)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)

at org.testng.internal.InvokeMethodRunnable.runOne(InvokeMethodRunnable.java:46)

at org.testng.internal.InvokeMethodRunnable.run(InvokeMethodRunnable.java:37)

at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)

at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)

at java.util.concurrent.FutureTask.run(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

 

===============================================

Default test

Tests run: 2, Failures: 1, Skips: 0

===============================================

 

===============================================

Default suite

Total tests run: 2, Failures: 1, Skips: 0

===============================================

[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@3a9bba: 62 ms

[TestNG] Time taken by org.testng.reporters.EmailableReporter@1ed1b0b: 16 ms

[TestNG] Time taken by org.testng.reporters.XMLReporter@1ae90c: 15 ms

[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@18352d8: 0 ms

[TestNG] Time taken by [TestListenerAdapter] Passed:0 Failed:0 Skipped:0]: 16 ms

 
 
 
 


From: testng...@googlegroups.com [mailto:testng...@googlegroups.com] On Behalf Of Krishnan Mahadevan
Sent: Friday, January 27, 2012 2:08 PM
To: testng...@googlegroups.com
Subject: Re: [testng-users] Question about timeOut parameter inside a a @Test

Cédric Beust ♔

unread,
Jan 27, 2012, 12:43:56 PM1/27/12
to testng...@googlegroups.com, Botsis, Minas, Sidereas, Sokratis
Hi Tsiakos,

It's a side effect of the implementation, which spawns a single thread with the given time out just for that method.

I'm aware that it causes issues in a few cases (such as when a transaction context is stored in thread local), but I haven't looked into fixing this yet (it's not trivial).

-- 
Cédric




--

gogo mts

unread,
Feb 1, 2012, 6:13:25 AM2/1/12
to testng...@googlegroups.com, Botsis, Minas, Sidereas, Sokratis
Hi Cedric,

The example of this case is not so rare since in most of the cases there are other method calls inside an @Test method. Don't you think you should give it a try or it is pretty complex?

Georgia


2012/1/27 Cédric Beust ♔ <ced...@beust.com>

dhuang

unread,
Jun 1, 2012, 1:45:52 PM6/1/12
to testng...@googlegroups.com, Botsis, Minas, Sidereas, Sokratis
Hi, 

I am running into the same problem as tsak stated above.  Is there a solution/workaround?  Thank you.
2012/1/27 Cédric Beust ♔ <ced...@beust.com>

-- 
Cédric




To unsubscribe from this group, send email to testng-users+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users+unsubscribe@googlegroups.com.

vikas jain

unread,
Jun 20, 2013, 5:59:09 PM6/20/13
to testng...@googlegroups.com, Botsis, Minas, Sidereas, Sokratis
I am having the same issue, in fact in my other method call are made to the other service, here Testng is skipping the sleep but sending multiple call to other service in same millisecond.

and which creates lot of adverse effect.

Regards
Vikas
Hi, 

2012/1/27 Cédric Beust ♔ <ced...@beust.com>

-- 
Cédric




To unsubscribe from this group, send email to testng-users...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.

Venkatesh Malepati

unread,
Jul 31, 2015, 1:31:24 PM7/31/15
to testng-users, minas....@siemens-enterprise.com, sokratis...@siemens-enterprise.com, panagioti...@siemens-enterprise.com
Yes, my team is also facing the same issue. Anybody has a work around. Thank you.
Reply all
Reply to author
Forward
0 new messages