diff --git a/google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/DatabaseAdminGaxTest.java b/google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/DatabaseAdminGaxTest.java index f568509173f6..3aa659ea3bca 100644 --- a/google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/DatabaseAdminGaxTest.java +++ b/google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/DatabaseAdminGaxTest.java @@ -16,6 +16,8 @@ package com.google.cloud.spanner; +import static org.junit.Assert.fail; + import com.google.api.core.ApiFunction; import com.google.api.gax.grpc.testing.LocalChannelProvider; import com.google.api.gax.longrunning.OperationFuture; @@ -243,28 +245,43 @@ public Void apply(Builder input) { return null; } }); - builder + + if (!builder .getDatabaseAdminStubSettingsBuilder() .createDatabaseOperationSettings() - .setInitialCallSettings( - builder - .getDatabaseAdminStubSettingsBuilder() - .createDatabaseOperationSettings() - .getInitialCallSettings() - .toBuilder() - .setRetrySettings(retrySettings) - .build()); - builder + .getInitialCallSettings() + .getRetryableCodes() + .isEmpty()) { + builder + .getDatabaseAdminStubSettingsBuilder() + .createDatabaseOperationSettings() + .setInitialCallSettings( + builder + .getDatabaseAdminStubSettingsBuilder() + .createDatabaseOperationSettings() + .getInitialCallSettings() + .toBuilder() + .setRetrySettings(retrySettings) + .build()); + } + if (!builder .getDatabaseAdminStubSettingsBuilder() .updateDatabaseDdlOperationSettings() - .setInitialCallSettings( - builder - .getDatabaseAdminStubSettingsBuilder() - .updateDatabaseDdlOperationSettings() - .getInitialCallSettings() - .toBuilder() - .setRetrySettings(retrySettings) - .build()); + .getInitialCallSettings() + .getRetryableCodes() + .isEmpty()) { + builder + .getDatabaseAdminStubSettingsBuilder() + .updateDatabaseDdlOperationSettings() + .setInitialCallSettings( + builder + .getDatabaseAdminStubSettingsBuilder() + .updateDatabaseDdlOperationSettings() + .getInitialCallSettings() + .toBuilder() + .setRetrySettings(retrySettings) + .build()); + } spanner = builder.build().getService(); client = spanner.getDatabaseAdminClient(); } @@ -369,19 +386,36 @@ public void createDatabaseTest() throws Exception { } mockDatabaseAdmin.addResponse(resultOperation); + boolean methodIsIdempotent = + !spanner + .getOptions() + .getDatabaseAdminStubSettings() + .createDatabaseOperationSettings() + .getInitialCallSettings() + .getRetryableCodes() + .isEmpty(); for (int i = 0; i < 2; i++) { OperationFuture actualResponse = client.createDatabase(INSTANCE, "DATABASE", Arrays.asList()); try { Database returnedInstance = actualResponse.get(); + if (!methodIsIdempotent && i == exceptionAtCall) { + fail("missing expected exception"); + } Assert.assertEquals(name.toString(), returnedInstance.getId().getName()); } catch (ExecutionException e) { - Throwables.throwIfUnchecked(e.getCause()); - throw e; + if (!exceptionType.isRetryable() || methodIsIdempotent || i != exceptionAtCall) { + Throwables.throwIfUnchecked(e.getCause()); + throw e; + } } } List actualRequests = mockDatabaseAdmin.getRequests(); - Assert.assertEquals(2, actualRequests.size()); + if (methodIsIdempotent) { + Assert.assertEquals(2, actualRequests.size()); + } else { + Assert.assertEquals(1, actualRequests.size()); + } } @Test diff --git a/google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/InstanceAdminGaxTest.java b/google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/InstanceAdminGaxTest.java index 32ad85ec339f..beee0522fbae 100644 --- a/google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/InstanceAdminGaxTest.java +++ b/google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/InstanceAdminGaxTest.java @@ -16,6 +16,8 @@ package com.google.cloud.spanner; +import static org.junit.Assert.fail; + import com.google.api.core.ApiFunction; import com.google.api.gax.grpc.testing.LocalChannelProvider; import com.google.api.gax.longrunning.OperationFuture; @@ -246,28 +248,42 @@ public Void apply(Builder input) { return null; } }); - builder + if (!builder .getInstanceAdminStubSettingsBuilder() .createInstanceOperationSettings() - .setInitialCallSettings( - builder - .getInstanceAdminStubSettingsBuilder() - .createInstanceOperationSettings() - .getInitialCallSettings() - .toBuilder() - .setRetrySettings(retrySettings) - .build()); - builder + .getInitialCallSettings() + .getRetryableCodes() + .isEmpty()) { + builder + .getInstanceAdminStubSettingsBuilder() + .createInstanceOperationSettings() + .setInitialCallSettings( + builder + .getInstanceAdminStubSettingsBuilder() + .createInstanceOperationSettings() + .getInitialCallSettings() + .toBuilder() + .setRetrySettings(retrySettings) + .build()); + } + if (!builder .getInstanceAdminStubSettingsBuilder() .updateInstanceOperationSettings() - .setInitialCallSettings( - builder - .getInstanceAdminStubSettingsBuilder() - .updateInstanceOperationSettings() - .getInitialCallSettings() - .toBuilder() - .setRetrySettings(retrySettings) - .build()); + .getInitialCallSettings() + .getRetryableCodes() + .isEmpty()) { + builder + .getInstanceAdminStubSettingsBuilder() + .updateInstanceOperationSettings() + .setInitialCallSettings( + builder + .getInstanceAdminStubSettingsBuilder() + .updateInstanceOperationSettings() + .getInitialCallSettings() + .toBuilder() + .setRetrySettings(retrySettings) + .build()); + } spanner = builder.build().getService(); client = spanner.getInstanceAdminClient(); } @@ -453,6 +469,14 @@ public void createInstanceTest() throws Exception { } mockInstanceAdmin.addResponse(resultOperation); + boolean methodIsIdempotent = + !spanner + .getOptions() + .getInstanceAdminStubSettings() + .createInstanceOperationSettings() + .getInitialCallSettings() + .getRetryableCodes() + .isEmpty(); for (int i = 0; i < 2; i++) { OperationFuture actualResponse = client.createInstance( @@ -462,14 +486,23 @@ public void createInstanceTest() throws Exception { .build()); try { Instance returnedInstance = actualResponse.get(); + if (!methodIsIdempotent && i == exceptionAtCall) { + fail("missing expected exception"); + } Assert.assertEquals(displayName, returnedInstance.getDisplayName()); } catch (ExecutionException e) { - Throwables.throwIfUnchecked(e.getCause()); - throw e; + if (!exceptionType.isRetryable() || methodIsIdempotent || i != exceptionAtCall) { + Throwables.throwIfUnchecked(e.getCause()); + throw e; + } } } List actualRequests = mockInstanceAdmin.getRequests(); - Assert.assertEquals(2, actualRequests.size()); + if (methodIsIdempotent) { + Assert.assertEquals(2, actualRequests.size()); + } else { + Assert.assertEquals(1, actualRequests.size()); + } } @Test @@ -501,6 +534,14 @@ public void updateInstanceTest() throws Exception { } mockInstanceAdmin.addResponse(resultOperation); + boolean methodIsIdempotent = + !spanner + .getOptions() + .getInstanceAdminStubSettings() + .updateInstanceOperationSettings() + .getInitialCallSettings() + .getRetryableCodes() + .isEmpty(); for (int i = 0; i < 2; i++) { OperationFuture actualResponse = client.updateInstance( @@ -510,15 +551,23 @@ public void updateInstanceTest() throws Exception { .build()); try { Instance returnedInstance = actualResponse.get(); + if (!methodIsIdempotent && i == exceptionAtCall) { + fail("missing expected exception"); + } Assert.assertEquals(displayName, returnedInstance.getDisplayName()); } catch (ExecutionException e) { - Throwables.throwIfUnchecked(e.getCause()); - throw e; + if (!exceptionType.isRetryable() || methodIsIdempotent || i != exceptionAtCall) { + Throwables.throwIfUnchecked(e.getCause()); + throw e; + } } } - List actualRequests = mockInstanceAdmin.getRequests(); - Assert.assertEquals(2, actualRequests.size()); + if (methodIsIdempotent) { + Assert.assertEquals(2, actualRequests.size()); + } else { + Assert.assertEquals(1, actualRequests.size()); + } } @Test