Search code examples
selenium-webdrivermaven-2testng

TestNG Parallel Tests are going in Deadlock


I have prepared a framework using selenium having parallel execution using the TestNG , i am generating the testng.xml file through code where in setting the parallel keyword to "tests". My tests are running fine but after the result are generated the build /JVM never terminates it goes into the deadlock .After debugging the code I found that the below line marked with ** is causing the issue of deadlock.

private Runnable getTask() {
    boolean timedOut = false; // Did the last poll() time out?

    for (;;) {
        int c = ctl.get();
        int rs = runStateOf(c);

        // Check if queue empty only if necessary.
        if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
            decrementWorkerCount();
            return null;
        }

        int wc = workerCountOf(c);

        // Are workers subject to culling?
        boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;

        if ((wc > maximumPoolSize || (timed && timedOut))
            && (wc > 1 || workQueue.isEmpty())) {
            if (compareAndDecrementWorkerCount(c))
                return null;
            continue;
        }

        try {
            Runnable r = timed ?
   **workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :**
                **workQueue.take();**

            if (r != null)
                return r;
            timedOut = true;
        } catch (InterruptedException retry) {
            timedOut = false;
        }
    }
}

Please help in case ..

<    Thread [main] (Running)
    Thread [TestNG] (Running)   
    Thread [TestNG] (Running)   
    Daemon Thread [Exec Default Executor] (Running)     
    Daemon Thread [Exec Stream Pumper] (Running)    
    Daemon Thread [Exec Stream Pumper] (Running) >

Test case script

    @Test public void Test_test() {
 try { 
////code/// 
driver.close(); 
} catch (Exception err) { 
System.out.println(err.getMessage()); 
Fail(err.getMessage()); 
}finally{ 
Closereport(); 
driver.quit(); 
} 
}

Solution

  • Updated the code to remove the deadlock , using synchronisation