Search in sources :

Example 6 with SimpleLock

use of net.javacrumbs.shedlock.core.SimpleLock 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 7 with SimpleLock

use of net.javacrumbs.shedlock.core.SimpleLock 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 8 with SimpleLock

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

the class HsqlJdbcTemplateLockProviderIntegrationTest method shouldBeAbleToSetCustomColumnNames.

@Test
public void shouldBeAbleToSetCustomColumnNames() throws SQLException {
    try (Connection conn = dbConfig.getDataSource().getConnection();
        Statement statement = conn.createStatement()) {
        statement.execute("CREATE TABLE shdlck(n VARCHAR(64), lck_untl TIMESTAMP(3), lckd_at TIMESTAMP(3), lckd_by  VARCHAR(255), PRIMARY KEY (n))");
    }
    JdbcTemplateLockProvider provider = new JdbcTemplateLockProvider(builder().withTableName("shdlck").withColumnNames(new ColumnNames("n", "lck_untl", "lckd_at", "lckd_by")).withJdbcTemplate(new JdbcTemplate(dbConfig.getDataSource())).withLockedByValue("my-value").build());
    Optional<SimpleLock> lock = provider.lock(new LockConfiguration(now(), "test", Duration.ofSeconds(10), Duration.ZERO));
    lock.get().unlock();
}
Also used : ColumnNames(net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider.ColumnNames) Statement(java.sql.Statement) LockConfiguration(net.javacrumbs.shedlock.core.LockConfiguration) SimpleLock(net.javacrumbs.shedlock.core.SimpleLock) Connection(java.sql.Connection) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Test(org.junit.jupiter.api.Test)

Example 9 with SimpleLock

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

the class MemcachedLockProviderIntegrationTest method doTestTimeout.

/**
 * memcached smallest unit is second.
 */
@Override
protected void doTestTimeout(Duration lockAtMostFor) throws InterruptedException {
    LockConfiguration configWithShortTimeout = lockConfig(LOCK_NAME1, lockAtMostFor, Duration.ZERO);
    Optional<SimpleLock> lock1 = getLockProvider().lock(configWithShortTimeout);
    assertThat(lock1).isNotEmpty();
    sleep(lockAtMostFor.toMillis() * 2);
    assertUnlocked(LOCK_NAME1);
    Optional<SimpleLock> lock2 = getLockProvider().lock(lockConfig(LOCK_NAME1, Duration.ofSeconds(1), Duration.ZERO));
    assertThat(lock2).isNotEmpty();
    lock2.get().unlock();
}
Also used : LockConfiguration(net.javacrumbs.shedlock.core.LockConfiguration) SimpleLock(net.javacrumbs.shedlock.core.SimpleLock)

Example 10 with SimpleLock

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

the class AbstractJdbcLockProviderIntegrationTest method shouldNotFailIfKeyNameTooLong.

@Test
@Disabled
public void shouldNotFailIfKeyNameTooLong() {
    LockConfiguration configuration = lockConfig("lock name that is too long Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.");
    Optional<SimpleLock> lock = getLockProvider().lock(configuration);
    assertThat(lock).isEmpty();
}
Also used : LockConfiguration(net.javacrumbs.shedlock.core.LockConfiguration) SimpleLock(net.javacrumbs.shedlock.core.SimpleLock) Test(org.junit.jupiter.api.Test) AbstractStorageBasedLockProviderIntegrationTest(net.javacrumbs.shedlock.test.support.AbstractStorageBasedLockProviderIntegrationTest) Disabled(org.junit.jupiter.api.Disabled)

Aggregations

SimpleLock (net.javacrumbs.shedlock.core.SimpleLock)18 Test (org.junit.jupiter.api.Test)16 LockConfiguration (net.javacrumbs.shedlock.core.LockConfiguration)9 LockProvider (net.javacrumbs.shedlock.core.LockProvider)3 Duration (java.time.Duration)2 AbstractLockProviderIntegrationTest (net.javacrumbs.shedlock.test.support.AbstractLockProviderIntegrationTest)2 Connection (java.sql.Connection)1 Statement (java.sql.Statement)1 Instant (java.time.Instant)1 ColumnNames (net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider.ColumnNames)1 StorageBasedLockProvider (net.javacrumbs.shedlock.support.StorageBasedLockProvider)1 AbstractStorageBasedLockProviderIntegrationTest (net.javacrumbs.shedlock.test.support.AbstractStorageBasedLockProviderIntegrationTest)1 Disabled (org.junit.jupiter.api.Disabled)1 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)1