|
| 1 | +/* |
| 2 | + * Copyright 2021 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 | + * https://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 | +package com.google.cloud.bigtable.data.v2.stub.readrows; |
| 17 | + |
| 18 | +import static com.google.common.truth.Truth.assertThat; |
| 19 | + |
| 20 | +import com.google.api.gax.grpc.GrpcCallContext; |
| 21 | +import com.google.api.gax.rpc.ApiCallContext; |
| 22 | +import com.google.api.gax.rpc.ResponseObserver; |
| 23 | +import com.google.api.gax.rpc.ServerStreamingCallable; |
| 24 | +import com.google.bigtable.v2.ReadRowsRequest; |
| 25 | +import com.google.bigtable.v2.RowRange; |
| 26 | +import com.google.bigtable.v2.RowSet; |
| 27 | +import com.google.common.collect.ImmutableList; |
| 28 | +import com.google.protobuf.ByteString; |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.List; |
| 31 | +import org.junit.Before; |
| 32 | +import org.junit.Rule; |
| 33 | +import org.junit.Test; |
| 34 | +import org.junit.runner.RunWith; |
| 35 | +import org.junit.runners.JUnit4; |
| 36 | +import org.mockito.ArgumentCaptor; |
| 37 | +import org.mockito.Mock; |
| 38 | +import org.mockito.Mockito; |
| 39 | +import org.mockito.junit.MockitoJUnit; |
| 40 | +import org.mockito.junit.MockitoRule; |
| 41 | +import org.threeten.bp.Duration; |
| 42 | + |
| 43 | +@RunWith(JUnit4.class) |
| 44 | +public class PointReadTimeoutCallableTest { |
| 45 | + @Rule public final MockitoRule moo = MockitoJUnit.rule(); |
| 46 | + |
| 47 | + @Mock private ServerStreamingCallable<ReadRowsRequest, Object> inner; |
| 48 | + private ArgumentCaptor<ApiCallContext> ctxCaptor; |
| 49 | + @Mock private ResponseObserver<Object> responseObserver; |
| 50 | + |
| 51 | + @Before |
| 52 | + public void setUp() throws Exception { |
| 53 | + ctxCaptor = ArgumentCaptor.forClass(ApiCallContext.class); |
| 54 | + |
| 55 | + Mockito.doNothing() |
| 56 | + .when(inner) |
| 57 | + .call( |
| 58 | + Mockito.isA(ReadRowsRequest.class), |
| 59 | + Mockito.any(ResponseObserver.class), |
| 60 | + ctxCaptor.capture()); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void promotesStreamWaitTimeout() { |
| 65 | + Duration duration = Duration.ofMillis(100); |
| 66 | + PointReadTimeoutCallable<Object> callable = new PointReadTimeoutCallable<>(inner); |
| 67 | + |
| 68 | + for (ReadRowsRequest req : createPointReadRequests()) { |
| 69 | + callable.call( |
| 70 | + req, responseObserver, GrpcCallContext.createDefault().withStreamWaitTimeout(duration)); |
| 71 | + |
| 72 | + assertThat(ctxCaptor.getValue().getTimeout()).isEqualTo(duration); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void promotesStreamWaitTimeoutForRowLimit() { |
| 78 | + Duration duration = Duration.ofMillis(100); |
| 79 | + PointReadTimeoutCallable<Object> callable = new PointReadTimeoutCallable<>(inner); |
| 80 | + |
| 81 | + for (ReadRowsRequest req : createPointReadRequests()) { |
| 82 | + callable.call( |
| 83 | + createRowsLimitRequest(), |
| 84 | + responseObserver, |
| 85 | + GrpcCallContext.createDefault().withStreamWaitTimeout(duration)); |
| 86 | + |
| 87 | + assertThat(ctxCaptor.getValue().getTimeout()).isEqualTo(duration); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void respectsExistingTimeout() { |
| 93 | + Duration duration = Duration.ofMillis(100); |
| 94 | + PointReadTimeoutCallable<Object> callable = new PointReadTimeoutCallable<>(inner); |
| 95 | + |
| 96 | + List<ReadRowsRequest> requests = |
| 97 | + ImmutableList.<ReadRowsRequest>builder() |
| 98 | + .addAll(createPointReadRequests()) |
| 99 | + .add(ReadRowsRequest.getDefaultInstance()) |
| 100 | + .build(); |
| 101 | + |
| 102 | + for (ReadRowsRequest req : requests) { |
| 103 | + callable.call(req, responseObserver, GrpcCallContext.createDefault().withTimeout(duration)); |
| 104 | + assertThat(ctxCaptor.getValue().getTimeout()).isEqualTo(duration); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void usesMinimum1() { |
| 110 | + Duration attemptTimeout = Duration.ofMillis(100); |
| 111 | + Duration streamTimeout = Duration.ofMillis(200); |
| 112 | + PointReadTimeoutCallable<Object> callable = new PointReadTimeoutCallable<>(inner); |
| 113 | + |
| 114 | + for (ReadRowsRequest req : createPointReadRequests()) { |
| 115 | + GrpcCallContext ctx = |
| 116 | + GrpcCallContext.createDefault() |
| 117 | + .withTimeout(attemptTimeout) |
| 118 | + .withStreamWaitTimeout(streamTimeout); |
| 119 | + callable.call(req, responseObserver, ctx); |
| 120 | + |
| 121 | + assertThat(ctxCaptor.getValue().getTimeout()).isEqualTo(attemptTimeout); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void usesMinimum2() { |
| 127 | + Duration attemptTimeout = Duration.ofMillis(200); |
| 128 | + Duration streamTimeout = Duration.ofMillis(100); |
| 129 | + PointReadTimeoutCallable<Object> callable = new PointReadTimeoutCallable<>(inner); |
| 130 | + |
| 131 | + for (ReadRowsRequest req : createPointReadRequests()) { |
| 132 | + GrpcCallContext ctx = |
| 133 | + GrpcCallContext.createDefault() |
| 134 | + .withTimeout(attemptTimeout) |
| 135 | + .withStreamWaitTimeout(streamTimeout); |
| 136 | + |
| 137 | + callable.call(req, responseObserver, ctx); |
| 138 | + |
| 139 | + assertThat(ctxCaptor.getValue().getTimeout()).isEqualTo(streamTimeout); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void nonPointReadsAreUntouched() { |
| 145 | + Duration streamTimeout = Duration.ofMillis(100); |
| 146 | + PointReadTimeoutCallable<Object> callable = new PointReadTimeoutCallable<>(inner); |
| 147 | + |
| 148 | + List<ReadRowsRequest> requests = |
| 149 | + Arrays.<ReadRowsRequest>asList( |
| 150 | + ReadRowsRequest.getDefaultInstance(), |
| 151 | + ReadRowsRequest.newBuilder() |
| 152 | + .setRows( |
| 153 | + RowSet.newBuilder() |
| 154 | + .addRowKeys(ByteString.copyFromUtf8("a")) |
| 155 | + .addRowKeys(ByteString.copyFromUtf8("ab"))) |
| 156 | + .build(), |
| 157 | + ReadRowsRequest.newBuilder() |
| 158 | + .setRows(RowSet.newBuilder().addRowRanges(RowRange.getDefaultInstance())) |
| 159 | + .build()); |
| 160 | + |
| 161 | + for (ReadRowsRequest req : requests) { |
| 162 | + callable.call( |
| 163 | + req, |
| 164 | + responseObserver, |
| 165 | + GrpcCallContext.createDefault().withStreamWaitTimeout(streamTimeout)); |
| 166 | + assertThat(ctxCaptor.getValue().getTimeout()).isNull(); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private List<ReadRowsRequest> createPointReadRequests() { |
| 171 | + return Arrays.asList(createRowsLimitRequest(), createRowKeyRequest()); |
| 172 | + } |
| 173 | + |
| 174 | + private ReadRowsRequest createRowsLimitRequest() { |
| 175 | + return ReadRowsRequest.newBuilder().setRowsLimit(1).build(); |
| 176 | + } |
| 177 | + |
| 178 | + private ReadRowsRequest createRowKeyRequest() { |
| 179 | + return ReadRowsRequest.newBuilder() |
| 180 | + .setRows(RowSet.newBuilder().addRowKeys(ByteString.copyFromUtf8("key"))) |
| 181 | + .build(); |
| 182 | + } |
| 183 | +} |
0 commit comments