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
/
ServerStreamingAttemptCallableTest.java
524 lines (437 loc) · 19.6 KB
/
ServerStreamingAttemptCallableTest.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*
* Copyright 2018 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;
import com.google.api.core.AbstractApiFuture;
import com.google.api.core.ApiFuture;
import com.google.api.gax.retrying.RetrySettings;
import com.google.api.gax.retrying.RetryingFuture;
import com.google.api.gax.retrying.ServerStreamingAttemptException;
import com.google.api.gax.retrying.StreamResumptionStrategy;
import com.google.api.gax.retrying.TimedAttemptSettings;
import com.google.api.gax.rpc.StatusCode.Code;
import com.google.api.gax.rpc.testing.FakeApiException;
import com.google.api.gax.rpc.testing.FakeCallContext;
import com.google.api.gax.rpc.testing.MockStreamingApi.MockServerStreamingCall;
import com.google.api.gax.rpc.testing.MockStreamingApi.MockServerStreamingCallable;
import com.google.api.gax.tracing.BaseApiTracer;
import com.google.common.collect.Queues;
import com.google.common.truth.Truth;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
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 ServerStreamingAttemptCallableTest {
private MockServerStreamingCallable<String, String> innerCallable;
private AccumulatingObserver observer;
private FakeRetryingFuture fakeRetryingFuture;
private StreamResumptionStrategy<String, String> resumptionStrategy;
private static Duration totalTimeout = Duration.ofHours(1);
private FakeCallContext mockedCallContext;
@Before
public void setUp() {
innerCallable = new MockServerStreamingCallable<>();
observer = new AccumulatingObserver(true);
resumptionStrategy = new MyStreamResumptionStrategy();
mockedCallContext = Mockito.mock(FakeCallContext.class);
}
private ServerStreamingAttemptCallable<String, String> createCallable() {
return createCallable(FakeCallContext.createDefault());
}
private ServerStreamingAttemptCallable<String, String> createCallable(ApiCallContext context) {
ServerStreamingAttemptCallable<String, String> callable =
new ServerStreamingAttemptCallable<>(
innerCallable, resumptionStrategy, "request", context, observer);
fakeRetryingFuture = new FakeRetryingFuture(callable);
callable.setExternalFuture(fakeRetryingFuture);
return callable;
}
@Test
public void testUserProvidedContextTimeout() {
// Mock up the ApiCallContext as if the user provided a timeout and streamWaitTimeout.
Mockito.doReturn(BaseApiTracer.getInstance()).when(mockedCallContext).getTracer();
Mockito.doReturn(Duration.ofHours(5)).when(mockedCallContext).getTimeout();
Mockito.doReturn(Duration.ofHours(5)).when(mockedCallContext).getStreamWaitTimeout();
ServerStreamingAttemptCallable<String, String> callable = createCallable(mockedCallContext);
callable.start();
// Ensure that the callable did not overwrite the user provided timeouts
Mockito.verify(mockedCallContext, Mockito.times(1)).getTimeout();
Mockito.verify(mockedCallContext, Mockito.never()).withTimeout(totalTimeout);
Mockito.verify(mockedCallContext, Mockito.times(1)).getStreamWaitTimeout();
Mockito.verify(mockedCallContext, Mockito.never())
.withStreamWaitTimeout(Mockito.any(Duration.class));
// Should notify outer observer
Truth.assertThat(observer.controller).isNotNull();
// Should configure the inner controller correctly.
MockServerStreamingCall<String, String> call = innerCallable.popLastCall();
Truth.assertThat(call.getController().isAutoFlowControlEnabled()).isTrue();
Truth.assertThat(call.getRequest()).isEqualTo("request");
// Send a response in auto flow mode.
call.getController().getObserver().onResponse("response1");
call.getController().getObserver().onResponse("response2");
call.getController().getObserver().onComplete();
// Make sure the responses are received
Truth.assertThat(observer.responses).containsExactly("response1", "response2").inOrder();
fakeRetryingFuture.assertSuccess();
}
@Test
public void testNoUserProvidedContextTimeout() {
// Mock up the ApiCallContext as if the user did not provide custom timeouts.
Mockito.doReturn(BaseApiTracer.getInstance()).when(mockedCallContext).getTracer();
Mockito.doReturn(null).when(mockedCallContext).getTimeout();
Mockito.doReturn(null).when(mockedCallContext).getStreamWaitTimeout();
Mockito.doReturn(mockedCallContext).when(mockedCallContext).withTimeout(totalTimeout);
Mockito.doReturn(mockedCallContext)
.when(mockedCallContext)
.withStreamWaitTimeout(Mockito.any(Duration.class));
ServerStreamingAttemptCallable<String, String> callable = createCallable(mockedCallContext);
callable.start();
// Ensure that the callable configured the timeouts via the Settings in the
// absence of user-defined timeouts.
Mockito.verify(mockedCallContext, Mockito.times(1)).getTimeout();
Mockito.verify(mockedCallContext, Mockito.times(1)).withTimeout(totalTimeout);
Mockito.verify(mockedCallContext, Mockito.times(1)).getStreamWaitTimeout();
Mockito.verify(mockedCallContext, Mockito.times(1))
.withStreamWaitTimeout(Mockito.any(Duration.class));
// Should notify outer observer
Truth.assertThat(observer.controller).isNotNull();
// Should configure the inner controller correctly.
MockServerStreamingCall<String, String> call = innerCallable.popLastCall();
Truth.assertThat(call.getController().isAutoFlowControlEnabled()).isTrue();
Truth.assertThat(call.getRequest()).isEqualTo("request");
// Send a response in auto flow mode.
call.getController().getObserver().onResponse("response1");
call.getController().getObserver().onResponse("response2");
call.getController().getObserver().onComplete();
// Make sure the responses are received
Truth.assertThat(observer.responses).containsExactly("response1", "response2").inOrder();
fakeRetryingFuture.assertSuccess();
}
@Test
public void testNoErrorsAutoFlow() {
ServerStreamingAttemptCallable<String, String> callable = createCallable();
callable.start();
// Should notify outer observer
Truth.assertThat(observer.controller).isNotNull();
// Should configure the inner controller correctly.
MockServerStreamingCall<String, String> call = innerCallable.popLastCall();
Truth.assertThat(call.getController().isAutoFlowControlEnabled()).isTrue();
Truth.assertThat(call.getRequest()).isEqualTo("request");
// Send a response in auto flow mode.
call.getController().getObserver().onResponse("response1");
call.getController().getObserver().onResponse("response2");
call.getController().getObserver().onComplete();
// Make sure the responses are received
Truth.assertThat(observer.responses).containsExactly("response1", "response2").inOrder();
fakeRetryingFuture.assertSuccess();
}
@Test
public void testNoErrorsManualFlow() {
observer = new AccumulatingObserver(false);
ServerStreamingAttemptCallable<String, String> callable = createCallable();
callable.start();
// Should notify outer observer.
Truth.assertThat(observer.controller).isNotNull();
// Should configure the inner controller correctly.
MockServerStreamingCall<String, String> call = innerCallable.popLastCall();
Truth.assertThat(call.getController().isAutoFlowControlEnabled()).isFalse();
Truth.assertThat(call.getRequest()).isEqualTo("request");
// Request and send message 1.
observer.controller.request(1);
Truth.assertThat(call.getController().popLastPull()).isEqualTo(1);
call.getController().getObserver().onResponse("response1");
// Request & send message 1.
observer.controller.request(1);
Truth.assertThat(call.getController().popLastPull()).isEqualTo(1);
call.getController().getObserver().onResponse("response2");
call.getController().getObserver().onComplete();
// Make sure the responses are received
Truth.assertThat(observer.responses).containsExactly("response1", "response2").inOrder();
fakeRetryingFuture.assertSuccess();
}
@Test
@SuppressWarnings("ConstantConditions")
public void testInitialRetry() {
resumptionStrategy = new MyStreamResumptionStrategy();
ServerStreamingAttemptCallable<String, String> callable = createCallable();
callable.start();
MockServerStreamingCall<String, String> call = innerCallable.popLastCall();
// Send initial error
FakeApiException initialError = new FakeApiException(null, Code.UNAVAILABLE, true);
call.getController().getObserver().onError(initialError);
// Should notify the outer future
Throwable outerError = null;
try {
fakeRetryingFuture.getAttemptResult().get(1, TimeUnit.SECONDS);
} catch (ExecutionException e) {
outerError = e.getCause();
} catch (Throwable e) {
outerError = e;
}
Truth.assertThat(outerError).isInstanceOf(ServerStreamingAttemptException.class);
Truth.assertThat(((ServerStreamingAttemptException) outerError).hasSeenResponses()).isFalse();
Truth.assertThat(((ServerStreamingAttemptException) outerError).canResume()).isTrue();
Truth.assertThat(outerError.getCause()).isEqualTo(initialError);
// Make the retry call
callable.call();
call = innerCallable.popLastCall();
// Verify the request and send a response
Truth.assertThat(call.getRequest()).isEqualTo("request > 0");
}
@Test
@SuppressWarnings("ConstantConditions")
public void testMidRetry() {
resumptionStrategy = new MyStreamResumptionStrategy();
ServerStreamingAttemptCallable<String, String> callable = createCallable();
callable.start();
MockServerStreamingCall<String, String> call = innerCallable.popLastCall();
// Respond to the initial request with a coupple responses and an error.
Truth.assertThat(call.getRequest()).isEqualTo("request");
call.getController().getObserver().onResponse("response1");
call.getController().getObserver().onResponse("response2");
FakeApiException innerError = new FakeApiException(null, Code.UNAVAILABLE, true);
call.getController().getObserver().onError(innerError);
// Should notify the outer future
Throwable outerError = null;
try {
fakeRetryingFuture.getAttemptResult().get(1, TimeUnit.SECONDS);
} catch (ExecutionException e) {
outerError = e.getCause();
} catch (Throwable e) {
outerError = e;
}
Truth.assertThat(outerError).isInstanceOf(ServerStreamingAttemptException.class);
Truth.assertThat(((ServerStreamingAttemptException) outerError).hasSeenResponses()).isTrue();
Truth.assertThat(((ServerStreamingAttemptException) outerError).canResume()).isTrue();
Truth.assertThat(outerError.getCause()).isEqualTo(innerError);
// Make the retry call
callable.call();
call = innerCallable.popLastCall();
// Verify that the request was narrowed and send a response
Truth.assertThat(call.getRequest()).isEqualTo("request > 2");
call.getController().getObserver().onResponse("response3");
Truth.assertThat(observer.responses)
.containsExactly("response1", "response2", "response3")
.inOrder();
}
@Test
public void testRequestCountIsPreserved() {
observer = new AccumulatingObserver(false);
ServerStreamingAttemptCallable<String, String> callable = createCallable();
callable.start();
observer.controller.request(5);
Truth.assertThat(observer.controller).isNotNull();
MockServerStreamingCall<String, String> call = innerCallable.popLastCall();
Truth.assertThat(call).isNotNull();
Truth.assertThat(call.getController().isAutoFlowControlEnabled()).isFalse();
Truth.assertThat(call.getController().popLastPull()).isEqualTo(5);
// decrement
call.getController().getObserver().onResponse("response");
// and then error
call.getController()
.getObserver()
.onError(new FakeApiException(null, Code.UNAUTHENTICATED, true));
// Make the retry call
callable.call();
call = innerCallable.popLastCall();
// Verify that the count is correct
Truth.assertThat(call.getController().popLastPull()).isEqualTo(4);
}
@Test
public void testCancel() {
observer = new AccumulatingObserver(false);
ServerStreamingAttemptCallable<String, String> callable = createCallable();
callable.start();
observer.controller.request(1);
Truth.assertThat(observer.controller).isNotNull();
MockServerStreamingCall<String, String> call = innerCallable.popLastCall();
Truth.assertThat(call).isNotNull();
Truth.assertThat(call.getController().isAutoFlowControlEnabled()).isFalse();
observer.controller.cancel();
// Check upstream is cancelled
Truth.assertThat(call.getController().isCancelled()).isTrue();
// and after upstream cancellation is processed, downstream is cancelled, but the cause is
// replaced
RuntimeException innerException =
new RuntimeException("Some internal representation of cancel");
call.getController().getObserver().onError(innerException);
Throwable outerError = null;
try {
fakeRetryingFuture.getAttemptResult().get(1, TimeUnit.SECONDS);
} catch (ExecutionException e) {
outerError = e.getCause();
} catch (Throwable e) {
outerError = e;
}
Truth.assertThat(outerError).isInstanceOf(ServerStreamingAttemptException.class);
Truth.assertThat(outerError.getCause()).isInstanceOf(CancellationException.class);
// Make sure that the stack trace of the cancellation is preserved
boolean includesMeInStackTrace = false;
for (StackTraceElement e : outerError.getCause().getStackTrace()) {
if (ServerStreamingAttemptCallableTest.class.getName().equals(e.getClassName())) {
includesMeInStackTrace = true;
break;
}
}
Truth.assertWithMessage("Cancel caller included in stack trace")
.that(includesMeInStackTrace)
.isTrue();
}
@Test
public void testResponseSubstitution() {
resumptionStrategy =
new MyStreamResumptionStrategy() {
@Override
public String processResponse(String response) {
return super.processResponse(response) + "+suffix";
}
};
observer = new AccumulatingObserver(false);
ServerStreamingAttemptCallable<String, String> callable = createCallable();
callable.start();
MockServerStreamingCall<String, String> call = innerCallable.popLastCall();
// Send initial response & then error
call.getController().getObserver().onResponse("first");
call.getController().getObserver().onError(new FakeApiException(null, Code.UNAVAILABLE, true));
// Make the retry call
callable.call();
call = innerCallable.popLastCall();
// Send another couple of responses (the first one will be ignored)
call.getController().getObserver().onResponse("second");
call.getController().getObserver().onResponse("third");
call.getController().getObserver().onComplete();
// Verify the request and send a response
Truth.assertThat(observer.responses)
.containsExactly("first+suffix", "second+suffix", "third+suffix");
}
static class MyStreamResumptionStrategy implements StreamResumptionStrategy<String, String> {
private int responseCount;
@Override
public StreamResumptionStrategy<String, String> createNew() {
return new MyStreamResumptionStrategy();
}
@Override
public String processResponse(String response) {
responseCount++;
return response;
}
@Override
public String getResumeRequest(String originalRequest) {
return originalRequest + " > " + responseCount;
}
@Override
public boolean canResume() {
return true;
}
}
static class AccumulatingObserver implements ResponseObserver<String> {
final boolean autoFlow;
StreamController controller;
final BlockingDeque<String> responses = Queues.newLinkedBlockingDeque();
private Throwable error;
private boolean complete;
AccumulatingObserver(boolean autoFlow) {
this.autoFlow = autoFlow;
}
@Override
public void onStart(StreamController controller) {
this.controller = controller;
if (!autoFlow) {
controller.disableAutoInboundFlowControl();
}
}
@Override
public void onResponse(String response) {
responses.add(response);
}
@Override
public void onError(Throwable t) {
this.error = t;
}
@Override
public void onComplete() {
this.complete = true;
}
}
private static class FakeRetryingFuture extends AbstractApiFuture<Void>
implements RetryingFuture<Void> {
private final ServerStreamingAttemptCallable<String, String> attemptCallable;
private ApiFuture<Void> attemptFuture;
private TimedAttemptSettings attemptSettings;
FakeRetryingFuture(ServerStreamingAttemptCallable<String, String> attemptCallable) {
this.attemptCallable = attemptCallable;
attemptSettings =
TimedAttemptSettings.newBuilder()
.setGlobalSettings(RetrySettings.newBuilder().setTotalTimeout(totalTimeout).build())
.setFirstAttemptStartTimeNanos(0)
.setAttemptCount(0)
.setOverallAttemptCount(0)
.setRandomizedRetryDelay(Duration.ofMillis(1))
.setRetryDelay(Duration.ofMillis(1))
.setRpcTimeout(Duration.ofMinutes(1))
.build();
}
@Override
public void setAttemptFuture(ApiFuture<Void> attemptFuture) {
this.attemptFuture = attemptFuture;
}
@Override
public ServerStreamingAttemptCallable<String, String> getCallable() {
return attemptCallable;
}
@Override
public TimedAttemptSettings getAttemptSettings() {
return attemptSettings;
}
@Override
public ApiFuture<Void> peekAttemptResult() {
throw new UnsupportedOperationException();
}
@Override
public ApiFuture<Void> getAttemptResult() {
return attemptFuture;
}
void assertSuccess() {
Throwable actualError = null;
try {
attemptFuture.get(1, TimeUnit.SECONDS);
} catch (Throwable t) {
actualError = t;
}
Truth.assertThat(actualError).isNull();
}
}
}