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

mockito-inline: verify with timeout on synchronized method is causing deadlock #2500

Open
brenuart opened this issue Dec 7, 2021 · 2 comments

Comments

@brenuart
Copy link

brenuart commented Dec 7, 2021

The problem arises only when "inline mocking" is enabled (enabled as described in Mocking Final Types.)

Consider the following test case:

public static class MyService {
    public synchronized void execute() {
        System.out.println("Do something");
    }
}

@Test
public void test() {
    MyService service = spy(new MyService());

    Thread t = new Thread() {
        public void run() {
            try {
                // wait such that verify(service).execute() enters the method before us then invoke the service
                Thread.sleep(1000);
                service.execute();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    t.start();

    verify(service, timeout(2000)).execute();
}

This test fails saying the execute() method has not been invoked before the timeout. After some investigations it appears that verify(service).execute() already holds a lock on the MyService instance preventing the async thread to access it.

On the other hand, everything works as expected when "inline mocking" is not enabled (test succeeds).
Is this scenario not supported ?
Can this be related to #2001 ?

Tested with Mockito 4.1.0 and OpenJDK 11.

@zhong2peng
Copy link

zhong2peng commented Dec 7, 2021 via email

@spmason
Copy link

spmason commented Jan 25, 2023

Note that this seems to happen by default since the move to use mockito-inline in 5.0.0 (see #2001 (comment))

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