|
| 1 | +/* |
| 2 | + * Copyright 2023 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.assertThrows; |
| 21 | + |
| 22 | +import com.google.api.gax.retrying.RetrySettings; |
| 23 | +import com.google.api.gax.rpc.StatusCode; |
| 24 | +import com.google.cloud.NoCredentials; |
| 25 | +import com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult; |
| 26 | +import org.junit.Test; |
| 27 | +import org.junit.runner.RunWith; |
| 28 | +import org.junit.runners.JUnit4; |
| 29 | +import org.threeten.bp.Duration; |
| 30 | + |
| 31 | +/** Tests for samples that use an in-mem mock server instead of running on real Cloud Spanner. */ |
| 32 | +@RunWith(JUnit4.class) |
| 33 | +public class SamplesMockServerTest extends AbstractMockServerTest { |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testSampleRetrySettings() { |
| 37 | + String sql = |
| 38 | + "INSERT INTO Singers (SingerId, FirstName, LastName)\n" |
| 39 | + + "VALUES (20, 'George', 'Washington')"; |
| 40 | + mockSpanner.putStatementResult(StatementResult.update(Statement.of(sql), 1L)); |
| 41 | + |
| 42 | + SpannerOptions.Builder builder = |
| 43 | + SpannerOptions.newBuilder() |
| 44 | + .setProjectId("p") |
| 45 | + .setCredentials(NoCredentials.getInstance()) |
| 46 | + .setChannelProvider(channelProvider); |
| 47 | + // Set a timeout value for the ExecuteSql RPC that is so low that it will always be triggered. |
| 48 | + // This should cause the RPC to fail with a DEADLINE_EXCEEDED error. |
| 49 | + builder |
| 50 | + .getSpannerStubSettingsBuilder() |
| 51 | + .executeSqlSettings() |
| 52 | + .setRetryableCodes(StatusCode.Code.UNAVAILABLE) |
| 53 | + .setRetrySettings( |
| 54 | + RetrySettings.newBuilder() |
| 55 | + .setInitialRetryDelay(Duration.ofMillis(500)) |
| 56 | + .setMaxRetryDelay(Duration.ofSeconds(16)) |
| 57 | + .setRetryDelayMultiplier(1.5) |
| 58 | + .setInitialRpcTimeout(Duration.ofNanos(1L)) |
| 59 | + .setMaxRpcTimeout(Duration.ofNanos(1L)) |
| 60 | + .setRpcTimeoutMultiplier(1.0) |
| 61 | + .setTotalTimeout(Duration.ofNanos(1L)) |
| 62 | + .build()); |
| 63 | + // Create a Spanner client using the custom retry and timeout settings. |
| 64 | + try (Spanner spanner = builder.build().getService()) { |
| 65 | + DatabaseClient client = spanner.getDatabaseClient(DatabaseId.of("p", "i", "d")); |
| 66 | + SpannerException exception = |
| 67 | + assertThrows( |
| 68 | + SpannerException.class, |
| 69 | + () -> |
| 70 | + client |
| 71 | + .readWriteTransaction() |
| 72 | + .run(transaction -> transaction.executeUpdate(Statement.of(sql)))); |
| 73 | + assertEquals(ErrorCode.DEADLINE_EXCEEDED, exception.getErrorCode()); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments