|
19 | 19 | import static org.hamcrest.CoreMatchers.equalTo; |
20 | 20 | import static org.hamcrest.CoreMatchers.is; |
21 | 21 | import static org.junit.Assert.assertThat; |
| 22 | +import static org.junit.Assert.fail; |
22 | 23 |
|
23 | 24 | import com.google.api.gax.grpc.testing.LocalChannelProvider; |
| 25 | +import com.google.api.gax.retrying.RetrySettings; |
24 | 26 | import com.google.cloud.NoCredentials; |
| 27 | +import com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime; |
25 | 28 | import com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult; |
| 29 | +import com.google.cloud.spanner.TransactionRunner.TransactionCallable; |
26 | 30 | import com.google.protobuf.ListValue; |
27 | 31 | import com.google.spanner.v1.ResultSetMetadata; |
28 | 32 | import com.google.spanner.v1.StructType; |
|
32 | 36 | import io.grpc.Status; |
33 | 37 | import io.grpc.inprocess.InProcessServerBuilder; |
34 | 38 | import java.io.IOException; |
| 39 | +import java.util.concurrent.ScheduledThreadPoolExecutor; |
35 | 40 | import org.junit.After; |
36 | 41 | import org.junit.AfterClass; |
37 | 42 | import org.junit.Before; |
38 | 43 | import org.junit.BeforeClass; |
39 | 44 | import org.junit.Test; |
40 | 45 | import org.junit.runner.RunWith; |
41 | 46 | import org.junit.runners.JUnit4; |
| 47 | +import org.threeten.bp.Duration; |
42 | 48 |
|
43 | 49 | @RunWith(JUnit4.class) |
44 | 50 | public class DatabaseClientImplTest { |
@@ -89,21 +95,24 @@ public static void startStaticServer() throws IOException { |
89 | 95 | String uniqueName = InProcessServerBuilder.generateName(); |
90 | 96 | server = |
91 | 97 | InProcessServerBuilder.forName(uniqueName) |
92 | | - .directExecutor() |
| 98 | + // We need to use a real executor for timeouts to occur. |
| 99 | + .scheduledExecutorService(new ScheduledThreadPoolExecutor(1)) |
93 | 100 | .addService(mockSpanner) |
94 | 101 | .build() |
95 | 102 | .start(); |
96 | 103 | channelProvider = LocalChannelProvider.create(uniqueName); |
97 | 104 | } |
98 | 105 |
|
99 | 106 | @AfterClass |
100 | | - public static void stopServer() { |
| 107 | + public static void stopServer() throws InterruptedException { |
101 | 108 | server.shutdown(); |
| 109 | + server.awaitTermination(); |
102 | 110 | } |
103 | 111 |
|
104 | 112 | @Before |
105 | 113 | public void setUp() throws IOException { |
106 | 114 | mockSpanner.reset(); |
| 115 | + mockSpanner.removeAllExecutionTimes(); |
107 | 116 | spanner = |
108 | 117 | SpannerOptions.newBuilder() |
109 | 118 | .setProjectId("[PROJECT]") |
@@ -158,4 +167,89 @@ public void testExecutePartitionedDmlWithException() { |
158 | 167 | spanner.getDatabaseClient(DatabaseId.of("[PROJECT]", "[INSTANCE]", "[DATABASE]")); |
159 | 168 | client.executePartitionedUpdate(INVALID_UPDATE_STATEMENT); |
160 | 169 | } |
| 170 | + |
| 171 | + @Test |
| 172 | + public void testPartitionedDmlDoesNotTimeout() throws Exception { |
| 173 | + mockSpanner.setExecuteSqlExecutionTime(SimulatedExecutionTime.ofMinimumAndRandomTime(10, 0)); |
| 174 | + final RetrySettings retrySettings = |
| 175 | + RetrySettings.newBuilder() |
| 176 | + .setInitialRpcTimeout(Duration.ofMillis(1L)) |
| 177 | + .setMaxRpcTimeout(Duration.ofMillis(1L)) |
| 178 | + .setMaxAttempts(1) |
| 179 | + .setTotalTimeout(Duration.ofMillis(1L)) |
| 180 | + .build(); |
| 181 | + SpannerOptions.Builder builder = |
| 182 | + SpannerOptions.newBuilder() |
| 183 | + .setProjectId("[PROJECT]") |
| 184 | + .setChannelProvider(channelProvider) |
| 185 | + .setCredentials(NoCredentials.getInstance()); |
| 186 | + // Set normal DML timeout value. |
| 187 | + builder.getSpannerStubSettingsBuilder().executeSqlSettings().setRetrySettings(retrySettings); |
| 188 | + try (Spanner spanner = builder.build().getService()) { |
| 189 | + DatabaseClient client = |
| 190 | + spanner.getDatabaseClient(DatabaseId.of("[PROJECT]", "[INSTANCE]", "[DATABASE]")); |
| 191 | + |
| 192 | + // PDML should not timeout with these settings. |
| 193 | + long updateCount = client.executePartitionedUpdate(UPDATE_STATEMENT); |
| 194 | + assertThat(updateCount, is(equalTo(UPDATE_COUNT))); |
| 195 | + |
| 196 | + // Normal DML should timeout. |
| 197 | + try { |
| 198 | + client |
| 199 | + .readWriteTransaction() |
| 200 | + .run( |
| 201 | + new TransactionCallable<Void>() { |
| 202 | + @Override |
| 203 | + public Void run(TransactionContext transaction) throws Exception { |
| 204 | + transaction.executeUpdate(UPDATE_STATEMENT); |
| 205 | + return null; |
| 206 | + } |
| 207 | + }); |
| 208 | + fail("expected DEADLINE_EXCEEDED"); |
| 209 | + } catch (SpannerException e) { |
| 210 | + if (e.getErrorCode() != ErrorCode.DEADLINE_EXCEEDED) { |
| 211 | + fail("expected DEADLINE_EXCEEDED"); |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + @Test |
| 218 | + public void testPartitionedDmlWithTimeout() throws Exception { |
| 219 | + mockSpanner.setExecuteSqlExecutionTime(SimulatedExecutionTime.ofMinimumAndRandomTime(10, 0)); |
| 220 | + SpannerOptions.Builder builder = |
| 221 | + SpannerOptions.newBuilder() |
| 222 | + .setProjectId("[PROJECT]") |
| 223 | + .setChannelProvider(channelProvider) |
| 224 | + .setCredentials(NoCredentials.getInstance()); |
| 225 | + // Set PDML timeout value. |
| 226 | + builder.setPartitionedDmlTimeout(Duration.ofMillis(1L)); |
| 227 | + try (Spanner spanner = builder.build().getService()) { |
| 228 | + DatabaseClient client = |
| 229 | + spanner.getDatabaseClient(DatabaseId.of("[PROJECT]", "[INSTANCE]", "[DATABASE]")); |
| 230 | + |
| 231 | + // PDML should timeout with these settings. |
| 232 | + try { |
| 233 | + client.executePartitionedUpdate(UPDATE_STATEMENT); |
| 234 | + fail("expected DEADLINE_EXCEEDED"); |
| 235 | + } catch (SpannerException e) { |
| 236 | + if (e.getErrorCode() != ErrorCode.DEADLINE_EXCEEDED) { |
| 237 | + fail("expected DEADLINE_EXCEEDED"); |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + // Normal DML should not timeout. |
| 242 | + long updateCount = |
| 243 | + client |
| 244 | + .readWriteTransaction() |
| 245 | + .run( |
| 246 | + new TransactionCallable<Long>() { |
| 247 | + @Override |
| 248 | + public Long run(TransactionContext transaction) throws Exception { |
| 249 | + return transaction.executeUpdate(UPDATE_STATEMENT); |
| 250 | + } |
| 251 | + }); |
| 252 | + assertThat(updateCount, is(equalTo(UPDATE_COUNT))); |
| 253 | + } |
| 254 | + } |
161 | 255 | } |
0 commit comments