Skip to content

Commit 751643d

Browse files
committed
Fix expiry tests so they are not so sensitive to server expiry timing.
Closes #1500.
1 parent 4f74c0b commit 751643d

File tree

2 files changed

+49
-33
lines changed

2 files changed

+49
-33
lines changed

src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateKeyValueIntegrationTests.java

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,13 @@ void findByIdWithExpiry() {
103103
User foundUser = couchbaseTemplate.findById(User.class).withExpiry(Duration.ofSeconds(1)).one(user1.getId());
104104
user1.setVersion(foundUser.getVersion());// version will have changed
105105
assertEquals(user1, foundUser);
106-
sleepMs(3000);
107-
108-
Collection<User> foundUsers = (Collection<User>) couchbaseTemplate.findById(User.class)
109-
.all(Arrays.asList(user1.getId(), user2.getId()));
106+
int tries = 0;
107+
Collection<User> foundUsers;
108+
do {
109+
sleepSecs(1);
110+
foundUsers = (Collection<User>) couchbaseTemplate.findById(User.class)
111+
.all(Arrays.asList(user1.getId(), user2.getId()));
112+
} while (tries++ < 10 && foundUsers.size() != 1 && !user2.equals(foundUsers.iterator().next()));
110113
assertEquals(1, foundUsers.size(), "should have found exactly 1 user");
111114
assertEquals(user2, foundUsers.iterator().next());
112115
} finally {
@@ -286,24 +289,28 @@ void withExpiryAndExpiryAnnotation()
286289
}
287290
}
288291
// check that they are gone after a few seconds.
289-
sleepSecs(4);
290-
List<String> errorList = new LinkedList();
291-
for (User user : users) {
292-
User found = couchbaseTemplate.findById(user.getClass()).one(user.getId());
293-
if (user.getId().endsWith(UserAnnotated3.class.getSimpleName())) {
294-
if (found == null) {
295-
errorList.add("\nfound should be non null as it was set to have no expiry " + user.getId());
292+
int tries = 0;
293+
List<String> errorList = new LinkedList<>();
294+
do {
295+
sleepSecs(1);
296+
for (User user : users) {
297+
errorList = new LinkedList<>();
298+
User found = couchbaseTemplate.findById(user.getClass()).one(user.getId());
299+
if (user.getId().endsWith(UserAnnotated3.class.getSimpleName())) {
300+
if (found == null) {
301+
errorList.add("\nfound should be non null as it was set to have no expiry " + user.getId());
302+
}
303+
} else {
304+
if (found != null) {
305+
errorList.add("\nfound should have been null as document should be expired " + user.getId());
306+
}
296307
}
297-
} else {
298308
if (found != null) {
299-
errorList.add("\nfound should have been null as document should be expired " + user.getId());
309+
couchbaseTemplate.removeById(user.getClass()).one(user.getId());
300310
}
301311
}
302-
if (found != null) {
303-
couchbaseTemplate.removeById(user.getClass()).one(user.getId());
304-
}
305-
}
306312

313+
} while (tries++ < 10 && !errorList.isEmpty());
307314
if (!errorList.isEmpty()) {
308315
throw new RuntimeException(errorList.toString());
309316
}

src/test/java/org/springframework/data/couchbase/core/ReactiveCouchbaseTemplateKeyValueIntegrationTests.java

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,14 @@ void findByIdWithExpiry() {
103103
.one(user1.getId()).block();
104104
user1.setVersion(foundUser.getVersion());// version will have changed
105105
assertEquals(user1, foundUser);
106-
sleepMs(3000);
107106

108-
Collection<User> foundUsers = (Collection<User>) reactiveCouchbaseTemplate.findById(User.class)
109-
.all(Arrays.asList(user1.getId(), user2.getId())).collectList().block();
107+
int tries = 0;
108+
Collection<User> foundUsers;
109+
do {
110+
sleepSecs(1);
111+
foundUsers = (Collection<User>) reactiveCouchbaseTemplate.findById(User.class)
112+
.all(Arrays.asList(user1.getId(), user2.getId())).collectList().block();
113+
} while (tries++ < 10 && foundUsers.size() != 1 && !user2.equals(foundUsers.iterator().next()));
110114
assertEquals(1, foundUsers.size(), "should have found exactly 1 user");
111115
assertEquals(user2, foundUsers.iterator().next());
112116
} finally {
@@ -230,23 +234,28 @@ void withExpiryAndExpiryAnnotation()
230234
}
231235
}
232236
// check that they are gone after a few seconds.
233-
sleepSecs(4);
234-
List<String> errorList = new LinkedList();
235-
for (User user : users) {
236-
User found = reactiveCouchbaseTemplate.findById(user.getClass()).one(user.getId()).block();
237-
if (user.getId().endsWith(UserAnnotated3.class.getSimpleName())) {
238-
if (found == null) {
239-
errorList.add("\nfound should be non null as it was set to have no expiry " + user.getId());
237+
int tries = 0;
238+
List<String> errorList = new LinkedList<>();
239+
do {
240+
sleepSecs(1);
241+
for (User user : users) {
242+
errorList = new LinkedList<>();
243+
User found = reactiveCouchbaseTemplate.findById(user.getClass()).one(user.getId()).block();
244+
if (user.getId().endsWith(UserAnnotated3.class.getSimpleName())) {
245+
if (found == null) {
246+
errorList.add("\nfound should be non null as it was set to have no expiry " + user.getId());
247+
}
248+
} else {
249+
if (found != null) {
250+
errorList.add("\nfound should have been null as document should be expired " + user.getId());
251+
}
240252
}
241-
} else {
242253
if (found != null) {
243-
errorList.add("\nfound should have been null as document should be expired " + user.getId());
254+
couchbaseTemplate.removeById(user.getClass()).one(user.getId());
244255
}
245256
}
246-
if (found != null) {
247-
couchbaseTemplate.removeById(user.getClass()).one(user.getId());
248-
}
249-
}
257+
258+
} while (tries++ < 10 && !errorList.isEmpty());
250259

251260
if (!errorList.isEmpty()) {
252261
throw new RuntimeException(errorList.toString());

0 commit comments

Comments
 (0)