1616
1717package com .google .cloud .spanner ;
1818
19+ import static org .junit .Assert .fail ;
20+
1921import com .google .api .core .ApiFunction ;
2022import com .google .api .gax .grpc .testing .LocalChannelProvider ;
2123import com .google .api .gax .longrunning .OperationFuture ;
@@ -246,28 +248,42 @@ public Void apply(Builder<?, ?> input) {
246248 return null ;
247249 }
248250 });
249- builder
251+ if (! builder
250252 .getInstanceAdminStubSettingsBuilder ()
251253 .createInstanceOperationSettings ()
252- .setInitialCallSettings (
253- builder
254- .getInstanceAdminStubSettingsBuilder ()
255- .createInstanceOperationSettings ()
256- .getInitialCallSettings ()
257- .toBuilder ()
258- .setRetrySettings (retrySettings )
259- .build ());
260- builder
254+ .getInitialCallSettings ()
255+ .getRetryableCodes ()
256+ .isEmpty ()) {
257+ builder
258+ .getInstanceAdminStubSettingsBuilder ()
259+ .createInstanceOperationSettings ()
260+ .setInitialCallSettings (
261+ builder
262+ .getInstanceAdminStubSettingsBuilder ()
263+ .createInstanceOperationSettings ()
264+ .getInitialCallSettings ()
265+ .toBuilder ()
266+ .setRetrySettings (retrySettings )
267+ .build ());
268+ }
269+ if (!builder
261270 .getInstanceAdminStubSettingsBuilder ()
262271 .updateInstanceOperationSettings ()
263- .setInitialCallSettings (
264- builder
265- .getInstanceAdminStubSettingsBuilder ()
266- .updateInstanceOperationSettings ()
267- .getInitialCallSettings ()
268- .toBuilder ()
269- .setRetrySettings (retrySettings )
270- .build ());
272+ .getInitialCallSettings ()
273+ .getRetryableCodes ()
274+ .isEmpty ()) {
275+ builder
276+ .getInstanceAdminStubSettingsBuilder ()
277+ .updateInstanceOperationSettings ()
278+ .setInitialCallSettings (
279+ builder
280+ .getInstanceAdminStubSettingsBuilder ()
281+ .updateInstanceOperationSettings ()
282+ .getInitialCallSettings ()
283+ .toBuilder ()
284+ .setRetrySettings (retrySettings )
285+ .build ());
286+ }
271287 spanner = builder .build ().getService ();
272288 client = spanner .getInstanceAdminClient ();
273289 }
@@ -453,6 +469,14 @@ public void createInstanceTest() throws Exception {
453469 }
454470 mockInstanceAdmin .addResponse (resultOperation );
455471
472+ boolean methodIsIdempotent =
473+ !spanner
474+ .getOptions ()
475+ .getInstanceAdminStubSettings ()
476+ .createInstanceOperationSettings ()
477+ .getInitialCallSettings ()
478+ .getRetryableCodes ()
479+ .isEmpty ();
456480 for (int i = 0 ; i < 2 ; i ++) {
457481 OperationFuture <Instance , CreateInstanceMetadata > actualResponse =
458482 client .createInstance (
@@ -462,14 +486,23 @@ public void createInstanceTest() throws Exception {
462486 .build ());
463487 try {
464488 Instance returnedInstance = actualResponse .get ();
489+ if (!methodIsIdempotent && i == exceptionAtCall ) {
490+ fail ("missing expected exception" );
491+ }
465492 Assert .assertEquals (displayName , returnedInstance .getDisplayName ());
466493 } catch (ExecutionException e ) {
467- Throwables .throwIfUnchecked (e .getCause ());
468- throw e ;
494+ if (!exceptionType .isRetryable () || methodIsIdempotent || i != exceptionAtCall ) {
495+ Throwables .throwIfUnchecked (e .getCause ());
496+ throw e ;
497+ }
469498 }
470499 }
471500 List <AbstractMessage > actualRequests = mockInstanceAdmin .getRequests ();
472- Assert .assertEquals (2 , actualRequests .size ());
501+ if (methodIsIdempotent ) {
502+ Assert .assertEquals (2 , actualRequests .size ());
503+ } else {
504+ Assert .assertEquals (1 , actualRequests .size ());
505+ }
473506 }
474507
475508 @ Test
@@ -501,6 +534,14 @@ public void updateInstanceTest() throws Exception {
501534 }
502535 mockInstanceAdmin .addResponse (resultOperation );
503536
537+ boolean methodIsIdempotent =
538+ !spanner
539+ .getOptions ()
540+ .getInstanceAdminStubSettings ()
541+ .updateInstanceOperationSettings ()
542+ .getInitialCallSettings ()
543+ .getRetryableCodes ()
544+ .isEmpty ();
504545 for (int i = 0 ; i < 2 ; i ++) {
505546 OperationFuture <Instance , UpdateInstanceMetadata > actualResponse =
506547 client .updateInstance (
@@ -510,15 +551,23 @@ public void updateInstanceTest() throws Exception {
510551 .build ());
511552 try {
512553 Instance returnedInstance = actualResponse .get ();
554+ if (!methodIsIdempotent && i == exceptionAtCall ) {
555+ fail ("missing expected exception" );
556+ }
513557 Assert .assertEquals (displayName , returnedInstance .getDisplayName ());
514558 } catch (ExecutionException e ) {
515- Throwables .throwIfUnchecked (e .getCause ());
516- throw e ;
559+ if (!exceptionType .isRetryable () || methodIsIdempotent || i != exceptionAtCall ) {
560+ Throwables .throwIfUnchecked (e .getCause ());
561+ throw e ;
562+ }
517563 }
518564 }
519-
520565 List <AbstractMessage > actualRequests = mockInstanceAdmin .getRequests ();
521- Assert .assertEquals (2 , actualRequests .size ());
566+ if (methodIsIdempotent ) {
567+ Assert .assertEquals (2 , actualRequests .size ());
568+ } else {
569+ Assert .assertEquals (1 , actualRequests .size ());
570+ }
522571 }
523572
524573 @ Test
0 commit comments