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