|
| 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; |
| 17 | + |
| 18 | +import static com.google.common.truth.Truth.assertThat; |
| 19 | +import static java.util.concurrent.TimeUnit.MINUTES; |
| 20 | + |
| 21 | +import com.google.api.core.ApiFuture; |
| 22 | +import com.google.api.gax.core.NoCredentialsProvider; |
| 23 | +import com.google.bigtable.v2.BigtableGrpc; |
| 24 | +import com.google.bigtable.v2.ReadRowsRequest; |
| 25 | +import com.google.bigtable.v2.ReadRowsResponse; |
| 26 | +import com.google.cloud.bigtable.data.v2.BigtableDataSettings; |
| 27 | +import com.google.cloud.bigtable.data.v2.FakeServiceHelper; |
| 28 | +import com.google.cloud.bigtable.data.v2.models.Query; |
| 29 | +import com.google.cloud.bigtable.data.v2.models.Row; |
| 30 | +import com.google.common.util.concurrent.SettableFuture; |
| 31 | +import io.grpc.stub.StreamObserver; |
| 32 | +import java.util.List; |
| 33 | +import java.util.concurrent.ExecutorService; |
| 34 | +import java.util.concurrent.Executors; |
| 35 | +import org.junit.After; |
| 36 | +import org.junit.Before; |
| 37 | +import org.junit.Test; |
| 38 | +import org.junit.runner.RunWith; |
| 39 | +import org.junit.runners.JUnit4; |
| 40 | + |
| 41 | +/** Ensure that an outstanding RPC will finish during a close */ |
| 42 | +@RunWith(JUnit4.class) |
| 43 | +public class EnhancedBigtableStubCloseTest { |
| 44 | + private static final String PROJECT_ID = "fake-project"; |
| 45 | + private static final String INSTANCE_ID = "fake-instance"; |
| 46 | + |
| 47 | + private ExecutorService testExecutor; |
| 48 | + private SettableFuture<Void> requestReceivedBarrier = SettableFuture.create(); |
| 49 | + private SettableFuture<Void> clientClosedBarrier = SettableFuture.create(); |
| 50 | + |
| 51 | + private FakeServiceHelper serviceHelper; |
| 52 | + private EnhancedBigtableStub stub; |
| 53 | + |
| 54 | + @Before |
| 55 | + public void setUp() throws Exception { |
| 56 | + testExecutor = Executors.newCachedThreadPool(); |
| 57 | + requestReceivedBarrier = SettableFuture.create(); |
| 58 | + clientClosedBarrier = SettableFuture.create(); |
| 59 | + |
| 60 | + serviceHelper = new FakeServiceHelper(new FakeBigtable()); |
| 61 | + serviceHelper.start(); |
| 62 | + |
| 63 | + EnhancedBigtableStubSettings stubSettings = |
| 64 | + BigtableDataSettings.newBuilderForEmulator(serviceHelper.getPort()) |
| 65 | + .setProjectId(PROJECT_ID) |
| 66 | + .setInstanceId(INSTANCE_ID) |
| 67 | + .setCredentialsProvider(NoCredentialsProvider.create()) |
| 68 | + .setRefreshingChannel(false) |
| 69 | + .build() |
| 70 | + .getStubSettings(); |
| 71 | + |
| 72 | + stub = EnhancedBigtableStub.create(stubSettings); |
| 73 | + } |
| 74 | + |
| 75 | + @After |
| 76 | + public void tearDown() throws Exception { |
| 77 | + testExecutor.shutdown(); |
| 78 | + stub.close(); |
| 79 | + serviceHelper.shutdown(); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void outstandingRequestsFinishAfterClose() throws Exception { |
| 84 | + ApiFuture<List<Row>> resultFuture = |
| 85 | + stub.readRowsCallable().all().futureCall(Query.create("table1")); |
| 86 | + |
| 87 | + // Wait for the server to receive the request |
| 88 | + requestReceivedBarrier.get(1, MINUTES); |
| 89 | + // Close the client - must happen in a separate thread because close will block until all |
| 90 | + // requests have completed, which can't happen until the clientClosedBarrier is released. |
| 91 | + testExecutor.submit( |
| 92 | + new Runnable() { |
| 93 | + @Override |
| 94 | + public void run() { |
| 95 | + stub.close(); |
| 96 | + } |
| 97 | + }); |
| 98 | + Thread.sleep(200); // give the closer a chance to run |
| 99 | + clientClosedBarrier.set(null); |
| 100 | + |
| 101 | + assertThat(resultFuture.get(1, MINUTES)).isEmpty(); |
| 102 | + } |
| 103 | + |
| 104 | + class FakeBigtable extends BigtableGrpc.BigtableImplBase { |
| 105 | + @Override |
| 106 | + public void readRows( |
| 107 | + ReadRowsRequest request, StreamObserver<ReadRowsResponse> responseObserver) { |
| 108 | + |
| 109 | + // signal that server received the request |
| 110 | + requestReceivedBarrier.set(null); |
| 111 | + // wait until the main thread closes the client |
| 112 | + try { |
| 113 | + clientClosedBarrier.get(); |
| 114 | + } catch (Exception e) { |
| 115 | + // Shouldn't happen |
| 116 | + responseObserver.onError(e); |
| 117 | + } |
| 118 | + |
| 119 | + // send the response |
| 120 | + responseObserver.onCompleted(); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments