BenWhitehead commented on a change in pull request #14261:
URL: https://github.com/apache/beam/pull/14261#discussion_r597915778



##########
File path: 
sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/firestore/BaseFirestoreV1WriteFnTest.java
##########
@@ -0,0 +1,640 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.sdk.io.gcp.firestore;
+
+import static 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Lists.newArrayList;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+import static org.powermock.api.mockito.PowerMockito.spy;
+
+import com.google.api.gax.grpc.GrpcStatusCode;
+import com.google.api.gax.rpc.ApiException;
+import com.google.api.gax.rpc.ApiExceptionFactory;
+import com.google.api.gax.rpc.UnaryCallable;
+import com.google.firestore.v1.BatchWriteRequest;
+import com.google.firestore.v1.BatchWriteResponse;
+import com.google.firestore.v1.Write;
+import com.google.rpc.Code;
+import com.google.rpc.Status;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.beam.sdk.io.gcp.firestore.FirestoreV1Fn.HasRpcAttemptContext;
+import 
org.apache.beam.sdk.io.gcp.firestore.FirestoreV1WriteFn.BaseBatchWriteFn;
+import org.apache.beam.sdk.io.gcp.firestore.FirestoreV1WriteFn.WriteElement;
+import org.apache.beam.sdk.io.gcp.firestore.RpcQos.RpcWriteAttempt.Element;
+import org.apache.beam.sdk.io.gcp.firestore.RpcQos.RpcWriteAttempt.FlushBuffer;
+import org.apache.beam.sdk.io.gcp.firestore.RpcQosImpl.FlushBufferImpl;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.apache.beam.sdk.transforms.windowing.BoundedWindow;
+import org.joda.time.Instant;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+
+@SuppressWarnings("initialization.fields.uninitialized") // mockito fields are 
initialized via the Mockito Runner
+abstract class BaseFirestoreV1WriteFnTest<Out, Fn extends 
BaseBatchWriteFn<Out> & HasRpcAttemptContext> extends 
BaseFirestoreV1FnTest<Write, Out, Fn> {
+
+  protected static final Status STATUS_OK = 
Status.newBuilder().setCode(Code.OK.getNumber()).build();
+  @Mock(lenient = true)
+  protected BoundedWindow window;
+  @Mock
+  protected DoFn<Write, Out>.FinishBundleContext finishBundleContext;
+  @Mock
+  protected UnaryCallable<BatchWriteRequest, BatchWriteResponse> callable;
+  @Mock
+  protected RpcQos.RpcWriteAttempt attempt;
+  @Mock
+  protected RpcQos.RpcWriteAttempt attempt2;
+
+  @Before
+  public final void setUp() {
+    when(rpcQos.newWriteAttempt(any())).thenReturn(attempt, attempt2);
+
+    when(ff.getRpcQos(any()))
+        .thenReturn(rpcQos);
+    when(ff.getFirestoreRpc(pipelineOptions))
+        .thenReturn(rpc);
+    when(rpc.batchWriteCallable())
+        .thenReturn(callable);
+  }
+
+  @Override
+  @Test
+  public final void attemptsExhaustedForRetryableError() throws Exception {
+    Instant attemptStart = Instant.ofEpochMilli(0);
+    Instant rpc1Sta = Instant.ofEpochMilli(1);
+    Instant rpc1End = Instant.ofEpochMilli(2);
+    Instant rpc2Sta = Instant.ofEpochMilli(3);
+    Instant rpc2End = Instant.ofEpochMilli(4);
+    Instant rpc3Sta = Instant.ofEpochMilli(5);
+    Instant rpc3End = Instant.ofEpochMilli(6);
+    Write write = FirestoreProtoHelpers.newWrite();
+    Element<Write> element1 = new WriteElement(0, write, window);
+
+    when(ff.getFirestoreRpc(any())).thenReturn(rpc);
+    when(ff.getRpcQos(any())).thenReturn(rpcQos);
+    
when(rpcQos.newWriteAttempt(FirestoreV1Fn.V1FnRpcAttemptContext.BatchWrite)).thenReturn(attempt);
+    when(rpc.batchWriteCallable()).thenReturn(callable);
+
+    FlushBuffer<Element<Write>> flushBuffer = 
spy(newFlushBuffer(RPC_QOS_OPTIONS));
+    when(attempt.awaitSafeToProceed(any())).thenReturn(true);
+    when(attempt.<Write, 
Element<Write>>newFlushBuffer(attemptStart)).thenReturn(flushBuffer);
+    when(flushBuffer.offer(element1)).thenReturn(true);
+    when(flushBuffer.iterator()).thenReturn(newArrayList(element1).iterator());
+    when(flushBuffer.getBufferedElementsCount()).thenReturn(1);
+    when(flushBuffer.isFull()).thenReturn(true);
+
+    when(callable.call(any())).thenThrow(RETRYABLE_ERROR, RETRYABLE_ERROR, 
RETRYABLE_ERROR);
+    doNothing().when(attempt).recordFailedRequest(any(), anyInt());
+    
doNothing().doNothing().doThrow(RETRYABLE_ERROR).when(attempt).checkCanRetry(RETRYABLE_ERROR);
+
+    when(processContext.element()).thenReturn(write);
+
+    try {
+      runFunction(getFn(clock, ff, RPC_QOS_OPTIONS));
+      fail("Expected ApiException to be throw after exhausted attempts");
+    } catch (ApiException e) {
+      assertSame(RETRYABLE_ERROR, e);
+    }
+
+    verify(attempt, times(1)).awaitSafeToProceed(attemptStart);
+    verify(attempt, times(1)).recordStartRequest(rpc1Sta);
+    verify(attempt, times(1)).recordFailedRequest(rpc1End, 1);
+    verify(attempt, times(1)).recordStartRequest(rpc2Sta);
+    verify(attempt, times(1)).recordFailedRequest(rpc2End, 1);
+    verify(attempt, times(1)).recordStartRequest(rpc3Sta);
+    verify(attempt, times(1)).recordFailedRequest(rpc3End, 1);
+    verify(attempt, times(0)).recordSuccessfulRequest(any(), anyInt());
+    verify(attempt, never()).completeSuccess();
+  }
+
+  @Override
+  @Test
+  public final void noRequestIsSentIfNotSafeToProceed() throws Exception {
+    when(ff.getFirestoreRpc(any())).thenReturn(rpc);
+    when(ff.getRpcQos(any())).thenReturn(rpcQos);
+    
when(rpcQos.newWriteAttempt(FirestoreV1Fn.V1FnRpcAttemptContext.BatchWrite)).thenReturn(attempt);
+
+    InterruptedException interruptedException = new InterruptedException();
+    
when(attempt.awaitSafeToProceed(any())).thenReturn(false).thenThrow(interruptedException);
+
+    
when(processContext.element()).thenReturn(FirestoreProtoHelpers.newWrite());
+
+    try {
+      runFunction(getFn(clock, ff, RPC_QOS_OPTIONS));
+      fail("Expected ApiException to be throw after exhausted attempts");
+    } catch (InterruptedException e) {
+      assertSame(interruptedException, e);
+    }
+
+    verify(rpc, times(1)).close();
+    verifyNoMoreInteractions(rpc);
+    verifyNoMoreInteractions(callable);
+    verify(attempt, times(0)).recordFailedRequest(any(), anyInt());
+    verify(attempt, times(0)).recordSuccessfulRequest(any(), anyInt());
+  }
+
+  @Test
+  public abstract void enqueueingWritesValidateBytesSize() throws Exception;
+
+  @Test
+  public final void endToEnd_success() throws Exception {
+
+    Write write = FirestoreProtoHelpers.newWrite();
+    BatchWriteRequest expectedRequest = BatchWriteRequest.newBuilder()
+        .setDatabase("projects/testing-project/databases/(default)")

Review comment:
       not in this case, the unit tests are all in process and use mocks for 
all RPCs.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to