From 61b32ac5b13805364102373585a87959f2554455 Mon Sep 17 00:00:00 2001 From: Lukas Krecan Date: Mon, 18 Nov 2024 20:32:04 +0100 Subject: [PATCH] Test cleanup --- .../EtcdLockProviderIntegrationTest.java | 2 +- .../jetstream/NatsJetStreamLockProvider.java | 2 +- ...sJetStreamLockProviderIntegrationTest.java | 32 ++++--------------- .../AbstractLockProviderIntegrationTest.java | 6 +++- 4 files changed, 14 insertions(+), 28 deletions(-) diff --git a/providers/etcd/shedlock-provider-etcd-jetcd/src/test/java/net/javacrumbs/shedlock/provider/etcd/jetcd/EtcdLockProviderIntegrationTest.java b/providers/etcd/shedlock-provider-etcd-jetcd/src/test/java/net/javacrumbs/shedlock/provider/etcd/jetcd/EtcdLockProviderIntegrationTest.java index efcab26e5..f4adacb4e 100644 --- a/providers/etcd/shedlock-provider-etcd-jetcd/src/test/java/net/javacrumbs/shedlock/provider/etcd/jetcd/EtcdLockProviderIntegrationTest.java +++ b/providers/etcd/shedlock-provider-etcd-jetcd/src/test/java/net/javacrumbs/shedlock/provider/etcd/jetcd/EtcdLockProviderIntegrationTest.java @@ -81,7 +81,7 @@ public void shouldTimeout() throws InterruptedException { @Test @Override public void shouldLockAtLeastFor() throws InterruptedException { - doTestShouldLockAtLeastFor(2000); + doTestShouldLockAtLeastFor(LOCK_AT_LEAST_FOR, 2000); } @Override diff --git a/providers/jetstream/shedlock-provider-jetstream/src/main/java/net/javacrumbs/shedlock/provider/nats/jetstream/NatsJetStreamLockProvider.java b/providers/jetstream/shedlock-provider-jetstream/src/main/java/net/javacrumbs/shedlock/provider/nats/jetstream/NatsJetStreamLockProvider.java index f49bd3045..0ccdb56f9 100644 --- a/providers/jetstream/shedlock-provider-jetstream/src/main/java/net/javacrumbs/shedlock/provider/nats/jetstream/NatsJetStreamLockProvider.java +++ b/providers/jetstream/shedlock-provider-jetstream/src/main/java/net/javacrumbs/shedlock/provider/nats/jetstream/NatsJetStreamLockProvider.java @@ -61,7 +61,7 @@ public Optional lock(LockConfiguration lockConfiguration) { LockContentHandler.writeContent(new LockContent( lockConfiguration.getLockAtLeastUntil(), lockConfiguration.getLockAtMostUntil()))); - log.debug("Accuired lock for bucketName: {}", bucketName); + log.debug("Acquired lock for bucketName: {}", bucketName); return Optional.of(new NatsJetStreamLock(connection, lockConfiguration)); } catch (JetStreamApiException e) { diff --git a/providers/jetstream/shedlock-provider-jetstream/src/test/java/net/javacrumbs/shedlock/provider/nats/jetstream/NatsJetStreamLockProviderIntegrationTest.java b/providers/jetstream/shedlock-provider-jetstream/src/test/java/net/javacrumbs/shedlock/provider/nats/jetstream/NatsJetStreamLockProviderIntegrationTest.java index edd0ec641..9456f2ec8 100644 --- a/providers/jetstream/shedlock-provider-jetstream/src/test/java/net/javacrumbs/shedlock/provider/nats/jetstream/NatsJetStreamLockProviderIntegrationTest.java +++ b/providers/jetstream/shedlock-provider-jetstream/src/test/java/net/javacrumbs/shedlock/provider/nats/jetstream/NatsJetStreamLockProviderIntegrationTest.java @@ -10,8 +10,8 @@ import io.nats.client.Options; import io.nats.client.api.KeyValueEntry; import java.time.Duration; -import java.util.UUID; import net.javacrumbs.shedlock.core.LockProvider; +import net.javacrumbs.shedlock.support.annotation.Nullable; import net.javacrumbs.shedlock.test.support.AbstractLockProviderIntegrationTest; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -68,38 +68,19 @@ protected void assertLocked(String lockName) { @Test public void shouldTimeout() throws InterruptedException { /** jetstreams smallest allowed unit is 100 milliseconds. */ - this.doTestTimeout(Duration.ofMillis(100)); - } - - @Override - protected void doTestTimeout(Duration lockAtMostFor) throws InterruptedException { - final var SHORT_LOCK_NAME = UUID.randomUUID().toString(); - - var configWithShortTimeout = lockConfig(SHORT_LOCK_NAME, lockAtMostFor, Duration.ZERO); - var lock1 = getLockProvider().lock(configWithShortTimeout); - assertThat(lock1).isNotEmpty(); - - // there is no config to control how fast NATS actually honors its TTL.. ie reaper timers ect - sleep(Duration.ofSeconds(2).toMillis()); - assertUnlocked(SHORT_LOCK_NAME); - - var lock2 = getLockProvider().lock(lockConfig(SHORT_LOCK_NAME, lockAtMostFor, Duration.ZERO)); - assertThat(lock2).isNotEmpty(); - lock2.get().unlock(); + this.doTestTimeout(Duration.ofMillis(100), Duration.ofSeconds(2)); } @Override @Test public void shouldLockAtLeastFor() throws InterruptedException { - doTestShouldLockAtLeastFor(100); + doTestShouldLockAtLeastFor(3_000); } @Override protected void doTestShouldLockAtLeastFor(int sleepForMs) throws InterruptedException { - final var ATLEAST_LOCK_NAME = UUID.randomUUID().toString(); - final var lockAtLeastFor = Duration.ofSeconds(1L); - - var lockConfig = lockConfig(ATLEAST_LOCK_NAME, lockAtLeastFor.multipliedBy(2), lockAtLeastFor); + Duration lockAtLeastFor = LOCK_AT_LEAST_FOR; + var lockConfig = lockConfig(LOCK_NAME1, lockAtLeastFor.multipliedBy(2), lockAtLeastFor); // Lock for LOCK_AT_LEAST_FOR - we do not expect the lock to be released before // this time @@ -108,7 +89,7 @@ protected void doTestShouldLockAtLeastFor(int sleepForMs) throws InterruptedExce lock1.get().unlock(); // Even though we have unlocked the lock, it will be held for some time - assertThat(getLockProvider().lock(lockConfig)) + assertThat(getLockProvider().lock(lockConfig(LOCK_NAME1))) .describedAs(getClass().getName() + "Can not acquire lock, grace period did not pass yet") .isEmpty(); @@ -117,6 +98,7 @@ protected void doTestShouldLockAtLeastFor(int sleepForMs) throws InterruptedExce sleep(lockAtLeastFor.toMillis() * 4); // Should be able to acquire now + // THIS is the difference when compared to the standard test var lock3 = getLockProvider().lock(lockConfig); assertThat(lock3) .describedAs(getClass().getName() + "Can acquire the lock after grace period") diff --git a/shedlock-test-support/src/main/java/net/javacrumbs/shedlock/test/support/AbstractLockProviderIntegrationTest.java b/shedlock-test-support/src/main/java/net/javacrumbs/shedlock/test/support/AbstractLockProviderIntegrationTest.java index 5f6954591..855175e73 100644 --- a/shedlock-test-support/src/main/java/net/javacrumbs/shedlock/test/support/AbstractLockProviderIntegrationTest.java +++ b/shedlock-test-support/src/main/java/net/javacrumbs/shedlock/test/support/AbstractLockProviderIntegrationTest.java @@ -87,11 +87,15 @@ public void shouldTimeout() throws InterruptedException { } protected void doTestTimeout(Duration lockAtMostFor) throws InterruptedException { + doTestTimeout(lockAtMostFor, lockAtMostFor.multipliedBy(2)); + } + + protected void doTestTimeout(Duration lockAtMostFor, Duration waitFor) throws InterruptedException { LockConfiguration configWithShortTimeout = lockConfig(LOCK_NAME1, lockAtMostFor, Duration.ZERO); Optional lock1 = getLockProvider().lock(configWithShortTimeout); assertThat(lock1).isNotEmpty(); - sleep(lockAtMostFor.toMillis() * 2); + sleep(waitFor.toMillis()); assertUnlocked(LOCK_NAME1); Optional lock2 =