Skip to content

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

@n-miles

Description

@n-miles

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

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions