/
ReframingResponseObserverTest.java
524 lines (440 loc) · 19.8 KB
/
ReframingResponseObserverTest.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
*
* 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.gaxx.reframing;
import com.google.api.gax.rpc.StreamController;
import com.google.cloud.bigtable.gaxx.testing.FakeStreamingApi.ServerStreamingStashCallable;
import com.google.cloud.bigtable.gaxx.testing.FakeStreamingApi.ServerStreamingStashCallable.StreamControllerStash;
import com.google.cloud.bigtable.gaxx.testing.MockStreamingApi.MockResponseObserver;
import com.google.cloud.bigtable.gaxx.testing.MockStreamingApi.MockServerStreamingCall;
import com.google.cloud.bigtable.gaxx.testing.MockStreamingApi.MockServerStreamingCallable;
import com.google.cloud.bigtable.gaxx.testing.MockStreamingApi.MockStreamController;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Queues;
import com.google.common.truth.Truth;
import java.util.Arrays;
import java.util.Queue;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mockito;
@RunWith(JUnit4.class)
public class ReframingResponseObserverTest {
private ExecutorService executor;
@Before
public void setUp() throws Exception {
executor = Executors.newCachedThreadPool();
}
@After
public void tearDown() throws Exception {
executor.shutdownNow();
}
@Test
public void testUnsolicitedResponseError() throws Exception {
// Have the outer observer request manual flow control
MockResponseObserver<String> outerObserver = new MockResponseObserver<>(false);
ReframingResponseObserver<String, String> middleware =
new ReframingResponseObserver<>(outerObserver, new DasherizingReframer(1));
MockServerStreamingCallable<String, String> innerCallable = new MockServerStreamingCallable<>();
innerCallable.call("request", middleware);
MockServerStreamingCall<String, String> lastCall = innerCallable.popLastCall();
MockStreamController<String> innerController = lastCall.getController();
// Nothing was requested by the outer observer (thats also in manual flow control)
Preconditions.checkState(innerController.popLastPull() == 0);
Throwable error = null;
try {
// send an unsolicited response
innerController.getObserver().onResponse("a");
} catch (Throwable t) {
error = t;
}
Truth.assertThat(error).isInstanceOf(IllegalStateException.class);
}
@Test
public void testConcurrentRequestAfterClose() throws Exception {
// Have the outer observer request manual flow control
GatedMockResponseObserver outerObserver = new GatedMockResponseObserver(false);
outerObserver.completeBreakpoint.enable();
ReframingResponseObserver<String, String> middleware =
new ReframingResponseObserver<>(outerObserver, new DasherizingReframer(1));
MockServerStreamingCallable<String, String> innerCallable = new MockServerStreamingCallable<>();
innerCallable.call("request", middleware);
MockServerStreamingCall<String, String> lastCall = innerCallable.popLastCall();
final MockStreamController<String> innerController = lastCall.getController();
// Asynchronously start the delivery loop for a completion by notifying the innermost
// observer, which will bubble up to the outer GatedMockResponseObserver and hit the
// completeBreakpoint.
Future<?> completeFuture =
executor.submit(
new Runnable() {
@Override
public void run() {
innerController.getObserver().onComplete();
}
});
// Wait until the delivery loop started in the other thread.
outerObserver.completeBreakpoint.awaitArrival();
// Concurrently request the next message.
outerObserver.getController().request(1);
// Resume the other thread
outerObserver.completeBreakpoint.release();
// Should have no errors delivered.
Truth.assertThat(outerObserver.getFinalError()).isNull();
// Should have no errors thrown.
Throwable error = null;
try {
completeFuture.get();
} catch (ExecutionException e) {
error = e.getCause();
}
Truth.assertThat(error).isNull();
}
@Test
public void testOneToOne() throws InterruptedException {
// Have the outer observer request manual flow control
MockResponseObserver<String> outerObserver = new MockResponseObserver<>(false);
ReframingResponseObserver<String, String> middleware =
new ReframingResponseObserver<>(outerObserver, new DasherizingReframer(1));
ServerStreamingStashCallable<String, String> innerCallable =
new ServerStreamingStashCallable<>(ImmutableList.of("a"));
innerCallable.call("request", middleware);
// simple path: downstream requests 1 response, the request is proxied to upstream & upstream
// delivers.
outerObserver.getController().request(1);
Truth.assertThat(outerObserver.popNextResponse()).isEqualTo("a");
Truth.assertThat(outerObserver.isDone()).isTrue();
}
@Test
public void testOneToOneAuto() throws InterruptedException {
MockResponseObserver<String> outerObserver = new MockResponseObserver<>(true);
ReframingResponseObserver<String, String> middleware =
new ReframingResponseObserver<>(outerObserver, new DasherizingReframer(1));
ServerStreamingStashCallable<String, String> innerCallable =
new ServerStreamingStashCallable<>(ImmutableList.of("a", "b"));
innerCallable.call("request", middleware);
Truth.assertThat(outerObserver.popNextResponse()).isEqualTo("a");
Truth.assertThat(outerObserver.popNextResponse()).isEqualTo("b");
Truth.assertThat(outerObserver.isDone()).isTrue();
}
@Test
public void testManyToOne() throws InterruptedException {
MockResponseObserver<String> outerObserver = new MockResponseObserver<>(false);
ReframingResponseObserver<String, String> middleware =
new ReframingResponseObserver<>(outerObserver, new DasherizingReframer(1));
ServerStreamingStashCallable<String, String> innerCallable =
new ServerStreamingStashCallable<>(ImmutableList.of("a-b"));
innerCallable.call("request", middleware);
Preconditions.checkState(outerObserver.popNextResponse() == null);
// First downstream request makes the upstream over produce
outerObserver.getController().request(1);
Truth.assertThat(outerObserver.popNextResponse()).isEqualTo("a");
Truth.assertThat(outerObserver.popNextResponse()).isEqualTo(null);
Truth.assertThat(outerObserver.isDone()).isFalse();
// Next downstream request should fetch from buffer
outerObserver.getController().request(1);
Truth.assertThat(outerObserver.popNextResponse()).isEqualTo("b");
// Make sure completion is delivered
Truth.assertThat(outerObserver.isDone()).isTrue();
}
@Test
public void testManyToOneAuto() throws InterruptedException {
MockResponseObserver<String> outerObserver = new MockResponseObserver<>(true);
ReframingResponseObserver<String, String> middleware =
new ReframingResponseObserver<>(outerObserver, new DasherizingReframer(1));
ServerStreamingStashCallable<String, String> innerCallable =
new ServerStreamingStashCallable<>(ImmutableList.of("a-b"));
innerCallable.call("request", middleware);
Truth.assertThat(outerObserver.popNextResponse()).isEqualTo("a");
Truth.assertThat(outerObserver.popNextResponse()).isEqualTo("b");
Truth.assertThat(outerObserver.isDone()).isTrue();
}
@Test
public void testManyToOneCancelEarly() throws InterruptedException {
MockResponseObserver<String> outerObserver = new MockResponseObserver<>(false);
ReframingResponseObserver<String, String> middleware =
new ReframingResponseObserver<>(outerObserver, new DasherizingReframer(1));
MockServerStreamingCallable<String, String> innerCallable = new MockServerStreamingCallable<>();
innerCallable.call("request", middleware);
MockServerStreamingCall<String, String> lastCall = innerCallable.popLastCall();
MockStreamController<String> innerController = lastCall.getController();
outerObserver.getController().request(1);
innerController.getObserver().onResponse("a-b");
outerObserver.popNextResponse();
outerObserver.getController().cancel();
Truth.assertThat(innerController.isCancelled()).isTrue();
innerController.getObserver().onError(new RuntimeException("Some other upstream error"));
Truth.assertThat(outerObserver.getFinalError()).isInstanceOf(CancellationException.class);
}
@Test
public void testOneToMany() throws InterruptedException {
MockResponseObserver<String> outerObserver = new MockResponseObserver<>(false);
ReframingResponseObserver<String, String> middleware =
new ReframingResponseObserver<>(outerObserver, new DasherizingReframer(2));
ServerStreamingStashCallable<String, String> innerCallable =
new ServerStreamingStashCallable<>(ImmutableList.of("a", "b"));
innerCallable.call("request", middleware);
Preconditions.checkState(outerObserver.popNextResponse() == null);
outerObserver.getController().request(1);
Truth.assertThat(outerObserver.popNextResponse()).isEqualTo("a-b");
Truth.assertThat(outerObserver.isDone()).isTrue();
Truth.assertThat(outerObserver.getFinalError()).isNull();
}
@Test
public void testOneToManyAuto() throws InterruptedException {
MockResponseObserver<String> outerObserver = new MockResponseObserver<>(true);
ReframingResponseObserver<String, String> middleware =
new ReframingResponseObserver<>(outerObserver, new DasherizingReframer(2));
ServerStreamingStashCallable<String, String> innerCallable =
new ServerStreamingStashCallable<>(ImmutableList.of("a", "b"));
innerCallable.call("request", middleware);
Truth.assertThat(outerObserver.popNextResponse()).isEqualTo("a-b");
Truth.assertThat(outerObserver.isDone()).isTrue();
Truth.assertThat(outerObserver.getFinalError()).isNull();
}
@Test
public void testOneToManyIncomplete() {
MockResponseObserver<String> outerObserver = new MockResponseObserver<>(true);
ReframingResponseObserver<String, String> middleware =
new ReframingResponseObserver<>(outerObserver, new DasherizingReframer(2));
ServerStreamingStashCallable<String, String> innerCallable =
new ServerStreamingStashCallable<>(ImmutableList.of("a"));
innerCallable.call("request", middleware);
// Make sure completion is delivered
Truth.assertThat(outerObserver.getFinalError()).isInstanceOf(IncompleteStreamException.class);
}
@Test
public void testConcurrentCancel() throws InterruptedException {
final MockResponseObserver<String> outerObserver = new MockResponseObserver<>(true);
ReframingResponseObserver<String, String> middleware =
new ReframingResponseObserver<>(outerObserver, new DasherizingReframer(2));
MockServerStreamingCallable<String, String> innerCallable = new MockServerStreamingCallable<>();
innerCallable.call("request", middleware);
MockServerStreamingCall<String, String> lastCall = innerCallable.popLastCall();
final MockStreamController<String> innerController = lastCall.getController();
final CountDownLatch latch = new CountDownLatch(2);
executor.submit(
new Runnable() {
@Override
public void run() {
while (!outerObserver.isDone()) {
outerObserver.popNextResponse();
}
latch.countDown();
}
});
executor.submit(
new Runnable() {
@Override
public void run() {
while (!innerController.isCancelled()) {
if (innerController.popLastPull() > 0) {
innerController.getObserver().onResponse("a");
}
}
innerController
.getObserver()
.onError(new RuntimeException("Some other upstream error"));
latch.countDown();
}
});
outerObserver.getController().cancel();
Truth.assertThat(latch.await(1, TimeUnit.MINUTES)).isTrue();
}
@Test
public void testReframerPushError() throws Exception {
MockResponseObserver<String> outerObserver = new MockResponseObserver<>(true);
Reframer<String, String> reframer =
new DasherizingReframer(1) {
@Override
public void push(String response) {
if ("boom".equals(response)) {
throw new IllegalStateException("fake error");
}
super.push(response);
}
};
ReframingResponseObserver<String, String> middleware =
new ReframingResponseObserver<>(outerObserver, reframer);
ServerStreamingStashCallable<String, String> innerCallable =
new ServerStreamingStashCallable<>(ImmutableList.of("a", "boom", "c"));
innerCallable.call("request", middleware);
Truth.assertThat(outerObserver.getFinalError()).isInstanceOf(IllegalStateException.class);
Truth.assertThat(outerObserver.getFinalError()).hasMessageThat().isEqualTo("fake error");
Truth.assertThat(outerObserver.popNextResponse()).isEqualTo("a");
Truth.assertThat(outerObserver.popNextResponse()).isNull();
}
@Test
public void testReframerPopError() {
final AtomicInteger popCount = new AtomicInteger();
MockResponseObserver<String> outerObserver = new MockResponseObserver<>(true);
Reframer<String, String> reframer =
new DasherizingReframer(1) {
@Override
public String pop() {
if (popCount.incrementAndGet() == 2) {
throw new IllegalStateException("fake error");
}
return super.pop();
}
};
ReframingResponseObserver<String, String> middleware =
new ReframingResponseObserver<>(outerObserver, reframer);
ServerStreamingStashCallable<String, String> innerCallable =
new ServerStreamingStashCallable<>(ImmutableList.of("a", "boom", "c"));
innerCallable.call("request", middleware);
StreamControllerStash<String> lastCall = innerCallable.popLastCall();
Truth.assertThat(outerObserver.getFinalError()).isInstanceOf(IllegalStateException.class);
Truth.assertThat(outerObserver.getFinalError()).hasMessageThat().isEqualTo("fake error");
Truth.assertThat(outerObserver.popNextResponse()).isEqualTo("a");
Truth.assertThat(outerObserver.popNextResponse()).isNull();
Truth.assertThat(popCount.get()).isEqualTo(2);
Truth.assertThat(lastCall.getError()).isInstanceOf(CancellationException.class);
Truth.assertThat(lastCall.getNumDelivered()).isEqualTo(2);
}
/**
* Test the scenario where the reframer throws an exception on incoming data and the upstream
* throws an exception during cleanup when cancel is called.
*/
@Test
public void testFailedRecoveryHandling() {
MockResponseObserver<String> outerObserver = new MockResponseObserver<>(true);
final RuntimeException fakeReframerError = new RuntimeException("fake reframer error");
Reframer<String, String> brokenReframer =
new Reframer<String, String>() {
@Override
public void push(String ignored) {
throw fakeReframerError;
}
@Override
public boolean hasFullFrame() {
return false;
}
@Override
public boolean hasPartialFrame() {
return false;
}
@Override
public String pop() {
throw new IllegalStateException("should not be called");
}
};
ReframingResponseObserver<String, String> middleware =
new ReframingResponseObserver<>(outerObserver, brokenReframer);
// Configure the mock inner controller to fail cancellation.
StreamController mockInnerController = Mockito.mock(StreamController.class);
RuntimeException fakeCancelError = new RuntimeException("fake cancel error");
Mockito.doThrow(fakeCancelError).when(mockInnerController).cancel();
// Jumpstart a call & feed it data
middleware.onStartImpl(mockInnerController);
middleware.onResponseImpl("1");
// Make sure that the outer observer was notified with the reframer, which contains a suppressed
// cancellation error.
Throwable finalError = outerObserver.getFinalError();
Truth.assertThat(finalError).isSameInstanceAs(fakeReframerError);
Truth.assertThat(ImmutableList.of(finalError.getSuppressed())).hasSize(1);
Truth.assertThat(finalError.getSuppressed()[0]).isInstanceOf(IllegalStateException.class);
Truth.assertThat(finalError.getSuppressed()[0])
.hasMessageThat()
.isEqualTo("Failed to cancel upstream while recovering from an unexpected error");
Truth.assertThat(finalError.getSuppressed()[0].getCause()).isSameInstanceAs(fakeCancelError);
}
/**
* A simple implementation of a {@link Reframer}. The input string is split by dash, and the
* output is concatenated by dashes. The test can verify M:N behavior by adjusting the
* partsPerResponse parameter and the number of dashes in the input.
*/
static class DasherizingReframer implements Reframer<String, String> {
final Queue<String> buffer = Queues.newArrayDeque();
final int partsPerResponse;
DasherizingReframer(int partsPerResponse) {
this.partsPerResponse = partsPerResponse;
}
@Override
public void push(String response) {
buffer.addAll(Arrays.asList(response.split("-")));
}
@Override
public boolean hasFullFrame() {
return buffer.size() >= partsPerResponse;
}
@Override
public boolean hasPartialFrame() {
return !buffer.isEmpty();
}
@Override
public String pop() {
String[] parts = new String[partsPerResponse];
for (int i = 0; i < partsPerResponse; i++) {
parts[i] = buffer.poll();
}
return Joiner.on("-").join(parts);
}
}
static class GatedMockResponseObserver extends MockResponseObserver<String> {
final Breakpoint completeBreakpoint = new Breakpoint();
final Breakpoint errorBreakpoint = new Breakpoint();
public GatedMockResponseObserver(boolean autoFlowControl) {
super(autoFlowControl);
}
@Override
protected void onErrorImpl(Throwable t) {
super.onErrorImpl(t);
errorBreakpoint.arrive();
}
@Override
protected void onCompleteImpl() {
super.onCompleteImpl();
completeBreakpoint.arrive();
}
}
static class Breakpoint {
private volatile CountDownLatch arriveLatch = new CountDownLatch(0);
private volatile CountDownLatch leaveLatch = new CountDownLatch(0);
public void enable() {
arriveLatch = new CountDownLatch(1);
leaveLatch = new CountDownLatch(1);
}
public void arrive() {
arriveLatch.countDown();
try {
leaveLatch.await(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
void awaitArrival() {
try {
arriveLatch.await(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
public void release() {
leaveLatch.countDown();
}
}
}