Search in sources :

Example 1 with PartitionQueryPage

use of com.google.cloud.firestore.v1.FirestoreClient.PartitionQueryPage in project beam by apache.

the class FirestoreV1FnPartitionQueryTest method resumeFromLastReadValue.

@Override
public void resumeFromLastReadValue() throws Exception {
    when(ff.getFirestoreStub(any())).thenReturn(stub);
    when(ff.getRpcQos(any())).thenReturn(rpcQos);
    when(rpcQos.newReadAttempt(any())).thenReturn(attempt);
    when(attempt.awaitSafeToProceed(any())).thenReturn(true);
    // First page of the response
    PartitionQueryRequest request1 = PartitionQueryRequest.newBuilder().setParent(String.format("projects/%s/databases/(default)/document", projectId)).build();
    PartitionQueryResponse response1 = PartitionQueryResponse.newBuilder().addPartitions(Cursor.newBuilder().addValues(Value.newBuilder().setReferenceValue("doc-100"))).addPartitions(Cursor.newBuilder().addValues(Value.newBuilder().setReferenceValue("doc-200"))).addPartitions(Cursor.newBuilder().addValues(Value.newBuilder().setReferenceValue("doc-300"))).setNextPageToken("page2").build();
    when(page1.getResponse()).thenReturn(response1);
    when(callable.call(request1)).thenReturn(pagedResponse1);
    doNothing().when(attempt).checkCanRetry(any(), eq(RETRYABLE_ERROR));
    when(pagedResponse1.iteratePages()).thenAnswer(invocation -> new Iterable<PartitionQueryPage>() {

        @Override
        public Iterator<PartitionQueryPage> iterator() {
            return new AbstractIterator<PartitionQueryPage>() {

                private boolean first = true;

                @Override
                protected PartitionQueryPage computeNext() {
                    if (first) {
                        first = false;
                        return page1;
                    } else {
                        throw RETRYABLE_ERROR;
                    }
                }
            };
        }
    });
    // Second page of the response
    PartitionQueryRequest request2 = PartitionQueryRequest.newBuilder().setParent(String.format("projects/%s/databases/(default)/document", projectId)).setPageToken("page2").build();
    PartitionQueryResponse response2 = PartitionQueryResponse.newBuilder().addPartitions(Cursor.newBuilder().addValues(Value.newBuilder().setReferenceValue("doc-400"))).build();
    PartitionQueryResponse expectedResponse = response1.toBuilder().clearNextPageToken().addAllPartitions(response2.getPartitionsList()).build();
    when(page2.getResponse()).thenReturn(response2);
    when(page2.hasNextPage()).thenReturn(false);
    when(callable.call(request2)).thenReturn(pagedResponse2);
    when(pagedResponse2.iteratePages()).thenReturn(ImmutableList.of(page2));
    when(stub.partitionQueryPagedCallable()).thenReturn(callable);
    when(ff.getFirestoreStub(any())).thenReturn(stub);
    ArgumentCaptor<PartitionQueryPair> responses = ArgumentCaptor.forClass(PartitionQueryPair.class);
    doNothing().when(processContext).output(responses.capture());
    when(processContext.element()).thenReturn(request1);
    PartitionQueryFn fn = new PartitionQueryFn(clock, ff, rpcQosOptions);
    runFunction(fn);
    List<PartitionQueryPair> expected = newArrayList(new PartitionQueryPair(request1, expectedResponse));
    List<PartitionQueryPair> allValues = responses.getAllValues();
    assertEquals(expected, allValues);
}
Also used : PartitionQueryPage(com.google.cloud.firestore.v1.FirestoreClient.PartitionQueryPage) PartitionQueryPair(org.apache.beam.sdk.io.gcp.firestore.FirestoreV1ReadFn.PartitionQueryPair) PartitionQueryRequest(com.google.firestore.v1.PartitionQueryRequest) Iterator(java.util.Iterator) AbstractIterator(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.AbstractIterator) PartitionQueryResponse(com.google.firestore.v1.PartitionQueryResponse) PartitionQueryFn(org.apache.beam.sdk.io.gcp.firestore.FirestoreV1ReadFn.PartitionQueryFn)

Aggregations

PartitionQueryPage (com.google.cloud.firestore.v1.FirestoreClient.PartitionQueryPage)1 PartitionQueryRequest (com.google.firestore.v1.PartitionQueryRequest)1 PartitionQueryResponse (com.google.firestore.v1.PartitionQueryResponse)1 Iterator (java.util.Iterator)1 PartitionQueryFn (org.apache.beam.sdk.io.gcp.firestore.FirestoreV1ReadFn.PartitionQueryFn)1 PartitionQueryPair (org.apache.beam.sdk.io.gcp.firestore.FirestoreV1ReadFn.PartitionQueryPair)1 AbstractIterator (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.AbstractIterator)1