Skip to content

Commit

Permalink
Test cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-krecan committed Nov 18, 2024
1 parent 9955124 commit 61b32ac
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Optional<SimpleLock> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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();

Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SimpleLock> lock1 = getLockProvider().lock(configWithShortTimeout);
assertThat(lock1).isNotEmpty();

sleep(lockAtMostFor.toMillis() * 2);
sleep(waitFor.toMillis());
assertUnlocked(LOCK_NAME1);

Optional<SimpleLock> lock2 =
Expand Down

0 comments on commit 61b32ac

Please sign in to comment.