SimpleMongoLock extends AbstractSimpleLock { @Override protected boolean release(final String key, final String storeId, final String token) { final var deleted = mongoTemplate.remove(Query.query(Criteria.where("_id").is(key).and("token").is(token)), storeId); final var released = deleted.getDeletedCount() == 1; if (released) { log.debug("Remove query successfully affected 1 record for key {} with token {} in store {}", key, token, storeId); } else if (deleted.getDeletedCount() > 0) { log.error("Unexpected result from release for key {} with token {} in store {}, released {}", key, token, storeId, deleted); } else { log.error("Remove query did not affect any records for key {} with token {} in store {}", key, token, storeId); } return released; } SimpleMongoLock(final Supplier<String> tokenSupplier, final MongoTemplate mongoTemplate);   }
@Test public void shouldRelease() { mongoTemplate.insert(new LockDocument("1", LocalDateTime.now().plusMinutes(1), "abc"), "locks"); final var released = lock.release(Collections.singletonList("1"), "locks", "abc"); assertThat(released).isTrue(); assertThat(mongoTemplate.findById("1", LockDocument.class, "locks")).isNull(); }
@Test public void shouldNotRelease() { mongoTemplate.insert(new LockDocument("1", LocalDateTime.now().plusMinutes(1), "def"), "locks"); final var released = lock.release(Collections.singletonList("1"), "locks", "abc"); assertThat(released).isFalse(); assertThat(mongoTemplate.findById("1", LockDocument.class, "locks").getToken()).isEqualTo("def"); }