|
| 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.assertNull; |
| 20 | +import static org.junit.Assert.assertTrue; |
| 21 | + |
| 22 | +import com.google.cloud.NoCredentials; |
| 23 | +import com.google.cloud.spanner.connection.AbstractMockServerTest; |
| 24 | +import io.grpc.ManagedChannelBuilder; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.concurrent.ExecutionException; |
| 27 | +import java.util.concurrent.ExecutorService; |
| 28 | +import java.util.concurrent.Executors; |
| 29 | +import java.util.concurrent.Future; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | +import org.junit.Test; |
| 32 | +import org.junit.runner.RunWith; |
| 33 | +import org.junit.runners.JUnit4; |
| 34 | +import org.threeten.bp.Duration; |
| 35 | + |
| 36 | +@RunWith(JUnit4.class) |
| 37 | +public class SpanExceptionTest extends AbstractMockServerTest { |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testReadOnlyTransaction() throws InterruptedException, ExecutionException { |
| 41 | + try (Spanner spanner = |
| 42 | + SpannerOptions.newBuilder() |
| 43 | + .setProjectId("my-project") |
| 44 | + .setHost(String.format("http://localhost:%d", getPort())) |
| 45 | + .setChannelConfigurator(ManagedChannelBuilder::usePlaintext) |
| 46 | + .setCredentials(NoCredentials.getInstance()) |
| 47 | + .setSessionPoolOption( |
| 48 | + SessionPoolOptions.newBuilder() |
| 49 | + .setMaxSessions(10) |
| 50 | + .setAcquireSessionTimeout(Duration.ofMillis(10)) |
| 51 | + // .setAcquireSessionTimeout(null) |
| 52 | + .build()) |
| 53 | + .build() |
| 54 | + .getService()) { |
| 55 | + DatabaseClient client = spanner.getDatabaseClient(DatabaseId.of("p", "i", "d")); |
| 56 | + |
| 57 | + int numThreads = 25; |
| 58 | + ExecutorService service = Executors.newFixedThreadPool(numThreads); |
| 59 | + ArrayList<Future<Void>> futures = new ArrayList<>(numThreads); |
| 60 | + try (ReadOnlyTransaction readOnlyTransaction = client.readOnlyTransaction()) { |
| 61 | + for (int i = 0; i < numThreads; i++) { |
| 62 | + futures.add(service.submit(() -> executeRandom(readOnlyTransaction))); |
| 63 | + } |
| 64 | + service.shutdown(); |
| 65 | + assertTrue(service.awaitTermination(60L, TimeUnit.SECONDS)); |
| 66 | + // Verify that all threads finished without any unexpected errors. |
| 67 | + for (Future<Void> future : futures) { |
| 68 | + assertNull(future.get()); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private Void executeRandom(ReadOnlyTransaction readOnlyTransaction) { |
| 75 | + try (ResultSet resultSet = readOnlyTransaction.executeQuery(SELECT_RANDOM_STATEMENT)) { |
| 76 | + while (resultSet.next()) { |
| 77 | + // ignore |
| 78 | + } |
| 79 | + } catch (SpannerException spannerException) { |
| 80 | + if (spannerException.getErrorCode() == ErrorCode.RESOURCE_EXHAUSTED) { |
| 81 | + // This is the expected error code, so ignore. |
| 82 | + return null; |
| 83 | + } |
| 84 | + throw spannerException; |
| 85 | + } |
| 86 | + return null; |
| 87 | + } |
| 88 | +} |
0 commit comments