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

@Isolated annotation doesn't work properly when @ResourceLock annotations are also used #2605

Closed
n-miles opened this issue Apr 30, 2021 · 2 comments · Fixed by #2614
Closed

Comments

@n-miles
Copy link

n-miles commented Apr 30, 2021

The documentaion for @Isolated states

@Isolated is used to declare that the annotated test class should be executed in isolation from other test classes.
When a test class is run in isolation, no other test class is executed concurrently. This can be used to enable parallel test execution for the entire test suite while running some tests in isolation (e.g. if they modify some global resource).

The @Isolated annotation is implemented as a global lock. At a glance, it looks like it's just shorthand for

@ResourceLock("org.junit.platform.engine.support.hierarchical.ExclusiveResource.GLOBAL_KEY")

This has a very surprising and I would argue buggy implication: if you use a @ResourceLock("something") annotation on one test and @Isolated on another, the @Isolated test will run in parallel with the @ResourceLocked test, violating the "no other test class is executed concurrently" part of the @Isolated contract.

Steps to reproduce

Put this in your src/test/resources/junit-platform.properties file

junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = concurrent

Define 3 test classes like this in some package:

@Isolated
class IsolatedTest {

    @Test
    void test() throws InterruptedException {
        System.out.println("Started isolated test");
        Thread.sleep(5000);
        System.out.println("Finished isolated test");
    }
}
@ResourceLock("something")
class ResourceLock1Test {

    @Test
    void test() throws InterruptedException {
        System.out.println("Started ResourceLock1Test");
        Thread.sleep(5000);
        System.out.println("Started ResourceLock1Test");
    }
}
@ResourceLock("something")
class ResourceLock2Test {

    @Test
    void test() throws InterruptedException {
        System.out.println("Started ResourceLock2Test");
        Thread.sleep(5000);
        System.out.println("Started ResourceLock2Test");
    }
}

Expected Behavior

I expect none of these tests to interleave - The @ResourceLock("something") tests acquire the read-write lock, so they should not interleave with each other, and the @Isolated test has the behavior that "no other test class is executed concurrently", so it should not interleave with either of the other tests.

Actual Behavior

The @Isolated test runs in parallel with the @ResourceLock("something") tests

Context

  • Used versions (Jupiter/Vintage/Platform): junit-jupiter-api:5.7.1
  • Build Tool/IDE: Confirmed behavior in Maven 3.8.1 and Intellij 2021.1
@sbrannen
Copy link
Member

sbrannen commented May 3, 2021

Tentatively slated for 5.8 M2 for team discussion

@marcphilipp marcphilipp self-assigned this May 7, 2021
@marcphilipp marcphilipp modified the milestones: 5.8 M2, 5.7.2 May 13, 2021
@marcphilipp
Copy link
Member

@n-miles Thanks for reporting! This is indeed a bug. I'm working on a fix.

marcphilipp added a commit that referenced this issue May 13, 2021
Prior to this commit, the global read lock was not acquired for test
classes with `@ResourceLock` annotations causing `@Isolated` tests to
run in parallel.

Fixes #2605.
marcphilipp added a commit that referenced this issue May 13, 2021
Prior to this commit, the global read lock was not acquired for test
classes with `@ResourceLock` annotations causing `@Isolated` tests to
run in parallel.

Fixes #2605.
marcphilipp added a commit that referenced this issue May 15, 2021
Prior to this commit, the global read lock was not acquired for test
classes with `@ResourceLock` annotations causing `@Isolated` tests to
run in parallel.

Fixes #2605.
marcphilipp added a commit that referenced this issue May 15, 2021
Prior to this commit, the global read lock was not acquired for test
classes with `@ResourceLock` annotations causing `@Isolated` tests to
run in parallel.

Fixes #2605.
runningcode pushed a commit to runningcode/junit5 that referenced this issue Feb 15, 2023
Prior to this commit, the global read lock was not acquired for test
classes with `@ResourceLock` annotations causing `@Isolated` tests to
run in parallel.

Fixes junit-team#2605.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants