testng icon indicating copy to clipboard operation
testng copied to clipboard

DependsOnMethods made parallel class use several threads

Open thibaut-sticky opened this issue 10 years ago • 38 comments

TestNG Version

6.9.12 and 6.9.13.8

Expected behavior

When setting TestNG to run tests in parallel by class, all the tests in a single class should be run in the same thread.

Actual behavior

If one of the test method depends on another, a new thread is created, which is really annoying for Selenium case for instance (BeforeClass open the browser, AfterClass kill it)

Test case sample

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="main" parallel="classes" thread-count="2" verbose="0" group-by-instances="false">
    <test name="all">
        <classes>
            <class name="TestFoo"/>
        </classes>
    </test>
</suite>
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class TestFoo {


  @BeforeClass
  public void navigateTo() {
    System.out.print("BeforeClass - ");
    printThread();
  }

  private void printThread() {
    System.out.println(Thread.currentThread().getName());
  }

  @AfterClass
  public void die() {
    System.out.print("AfterClass - ");
    printThread();
  }

  @Test
  public void t1() {
    System.out.print("T1 - ");
    printThread();
  }

  @Test(dependsOnMethods = "t1")
  public void t2() {
    System.out.print("T2 - ");
    printThread();
  }

}

BeforeClass - pool-1-thread-1
T1 - pool-1-thread-1
T2 - pool-1-thread-2
AfterClass - pool-1-thread-2

What I've tried:

  • singleThreaded = true
  • group-by-instance="true"

thibaut-sticky avatar Oct 06 '16 12:10 thibaut-sticky

Is it working as expected without the dependsOnMethods?

juherr avatar Oct 06 '16 18:10 juherr

@juherr yes, only 1 thread was used when I removed dependsOnMethods from the example above:

BeforeClass - pool-1-thread-1
T1 - pool-1-thread-1
T2 - pool-1-thread-1
AfterClass - pool-1-thread-1

ryanlevell avatar Oct 09 '16 18:10 ryanlevell

It's working fine also if I set thread-count to 1.

thibaut-sticky avatar Oct 11 '16 07:10 thibaut-sticky

Yes, the problem is located in GraphThreadPoolExecutor/DynamicGraph. But I don't know how to fix it for the moment. Don't expect a quick fix, sorry.

juherr avatar Oct 11 '16 07:10 juherr

@juherr At least it's already good to know where the issue come from. Except remove the dependsOnMethods and refactor a bit my class, do you see a workaround?

thibaut-sticky avatar Oct 11 '16 08:10 thibaut-sticky

In fact, dependsOn with parallel class can not work together. If it worked before, it was a side effect of another issue :/

juherr avatar Oct 11 '16 08:10 juherr

I don't understand why you said dependsOn with parallel class cannot work together. Indeed, in this paralell mode, all the test methods inside a class has to be run in the same thread, so it does not affect the dependsOn, this is the theory. Perhaps, you said it cannot work because the code does not allow it?

thibaut-sticky avatar Oct 12 '16 12:10 thibaut-sticky

Perhaps, you said it cannot work because the code does not allow it?

In the current state, yes. But it is supposed to work ;)

juherr avatar Oct 12 '16 12:10 juherr

Hi @juherr

Any update on this? I'm facing the same problem due to parallel execution with dependsOnMethod. I had a look to graph/GraphThreadPoolExecutor.java but it looks like my basic knowledge won't be enough here.

Thank you!

mathias21 avatar Jan 10 '17 09:01 mathias21

@mathias21 Nope, nothing new for the moment. Sorry.

juherr avatar Jan 10 '17 09:01 juherr

Hi @thibaut-sticky

I think I've found a workaround to solve this problem. It takes 2 dependencies: with the max amount of devices you are going to use at the same time, and also forcing you to release the session during @AfterClass method, but apparently it works. If you specify in your suite XML file "thread-count='2'", then you can define your suite as following:

<suite name="SmokeTest" configfailurepolicy="continue" parallel="classes" thread-count="2">
<test name="Test cases" >
        <classes>
            <class name="packageA.TestClass1"/>
            <class name="packageA.TestClass2"/>
        </classes>
    </test>

    <test name="Test cases2" >
        <classes>
            <class name="packageB.TestClass1"/>
            <class name="packageB.TestClass2"/>
        </classes>
    </test>
</suite>

It is not the best solution, but apparently runs your tests with proper sorting.

mathias21 avatar Jan 10 '17 09:01 mathias21

@mathias21 Thx you for your workaround, but sadly in my case I cannot apply it. But at least I can see I'm not the only one using parallel and dependsOnMethod features 👍

thibaut-sticky avatar Jan 10 '17 13:01 thibaut-sticky

i have the same problem with parallel=classes & using dependsOnMethod in one classe, and i do some prepare work using localThread var :(. it takes me a long time to find out the thread change. hope this bug could be fixed soon :)

toutoudnf avatar Jan 19 '17 02:01 toutoudnf

The similar problem happens when using priority. Described in #1066

dr29bart avatar Jan 22 '17 11:01 dr29bart

@juherr I am quite impacted by this issue as well, and saw you self-assigned the ticket. Does that mean you're actively working on a resolution? What kind of priority is this for you? This rather high for us... Thanks

sylvainbouxin avatar Jan 23 '17 15:01 sylvainbouxin

Hi all

Do we know if this is not happening in a concrete version? I'm also using 6.9.12. I believe this is a structural issue, not a punctual bug or typo, am I right?

mathias21 avatar Jan 23 '17 15:01 mathias21

@sylvainbouxin "actively" is not the good word but it is on the top of the list and I planned to work on it unless someone has enough motivation. And I'm open to any help! 😉

@mathias21 Exactly! There are some similar issues and the root cause is often DynamicGraph which manages the order of methods and the group of methods when parallel is used. So, just one of the most important part of TestNG which generate test regression each time I have to modify it! 😨

juherr avatar Jan 23 '17 16:01 juherr

@juherr nice challenge, indeed... I'm currently looking into this: I really need this fixed or write my own framework to manage the order of methods and classes with parallel execution.

sylvainbouxin avatar Jan 23 '17 16:01 sylvainbouxin

@sylvainbouxin Nice! Feel free to send me emails if you need to exchange some extra things.

juherr avatar Jan 23 '17 16:01 juherr

@juherr, you said:

Yes, the problem is located in GraphThreadPoolExecutor/DynamicGraph.

Can you elaborate on what the problem is exactly?

cbeust avatar Feb 14 '17 21:02 cbeust