Skip to content

Commit 1b57812

Browse files
authored
Merge pull request lukas-krecan#19 from lukas-krecan/check-times
Checking that lockAtMost is not before lockAtLeast
2 parents 9e6ae6d + 74e9701 commit 1b57812

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

shedlock-core/src/main/java/net/javacrumbs/shedlock/core/LockConfiguration.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ public LockConfiguration(String name, Instant lockUntil, Instant lockAtLeastUnti
4242
this.name = Objects.requireNonNull(name);
4343
this.lockAtMostUntil = Objects.requireNonNull(lockUntil);
4444
this.lockAtLeastUntil = Objects.requireNonNull(lockAtLeastUntil);
45+
if (lockAtLeastUntil.isAfter(lockAtMostUntil)) {
46+
throw new IllegalArgumentException("lockAtMost is before lockAtLeast for lock '" + name + "'.");
47+
}
48+
if (lockAtMostUntil.isBefore(Instant.now())) {
49+
throw new IllegalArgumentException("lockAtMost is in the past for lock '" + name + "'.");
50+
}
4551
}
4652

4753
public String getName() {

shedlock-provider-zookeeper-curator/src/test/java/net/javacrumbs/shedlock/provider/zookeeper/curator/ZookeeperCuratorLockProviderIntegrationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ public void shouldRemoveLockWhenClientStops() throws InterruptedException {
8282
@Test
8383
@Override
8484
public void shouldLockAtLeastFor() throws InterruptedException {
85-
LockConfiguration configWithGracePeriod = lockConfig(LOCK_NAME1, 0, LOCK_AT_LEAST_FOR);
86-
assertThatThrownBy(() -> getLockProvider().lock(configWithGracePeriod)).isInstanceOf(UnsupportedOperationException.class);
85+
LockConfiguration configWithAtLeastFor = lockConfig(LOCK_NAME1, LOCK_AT_LEAST_FOR, LOCK_AT_LEAST_FOR);
86+
assertThatThrownBy(() -> getLockProvider().lock(configWithAtLeastFor)).isInstanceOf(UnsupportedOperationException.class);
8787
}
8888

8989
@Override

shedlock-spring/src/test/java/net/javacrumbs/shedlock/spring/SpringLockConfigurationExtractorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
public class SpringLockConfigurationExtractorTest {
3838
public static final Duration DEFAULT_LOCK_TIME = Duration.of(30, ChronoUnit.MINUTES);
39-
public static final Duration DEFAULT_LOCK_AT_LEAST_FOR = Duration.of(5, ChronoUnit.MINUTES);
39+
public static final Duration DEFAULT_LOCK_AT_LEAST_FOR = Duration.of(5, ChronoUnit.MILLIS);
4040
private final SpringLockConfigurationExtractor extractor = new SpringLockConfigurationExtractor(DEFAULT_LOCK_TIME, DEFAULT_LOCK_AT_LEAST_FOR);
4141

4242

shedlock-test-support/src/main/java/net/javacrumbs/shedlock/test/support/AbstractLockProviderIntegrationTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@
2424
import java.time.Instant;
2525
import java.util.Optional;
2626
import java.util.concurrent.ExecutionException;
27-
import java.util.concurrent.TimeUnit;
2827

2928
import static java.lang.Thread.sleep;
30-
import static java.time.temporal.ChronoUnit.MILLIS;
29+
import static java.time.temporal.ChronoUnit.MINUTES;
30+
import static java.time.temporal.ChronoUnit.SECONDS;
3131
import static org.assertj.core.api.Assertions.assertThat;
3232

3333
public abstract class AbstractLockProviderIntegrationTest {
3434
protected static final String LOCK_NAME1 = "name";
35-
public static final Duration LOCK_AT_LEAST_FOR = Duration.of(1000, MILLIS);
35+
public static final Duration LOCK_AT_LEAST_FOR = Duration.of(5, SECONDS);
3636

3737
protected abstract LockProvider getLockProvider();
3838

@@ -85,7 +85,7 @@ public void shouldLockTwiceInARow() {
8585

8686
@Test
8787
public void shouldTimeout() throws InterruptedException {
88-
LockConfiguration configWithShortTimeout = lockConfig(LOCK_NAME1, 2, Duration.ZERO);
88+
LockConfiguration configWithShortTimeout = lockConfig(LOCK_NAME1, Duration.ofMillis(2), Duration.ZERO);
8989
Optional<SimpleLock> lock1 = getLockProvider().lock(configWithShortTimeout);
9090
assertThat(lock1).isNotEmpty();
9191

@@ -114,7 +114,7 @@ public void fuzzTestShouldPass() throws ExecutionException, InterruptedException
114114

115115
@Test
116116
public void shouldLockAtLeastFor() throws InterruptedException {
117-
LockConfiguration configWithGracePeriod = lockConfig(LOCK_NAME1, 0, LOCK_AT_LEAST_FOR);
117+
LockConfiguration configWithGracePeriod = lockConfig(LOCK_NAME1, LOCK_AT_LEAST_FOR, LOCK_AT_LEAST_FOR);
118118
Optional<SimpleLock> lock1 = getLockProvider().lock(configWithGracePeriod);
119119
assertThat(lock1).isNotEmpty();
120120
lock1.get().unlock();
@@ -131,12 +131,12 @@ public void shouldLockAtLeastFor() throws InterruptedException {
131131
}
132132

133133
protected static LockConfiguration lockConfig(String name) {
134-
return lockConfig(name, TimeUnit.MINUTES.toMillis(5), Duration.ZERO);
134+
return lockConfig(name, Duration.of(5, MINUTES), Duration.ZERO);
135135
}
136136

137-
protected static LockConfiguration lockConfig(String name, long timeoutMillis, Duration lockAtLeastFor) {
137+
protected static LockConfiguration lockConfig(String name, Duration lockAtMostFor, Duration lockAtLeastFor) {
138138
Instant now = Instant.now();
139-
return new LockConfiguration(name, now.plus(timeoutMillis, MILLIS), now.plus(lockAtLeastFor));
139+
return new LockConfiguration(name, now.plus(lockAtMostFor), now.plus(lockAtLeastFor));
140140
}
141141

142142
}

0 commit comments

Comments
 (0)