Skip to content

Commit b8e8b6d

Browse files
committed
address comments, add maxAttempts and refactor loop
1 parent ca1cd42 commit b8e8b6d

1 file changed

Lines changed: 14 additions & 21 deletions

File tree

google-cloud-datastore/src/test/java/com/google/cloud/datastore/it/ITDatastoreTest.java

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -139,25 +139,27 @@ public class ITDatastoreTest {
139139
private static final Entity ENTITY3 = Entity.newBuilder(ENTITY1).setKey(KEY3).remove("str")
140140
.set("null", NULL_VALUE).set("partial1", PARTIAL_ENTITY2).set("partial2", ENTITY2).build();
141141

142-
private <T> Iterator<T> getStronglyConsistentResults (Query scQuery, Query query) {
142+
private <T> Iterator<T> getStronglyConsistentResults (Query scQuery, Query query) throws InterruptedException {
143+
//scQuery is equivalent to query, but with an ancestor filter in it
144+
//This makes scQuery strongly consistent
143145
QueryResults<T> scResults = DATASTORE.run(scQuery);
144146
List<T> scResultsCopy = makeResultsCopy(scResults);
145147
Set<T> scResultsSet = new HashSet<>(scResultsCopy);
148+
int maxAttempts = 20;
146149

147-
infiniteloop:
148-
while(true) {
150+
while(maxAttempts > 0) {
151+
--maxAttempts;
149152
QueryResults<T> results = DATASTORE.run(query);
150153
List<T> resultsCopy = makeResultsCopy(results);
151-
if (!haveSameSize(scResultsCopy.iterator(), resultsCopy.iterator())) {
152-
continue;
154+
Set<T> resultsSet = new HashSet<>(resultsCopy);
155+
if (scResultsSet.size() == resultsSet.size()
156+
&& scResultsSet.containsAll(resultsSet)) {
157+
return resultsCopy.iterator();
153158
}
154-
for (T res: resultsCopy) {
155-
if (! scResultsSet.contains(res)) {
156-
continue infiniteloop;
157-
}
158-
}
159-
return resultsCopy.iterator();
159+
Thread.sleep(500);
160160
}
161+
162+
throw new RuntimeException("reached max number of attempts to get strongly consistent results.");
161163
}
162164

163165
private <T> List<T> makeResultsCopy(QueryResults<T> scResults) {
@@ -169,16 +171,6 @@ private <T> List<T> makeResultsCopy(QueryResults<T> scResults) {
169171
return results;
170172
}
171173

172-
private <T, S> boolean haveSameSize(Iterator<T> it1, Iterator<S> it2) {
173-
Preconditions.checkNotNull(it1);
174-
Preconditions.checkNotNull(it2);
175-
while(it1.hasNext() && it2.hasNext()) {
176-
it1.next();
177-
it2.next();
178-
}
179-
return !it1.hasNext() && !it2.hasNext();
180-
}
181-
182174
@Rule
183175
public Timeout globalTimeout = Timeout.seconds(100);
184176

@@ -438,6 +430,7 @@ public void testRunGqlQueryNoCasting() throws InterruptedException {
438430
.build();
439431
Query<ProjectionEntity> scKeyProjectionQuery =
440432
Query.newProjectionEntityQueryBuilder()
433+
.addProjection("__key__")
441434
.setNamespace(NAMESPACE)
442435
.setKind(KIND1)
443436
.setFilter(PropertyFilter.hasAncestor(ROOT_KEY))

0 commit comments

Comments
 (0)