Challenge - 5 Problems
Resource Locking Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of concurrent tests with @ResourceLock
Consider the following JUnit 5 test class using @ResourceLock to manage concurrent access. What will be the output order of the printed statements when running all tests in parallel?
JUnit
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.parallel.ResourceLock; import org.junit.jupiter.api.parallel.Resources; import org.junit.jupiter.api.parallel.Execution; import org.junit.jupiter.api.parallel.ExecutionMode; @Execution(ExecutionMode.CONCURRENT) public class ResourceLockTest { @Test @ResourceLock(value = "RESOURCE_A") void test1() throws InterruptedException { System.out.println("Start test1"); Thread.sleep(100); System.out.println("End test1"); } @Test @ResourceLock(value = "RESOURCE_A") void test2() throws InterruptedException { System.out.println("Start test2"); Thread.sleep(100); System.out.println("End test2"); } @Test void test3() { System.out.println("Start test3"); System.out.println("End test3"); } }
Attempts:
2 left
💡 Hint
Remember that tests locking the same resource run sequentially, but tests without locks can run concurrently.
✗ Incorrect
Tests test1 and test2 both lock RESOURCE_A, so they run one after another. test3 has no lock and can run concurrently, so it may start and finish before or during the others. The only guaranteed order is that test1 and test2 do not overlap.
❓ assertion
intermediate1:30remaining
Correct assertion for resource lock count
You have a test method annotated with @ResourceLock("DB_CONNECTION") that increments a shared counter. Which assertion correctly verifies that the counter increments exactly once per test execution when tests run concurrently?
JUnit
import static org.junit.jupiter.api.Assertions.assertEquals; int counter = 0; @Test @ResourceLock("DB_CONNECTION") void incrementCounter() { counter++; }
Attempts:
2 left
💡 Hint
The resource lock ensures only one test increments at a time, so the counter should be exactly 1 after one test.
✗ Incorrect
Because the resource lock serializes access, the counter increments once per test execution. The correct assertion checks for exactly 1.
🔧 Debug
advanced2:30remaining
Identify the cause of test deadlock with @ResourceLock
Given two test methods each annotated with @ResourceLock on different resources but run concurrently, the test suite hangs indefinitely. What is the most likely cause?
JUnit
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.parallel.ResourceLock; import org.junit.jupiter.api.parallel.Execution; import org.junit.jupiter.api.parallel.ExecutionMode; @Execution(ExecutionMode.CONCURRENT) public class DeadlockTest { @Test @ResourceLock(value = "RESOURCE_1") void testA() { // locks RESOURCE_1 // then tries to lock RESOURCE_2 inside } @Test @ResourceLock(value = "RESOURCE_2") void testB() { // locks RESOURCE_2 // then tries to lock RESOURCE_1 inside } }
Attempts:
2 left
💡 Hint
Think about what happens when two tests wait on each other's locked resources.
✗ Incorrect
Deadlock occurs when testA holds RESOURCE_1 and waits for RESOURCE_2, while testB holds RESOURCE_2 and waits for RESOURCE_1, causing circular wait.
❓ framework
advanced1:30remaining
Behavior of @ResourceLock with multiple resources
What happens when a test method is annotated with @ResourceLock(value = {"RESOURCE_X", "RESOURCE_Y"}) in JUnit 5?
Attempts:
2 left
💡 Hint
Consider how @ResourceLock handles multiple resource names in its value array.
✗ Incorrect
Annotating with multiple resources locks all listed resources simultaneously to prevent concurrent access conflicts.
🧠 Conceptual
expert3:00remaining
Effect of @ResourceLock on test execution order and concurrency
In a large test suite with many tests annotated with different @ResourceLock values, how does JUnit 5 determine which tests can run concurrently and which must run sequentially?
Attempts:
2 left
💡 Hint
Think about resource locking as a way to prevent simultaneous access to shared resources.
✗ Incorrect
JUnit uses resource locks to serialize tests that share the same resource name, allowing tests with different resource locks to run in parallel safely.