Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test run in multiple threads #20

Open
slawekjaranowski opened this issue Nov 28, 2020 · 3 comments
Open

Test run in multiple threads #20

slawekjaranowski opened this issue Nov 28, 2020 · 3 comments

Comments

@slawekjaranowski
Copy link
Member

We can annotate test with:

    @Mock
    private MyType mock;

    @Test(threadPoolSize = 3, invocationCount = 8)
    public void testParallel() {
     ....
    }

In this case MockitoTestNGListener will not work as we want.
In current implementation MockitoSession is bind to current test instance, but TestNG create one test instance and run test in multiple thread.

Issue need more investigate how to resolve problem.

We must to check and discover how mocked field should be distinguished on one test instance and multiple thread.

Please like / react on issue to we see how many people are interested this issue.

@slawekjaranowski
Copy link
Member Author

This testng-team/testng#1872 is probably connected.

@mlieshoff
Copy link

I strongly guess it throws ConcurrentModificationExceptions because of the usage of HashMaps instead of ConcurrentHashMaps in the MockitoTestNGListener class. The (not thread safe) handling of the fixtures seems to be a problem in the framework and related to the referenced issue. But at least the listener itself could be thread safe with changing the Map implementation and make life a bit easier :)

@ben-manes
Copy link

ben-manes commented Dec 15, 2024

I ran into the ConcurrentModificationException error as well due to using TestNG's parallel methods support in its test runner. This is distinctly different from threadPoolSize because different test methods are run concurrently.

I had used Mockito as shorthand for dummy instances where I am testing a builder class so the mocks are never invoked or configured. This was simple to change to actual instances that error if invoked and that found a mistake where using the mocks was desirable and its state leaked across test methods.

A correct actual usages of the listener for initializing fields should require single threaded tests and that should be explicit in the documentation and ideally detected at runtime if possible. The internal crash implies a bug in the listener rather than in the developer's test code.

java.util.ConcurrentModificationException
    at java.base/java.util.HashMap.computeIfAbsent(HashMap.java:1135)
    at org.mockito.testng.MockitoTestNGListener.beforeInvocation(MockitoTestNGListener.java:101)
    at org.testng.internal.invokers.InvokedMethodListenerInvoker.invokeListener(InvokedMethodListenerInvoker.java:55)
    at org.testng.internal.invokers.BaseInvoker.runInvokedMethodListeners(BaseInvoker.java:82)
    at org.testng.internal.invokers.TestInvoker.invokeMethod(TestInvoker.java:651)
    at org.testng.internal.invokers.TestInvoker.invokeTestMethod(TestInvoker.java:230)
    at org.testng.internal.invokers.MethodRunner.runInSequence(MethodRunner.java:63)
    at org.testng.internal.invokers.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:992)
    at org.testng.internal.invokers.TestInvoker.invokeTestMethods(TestInvoker.java:203)
    at org.testng.internal.invokers.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:154)
    at org.testng.internal.invokers.TestMethodWorker.run(TestMethodWorker.java:134)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at org.testng.internal.thread.graph.TestNGFutureTask.run(TestNGFutureTask.java:22)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants