Take expiry from entity class annotation for getAndTouch. (#1640)

Closes #1634.
This commit is contained in:
Michael Reiche
2023-01-10 14:14:38 -08:00
committed by GitHub
parent 1d6727bc34
commit 897d3fc5c2
2 changed files with 54 additions and 17 deletions

View File

@@ -118,6 +118,43 @@ class CouchbaseTemplateKeyValueIntegrationTests extends JavaIntegrationTests {
}
@Test
void findByIdWithExpiryAnnotation() {
try {
UserAnnotated user1 = new UserAnnotated(UUID.randomUUID().toString(), "user1", "user1");
UserAnnotated user2 = new UserAnnotated(UUID.randomUUID().toString(), "user2", "user2");
Collection<UserAnnotated> upserts = (Collection<UserAnnotated>) couchbaseTemplate.upsertById(UserAnnotated.class)
.all(Arrays.asList(user1, user2));
// explicitly set expiry to 10 seconds
UserAnnotated foundUser1 = couchbaseTemplate.findById(UserAnnotated.class).withExpiry(Duration.ofSeconds(10)).one(user1.getId());
user1.setVersion(foundUser1.getVersion());// version will have changed
assertEquals(user1, foundUser1);
UserAnnotated foundUser2 = couchbaseTemplate.findById(UserAnnotated.class).withExpiry(Duration.ofSeconds(10)).one(user2.getId());
user2.setVersion(foundUser2.getVersion());// version will have changed
assertEquals(user2, foundUser2);
// now set user1 expiration back to 1 second with getAndTouch using the @Document(expiry=1) annotation
foundUser1 = couchbaseTemplate.findById(UserAnnotated.class).one(user1.getId());
user1.setVersion(foundUser1.getVersion());// version will have changed
assertEquals(user1, foundUser1);
// user1 should be gone, user2 should still be there
int tries = 0;
Collection<User> foundUsers;
do {
sleepSecs(1);
foundUsers = (Collection<User>) couchbaseTemplate.findById(User.class)
.all(Arrays.asList(user1.getId(), user2.getId()));
} while (tries++ < 7 && foundUsers.size() != 1 && !user2.equals(foundUsers.iterator().next()));
assertEquals(1, foundUsers.size(), "should have found exactly 1 user");
assertEquals(user2, foundUsers.iterator().next());
} finally {
couchbaseTemplate.removeByQuery(User.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).all();
}
}
@Test
void upsertAndFindById() {
User user = new User(UUID.randomUUID().toString(), "firstname", "lastname");