SimpleJdbcLock extends AbstractSimpleLock { @Override protected String acquire(final String key, final String storeId, final String token, final long expiration) { return lockSingleKeyService.acquire(key, storeId, token, expiration); } SimpleJdbcLock(final Supplier<String> tokenSupplier, final JdbcLockSingleKeyService lockSingleKeyService);   }
@Test public void shouldLock() { final var now = System.currentTimeMillis(); final var token = lock.acquire(Collections.singletonList("1"), "locks", 1000); assertThat(token).isEqualTo("abc"); final var acquiredLockMap = jdbcTemplate.queryForObject("SELECT * FROM locks WHERE id = 1", new ColumnMapRowMapper()); assertThat(acquiredLockMap).containsAllEntriesOf(values("1", "abc")); final var expireAt = acquiredLockMap.get("expireAt"); assertThat(((Date) expireAt).getTime()).isCloseTo(now + 1000, Offset.offset(100L)); }
@Test public void shouldNotLock() { new SimpleJdbcInsert(jdbcTemplate) .withTableName("locks") .usingGeneratedKeyColumns("id") .executeAndReturnKey(values("1", "def")); final var token = lock.acquire(Collections.singletonList("1"), "locks", 1000); assertThat(token).isNull(); final var acquiredLockMap = jdbcTemplate.queryForObject("SELECT * FROM locks WHERE id = 1", new ColumnMapRowMapper()); assertThat(acquiredLockMap).containsAllEntriesOf(values("1", "def")); }