|
| 1 | +/* |
| 2 | +* Copyright 2020 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 com.google.common.truth.Truth.assertThat; |
| 20 | + |
| 21 | +import com.google.api.gax.grpc.GrpcStatusCode; |
| 22 | +import com.google.api.gax.rpc.InternalException; |
| 23 | +import com.google.common.base.Predicate; |
| 24 | +import io.grpc.Status; |
| 25 | +import io.grpc.Status.Code; |
| 26 | +import io.grpc.StatusRuntimeException; |
| 27 | +import org.junit.Before; |
| 28 | +import org.junit.Test; |
| 29 | +import org.junit.runner.RunWith; |
| 30 | +import org.junit.runners.JUnit4; |
| 31 | + |
| 32 | +@SuppressWarnings("unchecked") |
| 33 | +@RunWith(JUnit4.class) |
| 34 | +public class IsRetryableInternalErrorTest { |
| 35 | + |
| 36 | +private Predicate<Throwable> predicate; |
| 37 | + |
| 38 | +@Before |
| 39 | +public void setUp() { |
| 40 | +predicate = new IsRetryableInternalError(); |
| 41 | +} |
| 42 | + |
| 43 | +@Test |
| 44 | +public void http2ErrorStatusRuntimeExceptionIsRetryable() { |
| 45 | +final StatusRuntimeException e = |
| 46 | +new StatusRuntimeException( |
| 47 | +Status.fromCode(Code.INTERNAL) |
| 48 | +.withDescription("INTERNAL: HTTP/2 error code: INTERNAL_ERROR.")); |
| 49 | + |
| 50 | +assertThat(predicate.apply(e)).isTrue(); |
| 51 | +} |
| 52 | + |
| 53 | +@Test |
| 54 | +public void http2ErrorInternalExceptionIsRetryable() { |
| 55 | +final InternalException e = |
| 56 | +new InternalException( |
| 57 | +"INTERNAL: HTTP/2 error code: INTERNAL_ERROR.", |
| 58 | +null, |
| 59 | +GrpcStatusCode.of(Code.INTERNAL), |
| 60 | +false); |
| 61 | + |
| 62 | +assertThat(predicate.apply(e)).isTrue(); |
| 63 | +} |
| 64 | + |
| 65 | +@Test |
| 66 | +public void connectionClosedStatusRuntimeExceptionIsRetryable() { |
| 67 | +final StatusRuntimeException e = |
| 68 | +new StatusRuntimeException( |
| 69 | +Status.fromCode(Code.INTERNAL) |
| 70 | +.withDescription("INTERNAL: Connection closed with unknown cause.")); |
| 71 | + |
| 72 | +assertThat(predicate.apply(e)).isTrue(); |
| 73 | +} |
| 74 | + |
| 75 | +@Test |
| 76 | +public void connectionClosedInternalExceptionIsRetryable() { |
| 77 | +final InternalException e = |
| 78 | +new InternalException( |
| 79 | +"INTERNAL: Connection closed with unknown cause.", |
| 80 | +null, |
| 81 | +GrpcStatusCode.of(Code.INTERNAL), |
| 82 | +false); |
| 83 | + |
| 84 | +assertThat(predicate.apply(e)).isTrue(); |
| 85 | +} |
| 86 | + |
| 87 | +@Test |
| 88 | +public void eosStatusRuntimeExceptionIsRetryable() { |
| 89 | +final StatusRuntimeException e = |
| 90 | +new StatusRuntimeException( |
| 91 | +Status.fromCode(Code.INTERNAL) |
| 92 | +.withDescription("INTERNAL: Received unexpected EOS on DATA frame from server.")); |
| 93 | + |
| 94 | +assertThat(predicate.apply(e)).isTrue(); |
| 95 | +} |
| 96 | + |
| 97 | +@Test |
| 98 | +public void eosInternalExceptionIsRetryable() { |
| 99 | +final InternalException e = |
| 100 | +new InternalException( |
| 101 | +"INTERNAL: Received unexpected EOS on DATA frame from server.", |
| 102 | +null, |
| 103 | +GrpcStatusCode.of(Code.INTERNAL), |
| 104 | +false); |
| 105 | + |
| 106 | +assertThat(predicate.apply(e)).isTrue(); |
| 107 | +} |
| 108 | + |
| 109 | +@Test |
| 110 | +public void genericInternalStatusRuntimeExceptionIsRetryable() { |
| 111 | +final StatusRuntimeException e = |
| 112 | +new StatusRuntimeException( |
| 113 | +Status.fromCode(Code.INTERNAL).withDescription("INTERNAL: Generic.")); |
| 114 | + |
| 115 | +assertThat(predicate.apply(e)).isFalse(); |
| 116 | +} |
| 117 | + |
| 118 | +@Test |
| 119 | +public void genericInternalExceptionIsNotRetryable() { |
| 120 | +final InternalException e = |
| 121 | +new InternalException("INTERNAL: Generic.", null, GrpcStatusCode.of(Code.INTERNAL), false); |
| 122 | + |
| 123 | +assertThat(predicate.apply(e)).isFalse(); |
| 124 | +} |
| 125 | + |
| 126 | +@Test |
| 127 | +public void nullIsNotRetryable() { |
| 128 | +assertThat(predicate.apply(null)).isFalse(); |
| 129 | +} |
| 130 | + |
| 131 | +@Test |
| 132 | +public void genericExceptionIsNotRetryable() { |
| 133 | +assertThat(predicate.apply(new Exception())).isFalse(); |
| 134 | +} |
| 135 | +} |
0 commit comments