Search in sources :

Example 1 with LockConfiguration

use of net.javacrumbs.shedlock.core.LockConfiguration in project ShedLock by lukas-krecan.

the class AbstractLockProviderIntegrationTest method shouldBeAbleToLockRightAfterUnlock.

@Test
public void shouldBeAbleToLockRightAfterUnlock() {
    LockConfiguration lockConfiguration = lockConfig(LOCK_NAME1);
    for (int i = 0; i < 10; i++) {
        Optional<SimpleLock> lock = getLockProvider().lock(lockConfiguration);
        assertThat(lock).describedAs("Successfully locked").isNotEmpty();
        assertThat(getLockProvider().lock(lockConfiguration)).isEmpty();
        assertThat(lock).isNotEmpty();
        lock.get().unlock();
    }
}
Also used : LockConfiguration(net.javacrumbs.shedlock.core.LockConfiguration) SimpleLock(net.javacrumbs.shedlock.core.SimpleLock) Test(org.junit.jupiter.api.Test)

Example 2 with LockConfiguration

use of net.javacrumbs.shedlock.core.LockConfiguration in project ShedLock by lukas-krecan.

the class AbstractStorageBasedLockProviderIntegrationTest method lockShouldSurviveCacheClearingInTheMiddle.

@Test
public void lockShouldSurviveCacheClearingInTheMiddle() {
    StorageBasedLockProvider provider = getLockProvider();
    LockConfiguration configuration = lockConfig(LOCK_NAME1);
    Optional<SimpleLock> lock = provider.lock(configuration);
    assertThat(lock).isPresent();
    provider.clearCache();
    // lock is still locked
    assertThat(provider.lock(lockConfig(LOCK_NAME1))).isEmpty();
    lock.get().unlock();
}
Also used : LockConfiguration(net.javacrumbs.shedlock.core.LockConfiguration) SimpleLock(net.javacrumbs.shedlock.core.SimpleLock) StorageBasedLockProvider(net.javacrumbs.shedlock.support.StorageBasedLockProvider) Test(org.junit.jupiter.api.Test)

Example 3 with LockConfiguration

use of net.javacrumbs.shedlock.core.LockConfiguration in project ShedLock by lukas-krecan.

the class ConsulLockProviderIntegrationTest method shouldTimeout.

