SimpleMongoLock extends AbstractSimpleLock { @Override protected boolean refresh(final String key, final String storeId, final String token, final long expiration) { final var updated = mongoTemplate.updateFirst(Query.query(Criteria.where("_id").is(key).and("token").is(token)), Update.update("expireAt", LocalDateTime.now().plus(expiration, ChronoUnit.MILLIS)), storeId); final var refreshed = updated.getModifiedCount() == 1; if (refreshed) { log.debug("Refresh query successfully affected 1 record for key {} with token {} in store {}", key, token, storeId); } else if (updated.getModifiedCount() > 0) { log.error("Unexpected result from refresh for key {} with token {} in store {}, released {}", key, token, storeId, updated); } else { log.warn("Refresh query did not affect any records for key {} with token {} in store {}. This is possible when refresh interval fires for the final time after the lock has been released", key, token, storeId); } return refreshed; } SimpleMongoLock(final Supplier<String> tokenSupplier, final MongoTemplate mongoTemplate);   }
@Test public void shouldNotRefreshBecauseKeyExpired() { assertThat(lock.refresh(Collections.singletonList("1"), "locks", "abc", 1000)).isFalse(); assertThat(mongoTemplate.findAll(LockDocument.class)).isNullOrEmpty(); }