From c4fe2f9869ae929e5cbc9a2bbdb63ab4891f6907 Mon Sep 17 00:00:00 2001 From: Fabian Linz Date: Thu, 7 Dec 2023 15:22:49 +0100 Subject: [PATCH] #1640 Fix LockAssert.makeAllAssertsPass(true) to not start more than one test lock --- .../javacrumbs/shedlock/core/LockAssert.java | 4 +- .../shedlock/core/LockAssertTest.java | 37 ++++++++++++++----- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/shedlock-core/src/main/java/net/javacrumbs/shedlock/core/LockAssert.java b/shedlock-core/src/main/java/net/javacrumbs/shedlock/core/LockAssert.java index b8bc8d7ea..04b787a2e 100644 --- a/shedlock-core/src/main/java/net/javacrumbs/shedlock/core/LockAssert.java +++ b/shedlock-core/src/main/java/net/javacrumbs/shedlock/core/LockAssert.java @@ -76,7 +76,9 @@ public static class TestHelper { */ public static void makeAllAssertsPass(boolean pass) { if (pass) { - LockAssert.startLock(TEST_LOCK_NAME); + if (!LockAssert.alreadyLockedBy(TEST_LOCK_NAME)) { + LockAssert.startLock(TEST_LOCK_NAME); + } } else { if (LockAssert.alreadyLockedBy(TEST_LOCK_NAME)) { LockAssert.endLock(); diff --git a/shedlock-core/src/test/java/net/javacrumbs/shedlock/core/LockAssertTest.java b/shedlock-core/src/test/java/net/javacrumbs/shedlock/core/LockAssertTest.java index 077b8f366..c2d6c3f49 100644 --- a/shedlock-core/src/test/java/net/javacrumbs/shedlock/core/LockAssertTest.java +++ b/shedlock-core/src/test/java/net/javacrumbs/shedlock/core/LockAssertTest.java @@ -22,6 +22,8 @@ import java.time.Duration; import java.util.Optional; + +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; class LockAssertTest { @@ -67,17 +69,32 @@ void shouldWorkWithNestedLocks() { assertThatThrownBy(LockAssert::assertLocked).isInstanceOf(IllegalStateException.class); } - @Test - void assertShouldNotFailIfConfiguredForTests() { - LockAssert.TestHelper.makeAllAssertsPass(true); - LockAssert.assertLocked(); + @Nested + class TestHelper { + @Test + void assertShouldNotFailIfConfiguredForTests() { + LockAssert.TestHelper.makeAllAssertsPass(true); + LockAssert.assertLocked(); - LockAssert.TestHelper.makeAllAssertsPass(false); - assertThatThrownBy(LockAssert::assertLocked).isInstanceOf(IllegalStateException.class); - } + LockAssert.TestHelper.makeAllAssertsPass(false); + assertThatThrownBy(LockAssert::assertLocked).isInstanceOf(IllegalStateException.class); + } - @Test - void makeAllAssertsPassShouldNotFail() { - LockAssert.TestHelper.makeAllAssertsPass(false); + @Test + void makeAllAssertsPassShouldNotFail() { + LockAssert.TestHelper.makeAllAssertsPass(false); + } + + @Test + void shouldNotStartMoreThanOneTestLock() { + assertThatThrownBy(LockAssert::assertLocked).isInstanceOf(IllegalStateException.class); + LockAssert.TestHelper.makeAllAssertsPass(true); + LockAssert.TestHelper.makeAllAssertsPass(true); + + LockAssert.TestHelper.makeAllAssertsPass(false); + + assertThatThrownBy(LockAssert::assertLocked).isInstanceOf(IllegalStateException.class); + } } + }