Skip to content

Commit c6448fc

Browse files
ehsannascherylEnkidugcf-owl-bot[bot]
authored
feat: Logical termination for firestore.getAll(...). (#1517)
* feat: Logical termination for firestore.getAll(...). * Using existing unit tests to verify the behaviour * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * transaction unit tests are not mocking response correctly, so it cannot run with logical termination --------- Co-authored-by: cherylEnkidu <cheryllin@google.com> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent e25ae13 commit c6448fc

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreImpl.java

+10
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ void getAll(
222222
ResponseObserver<BatchGetDocumentsResponse> responseObserver =
223223
new ResponseObserver<BatchGetDocumentsResponse>() {
224224
int numResponses;
225+
boolean hasCompleted = false;
225226

226227
@Override
227228
public void onStart(StreamController streamController) {}
@@ -265,6 +266,13 @@ public void onResponse(BatchGetDocumentsResponse response) {
265266
return;
266267
}
267268
apiStreamObserver.onNext(documentSnapshot);
269+
270+
// Logical termination: if we have already received as many documents as we had
271+
// requested, we can
272+
// raise the results without waiting for the termination from the server.
273+
if (numResponses == documentReferences.length) {
274+
onComplete();
275+
}
268276
}
269277

270278
@Override
@@ -277,6 +285,8 @@ public void onError(Throwable throwable) {
277285

278286
@Override
279287
public void onComplete() {
288+
if (hasCompleted) return;
289+
hasCompleted = true;
280290
tracer
281291
.getCurrentSpan()
282292
.addAnnotation(TraceUtil.SPAN_NAME_BATCHGETDOCUMENTS + ": Complete");

google-cloud-firestore/src/test/java/com/google/cloud/firestore/FirestoreTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import static com.google.cloud.firestore.LocalFirestoreHelper.arrayUnion;
2424
import static com.google.cloud.firestore.LocalFirestoreHelper.commit;
2525
import static com.google.cloud.firestore.LocalFirestoreHelper.commitResponse;
26-
import static com.google.cloud.firestore.LocalFirestoreHelper.getAllResponse;
26+
import static com.google.cloud.firestore.LocalFirestoreHelper.getAllResponseWithoutOnComplete;
2727
import static com.google.cloud.firestore.LocalFirestoreHelper.transform;
2828
import static com.google.cloud.firestore.LocalFirestoreHelper.update;
2929
import static org.junit.Assert.assertEquals;
@@ -80,7 +80,7 @@ public void encodeFieldPath() {
8080

8181
@Test
8282
public void illegalFieldPath() throws Exception {
83-
doAnswer(getAllResponse(SINGLE_FIELD_PROTO))
83+
doAnswer(getAllResponseWithoutOnComplete(SINGLE_FIELD_PROTO))
8484
.when(firestoreMock)
8585
.streamRequest(
8686
getAllCapture.capture(),
@@ -110,7 +110,7 @@ public void exposesOptions() {
110110
@Test
111111
public void getAll() throws Exception {
112112
doAnswer(
113-
getAllResponse(
113+
getAllResponseWithoutOnComplete(
114114
SINGLE_FIELD_PROTO, SINGLE_FIELD_PROTO, SINGLE_FIELD_PROTO, SINGLE_FIELD_PROTO))
115115
.when(firestoreMock)
116116
.streamRequest(
@@ -132,7 +132,7 @@ public void getAll() throws Exception {
132132

133133
@Test
134134
public void getAllWithFieldMask() throws Exception {
135-
doAnswer(getAllResponse(SINGLE_FIELD_PROTO))
135+
doAnswer(getAllResponseWithoutOnComplete(SINGLE_FIELD_PROTO))
136136
.when(firestoreMock)
137137
.streamRequest(
138138
getAllCapture.capture(),

google-cloud-firestore/src/test/java/com/google/cloud/firestore/LocalFirestoreHelper.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,16 @@ public static Map<String, Object> map() {
262262

263263
public static Answer<BatchGetDocumentsResponse> getAllResponse(
264264
final Map<String, Value>... fields) {
265+
return getAllResponseImpl(true, fields);
266+
}
267+
268+
public static Answer<BatchGetDocumentsResponse> getAllResponseWithoutOnComplete(
269+
final Map<String, Value>... fields) {
270+
return getAllResponseImpl(false, fields);
271+
}
272+
273+
public static Answer<BatchGetDocumentsResponse> getAllResponseImpl(
274+
boolean withOnComplete, final Map<String, Value>... fields) {
265275
BatchGetDocumentsResponse[] responses = new BatchGetDocumentsResponse[fields.length];
266276

267277
for (int i = 0; i < fields.length; ++i) {
@@ -281,7 +291,13 @@ public static Answer<BatchGetDocumentsResponse> getAllResponse(
281291
responses[i] = response.build();
282292
}
283293

284-
return streamingResponse(responses, null);
294+
if (withOnComplete) {
295+
return streamingResponse(responses, null);
296+
} else {
297+
// Verify with logical termination, the return of results no longer depends on calling
298+
// OnComplete.
299+
return streamingResponseWithoutOnComplete(responses);
300+
}
285301
}
286302

287303
public static ApiFuture<Empty> rollbackResponse() {

0 commit comments

Comments
 (0)