|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.spanner; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertFalse; |
| 21 | +import static org.junit.Assert.assertTrue; |
| 22 | + |
| 23 | +import com.google.cloud.NoCredentials; |
| 24 | +import com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime; |
| 25 | +import com.google.cloud.spanner.connection.AbstractMockServerTest; |
| 26 | +import com.google.spanner.v1.BatchCreateSessionsRequest; |
| 27 | +import com.google.spanner.v1.ExecuteSqlRequest; |
| 28 | +import io.grpc.ManagedChannelBuilder; |
| 29 | +import io.grpc.Status; |
| 30 | +import org.junit.Test; |
| 31 | +import org.junit.runner.RunWith; |
| 32 | +import org.junit.runners.JUnit4; |
| 33 | +import org.threeten.bp.Duration; |
| 34 | + |
| 35 | +@RunWith(JUnit4.class) |
| 36 | +public class RetryableInternalErrorTest extends AbstractMockServerTest { |
| 37 | + @Test |
| 38 | + public void testTranslateInternalException() { |
| 39 | + mockSpanner.setBatchCreateSessionsExecutionTime( |
| 40 | + SimulatedExecutionTime.ofException( |
| 41 | + Status.INTERNAL |
| 42 | + .withDescription("Authentication backend internal server error. Please retry.") |
| 43 | + .asRuntimeException())); |
| 44 | + mockSpanner.setExecuteStreamingSqlExecutionTime( |
| 45 | + SimulatedExecutionTime.ofException( |
| 46 | + Status.INTERNAL |
| 47 | + .withDescription("Authentication backend internal server error. Please retry.") |
| 48 | + .asRuntimeException())); |
| 49 | + |
| 50 | + try (Spanner spanner = |
| 51 | + SpannerOptions.newBuilder() |
| 52 | + .setProjectId("my-project") |
| 53 | + .setHost(String.format("http://localhost:%d", getPort())) |
| 54 | + .setChannelConfigurator(ManagedChannelBuilder::usePlaintext) |
| 55 | + .setCredentials(NoCredentials.getInstance()) |
| 56 | + .setSessionPoolOption( |
| 57 | + SessionPoolOptions.newBuilder() |
| 58 | + .setMinSessions(1) |
| 59 | + .setMaxSessions(1) |
| 60 | + .setWaitForMinSessions(Duration.ofSeconds(5)) |
| 61 | + .build()) |
| 62 | + .build() |
| 63 | + .getService()) { |
| 64 | + |
| 65 | + DatabaseClient client = spanner.getDatabaseClient(DatabaseId.of("p", "i", "d")); |
| 66 | + // Execute a query. This will block until a BatchCreateSessions call has finished and then |
| 67 | + // invoke ExecuteStreamingSql. Both of these RPCs should be retried. |
| 68 | + try (ResultSet resultSet = client.singleUse().executeQuery(SELECT1_STATEMENT)) { |
| 69 | + assertTrue(resultSet.next()); |
| 70 | + assertFalse(resultSet.next()); |
| 71 | + } |
| 72 | + // Verify that both the BatchCreateSessions call and the ExecuteStreamingSql call were |
| 73 | + // retried. |
| 74 | + assertEquals(2, mockSpanner.countRequestsOfType(BatchCreateSessionsRequest.class)); |
| 75 | + assertEquals(2, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class)); |
| 76 | + // Clear the requests before the next test. |
| 77 | + mockSpanner.clearRequests(); |
| 78 | + |
| 79 | + // Execute a DML statement. This uses the ExecuteSql RPC. |
| 80 | + assertEquals(0, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class)); |
| 81 | + mockSpanner.setExecuteSqlExecutionTime( |
| 82 | + SimulatedExecutionTime.ofException( |
| 83 | + Status.INTERNAL |
| 84 | + .withDescription("Authentication backend internal server error. Please retry.") |
| 85 | + .asRuntimeException())); |
| 86 | + assertEquals( |
| 87 | + Long.valueOf(1L), |
| 88 | + client |
| 89 | + .readWriteTransaction() |
| 90 | + .run(transaction -> transaction.executeUpdate(INSERT_STATEMENT))); |
| 91 | + // Verify that also this request was retried. |
| 92 | + assertEquals(2, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class)); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments