TestNG @Test attributes - threadPoolSize, invocationCount, timeOut

Let's see invocationCount implementation

public class InvocationCnt {
 
      @Test(invocationCount=5)
      public void test1() throws InterruptedException
      {
   System.out.println("Thread ID: "+Thread.currentThread().getId());  
      }
}

OutPut:
Thread ID: 1
Thread ID: 1
Thread ID: 1
Thread ID: 1
Thread ID: 1
above method ran 5 times, a single thread will be assigned to run the method one by one..

Now let's see with threadPoolSize implementation




public class InvocationCnt {
  
  @Test(threadPoolSize=3, invocationCount=5)
  public void test1() throws InterruptedException
  {
        System.out.println("Thread ID: "+Thread.currentThread().getId());  
  }
}

OutPut:
[ThreadUtil] Starting executor timeOut:0ms workers:5 threadPoolSize:3
Thread ID: 11
Thread ID: 12
Thread ID: 13
Thread ID: 12
Thread ID: 11
Above method ran 5 times, but 3 threads shared the 5 times of test run...(threads 11, 12 and 13)

Now let's see threadPoolSize with timeOut implementation

public class InvocationCnt {
  
  @Test(threadPoolSize=3, invocationCount=5, timeOut=2000) 
  public void test1() throws InterruptedException
  {
     Thread.sleep(1500);
     System.out.println("Thread ID: "+Thread.currentThread().getId());  
  }
}

OutPut:
[ThreadUtil] Starting executor timeOut:2000ms workers:5 threadPoolSize:3
Thread ID: 11
Thread ID: 12
Thread ID: 13
Above method ran only 3 times, as the timeOut is set to 2sec (a particular thread only holds for 2sec), and testcase takes 1.5sec (by knowing we kept 1.5sec delay), and ran the tests no of times with in the 2sec, so each thread only ran once...

No comments:

Post a Comment