forked from googleapis/java-bigtable
/
EnhancedBigtableStubTest.java
194 lines (170 loc) · 7.31 KB
/
EnhancedBigtableStubTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigtable.data.v2.stub;
import static com.google.common.truth.Truth.assertThat;
import com.google.api.gax.core.NoCredentialsProvider;
import com.google.api.gax.rpc.ServerStreamingCallable;
import com.google.bigtable.v2.BigtableGrpc;
import com.google.bigtable.v2.ReadRowsRequest;
import com.google.bigtable.v2.ReadRowsResponse;
import com.google.bigtable.v2.RowSet;
import com.google.cloud.bigtable.admin.v2.internal.NameUtil;
import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
import com.google.cloud.bigtable.data.v2.FakeServiceHelper;
import com.google.cloud.bigtable.data.v2.internal.RequestContext;
import com.google.cloud.bigtable.data.v2.models.DefaultRowAdapter;
import com.google.cloud.bigtable.data.v2.models.Query;
import com.google.cloud.bigtable.data.v2.models.Row;
import com.google.common.collect.Queues;
import com.google.protobuf.ByteString;
import com.google.protobuf.BytesValue;
import com.google.protobuf.StringValue;
import io.grpc.Metadata;
import io.grpc.ServerCall;
import io.grpc.ServerCall.Listener;
import io.grpc.ServerCallHandler;
import io.grpc.ServerInterceptor;
import io.grpc.stub.StreamObserver;
import java.io.IOException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class EnhancedBigtableStubTest {
private static final String PROJECT_ID = "fake-project";
private static final String INSTANCE_ID = "fake-instance";
private static final String TABLE_NAME =
NameUtil.formatTableName(PROJECT_ID, INSTANCE_ID, "fake-table");
private static final String APP_PROFILE_ID = "app-profile-id";
FakeServiceHelper serviceHelper;
private MetadataInterceptor metadataInterceptor;
private FakeDataService fakeDataService;
private EnhancedBigtableStubSettings defaultSettings;
private EnhancedBigtableStub enhancedBigtableStub;
@Before
public void setUp() throws IOException, IllegalAccessException, InstantiationException {
metadataInterceptor = new MetadataInterceptor();
fakeDataService = new FakeDataService();
serviceHelper = new FakeServiceHelper(metadataInterceptor, fakeDataService);
serviceHelper.start();
defaultSettings =
BigtableDataSettings.newBuilderForEmulator(serviceHelper.getPort())
.setProjectId(PROJECT_ID)
.setInstanceId(INSTANCE_ID)
.setAppProfileId(APP_PROFILE_ID)
.setCredentialsProvider(NoCredentialsProvider.create())
.build()
.getStubSettings();
enhancedBigtableStub = EnhancedBigtableStub.create(defaultSettings);
}
@After
public void tearDown() {
serviceHelper.shutdown();
}
@Test
public void testCreateReadRowsCallable() throws InterruptedException {
ServerStreamingCallable<Query, Row> streamingCallable =
enhancedBigtableStub.createReadRowsCallable(new DefaultRowAdapter());
Query request = Query.create("table-id").rowKey("row-key");
streamingCallable.call(request).iterator().next();
ReadRowsRequest expected =
request.toProto(RequestContext.create(PROJECT_ID, INSTANCE_ID, APP_PROFILE_ID));
assertThat(fakeDataService.popLastRequest()).isEqualTo(expected);
}
@Test
public void testCreateReadRowsRawCallable() throws InterruptedException {
ServerStreamingCallable<ReadRowsRequest, Row> callable =
enhancedBigtableStub.createReadRowsRawCallable(new DefaultRowAdapter());
ReadRowsRequest expectedRequest =
ReadRowsRequest.newBuilder()
.setTableName(TABLE_NAME)
.setAppProfileId("app-profile-1")
.setRows(RowSet.newBuilder().addRowKeys(ByteString.copyFromUtf8("test-row-key")))
.build();
callable.call(expectedRequest).iterator().next();
assertThat(fakeDataService.popLastRequest()).isEqualTo(expectedRequest);
ReadRowsRequest expectedRequest2 =
ReadRowsRequest.newBuilder()
.setTableName(TABLE_NAME)
.setAppProfileId("app-profile-2")
.build();
callable.call(expectedRequest2).iterator().next();
assertThat(fakeDataService.popLastRequest()).isEqualTo(expectedRequest2);
}
@Test
public void testChannelPrimerConfigured() throws IOException {
EnhancedBigtableStubSettings settings =
defaultSettings
.toBuilder()
.setRefreshingChannel(true)
.setPrimedTableIds("table1", "table2")
.build();
try (EnhancedBigtableStub ignored = EnhancedBigtableStub.create(settings)) {
// priming will issue a request per table on startup
assertThat(fakeDataService.requests).hasSize(2);
}
}
@Test
public void testUserAgent() throws InterruptedException {
ServerStreamingCallable<Query, Row> streamingCallable =
enhancedBigtableStub.createReadRowsCallable(new DefaultRowAdapter());
Query request = Query.create("table-id").rowKey("row-key");
streamingCallable.call(request).iterator().next();
assertThat(metadataInterceptor.headers).hasSize(1);
Metadata metadata = metadataInterceptor.headers.take();
assertThat(metadata.get(Metadata.Key.of("user-agent", Metadata.ASCII_STRING_MARSHALLER)))
.containsMatch("bigtable-java/\\d+\\.\\d+\\.\\d+(?:-SNAPSHOT)?");
}
private static class MetadataInterceptor implements ServerInterceptor {
final BlockingQueue<Metadata> headers = Queues.newLinkedBlockingDeque();
@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(
ServerCall<ReqT, RespT> serverCall,
Metadata metadata,
ServerCallHandler<ReqT, RespT> serverCallHandler) {
headers.add(metadata);
return serverCallHandler.startCall(serverCall, metadata);
}
}
private static class FakeDataService extends BigtableGrpc.BigtableImplBase {
final BlockingQueue<ReadRowsRequest> requests = Queues.newLinkedBlockingDeque();
@SuppressWarnings("unchecked")
ReadRowsRequest popLastRequest() throws InterruptedException {
return requests.poll(1, TimeUnit.SECONDS);
}
@Override
public void readRows(
ReadRowsRequest request, StreamObserver<ReadRowsResponse> responseObserver) {
requests.add(request);
// Dummy row for stream
responseObserver.onNext(
ReadRowsResponse.newBuilder()
.addChunks(
ReadRowsResponse.CellChunk.newBuilder()
.setCommitRow(true)
.setRowKey(ByteString.copyFromUtf8("a"))
.setFamilyName(StringValue.getDefaultInstance())
.setQualifier(BytesValue.getDefaultInstance())
.setValueSize(0))
.build());
responseObserver.onCompleted();
}
}
}