MultiRedisLock implements Lock { @Override public boolean release(final List<String> keys, final String storeId, final String token) { final var keysWithStoreIdPrefix = keys.stream().map(key -> storeId + ":" + key).collect(Collectors.toList()); final var released = stringRedisTemplate.execute(lockReleaseScript, keysWithStoreIdPrefix, token); if (released) { log.debug("Release script deleted the record for keys {} with token {} in store {}", keys, token, storeId); } else { log.error("Release script failed for keys {} with token {} in store {}", keys, token, storeId); } return released; } MultiRedisLock(final StringRedisTemplate stringRedisTemplate); @Override String acquire(final List<String> keys, final String storeId, final long expiration); @Override boolean release(final List<String> keys, final String storeId, final String token); @Override boolean refresh(final List<String> keys, final String storeId, final String token, final long expiration);  }
@Test public void shouldReleaseSingleKey() { redisTemplate.opsForValue().set("locks:1", "abc"); lock.release(Collections.singletonList("1"), "locks", "abc"); assertThat(redisTemplate.opsForValue().get("locks:1")).isNull(); }
@Test public void shouldReleaseMultipleKeys() { redisTemplate.opsForValue().set("locks:1", "abc"); redisTemplate.opsForValue().set("locks:2", "abc"); lock.release(Arrays.asList("1", "2"), "locks", "abc"); assertThat(redisTemplate.opsForValue().get("locks:1")).isNull(); assertThat(redisTemplate.opsForValue().get("locks:2")).isNull(); }
@Test public void shouldNotReleaseWhenTokenDoesNotFullyMatch() { redisTemplate.opsForValue().set("locks:1", "def"); redisTemplate.opsForValue().set("locks:2", "ghi"); lock.release(Arrays.asList("1", "2"), "locks", "abc"); assertThat(redisTemplate.opsForValue().get("locks:1")).isEqualTo("def"); assertThat(redisTemplate.opsForValue().get("locks:2")).isEqualTo("ghi"); }
@Test public void shouldNotReleaseWhenTokenDoesNotPartiallyMatch() { redisTemplate.opsForValue().set("locks:1", "def"); lock.release(Arrays.asList("1", "2"), "locks", "abc"); assertThat(redisTemplate.opsForValue().get("locks:1")).isEqualTo("def"); assertThat(redisTemplate.opsForValue().get("locks:2")).isNull(); }