@Test
@Override
public void shouldTimeout() throws InterruptedException {
    // as consul has 10 seconds ttl minimum and has double ttl unlocking time, you have to wait for 20 seconds for the unlock time.
    Duration lockAtMostFor = Duration.ofSeconds(11);
    LockConfiguration configWithShortTimeout = lockConfig(LOCK_NAME1, lockAtMostFor, Duration.ZERO);
    Optional<SimpleLock> lock1 = getLockProvider().lock(configWithShortTimeout);
    assertThat(lock1).isNotEmpty();
    sleep(lockAtMostFor.multipliedBy(2).toMillis() + 100);
    assertUnlocked(LOCK_NAME1);
    Optional<SimpleLock> lock2 = getLockProvider().lock(lockConfig(LOCK_NAME1, Duration.ofMillis(50), Duration.ZERO));
    assertThat(lock2).isNotEmpty();
    lock2.get().unlock();
}
Also used : LockConfiguration(net.javacrumbs.shedlock.core.LockConfiguration) SimpleLock(net.javacrumbs.shedlock.core.SimpleLock) Duration(java.time.Duration) AbstractLockProviderIntegrationTest(net.javacrumbs.shedlock.test.support.AbstractLockProviderIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 4 with LockConfiguration

use of net.javacrumbs.shedlock.core.LockConfiguration in project ShedLock by lukas-krecan.

the class ConsulLockProviderIntegrationTest method shouldNotTimeoutIfLessThanMinTtlPassed.

@Test
public void shouldNotTimeoutIfLessThanMinTtlPassed() throws InterruptedException {
    Duration lockAtMostFor = Duration.ofSeconds(1);
    LockConfiguration configWithShortTimeout = lockConfig(LOCK_NAME1, lockAtMostFor, Duration.ZERO);
    Optional<SimpleLock> lock1 = getLockProvider().lock(configWithShortTimeout);
    assertThat(lock1).isNotEmpty();
    sleep(lockAtMostFor.multipliedBy(2).toMillis() + 100);
    assertLocked(LOCK_NAME1);
    // release lock to satisfy condition for #checkSessions()
    lock1.get().unlock();
}
Also used : LockConfiguration(net.javacrumbs.shedlock.core.LockConfiguration) SimpleLock(net.javacrumbs.shedlock.core.SimpleLock) Duration(java.time.Duration) AbstractLockProviderIntegrationTest(net.javacrumbs.shedlock.test.support.AbstractLockProviderIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 5 with LockConfiguration

use of net.javacrumbs.shedlock.core.LockConfiguration in project ShedLock by lukas-krecan.

the class AbstractJdbcTemplateStorageAccessorTest method shouldNotUpdateOtherLockConfigurations.

private void shouldNotUpdateOtherLockConfigurations(boolean usingDbTime) throws InterruptedException {
    JdbcTemplateStorageAccessor accessor = getAccessor(usingDbTime);
    Duration lockAtMostFor = Duration.ofMillis(10);
    assertThat(accessor.insertRecord(lockConfig(MY_LOCK, lockAtMostFor))).isEqualTo(true);
    assertThat(accessor.insertRecord(lockConfig(OTHER_LOCK, lockAtMostFor))).isEqualTo(true);
    Timestamp myLockLockedUntil = testUtils.getLockedUntil(MY_LOCK);
    Timestamp otherLockLockedUntil = testUtils.getLockedUntil(OTHER_LOCK);
    // wait for a while so there will be a difference in the timestamp
    // when system time is used seems there is no milliseconds in the timestamp so to make a difference we have to wait for at least a second
    sleep(1000);
    // act
    assertThat(accessor.updateRecord(new LockConfiguration(now(), MY_LOCK, lockAtMostFor, Duration.ZERO))).isEqualTo(true);
    // assert
    assertThat(testUtils.getLockedUntil(MY_LOCK)).isAfter(myLockLockedUntil);
    // check that the other lock has not been affected by "my-lock" update
    assertThat(testUtils.getLockedUntil(OTHER_LOCK)).isEqualTo(otherLockLockedUntil);
}
Also used : LockConfiguration(net.javacrumbs.shedlock.core.LockConfiguration) Duration(java.time.Duration) Timestamp(java.sql.Timestamp)

Aggregations

LockConfiguration (net.javacrumbs.shedlock.core.LockConfiguration)15 Test (org.junit.jupiter.api.Test)10 SimpleLock (net.javacrumbs.shedlock.core.SimpleLock)9 Duration (java.time.Duration)4 Instant (java.time.Instant)3 ScheduledMethodRunnable (org.springframework.scheduling.support.ScheduledMethodRunnable)3 AbstractLockProviderIntegrationTest (net.javacrumbs.shedlock.test.support.AbstractLockProviderIntegrationTest)2 R2dbcDataIntegrityViolationException (io.r2dbc.spi.R2dbcDataIntegrityViolationException)1 Statement (io.r2dbc.spi.Statement)1 Connection (java.sql.Connection)1 Statement (java.sql.Statement)1 Timestamp (java.sql.Timestamp)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 BiFunction (java.util.function.BiFunction)1 Function (java.util.function.Function)1 ClockProvider (net.javacrumbs.shedlock.core.ClockProvider)1 ColumnNames (net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider.ColumnNames)1 AbstractStorageAccessor (net.javacrumbs.shedlock.support.AbstractStorageAccessor)1 LockException (net.javacrumbs.shedlock.support.LockException)1 StorageBasedLockProvider (net.javacrumbs.shedlock.support.StorageBasedLockProvider)1