This repository has been archived by the owner on Sep 26, 2023. It is now read-only.
/
GrpcCallContextTest.java
411 lines (358 loc) · 16.6 KB
/
GrpcCallContextTest.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
* Copyright 2017 Google LLC
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google LLC nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.api.gax.grpc;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import com.google.api.gax.retrying.RetrySettings;
import com.google.api.gax.rpc.ApiCallContext;
import com.google.api.gax.rpc.StatusCode;
import com.google.api.gax.rpc.testing.FakeCallContext;
import com.google.api.gax.rpc.testing.FakeChannel;
import com.google.api.gax.rpc.testing.FakeTransportChannel;
import com.google.api.gax.tracing.ApiTracer;
import com.google.auth.Credentials;
import com.google.common.collect.ImmutableMap;
import com.google.common.truth.Truth;
import io.grpc.CallOptions;
import io.grpc.ManagedChannel;
import io.grpc.Metadata.Key;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mockito;
import org.threeten.bp.Duration;
@RunWith(JUnit4.class)
public class GrpcCallContextTest {
@Test
public void testNullToSelfWrongType() {
try {
GrpcCallContext.createDefault().nullToSelf(FakeCallContext.createDefault());
Assert.fail("GrpcCallContext should have thrown an exception");
} catch (IllegalArgumentException expected) {
Truth.assertThat(expected)
.hasMessageThat()
.contains("context must be an instance of GrpcCallContext");
}
}
@Test
public void testWithCredentials() {
Credentials credentials = Mockito.mock(Credentials.class);
GrpcCallContext emptyContext = GrpcCallContext.createDefault();
assertNull(emptyContext.getCallOptions().getCredentials());
GrpcCallContext context = emptyContext.withCredentials(credentials);
assertNotNull(context.getCallOptions().getCredentials());
}
@Test
public void testWithTransportChannel() {
ManagedChannel channel = Mockito.mock(ManagedChannel.class);
GrpcCallContext context =
GrpcCallContext.createDefault().withTransportChannel(GrpcTransportChannel.create(channel));
Truth.assertThat(context.getChannel()).isSameInstanceAs(channel);
}
@Test
public void testWithTransportChannelWrongType() {
FakeChannel channel = new FakeChannel();
try {
GrpcCallContext.createDefault().withTransportChannel(FakeTransportChannel.create(channel));
Assert.fail("GrpcCallContext should have thrown an exception");
} catch (IllegalArgumentException expected) {
Truth.assertThat(expected).hasMessageThat().contains("Expected GrpcTransportChannel");
}
}
@Test
public void testMergeWrongType() {
try {
GrpcCallContext.createDefault().merge(FakeCallContext.createDefault());
Assert.fail("GrpcCallContext should have thrown an exception");
} catch (IllegalArgumentException expected) {
Truth.assertThat(expected)
.hasMessageThat()
.contains("context must be an instance of " + "GrpcCallContext");
}
}
@Test
public void testWithRequestParamsDynamicHeaderOption() {
String encodedRequestParams = "param1=value¶m2.param3=value23";
GrpcCallContext context =
GrpcCallContext.createDefault().withRequestParamsDynamicHeaderOption(encodedRequestParams);
Map<Key<String>, String> headers =
CallOptionsUtil.getDynamicHeadersOption(context.getCallOptions());
assertEquals(
ImmutableMap.of(CallOptionsUtil.REQUEST_PARAMS_HEADER_KEY, encodedRequestParams), headers);
}
@Test
public void testWithTimeout() {
assertNull(GrpcCallContext.createDefault().withTimeout(null).getTimeout());
}
@Test
public void testWithNegativeTimeout() {
assertNull(GrpcCallContext.createDefault().withTimeout(Duration.ofSeconds(-1L)).getTimeout());
}
@Test
public void testWithZeroTimeout() {
assertNull(GrpcCallContext.createDefault().withTimeout(Duration.ofSeconds(0L)).getTimeout());
}
@Test
public void testWithShorterTimeout() {
GrpcCallContext ctxWithLongTimeout =
GrpcCallContext.createDefault().withTimeout(Duration.ofSeconds(10));
// Sanity check
Truth.assertThat(ctxWithLongTimeout.getTimeout()).isEqualTo(Duration.ofSeconds(10));
// Shorten the timeout and make sure it changed
GrpcCallContext ctxWithShorterTimeout = ctxWithLongTimeout.withTimeout(Duration.ofSeconds(5));
Truth.assertThat(ctxWithShorterTimeout.getTimeout()).isEqualTo(Duration.ofSeconds(5));
}
@Test
public void testWithLongerTimeout() {
GrpcCallContext ctxWithShortTimeout =
GrpcCallContext.createDefault().withTimeout(Duration.ofSeconds(5));
// Sanity check
Truth.assertThat(ctxWithShortTimeout.getTimeout()).isEqualTo(Duration.ofSeconds(5));
// Try to extend the timeout and verify that it was ignored
GrpcCallContext ctxWithUnchangedTimeout =
ctxWithShortTimeout.withTimeout(Duration.ofSeconds(10));
Truth.assertThat(ctxWithUnchangedTimeout.getTimeout()).isEqualTo(Duration.ofSeconds(5));
}
@Test
public void testMergeWithNullTimeout() {
Duration timeout = Duration.ofSeconds(10);
GrpcCallContext baseContext = GrpcCallContext.createDefault().withTimeout(timeout);
GrpcCallContext defaultOverlay = GrpcCallContext.createDefault();
Truth.assertThat(baseContext.merge(defaultOverlay).getTimeout()).isEqualTo(timeout);
GrpcCallContext explicitNullOverlay = GrpcCallContext.createDefault().withTimeout(null);
Truth.assertThat(baseContext.merge(explicitNullOverlay).getTimeout()).isEqualTo(timeout);
}
@Test
public void testMergeWithTimeout() {
Duration timeout = Duration.ofSeconds(19);
GrpcCallContext ctx1 = GrpcCallContext.createDefault();
GrpcCallContext ctx2 = GrpcCallContext.createDefault().withTimeout(timeout);
Truth.assertThat(ctx1.merge(ctx2).getTimeout()).isEqualTo(timeout);
}
@Test
public void testWithStreamingWaitTimeout() {
Duration timeout = Duration.ofSeconds(15);
GrpcCallContext context = GrpcCallContext.createDefault().withStreamWaitTimeout(timeout);
Truth.assertThat(context.getStreamWaitTimeout()).isEqualTo(timeout);
}
@Test
public void testMergeWithNullStreamingWaitTimeout() {
Duration timeout = Duration.ofSeconds(10);
GrpcCallContext baseContext = GrpcCallContext.createDefault().withStreamWaitTimeout(timeout);
GrpcCallContext defaultOverlay = GrpcCallContext.createDefault();
Truth.assertThat(baseContext.merge(defaultOverlay).getStreamWaitTimeout()).isEqualTo(timeout);
GrpcCallContext explicitNullOverlay =
GrpcCallContext.createDefault().withStreamWaitTimeout(null);
Truth.assertThat(baseContext.merge(explicitNullOverlay).getStreamWaitTimeout())
.isEqualTo(timeout);
}
@Test
public void testWithZeroStreamingWaitTimeout() {
Duration timeout = Duration.ZERO;
Truth.assertThat(
GrpcCallContext.createDefault().withStreamWaitTimeout(timeout).getStreamWaitTimeout())
.isEqualTo(timeout);
}
@Test
public void testMergeWithStreamingWaitTimeout() {
Duration timeout = Duration.ofSeconds(19);
GrpcCallContext ctx1 = GrpcCallContext.createDefault();
GrpcCallContext ctx2 = GrpcCallContext.createDefault().withStreamWaitTimeout(timeout);
Truth.assertThat(ctx1.merge(ctx2).getStreamWaitTimeout()).isEqualTo(timeout);
}
@Test
public void testWithStreamingIdleTimeout() {
Duration timeout = Duration.ofSeconds(15);
GrpcCallContext context = GrpcCallContext.createDefault().withStreamIdleTimeout(timeout);
Truth.assertThat(context.getStreamIdleTimeout()).isEqualTo(timeout);
}
@Test
public void testMergeWithNullStreamingIdleTimeout() {
Duration timeout = Duration.ofSeconds(10);
GrpcCallContext baseContext = GrpcCallContext.createDefault().withStreamIdleTimeout(timeout);
GrpcCallContext defaultOverlay = GrpcCallContext.createDefault();
Truth.assertThat(baseContext.merge(defaultOverlay).getStreamIdleTimeout()).isEqualTo(timeout);
GrpcCallContext explicitNullOverlay =
GrpcCallContext.createDefault().withStreamIdleTimeout(null);
Truth.assertThat(baseContext.merge(explicitNullOverlay).getStreamIdleTimeout())
.isEqualTo(timeout);
}
@Test
public void testWithZeroStreamingIdleTimeout() {
Duration timeout = Duration.ZERO;
Truth.assertThat(
GrpcCallContext.createDefault().withStreamIdleTimeout(timeout).getStreamIdleTimeout())
.isEqualTo(timeout);
}
@Test
public void testMergeWithStreamingIdleTimeout() {
Duration timeout = Duration.ofSeconds(19);
GrpcCallContext ctx1 = GrpcCallContext.createDefault();
GrpcCallContext ctx2 = GrpcCallContext.createDefault().withStreamIdleTimeout(timeout);
Truth.assertThat(ctx1.merge(ctx2).getStreamIdleTimeout()).isEqualTo(timeout);
}
@Test
public void testMergeWithCustomCallOptions() {
CallOptions.Key<String> key = CallOptions.Key.createWithDefault("somekey", "somedefault");
GrpcCallContext ctx1 = GrpcCallContext.createDefault();
GrpcCallContext ctx2 =
GrpcCallContext.createDefault()
.withCallOptions(CallOptions.DEFAULT.withOption(key, "somevalue"));
GrpcCallContext merged = (GrpcCallContext) ctx1.merge(ctx2);
Truth.assertThat(merged.getCallOptions().getOption(key))
.isNotEqualTo(ctx1.getCallOptions().getOption(key));
Truth.assertThat(merged.getCallOptions().getOption(key))
.isEqualTo(ctx2.getCallOptions().getOption(key));
}
@Test
public void testWithExtraHeaders() {
Map<String, List<String>> extraHeaders =
createTestExtraHeaders("key1", "value1", "key1", "value2");
GrpcCallContext ctx = GrpcCallContext.createDefault().withExtraHeaders(extraHeaders);
Map<String, List<String>> moreExtraHeaders =
createTestExtraHeaders("key1", "value2", "key2", "value2");
ctx = ctx.withExtraHeaders(moreExtraHeaders);
Map<String, List<String>> gotExtraHeaders = ctx.getExtraHeaders();
Map<String, List<String>> expectedExtraHeaders =
createTestExtraHeaders(
"key1", "value1", "key1", "value2", "key1", "value2", "key2", "value2");
Truth.assertThat(gotExtraHeaders).containsExactlyEntriesIn(expectedExtraHeaders);
}
@Test
public void testMergeWithExtraHeaders() {
Map<String, List<String>> extraHeaders1 =
createTestExtraHeaders("key1", "value1", "key1", "value2");
GrpcCallContext ctx1 = GrpcCallContext.createDefault().withExtraHeaders(extraHeaders1);
Map<String, List<String>> extraHeaders2 =
createTestExtraHeaders("key1", "value2", "key2", "value2");
GrpcCallContext ctx2 = GrpcCallContext.createDefault().withExtraHeaders(extraHeaders2);
ApiCallContext mergedApiCallContext = ctx1.merge(ctx2);
Truth.assertThat(mergedApiCallContext).isInstanceOf(GrpcCallContext.class);
GrpcCallContext mergedGrpcCallContext = (GrpcCallContext) mergedApiCallContext;
Map<String, List<String>> gotExtraHeaders = mergedGrpcCallContext.getExtraHeaders();
Map<String, List<String>> expectedExtraHeaders =
createTestExtraHeaders(
"key1", "value1", "key1", "value2", "key1", "value2", "key2", "value2");
Truth.assertThat(gotExtraHeaders).containsExactlyEntriesIn(expectedExtraHeaders);
}
@Test
public void testMergeWithTracer() {
ApiTracer explicitTracer = Mockito.mock(ApiTracer.class);
GrpcCallContext ctxWithExplicitTracer =
GrpcCallContext.createDefault().withTracer(explicitTracer);
GrpcCallContext ctxWithDefaultTracer = GrpcCallContext.createDefault();
ApiTracer defaultTracer = ctxWithDefaultTracer.getTracer();
// Explicit tracer overrides the default tracer.
Truth.assertThat(ctxWithDefaultTracer.merge(ctxWithExplicitTracer).getTracer())
.isSameInstanceAs(explicitTracer);
// Default tracer does not override an explicit tracer.
Truth.assertThat(ctxWithExplicitTracer.merge(ctxWithDefaultTracer).getTracer())
.isSameInstanceAs(explicitTracer);
// Default tracer does not override another default tracer.
Truth.assertThat(ctxWithDefaultTracer.merge(GrpcCallContext.createDefault()).getTracer())
.isSameInstanceAs(defaultTracer);
}
@Test
public void testWithRetrySettings() {
RetrySettings retrySettings = Mockito.mock(RetrySettings.class);
GrpcCallContext emptyContext = GrpcCallContext.createDefault();
assertNull(emptyContext.getRetrySettings());
GrpcCallContext context = emptyContext.withRetrySettings(retrySettings);
assertNotNull(context.getRetrySettings());
}
@Test
public void testWithRetryableCodes() {
Set<StatusCode.Code> codes = Collections.singleton(StatusCode.Code.UNAVAILABLE);
GrpcCallContext emptyContext = GrpcCallContext.createDefault();
assertNull(emptyContext.getRetryableCodes());
GrpcCallContext context = emptyContext.withRetryableCodes(codes);
assertNotNull(context.getRetryableCodes());
}
@Test
public void testWithOptions() {
GrpcCallContext emptyCallContext = GrpcCallContext.createDefault();
ApiCallContext.Key<String> contextKey1 = ApiCallContext.Key.create("testKey1");
ApiCallContext.Key<String> contextKey2 = ApiCallContext.Key.create("testKey2");
String testContext1 = "test1";
String testContext2 = "test2";
String testContextOverwrite = "test1Overwrite";
GrpcCallContext context =
emptyCallContext
.withOption(contextKey1, testContext1)
.withOption(contextKey2, testContext2);
assertEquals(testContext1, context.getOption(contextKey1));
assertEquals(testContext2, context.getOption(contextKey2));
GrpcCallContext newContext = context.withOption(contextKey1, testContextOverwrite);
assertEquals(testContextOverwrite, newContext.getOption(contextKey1));
}
@Test
public void testMergeOptions() {
GrpcCallContext emptyCallContext = GrpcCallContext.createDefault();
ApiCallContext.Key<String> contextKey1 = ApiCallContext.Key.create("testKey1");
ApiCallContext.Key<String> contextKey2 = ApiCallContext.Key.create("testKey2");
ApiCallContext.Key<String> contextKey3 = ApiCallContext.Key.create("testKey3");
String testContext1 = "test1";
String testContext2 = "test2";
String testContext3 = "test3";
String testContextOverwrite = "test1Overwrite";
GrpcCallContext context1 =
emptyCallContext
.withOption(contextKey1, testContext1)
.withOption(contextKey2, testContext2);
GrpcCallContext context2 =
emptyCallContext
.withOption(contextKey1, testContextOverwrite)
.withOption(contextKey3, testContext3);
ApiCallContext mergedContext = context1.merge(context2);
assertEquals(testContextOverwrite, mergedContext.getOption(contextKey1));
assertEquals(testContext2, mergedContext.getOption(contextKey2));
assertEquals(testContext3, mergedContext.getOption(contextKey3));
}
private static Map<String, List<String>> createTestExtraHeaders(String... keyValues) {
Map<String, List<String>> extraHeaders = new HashMap<>();
for (int i = 0; i < keyValues.length; i += 2) {
String key = keyValues[i];
String value = keyValues[i + 1];
if (!extraHeaders.containsKey(key)) {
extraHeaders.put(key, new ArrayList<String>());
}
extraHeaders.get(key).add(value);
}
return extraHeaders;
}
}