testng-team / testng

TestNG testing framework
https://testng.org
Apache License 2.0
1.98k stars 1.02k forks source link

Number of threads are exceeding the `thread-count` value while using data providers in parallel #1775

Closed shridharkalagi closed 6 years ago

shridharkalagi commented 6 years ago

TestNG Version

6.14.3

Expected behavior

Number of threads shouldn't exceed the value given for thread-count attribute in suite tag, even when data providers are used

Actual behavior

Number of threads spinned is equal to thread-count + data-provider-thread-count when @DataProvider(parallel = true)

Is the issue reproductible on runner?

Test case sample

testng.xml file -

<suite name="My Suite" verbose="2" parallel="methods" thread-count="2" data-provider-thread-count="2">
    <test name="Some Workflow Tests" >
        <classes>
            <class name="com.appium.webtest.SampleDemo"/>
        </classes>
    </test>
</suite>

Test.java ->

public class SampleDemo {
   @Test(dataProvider = "dataMethod")
    public void testMethodsOne(String count) {
        long id = Thread.currentThread().getId();
        System.out.println("Simple test-method One. Thread id is: " + id +"and with count " + count);
    }

    @Test
    public void testMethodsThree() {
        long id = Thread.currentThread().getId();
        System.out.println("Simple test-method three. Thread id is: " + id);
    }

    @Test
    public void testMethodsFour() {
        long id = Thread.currentThread().getId();
        System.out.println("Simple test-method four. Thread id is: " + id);
    }
 @DataProvider(parallel = true)
    public Object[][] dataMethod() {
        return new Object[][]{{"first"}, {"second"}, {"third"}, {"four"}, {"five"}, {"six"}, {"seven"}, 
        {"eight"}, {"nine"}, {"ten"}};
    }
}

4 threads are spinned up in the above scenario, where I was expecting only 2

krmahadevan commented 6 years ago

I don't think this is an issue. TestNG basically makes use of two different thread pools (one for ordinary methods and another one for data provider driven methods). This should be evident from the fact that there are two different attributes to control the threadpool size.

Your thread pool size is 2. So basically your 3 test methods will run in these two threads. But since one of the test methods is basically a data driven method, it additionally spins of 2 more threads (since your suite xml has configured the data provider thread pool size to be 2), you see two more threads...

Here's a modified variant of your sample test code.. and then the output of your execution..

There are basically two thread pools (its evident from the thread name prefixes) in the output.

Hope that explains the behavior. Closing this issue.

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class SampleDemo {
  @Test(dataProvider = "dataMethod")
  public void testMethodsOne(String count) {
    String prefix = "Belonging to [" + Thread.currentThread().getName() + "]. ";
    long id = Thread.currentThread().getId();
    System.out.println(
        prefix + "Simple test-method One. Thread id is: " + id + "and with count " + count);
  }

  @Test
  public void testMethodsThree() {
    String prefix = "Belonging to [" + Thread.currentThread().getName() + "]. ";
    long id = Thread.currentThread().getId();
    System.out.println(prefix + "Simple test-method three. Thread id is: " + id);
  }

  @Test
  public void testMethodsFour() {
    String prefix = "Belonging to [" + Thread.currentThread().getName() + "]. ";
    long id = Thread.currentThread().getId();
    System.out.println(prefix + "Simple test-method four. Thread id is: " + id);
  }

  @DataProvider(parallel = true)
  public Object[][] dataMethod() {
    return new Object[][] {
      {"first"},
      {"second"},
      {"third"},
      {"four"},
      {"five"},
      {"six"},
      {"seven"},
      {"eight"},
      {"nine"},
      {"ten"}
    };
  }
}

Execution output

Belonging to [TestNG-test=xml_test-1]. Simple test-method four. Thread id is: 11
Belonging to [TestNG-PoolService-0]. Simple test-method One. Thread id is: 13and with count first
Belonging to [TestNG-PoolService-1]. Simple test-method One. Thread id is: 14and with count second
Belonging to [TestNG-PoolService-1]. Simple test-method One. Thread id is: 14and with count third
Belonging to [TestNG-PoolService-0]. Simple test-method One. Thread id is: 13and with count four
Belonging to [TestNG-PoolService-1]. Simple test-method One. Thread id is: 14and with count five
Belonging to [TestNG-PoolService-0]. Simple test-method One. Thread id is: 13and with count six
Belonging to [TestNG-PoolService-1]. Simple test-method One. Thread id is: 14and with count seven
Belonging to [TestNG-test=xml_test-1]. Simple test-method three. Thread id is: 11
Belonging to [TestNG-PoolService-0]. Simple test-method One. Thread id is: 13and with count eight
Belonging to [TestNG-PoolService-1]. Simple test-method One. Thread id is: 14and with count nine
Belonging to [TestNG-PoolService-0]. Simple test-method One. Thread id is: 13and with count ten
PASSED: testMethodsOne("second")
PASSED: testMethodsFour
PASSED: testMethodsOne("first")
PASSED: testMethodsOne("third")
PASSED: testMethodsOne("four")
PASSED: testMethodsOne("five")
PASSED: testMethodsOne("six")
PASSED: testMethodsThree
PASSED: testMethodsOne("seven")
PASSED: testMethodsOne("eight")
PASSED: testMethodsOne("nine")
PASSED: testMethodsOne("ten")

===============================================
    xml_test
    Tests run: 12, Failures: 0, Skips: 0
===============================================

===============================================
xml_suite
Total tests run: 12, Failures: 0, Skips: 0
===============================================

Process finished with exit code 0