zoercai commented on a change in pull request #16846:
URL: https://github.com/apache/beam/pull/16846#discussion_r806469295



##########
File path: 
sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/SpannerChangeStreamErrorTest.java
##########
@@ -0,0 +1,494 @@
+/*
+ * 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.spanner.changestreams;
+
+import static 
org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_CREATED_AT;
+import static 
org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_END_TIMESTAMP;
+import static 
org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_FINISHED_AT;
+import static 
org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_HEARTBEAT_MILLIS;
+import static 
org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_PARENT_TOKENS;
+import static 
org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_PARTITION_TOKEN;
+import static 
org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_RUNNING_AT;
+import static 
org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_SCHEDULED_AT;
+import static 
org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_START_TIMESTAMP;
+import static 
org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_STATE;
+import static 
org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_WATERMARK;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import com.google.api.gax.grpc.testing.MockServiceHelper;
+import com.google.api.gax.retrying.RetrySettings;
+import com.google.cloud.Timestamp;
+import com.google.cloud.spanner.ErrorCode;
+import com.google.cloud.spanner.MockSpannerServiceImpl;
+import com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime;
+import com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult;
+import com.google.cloud.spanner.Statement;
+import com.google.protobuf.ListValue;
+import com.google.protobuf.NullValue;
+import com.google.protobuf.Value;
+import com.google.spanner.v1.ExecuteSqlRequest;
+import com.google.spanner.v1.ResultSet;
+import com.google.spanner.v1.ResultSetMetadata;
+import com.google.spanner.v1.StructType;
+import com.google.spanner.v1.StructType.Field;
+import com.google.spanner.v1.Type;
+import com.google.spanner.v1.TypeCode;
+import io.grpc.Status;
+import java.io.Serializable;
+import java.util.Collections;
+import org.apache.beam.sdk.Pipeline.PipelineExecutionException;
+import org.apache.beam.sdk.io.gcp.spanner.SpannerConfig;
+import org.apache.beam.sdk.io.gcp.spanner.SpannerIO;
+import org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.DaoFactory;
+import 
org.apache.beam.sdk.io.gcp.spanner.changestreams.model.PartitionMetadata.State;
+import org.apache.beam.sdk.options.ValueProvider.StaticValueProvider;
+import org.apache.beam.sdk.testing.TestPipeline;
+import 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableSet;
+import org.hamcrest.Matchers;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+@RunWith(JUnit4.class)
+public class SpannerChangeStreamErrorTest implements Serializable {
+
+  public static final String SPANNER_HOST = "my-host";
+  private static final String TEST_PROJECT = "my-project";
+  private static final String TEST_INSTANCE = "my-instance";
+  private static final String TEST_DATABASE = "my-database";
+  private static final String TEST_TABLE = "my-metadata-table";
+  private static final String TEST_CHANGE_STREAM = "my-change-stream";
+
+  @Rule
+  public final transient TestPipeline pipeline =
+      TestPipeline.create().enableAbandonedNodeEnforcement(false);
+
+  @Rule public final transient ExpectedException thrown = 
ExpectedException.none();
+
+  private MockSpannerServiceImpl mockSpannerService;
+  private MockServiceHelper serviceHelper;
+
+  @Before
+  public void setUp() throws Exception {
+    mockSpannerService = new MockSpannerServiceImpl();
+    serviceHelper =
+        new MockServiceHelper(SPANNER_HOST, 
Collections.singletonList(mockSpannerService));
+    serviceHelper.start();
+    serviceHelper.reset();
+  }
+
+  @After
+  public void tearDown() throws NoSuchFieldException, IllegalAccessException {
+    serviceHelper.reset();
+    serviceHelper.stop();
+    mockSpannerService.reset();
+    resetDaoFactoryFields();
+  }
+
+  @Test
+  public void testResourceExhaustedDoesNotRetry() {
+    mockSpannerService.setExecuteStreamingSqlExecutionTime(
+        
SimulatedExecutionTime.ofStickyException(Status.RESOURCE_EXHAUSTED.asRuntimeException()));
+
+    final Timestamp now = Timestamp.now();
+    final Timestamp after3Seconds =
+        Timestamp.ofTimeSecondsAndNanos(now.getSeconds() + 3, now.getNanos());
+    try {
+      pipeline.apply(
+          SpannerIO.readChangeStream()
+              .withSpannerConfig(getSpannerConfig())
+              .withChangeStreamName(TEST_CHANGE_STREAM)
+              .withMetadataDatabase(TEST_DATABASE)
+              .withInclusiveStartAt(now)
+              .withInclusiveEndAt(after3Seconds));
+      pipeline.run().waitUntilFinish();
+    } finally {
+      thrown.expect(PipelineExecutionException.class);
+      thrown.expectMessage(ErrorCode.RESOURCE_EXHAUSTED.name());
+    }
+  }
+
+  @Test
+  public void testUnavailableExceptionRetries() {
+    mockSpannerService.setExecuteStreamingSqlExecutionTime(
+        SimulatedExecutionTime.ofExceptions(
+            ImmutableSet.of(
+                Status.UNAVAILABLE.asRuntimeException(),
+                Status.RESOURCE_EXHAUSTED.asRuntimeException())));
+
+    final Timestamp now = Timestamp.now();
+    final Timestamp after3Seconds =
+        Timestamp.ofTimeSecondsAndNanos(now.getSeconds() + 3, now.getNanos());
+    try {
+      pipeline.apply(
+          SpannerIO.readChangeStream()
+              .withSpannerConfig(getSpannerConfig())
+              .withChangeStreamName(TEST_CHANGE_STREAM)
+              .withMetadataDatabase(TEST_DATABASE)
+              .withInclusiveStartAt(now)
+              .withInclusiveEndAt(after3Seconds));
+      pipeline.run().waitUntilFinish();
+    } finally {
+      assertThat(
+          mockSpannerService.countRequestsOfType(ExecuteSqlRequest.class), 
Matchers.greaterThan(1));
+      thrown.expect(PipelineExecutionException.class);
+      thrown.expectMessage(ErrorCode.RESOURCE_EXHAUSTED.name());
+    }
+  }
+
+  @Test
+  public void testAbortedExceptionRetries() {
+    mockSpannerService.setExecuteStreamingSqlExecutionTime(
+        
SimulatedExecutionTime.ofStickyException(Status.ABORTED.asRuntimeException()));
+
+    final Timestamp now = Timestamp.now();
+    final Timestamp after3Seconds =
+        Timestamp.ofTimeSecondsAndNanos(now.getSeconds() + 3, now.getNanos());
+    try {
+      pipeline.apply(
+          SpannerIO.readChangeStream()
+              .withSpannerConfig(getSpannerConfig())
+              .withChangeStreamName(TEST_CHANGE_STREAM)
+              .withMetadataDatabase(TEST_DATABASE)
+              .withInclusiveStartAt(now)
+              .withInclusiveEndAt(after3Seconds));
+      pipeline.run().waitUntilFinish();
+    } finally {
+      assertThat(
+          mockSpannerService.countRequestsOfType(ExecuteSqlRequest.class), 
Matchers.greaterThan(1));
+      thrown.expect(PipelineExecutionException.class);
+      thrown.expectMessage(ErrorCode.ABORTED.name());
+    }
+  }
+
+  @Test
+  public void testUnknownExceptionDoesNotRetry() {
+    mockSpannerService.setExecuteStreamingSqlExecutionTime(
+        
SimulatedExecutionTime.ofStickyException(Status.UNKNOWN.asRuntimeException()));
+
+    final Timestamp now = Timestamp.now();
+    final Timestamp after3Seconds =
+        Timestamp.ofTimeSecondsAndNanos(now.getSeconds() + 3, now.getNanos());
+    try {
+      pipeline.apply(
+          SpannerIO.readChangeStream()
+              .withSpannerConfig(getSpannerConfig())
+              .withChangeStreamName(TEST_CHANGE_STREAM)
+              .withMetadataDatabase(TEST_DATABASE)
+              .withInclusiveStartAt(now)
+              .withInclusiveEndAt(after3Seconds));
+      pipeline.run().waitUntilFinish();
+    } finally {
+      assertThat(
+          mockSpannerService.countRequestsOfType(ExecuteSqlRequest.class), 
Matchers.equalTo(1));
+      thrown.expect(PipelineExecutionException.class);
+      thrown.expectMessage(ErrorCode.UNKNOWN.name());
+    }
+  }
+
+  @Test
+  public void testInvalidRecordReceived() {
+    final Timestamp now = Timestamp.now();
+    final Timestamp after3Seconds =
+        Timestamp.ofTimeSecondsAndNanos(now.getSeconds() + 3, now.getNanos());
+
+    mockTableExists();
+    ResultSet getPartitionResultSet = mockGetParentPartition(now, 
after3Seconds);
+    mockGetWatermark(now);
+    mockGetPartitionsAfter(
+        Timestamp.ofTimeSecondsAndNanos(now.getSeconds(), now.getNanos() + 
1000),
+        getPartitionResultSet);
+    mockGetPartitionsAfter(
+        Timestamp.ofTimeSecondsAndNanos(now.getSeconds(), now.getNanos() - 
1000),
+        getPartitionResultSet);
+
+    Statement changeStreamQueryStatement =
+        Statement.newBuilder(
+                "SELECT * FROM READ_my-change-stream(   start_timestamp => 
@startTimestamp,   end_timestamp => @endTimestamp,   partition_token => 
@partitionToken,   read_options => null,   heartbeat_milliseconds => 
@heartbeatMillis)")

Review comment:
       This is actually how it looks in the actual request statement, I believe 
the test will fail if I take out the spaces — 
https://github.com/googleapis/java-spanner/blob/main/google-cloud-spanner/src/test/java/com/google/cloud/spanner/MockSpannerServiceImpl.java#L113




-- 
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.

To unsubscribe, e-mail: [email protected]

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


Reply via email to