This repository has been archived by the owner on Sep 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 119
/
FakeCallContext.java
376 lines (338 loc) · 11.3 KB
/
FakeCallContext.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
/*
* 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.rpc.testing;
import com.google.api.core.InternalApi;
import com.google.api.gax.retrying.RetrySettings;
import com.google.api.gax.rpc.ApiCallContext;
import com.google.api.gax.rpc.ClientContext;
import com.google.api.gax.rpc.StatusCode;
import com.google.api.gax.rpc.TransportChannel;
import com.google.api.gax.rpc.internal.Headers;
import com.google.api.gax.tracing.ApiTracer;
import com.google.api.gax.tracing.BaseApiTracer;
import com.google.auth.Credentials;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.threeten.bp.Duration;
@InternalApi("for testing")
public class FakeCallContext implements ApiCallContext {
private final Credentials credentials;
private final FakeChannel channel;
private final Duration timeout;
private final Duration streamWaitTimeout;
private final Duration streamIdleTimeout;
private final ImmutableMap<String, List<String>> extraHeaders;
private final ApiTracer tracer;
private final RetrySettings retrySettings;
private final ImmutableSet<StatusCode.Code> retryableCodes;
private FakeCallContext(
Credentials credentials,
FakeChannel channel,
Duration timeout,
Duration streamWaitTimeout,
Duration streamIdleTimeout,
ImmutableMap<String, List<String>> extraHeaders,
ApiTracer tracer,
RetrySettings retrySettings,
Set<StatusCode.Code> retryableCodes) {
this.credentials = credentials;
this.channel = channel;
this.timeout = timeout;
this.streamWaitTimeout = streamWaitTimeout;
this.streamIdleTimeout = streamIdleTimeout;
this.extraHeaders = extraHeaders;
this.tracer = tracer;
this.retrySettings = retrySettings;
this.retryableCodes = retryableCodes == null ? null : ImmutableSet.copyOf(retryableCodes);
}
public static FakeCallContext createDefault() {
return new FakeCallContext(
null, null, null, null, null, ImmutableMap.<String, List<String>>of(), null, null, null);
}
@Override
public FakeCallContext nullToSelf(ApiCallContext inputContext) {
FakeCallContext fakeCallContext;
if (inputContext == null) {
fakeCallContext = this;
} else {
if (!(inputContext instanceof FakeCallContext)) {
throw new IllegalArgumentException(
"context must be an instance of FakeCallContext, but found "
+ inputContext.getClass().getName());
}
fakeCallContext = (FakeCallContext) inputContext;
}
return fakeCallContext;
}
@Override
public ApiCallContext merge(ApiCallContext inputCallContext) {
if (inputCallContext == null) {
return this;
}
if (!(inputCallContext instanceof FakeCallContext)) {
throw new IllegalArgumentException(
"context must be an instance of FakeCallContext, but found "
+ inputCallContext.getClass().getName());
}
FakeCallContext fakeCallContext = (FakeCallContext) inputCallContext;
FakeChannel newChannel = fakeCallContext.channel;
if (newChannel == null) {
newChannel = channel;
}
Credentials newCallCredentials = fakeCallContext.credentials;
if (newCallCredentials == null) {
newCallCredentials = credentials;
}
Duration newTimeout = fakeCallContext.timeout;
if (newTimeout == null) {
newTimeout = timeout;
}
Duration newStreamWaitTimeout = fakeCallContext.streamWaitTimeout;
if (newStreamWaitTimeout == null) {
newStreamWaitTimeout = streamWaitTimeout;
}
Duration newStreamIdleTimeout = fakeCallContext.streamIdleTimeout;
if (newStreamIdleTimeout == null) {
newStreamIdleTimeout = streamIdleTimeout;
}
ApiTracer newTracer = fakeCallContext.tracer;
if (newTracer == null) {
newTracer = this.tracer;
}
RetrySettings newRetrySettings = fakeCallContext.retrySettings;
if (newRetrySettings == null) {
newRetrySettings = this.retrySettings;
}
Set<StatusCode.Code> newRetryableCodes = fakeCallContext.retryableCodes;
if (newRetryableCodes == null) {
newRetryableCodes = this.retryableCodes;
}
ImmutableMap<String, List<String>> newExtraHeaders =
Headers.mergeHeaders(extraHeaders, fakeCallContext.extraHeaders);
return new FakeCallContext(
newCallCredentials,
newChannel,
newTimeout,
newStreamWaitTimeout,
newStreamIdleTimeout,
newExtraHeaders,
newTracer,
newRetrySettings,
newRetryableCodes);
}
public RetrySettings getRetrySettings() {
return retrySettings;
}
public FakeCallContext withRetrySettings(RetrySettings retrySettings) {
return new FakeCallContext(
this.credentials,
this.channel,
this.timeout,
this.streamWaitTimeout,
this.streamIdleTimeout,
this.extraHeaders,
this.tracer,
retrySettings,
this.retryableCodes);
}
public Set<StatusCode.Code> getRetryableCodes() {
return retryableCodes;
}
public FakeCallContext withRetryableCodes(Set<StatusCode.Code> retryableCodes) {
return new FakeCallContext(
this.credentials,
this.channel,
this.timeout,
this.streamWaitTimeout,
this.streamIdleTimeout,
this.extraHeaders,
this.tracer,
this.retrySettings,
retryableCodes);
}
public Credentials getCredentials() {
return credentials;
}
public FakeChannel getChannel() {
return channel;
}
@Override
public Duration getTimeout() {
return timeout;
}
@Nullable
@Override
public Duration getStreamWaitTimeout() {
return streamWaitTimeout;
}
@Nullable
@Override
public Duration getStreamIdleTimeout() {
return streamIdleTimeout;
}
@Override
public FakeCallContext withCredentials(Credentials credentials) {
return new FakeCallContext(
credentials,
this.channel,
this.timeout,
this.streamWaitTimeout,
this.streamIdleTimeout,
this.extraHeaders,
this.tracer,
this.retrySettings,
this.retryableCodes);
}
@Override
public FakeCallContext withTransportChannel(TransportChannel inputChannel) {
Preconditions.checkNotNull(inputChannel);
if (!(inputChannel instanceof FakeTransportChannel)) {
throw new IllegalArgumentException(
"Expected FakeTransportChannel, got " + inputChannel.getClass().getName());
}
FakeTransportChannel transportChannel = (FakeTransportChannel) inputChannel;
return withChannel(transportChannel.getChannel());
}
public FakeCallContext withChannel(FakeChannel channel) {
return new FakeCallContext(
this.credentials,
channel,
this.timeout,
this.streamWaitTimeout,
this.streamIdleTimeout,
this.extraHeaders,
this.tracer,
this.retrySettings,
this.retryableCodes);
}
@Override
public FakeCallContext withTimeout(Duration timeout) {
// Default RetrySettings use 0 for RPC timeout. Treat that as disabled timeouts.
if (timeout != null && (timeout.isZero() || timeout.isNegative())) {
timeout = null;
}
// Prevent expanding timeouts
if (timeout != null && this.timeout != null && this.timeout.compareTo(timeout) <= 0) {
return this;
}
return new FakeCallContext(
this.credentials,
this.channel,
timeout,
this.streamWaitTimeout,
this.streamIdleTimeout,
this.extraHeaders,
this.tracer,
this.retrySettings,
this.retryableCodes);
}
@Override
public ApiCallContext withStreamWaitTimeout(@Nullable Duration streamWaitTimeout) {
return new FakeCallContext(
this.credentials,
this.channel,
this.timeout,
streamWaitTimeout,
this.streamIdleTimeout,
this.extraHeaders,
this.tracer,
this.retrySettings,
this.retryableCodes);
}
@Override
public ApiCallContext withStreamIdleTimeout(@Nullable Duration streamIdleTimeout) {
Preconditions.checkNotNull(streamIdleTimeout);
return new FakeCallContext(
this.credentials,
this.channel,
this.timeout,
this.streamWaitTimeout,
streamIdleTimeout,
this.extraHeaders,
this.tracer,
this.retrySettings,
this.retryableCodes);
}
@Override
public ApiCallContext withExtraHeaders(Map<String, List<String>> extraHeaders) {
Preconditions.checkNotNull(extraHeaders);
ImmutableMap<String, List<String>> newExtraHeaders =
Headers.mergeHeaders(this.extraHeaders, extraHeaders);
return new FakeCallContext(
credentials,
channel,
timeout,
streamWaitTimeout,
streamIdleTimeout,
newExtraHeaders,
this.tracer,
this.retrySettings,
this.retryableCodes);
}
@Override
public Map<String, List<String>> getExtraHeaders() {
return this.extraHeaders;
}
/** {@inheritDoc} */
@Override
@Nonnull
public ApiTracer getTracer() {
if (tracer == null) {
return BaseApiTracer.getInstance();
}
return tracer;
}
/** {@inheritDoc} */
@Override
public ApiCallContext withTracer(@Nonnull ApiTracer tracer) {
Preconditions.checkNotNull(tracer);
return new FakeCallContext(
this.credentials,
this.channel,
this.timeout,
this.streamWaitTimeout,
this.streamIdleTimeout,
this.extraHeaders,
tracer,
this.retrySettings,
this.retryableCodes);
}
public static FakeCallContext create(ClientContext clientContext) {
return FakeCallContext.createDefault()
.withTransportChannel(clientContext.getTransportChannel())
.withCredentials(clientContext.getCredentials());
}
}