Skip to content

How to dynamically adjust the number of TestNG threads after IExecutorFactory is deprecated? #3066

Closed

Description

@alex-postnov

TestNG Version

Note: only the latest version is supported

7.9+

Expected behavior

Hi folks!

We are using TestNG to launch long running mobile e2e tests. One test suite run can take 30 minutes or even more. Throughout the test run, the availability of devices may fluctuate—some devices may become faulty, while others become ready to run tests. Ideally, the number of available devices should match the number of threads allocated in TestNG, ensuring that neither tests nor devices remain idle.

We aim to dynamically adjust the number of TestNG threads during test execution. Java's ThreadPoolExecutor supports this functionality through its methods setCorePoolSize() and setMaximumPoolSize(). Thus, our primary requirement is to obtain references to the ThreadPoolExecutors.

To get the references, we introduced a class that implements IExecutorFactory and stores weak references to created Executors. This class is then passed to TestNG via the -threadPoolFactoryClass parameter. And we can access the executors.

This scheme is working fine for us at the moment.

Actual behavior

However, in the latest version of TestNG IExecutorFactory is marked as deprecated and is supposed to be removed in one of the future versions.

Question: How can we achieve the same functionality in the new versions once IExecutorFactory is removed? Alternatively, do you have suggestions for a better approach to dynamically adjusting the number of running threads?

Is the issue reproducible on runner?

  • Shell
  • Maven
  • Gradle
  • Ant
  • Eclipse
  • IntelliJ
  • NetBeans

Test case sample

Please, share the test case (as small as possible) which shows the issue

Contribution guidelines

Incase you plan to raise a pull request to fix this issue, please make sure you refer our Contributing section for detailed set of steps.

Some code examples:

class ThreadPoolExecutorFactory : IExecutorFactory {
    companion object {
        val suiteExecutorInstances = mutableListOf<WeakReference<GraphThreadPoolExecutor<ISuite>>>()
        val methodExecutorInstances = mutableListOf<WeakReference<GraphThreadPoolExecutor<ITestNGMethod>>>()
    }

    override fun newSuiteExecutor(
        name: String?,
        graph: IDynamicGraph<ISuite>?,
        factory: IThreadWorkerFactory<ISuite>?,
        corePoolSize: Int,
        maximumPoolSize: Int,
        keepAliveTime: Long,
        unit: TimeUnit?,
        workQueue: BlockingQueue<Runnable>?,
        comparator: Comparator<ISuite>?
    ): ITestNGThreadPoolExecutor {
        val instance = GraphThreadPoolExecutor(
            name,
            graph,
            factory,
            corePoolSize,
            maximumPoolSize,
            keepAliveTime,
            unit,
            workQueue,
            comparator
        )
        suiteExecutorInstances.add(WeakReference(instance))
        return instance
    }

    override fun newTestMethodExecutor(
        name: String?,
        graph: IDynamicGraph<ITestNGMethod>?,
        factory: IThreadWorkerFactory<ITestNGMethod>?,
        corePoolSize: Int,
        maximumPoolSize: Int,
        keepAliveTime: Long,
        unit: TimeUnit?,
        workQueue: BlockingQueue<Runnable>?,
        comparator: Comparator<ITestNGMethod>?
    ): ITestNGThreadPoolExecutor {
        val instance = GraphThreadPoolExecutor(
            name,
            graph,
            factory,
            corePoolSize,
            maximumPoolSize,
            keepAliveTime,
            unit,
            workQueue,
            comparator
        )
        methodExecutorInstances.add(WeakReference(instance))
        return instance
    }
}
class TestNGThreadPoolSize {
    private val logger: Logger = LoggerFactory.getLogger("TestNGTheadPoolSize")

    fun currentPoolSize(): Int {
        return getMethodExecutor().poolSize
    }

    fun currentCorePoolSize(): Int {
        return getMethodExecutor().corePoolSize
    }

    fun currentMaximumPoolSize(): Int {
        return getMethodExecutor().maximumPoolSize
    }

    @Synchronized
    fun increaseByOne() {
        logger.info("Increasing TestNG thread pool size by 1")
//        Order of the following calls matters. Wrong order will result in an IllegalArgumentException
        getMethodExecutor().maximumPoolSize++
        getMethodExecutor().corePoolSize++
    }

    @Synchronized
    fun decreaseByOne() {
        logger.info("Decreasing TestNG thread pool size by 1")
//        Order of the following calls matters. Wrong order will result in an IllegalArgumentException
        getMethodExecutor().corePoolSize -= 1
        getMethodExecutor().maximumPoolSize -= 1
    }

    private fun getMethodExecutor(): GraphThreadPoolExecutor<ITestNGMethod> {
        val runningExecutors = ThreadPoolExecutorFactory.methodExecutorInstances
            .mapNotNull { it.get() }
            .filterNot { it.isTerminated }

//        currently we only allow 1 suite to be run at a time so only one method executor should be in Running state
        val runningExecutorsCount = runningExecutors.size
        if (runningExecutorsCount != 1) {
            throw CountMismatchException(
                "Unexpected amount of running thread pool executor instances: $runningExecutorsCount. Should be 1."
            )
        }

        return runningExecutors.first()
    }
}

Activity

  1. added a commit that references this issue on Feb 20, 2024
    1efd809
  2. added this to the 7.10.0 milestone on Feb 20, 2024
  3. krmahadevan commented on Feb 20, 2024

    @krmahadevan
    Member

    @alex-postnov - Please check the PR and share your observations. A minimal user facing documentation is available in the commit message.

  4. added 3 commits that reference this issue on Feb 20, 2024
    cf6cddd
    b78ef23
    2cc332d
  5. added
    area: parallel execution
    and removed on Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: parallel execution

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions