/
ErrorHandlingTest.java
643 lines (588 loc) · 28.4 KB
/
ErrorHandlingTest.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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/*
* Copyright © 2019 Apple Inc. and the ServiceTalk project authors
*
* 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
*
* http://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 io.servicetalk.grpc.netty;
import io.servicetalk.concurrent.BlockingIterator;
import io.servicetalk.concurrent.api.Publisher;
import io.servicetalk.concurrent.api.Single;
import io.servicetalk.concurrent.api.TestPublisherSubscriber;
import io.servicetalk.concurrent.internal.ServiceTalkTestTimeout;
import io.servicetalk.grpc.api.GrpcClientBuilder;
import io.servicetalk.grpc.api.GrpcPayloadWriter;
import io.servicetalk.grpc.api.GrpcServiceContext;
import io.servicetalk.grpc.api.GrpcStatusCode;
import io.servicetalk.grpc.api.GrpcStatusException;
import io.servicetalk.grpc.netty.TesterProto.TestRequest;
import io.servicetalk.grpc.netty.TesterProto.TestResponse;
import io.servicetalk.grpc.netty.TesterProto.Tester.BlockingTesterClient;
import io.servicetalk.grpc.netty.TesterProto.Tester.BlockingTesterService;
import io.servicetalk.grpc.netty.TesterProto.Tester.ClientFactory;
import io.servicetalk.grpc.netty.TesterProto.Tester.ServiceFactory;
import io.servicetalk.grpc.netty.TesterProto.Tester.TesterClient;
import io.servicetalk.grpc.netty.TesterProto.Tester.TesterService;
import io.servicetalk.grpc.netty.TesterProto.Tester.TesterServiceFilter;
import io.servicetalk.http.api.FilterableStreamingHttpClient;
import io.servicetalk.http.api.HttpExecutionStrategy;
import io.servicetalk.http.api.HttpServiceContext;
import io.servicetalk.http.api.StreamingHttpClientFilter;
import io.servicetalk.http.api.StreamingHttpClientFilterFactory;
import io.servicetalk.http.api.StreamingHttpRequest;
import io.servicetalk.http.api.StreamingHttpRequester;
import io.servicetalk.http.api.StreamingHttpResponse;
import io.servicetalk.http.api.StreamingHttpResponseFactory;
import io.servicetalk.http.api.StreamingHttpService;
import io.servicetalk.http.api.StreamingHttpServiceFilter;
import io.servicetalk.http.api.StreamingHttpServiceFilterFactory;
import io.servicetalk.transport.api.HostAndPort;
import io.servicetalk.transport.api.ServerContext;
import com.google.rpc.Status;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.net.InetSocketAddress;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import static io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable;
import static io.servicetalk.concurrent.api.Completable.completed;
import static io.servicetalk.concurrent.api.SourceAdapters.toSource;
import static io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION;
import static io.servicetalk.transport.netty.internal.AddressUtils.localAddress;
import static io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort;
import static io.servicetalk.utils.internal.PlatformDependent.throwException;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(Parameterized.class)
public class ErrorHandlingTest {
private static final StreamingHttpClientFilterFactory IDENTITY_CLIENT_FILTER =
c -> new StreamingHttpClientFilter(c) { };
private static final StreamingHttpServiceFilterFactory IDENTITY_FILTER =
s -> new StreamingHttpServiceFilter(s) { };
private final TestMode testMode;
private final TestResponse cannedResponse;
private final BlockingTesterClient blockingClient;
private enum TestMode {
HttpClientFilterThrows,
HttpClientFilterThrowsGrpcException,
HttpClientFilterEmitsError,
HttpClientFilterEmitsGrpcException,
HttpFilterThrows,
HttpFilterThrowsGrpcException,
HttpFilterEmitsError,
HttpFilterEmitsGrpcException,
FilterThrows,
FilterThrowsGrpcException,
FilterEmitsError,
FilterEmitsGrpcException,
ServiceThrows,
ServiceThrowsGrpcException,
ServiceEmitsError,
ServiceEmitsGrpcException,
ServiceEmitsDataThenError,
ServiceEmitsDataThenGrpcException,
BlockingServiceThrows,
BlockingServiceThrowsGrpcException,
BlockingServiceWritesThenThrows,
BlockingServiceWritesThenThrowsGrpcException
}
@Rule
public final Timeout timeout = new ServiceTalkTestTimeout();
private final GrpcStatusException cannedException =
GrpcStatusException.of(Status.newBuilder().setCode(GrpcStatusCode.ABORTED.value())
.setMessage("Deliberate abort").build());
private final ServerContext serverContext;
private final TesterClient client;
public ErrorHandlingTest(TestMode testMode) throws Exception {
this.testMode = testMode;
cannedResponse = TestResponse.newBuilder().setMessage("foo").build();
ServiceFactory serviceFactory;
TesterService filter = mockTesterService();
StreamingHttpServiceFilterFactory serviceFilterFactory = IDENTITY_FILTER;
StreamingHttpClientFilterFactory clientFilterFactory = IDENTITY_CLIENT_FILTER;
switch (testMode) {
case HttpClientFilterThrows:
clientFilterFactory = new ErrorProducingClientFilter(true, DELIBERATE_EXCEPTION);
serviceFactory = setupForSuccess();
break;
case HttpClientFilterThrowsGrpcException:
clientFilterFactory = new ErrorProducingClientFilter(true, cannedException);
serviceFactory = setupForSuccess();
break;
case HttpClientFilterEmitsError:
clientFilterFactory = new ErrorProducingClientFilter(false, DELIBERATE_EXCEPTION);
serviceFactory = setupForSuccess();
break;
case HttpClientFilterEmitsGrpcException:
clientFilterFactory = new ErrorProducingClientFilter(false, cannedException);
serviceFactory = setupForSuccess();
break;
case HttpFilterThrows:
serviceFilterFactory = new ErrorProducingSvcFilter(true, DELIBERATE_EXCEPTION);
serviceFactory = setupForSuccess();
break;
case HttpFilterThrowsGrpcException:
serviceFilterFactory = new ErrorProducingSvcFilter(true, cannedException);
serviceFactory = setupForSuccess();
break;
case HttpFilterEmitsError:
serviceFilterFactory = new ErrorProducingSvcFilter(false, DELIBERATE_EXCEPTION);
serviceFactory = setupForSuccess();
break;
case HttpFilterEmitsGrpcException:
serviceFilterFactory = new ErrorProducingSvcFilter(false, cannedException);
serviceFactory = setupForSuccess();
break;
case FilterThrows:
setupForServiceThrows(filter, DELIBERATE_EXCEPTION);
serviceFactory = configureFilter(filter);
break;
case FilterThrowsGrpcException:
setupForServiceThrows(filter, cannedException);
serviceFactory = configureFilter(filter);
break;
case FilterEmitsError:
setupForServiceEmitsError(filter, DELIBERATE_EXCEPTION);
serviceFactory = configureFilter(filter);
break;
case FilterEmitsGrpcException:
setupForServiceEmitsError(filter, cannedException);
serviceFactory = configureFilter(filter);
break;
case ServiceThrows:
serviceFactory = setupForServiceThrows(DELIBERATE_EXCEPTION);
break;
case ServiceThrowsGrpcException:
serviceFactory = setupForServiceThrows(cannedException);
break;
case ServiceEmitsError:
serviceFactory = setupForServiceEmitsError(DELIBERATE_EXCEPTION);
break;
case ServiceEmitsGrpcException:
serviceFactory = setupForServiceEmitsError(cannedException);
break;
case ServiceEmitsDataThenError:
serviceFactory = setupForServiceEmitsDataThenError(DELIBERATE_EXCEPTION);
break;
case ServiceEmitsDataThenGrpcException:
serviceFactory = setupForServiceEmitsDataThenError(cannedException);
break;
case BlockingServiceThrows:
serviceFactory = setupForBlockingServiceThrows(DELIBERATE_EXCEPTION);
break;
case BlockingServiceThrowsGrpcException:
serviceFactory = setupForBlockingServiceThrows(cannedException);
break;
case BlockingServiceWritesThenThrows:
serviceFactory = setupForBlockingServiceWritesThenThrows(DELIBERATE_EXCEPTION);
break;
case BlockingServiceWritesThenThrowsGrpcException:
serviceFactory = setupForBlockingServiceWritesThenThrows(cannedException);
break;
default:
throw new IllegalArgumentException("Unknown mode: " + testMode);
}
serverContext = GrpcServers.forAddress(localAddress(0)).appendHttpServiceFilter(serviceFilterFactory)
.listenAndAwait(serviceFactory);
GrpcClientBuilder<HostAndPort, InetSocketAddress> clientBuilder =
GrpcClients.forAddress(serverHostAndPort(serverContext)).appendHttpClientFilter(clientFilterFactory);
client = clientBuilder.build(new ClientFactory());
blockingClient = clientBuilder.buildBlocking(new ClientFactory());
}
private ServiceFactory configureFilter(final TesterService filter) {
final ServiceFactory serviceFactory;
final TesterService service = mockTesterService();
serviceFactory = new ServiceFactory(service);
serviceFactory.appendServiceFilter(original ->
new ErrorSimulatingTesterServiceFilter(original, filter));
return serviceFactory;
}
private ServiceFactory setupForSuccess() {
return new ServiceFactory(new TesterService() {
@Override
public Publisher<TestResponse> testBiDiStream(final GrpcServiceContext ctx,
final Publisher<TestRequest> request) {
return request.map(testRequest -> TestResponse.newBuilder().setMessage(testRequest.getName()).build());
}
@Override
public Single<TestResponse> testRequestStream(final GrpcServiceContext ctx,
final Publisher<TestRequest> request) {
return request.collect(StringBuilder::new, (names, testRequest) -> names.append(testRequest.getName()))
.map(names -> TestResponse.newBuilder().setMessage(names.toString()).build());
}
@Override
public Publisher<TestResponse> testResponseStream(final GrpcServiceContext ctx, final TestRequest request) {
return Publisher.from(TestResponse.newBuilder().setMessage(request.getName()).build());
}
@Override
public Single<TestResponse> test(final GrpcServiceContext ctx, final TestRequest request) {
return Single.succeeded(TestResponse.newBuilder().setMessage(request.getName()).build());
}
});
}
private ServiceFactory setupForServiceThrows(final Throwable toThrow) {
final TesterService service = mockTesterService();
setupForServiceThrows(service, toThrow);
return new ServiceFactory(service);
}
private void setupForServiceThrows(final TesterService service, final Throwable toThrow) {
when(service.test(any(), any())).thenThrow(toThrow);
when(service.testBiDiStream(any(), any())).thenThrow(toThrow);
when(service.testRequestStream(any(), any())).thenThrow(toThrow);
when(service.testResponseStream(any(), any())).thenThrow(toThrow);
}
private ServiceFactory setupForServiceEmitsError(final Throwable toThrow) {
final TesterService service = mockTesterService();
setupForServiceEmitsError(service, toThrow);
return new ServiceFactory(service);
}
private void setupForServiceEmitsError(final TesterService service, final Throwable toThrow) {
when(service.test(any(), any())).thenReturn(Single.failed(toThrow));
when(service.testBiDiStream(any(), any())).thenReturn(Publisher.failed(toThrow));
when(service.testRequestStream(any(), any())).thenReturn(Single.failed(toThrow));
when(service.testResponseStream(any(), any())).thenReturn(Publisher.failed(toThrow));
}
private ServiceFactory setupForServiceEmitsDataThenError(final Throwable toThrow) {
final TesterService service = mockTesterService();
setupForServiceEmitsDataThenError(service, toThrow);
return new ServiceFactory(service);
}
private void setupForServiceEmitsDataThenError(final TesterService service, final Throwable toThrow) {
when(service.test(any(), any())).thenReturn(Single.failed(toThrow));
when(service.testBiDiStream(any(), any()))
.thenReturn(Publisher.from(cannedResponse).concat(Publisher.failed(toThrow)));
when(service.testRequestStream(any(), any())).thenReturn(Single.failed(toThrow));
when(service.testResponseStream(any(), any()))
.thenReturn(Publisher.from(cannedResponse)
.concat(Publisher.failed(toThrow)));
}
private ServiceFactory setupForBlockingServiceThrows(final Throwable toThrow) throws Exception {
final BlockingTesterService blockingService = mock(BlockingTesterService.class);
setupForBlockingServiceThrows(blockingService, toThrow);
return new ServiceFactory(blockingService);
}
private void setupForBlockingServiceThrows(final BlockingTesterService blockingService, final Throwable toThrow)
throws Exception {
when(blockingService.test(any(), any())).thenThrow(toThrow);
doThrow(toThrow).when(blockingService).testBiDiStream(any(), any(), any());
when(blockingService.testRequestStream(any(), any())).thenThrow(toThrow);
doThrow(toThrow).when(blockingService).testResponseStream(any(), any(), any());
}
private ServiceFactory setupForBlockingServiceWritesThenThrows(final Throwable toThrow) throws Exception {
final BlockingTesterService blockingService = mock(BlockingTesterService.class);
setupForBlockingServiceWritesThenThrows(blockingService, toThrow);
return new ServiceFactory(blockingService);
}
private void setupForBlockingServiceWritesThenThrows(final BlockingTesterService blockingService,
final Throwable toThrow) throws Exception {
when(blockingService.test(any(), any())).thenThrow(toThrow);
doAnswer(invocation -> {
GrpcPayloadWriter<TestResponse> responseWriter = invocation.getArgument(2);
responseWriter.write(cannedResponse);
throw toThrow;
}).when(blockingService).testBiDiStream(any(), any(), any());
when(blockingService.testRequestStream(any(), any())).thenThrow(toThrow);
doAnswer(invocation -> {
GrpcPayloadWriter<TestResponse> responseWriter = invocation.getArgument(2);
responseWriter.write(cannedResponse);
throw toThrow;
}).when(blockingService).testResponseStream(any(), any(), any());
}
@Parameterized.Parameters(name = "{index}: mode = {0}")
public static Collection<TestMode> data() {
return asList(TestMode.values());
}
@After
public void tearDown() throws Exception {
try {
blockingClient.close();
} finally {
newCompositeCloseable().appendAll(client, serverContext).close();
}
}
@Test
public void scalar() throws Exception {
verifyException(client.test(TestRequest.newBuilder().build()).toFuture());
}
@Test
public void bidiStreaming() throws Exception {
verifyStreamingResponse(client.testBiDiStream(Publisher.from(TestRequest.newBuilder().build())));
}
@Test
public void requestStreaming() throws Exception {
verifyException(client.testRequestStream(Publisher.from(TestRequest.newBuilder().build()))
.toFuture());
}
@Test
public void responseStreaming() throws Exception {
verifyStreamingResponse(client.testResponseStream(TestRequest.newBuilder().build()));
}
@Test
public void scalarFromBlockingClient() throws Exception {
try {
blockingClient.test(TestRequest.newBuilder().build());
fail("Expected blocking scalar response to fail.");
} catch (GrpcStatusException e) {
assertThat("Unexpected grpc status.", e.status().code(), equalTo(expectedStatus()));
}
}
@Test
public void bidiStreamingFromBlockingClient() throws Exception {
try {
BlockingIterator<TestResponse> resp =
blockingClient.testBiDiStream(singletonList(TestRequest.newBuilder().setName("foo").build()))
.iterator();
verifyStreamingResponse(resp);
} catch (GrpcStatusException e) {
assertThat("Unexpected grpc status.", e.status().code(), equalTo(expectedStatus()));
}
}
@Test
public void requestStreamingFromBlockingClient() throws Exception {
try {
blockingClient.testRequestStream(singletonList(TestRequest.newBuilder().setName("foo").build()));
fail("Expected blocking request streaming to fail.");
} catch (GrpcStatusException e) {
assertThat("Unexpected grpc status.", e.status().code(), equalTo(expectedStatus()));
}
}
@Test
public void responseStreamingFromBlockingClient() throws Exception {
try {
BlockingIterator<TestResponse> resp =
blockingClient.testResponseStream(TestRequest.newBuilder().build()).iterator();
verifyStreamingResponse(resp);
} catch (GrpcStatusException e) {
assertThat("Unexpected grpc status.", e.status().code(), equalTo(expectedStatus()));
}
}
private TesterService mockTesterService() {
TesterService filter = mock(TesterService.class);
when(filter.closeAsync()).thenReturn(completed());
when(filter.closeAsyncGracefully()).thenReturn(completed());
return filter;
}
private void verifyStreamingResponse(final BlockingIterator<TestResponse> resp) {
switch (testMode) {
case ServiceEmitsDataThenError:
case ServiceEmitsDataThenGrpcException:
case BlockingServiceWritesThenThrows:
case BlockingServiceWritesThenThrowsGrpcException:
assertThat("Unexpected response.", resp.next(), equalTo(cannedResponse));
resp.next(); // should throw
fail("Expected streaming response to fail");
break;
case HttpClientFilterThrows:
case HttpClientFilterThrowsGrpcException:
case HttpClientFilterEmitsError:
case HttpClientFilterEmitsGrpcException:
case HttpFilterThrows:
case HttpFilterThrowsGrpcException:
case HttpFilterEmitsError:
case HttpFilterEmitsGrpcException:
case FilterThrows:
case FilterThrowsGrpcException:
case FilterEmitsError:
case FilterEmitsGrpcException:
case ServiceThrows:
case ServiceThrowsGrpcException:
case ServiceEmitsError:
case ServiceEmitsGrpcException:
case BlockingServiceThrows:
case BlockingServiceThrowsGrpcException:
resp.next(); // should throw
fail("Expected streaming response to fail");
break;
default:
throw new IllegalArgumentException("Unknown mode: " + testMode);
}
}
private void verifyStreamingResponse(final Publisher<TestResponse> resp) throws Exception {
TestPublisherSubscriber<TestResponse> subscriber = new TestPublisherSubscriber<>();
CountDownLatch terminationLatch = new CountDownLatch(1);
toSource(resp.afterFinally(terminationLatch::countDown)).subscribe(subscriber);
subscriber.request(Long.MAX_VALUE);
terminationLatch.await();
Throwable cause;
switch (testMode) {
case ServiceEmitsDataThenError:
case ServiceEmitsDataThenGrpcException:
case BlockingServiceWritesThenThrows:
case BlockingServiceWritesThenThrowsGrpcException:
List<TestResponse> items = subscriber.takeItems();
assertThat("Unexpected response.", items, hasSize(1));
assertThat("Unexpected response.", items, contains(cannedResponse));
cause = subscriber.takeError();
assertThat("Unexpected termination.", cause, is(notNullValue()));
verifyException(cause);
break;
case HttpClientFilterThrows:
case HttpClientFilterThrowsGrpcException:
case HttpClientFilterEmitsError:
case HttpClientFilterEmitsGrpcException:
case HttpFilterThrows:
case HttpFilterThrowsGrpcException:
case HttpFilterEmitsError:
case HttpFilterEmitsGrpcException:
case FilterThrows:
case FilterThrowsGrpcException:
case FilterEmitsError:
case FilterEmitsGrpcException:
case ServiceThrows:
case ServiceThrowsGrpcException:
case ServiceEmitsError:
case ServiceEmitsGrpcException:
case BlockingServiceThrows:
case BlockingServiceThrowsGrpcException:
cause = subscriber.takeError();
assertThat("Unexpected termination.", cause, is(notNullValue()));
verifyException(cause);
break;
default:
throw new IllegalArgumentException("Unknown mode: " + testMode);
}
}
private void verifyException(final Future<?> result)
throws Exception {
try {
result.get();
} catch (ExecutionException e) {
Throwable cause = e.getCause();
verifyException(cause);
}
}
private void verifyException(final Throwable cause) {
assertThat("Unexpected error.", cause, instanceOf(GrpcStatusException.class));
GrpcStatusException gse = (GrpcStatusException) cause;
assertThat("Unexpected grpc status.", gse.status().code(), equalTo(expectedStatus()));
}
private GrpcStatusCode expectedStatus() {
switch (testMode) {
case HttpClientFilterThrows:
case HttpClientFilterEmitsError:
case HttpFilterThrows:
case HttpFilterEmitsError:
case FilterThrows:
case FilterEmitsError:
case ServiceThrows:
case BlockingServiceThrows:
case ServiceEmitsError:
case ServiceEmitsDataThenError:
case BlockingServiceWritesThenThrows:
return GrpcStatusCode.UNKNOWN;
case HttpClientFilterThrowsGrpcException:
case HttpClientFilterEmitsGrpcException:
case HttpFilterThrowsGrpcException:
case HttpFilterEmitsGrpcException:
case FilterEmitsGrpcException:
case FilterThrowsGrpcException:
case ServiceThrowsGrpcException:
case ServiceEmitsDataThenGrpcException:
case ServiceEmitsGrpcException:
case BlockingServiceThrowsGrpcException:
case BlockingServiceWritesThenThrowsGrpcException:
return cannedException.status().code();
default:
throw new IllegalArgumentException("Unknown mode: " + testMode);
}
}
private static final class ErrorSimulatingTesterServiceFilter extends TesterServiceFilter {
private final TesterService simulator;
ErrorSimulatingTesterServiceFilter(final TesterService original, final TesterService simulator) {
super(original);
this.simulator = simulator;
}
@Override
public Single<TestResponse> test(final GrpcServiceContext ctx, final TestRequest request) {
return simulator.test(ctx, request);
}
@Override
public Publisher<TestResponse> testBiDiStream(final GrpcServiceContext ctx,
final Publisher<TestRequest> request) {
return simulator.testBiDiStream(ctx, request);
}
@Override
public Publisher<TestResponse> testResponseStream(final GrpcServiceContext ctx,
final TestRequest request) {
return simulator.testResponseStream(ctx, request);
}
@Override
public Single<TestResponse> testRequestStream(final GrpcServiceContext ctx,
final Publisher<TestRequest> request) {
return simulator.testRequestStream(ctx, request);
}
}
private static final class ErrorProducingClientFilter implements StreamingHttpClientFilterFactory {
private final boolean throwEx;
private final Throwable cause;
ErrorProducingClientFilter(final boolean throwEx, final Throwable cause) {
this.throwEx = throwEx;
this.cause = cause;
}
@Override
public StreamingHttpClientFilter create(final FilterableStreamingHttpClient client) {
return new StreamingHttpClientFilter(client) {
@Override
protected Single<StreamingHttpResponse> request(final StreamingHttpRequester delegate,
final HttpExecutionStrategy strategy,
final StreamingHttpRequest request) {
if (throwEx) {
return throwException(cause);
}
return Single.failed(cause);
}
};
}
}
private static final class ErrorProducingSvcFilter implements StreamingHttpServiceFilterFactory {
private final boolean throwEx;
private final Throwable cause;
ErrorProducingSvcFilter(final boolean throwEx, final Throwable cause) {
this.throwEx = throwEx;
this.cause = cause;
}
@Override
public StreamingHttpServiceFilter create(final StreamingHttpService service) {
return new StreamingHttpServiceFilter(service) {
@Override
public Single<StreamingHttpResponse> handle(final HttpServiceContext ctx,
final StreamingHttpRequest request,
final StreamingHttpResponseFactory responseFactory) {
if (throwEx) {
return throwException(cause);
}
return Single.failed(cause);
}
};
}
}
}