|
55 | 55 | import org.springframework.data.couchbase.domain.UserAnnotated;
|
56 | 56 | import org.springframework.data.couchbase.domain.UserAnnotated2;
|
57 | 57 | import org.springframework.data.couchbase.domain.UserAnnotated3;
|
| 58 | +import org.springframework.data.couchbase.domain.UserAnnotatedTouchOnRead; |
58 | 59 | import org.springframework.data.couchbase.domain.UserSubmission;
|
59 | 60 | import org.springframework.data.couchbase.util.ClusterType;
|
60 | 61 | import org.springframework.data.couchbase.util.IgnoreWhen;
|
@@ -130,24 +131,60 @@ void findByIdWithExpiryAnnotation() {
|
130 | 131 | assertEquals(user2, foundUser2);
|
131 | 132 |
|
132 | 133 | // now set user1 expiration back to 1 second with getAndTouch using the @Document(expiry=1) annotation
|
| 134 | + // This will have no effect as UserAnnotated does not have touchOnGet |
133 | 135 | foundUser1 = couchbaseTemplate.findById(UserAnnotated.class).one(user1.getId());
|
134 | 136 | user1.setVersion(foundUser1.getVersion());// version will have changed
|
135 | 137 | assertEquals(user1, foundUser1);
|
136 | 138 |
|
137 | 139 | // user1 should be gone, user2 should still be there
|
138 | 140 | int tries = 0;
|
139 |
| - Collection<User> foundUsers; |
| 141 | + Collection<UserAnnotated> foundUsers; |
140 | 142 | do {
|
141 |
| - sleepSecs(1); |
142 |
| - foundUsers = (Collection<User>) couchbaseTemplate.findById(User.class) |
| 143 | + sleepSecs(3); |
| 144 | + foundUsers = (Collection<UserAnnotated>) couchbaseTemplate.findById(UserAnnotated.class) |
143 | 145 | .all(Arrays.asList(user1.getId(), user2.getId()));
|
144 |
| - } while (tries++ < 7 && foundUsers.size() != 1 && !user2.equals(foundUsers.iterator().next())); |
145 |
| - assertEquals(1, foundUsers.size(), "should have found exactly 1 user"); |
146 |
| - assertEquals(user2, foundUsers.iterator().next()); |
| 146 | + } while (tries++ < 7 && foundUsers.size() != 2 && !user2.equals(foundUsers.iterator().next())); |
| 147 | + assertEquals(2, foundUsers.size(), "should have found exactly 2 users"); |
147 | 148 | } finally {
|
148 |
| - couchbaseTemplate.removeByQuery(User.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).all(); |
| 149 | + couchbaseTemplate.removeByQuery(UserAnnotated.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).all(); |
149 | 150 | }
|
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + void findByIdWithExpiryAnnotationTouchOnRead() { |
| 155 | + try { |
| 156 | + UserAnnotatedTouchOnRead user1 = new UserAnnotatedTouchOnRead(UUID.randomUUID().toString(), "user1", "user1"); |
| 157 | + UserAnnotatedTouchOnRead user2 = new UserAnnotatedTouchOnRead(UUID.randomUUID().toString(), "user2", "user2"); |
| 158 | + |
| 159 | + Collection<UserAnnotatedTouchOnRead> upserts = (Collection<UserAnnotatedTouchOnRead>) couchbaseTemplate.upsertById(UserAnnotatedTouchOnRead.class) |
| 160 | + .all(Arrays.asList(user1, user2)); |
| 161 | + |
| 162 | + // explicitly set expiry to 10 seconds |
| 163 | + UserAnnotatedTouchOnRead foundUser1 = couchbaseTemplate.findById(UserAnnotatedTouchOnRead.class).withExpiry(Duration.ofSeconds(10)).one(user1.getId()); |
| 164 | + user1.setVersion(foundUser1.getVersion());// version will have changed |
| 165 | + assertEquals(user1, foundUser1); |
| 166 | + UserAnnotatedTouchOnRead foundUser2 = couchbaseTemplate.findById(UserAnnotatedTouchOnRead.class).withExpiry(Duration.ofSeconds(10)).one(user2.getId()); |
| 167 | + user2.setVersion(foundUser2.getVersion());// version will have changed |
| 168 | + assertEquals(user2, foundUser2); |
| 169 | + |
| 170 | + // now set user1 expiration back to 1 second with getAndTouch using the @Document(expiry=1) annotation |
| 171 | + foundUser1 = couchbaseTemplate.findById(UserAnnotatedTouchOnRead.class).one(user1.getId()); |
| 172 | + user1.setVersion(foundUser1.getVersion());// version will have changed |
| 173 | + assertEquals(user1, foundUser1); |
150 | 174 |
|
| 175 | + // user1 should be gone, user2 should still be there |
| 176 | + int tries = 0; |
| 177 | + Collection<UserAnnotatedTouchOnRead> foundUsers; |
| 178 | + do { |
| 179 | + sleepSecs(3); |
| 180 | + foundUsers = (Collection<UserAnnotatedTouchOnRead>) couchbaseTemplate.findById(UserAnnotatedTouchOnRead.class) |
| 181 | + .all(Arrays.asList(user1.getId(), user2.getId())); |
| 182 | + } while (tries++ < 7 && foundUsers.size() != 1 && !user2.equals(foundUsers.iterator().next())); |
| 183 | + assertEquals(1, foundUsers.size(), "should have found exactly 1 user1"); |
| 184 | + assertEquals(user2.getId(), foundUsers.iterator().next().getId()); |
| 185 | + } finally { |
| 186 | + couchbaseTemplate.removeByQuery(UserAnnotatedTouchOnRead.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).all(); |
| 187 | + } |
151 | 188 | }
|
152 | 189 | @Test
|
153 | 190 | void upsertAndFindById() {
|
|
0 commit comments