forked from grpc/grpc-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientInterceptorsTest.java
481 lines (440 loc) · 17.4 KB
/
ClientInterceptorsTest.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
/*
* Copyright 2014 The gRPC 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.grpc;
import static com.google.common.truth.Truth.assertThat;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.AdditionalAnswers.delegatesTo;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.ArgumentMatchers.same;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import io.grpc.ClientInterceptors.CheckedForwardingClientCall;
import io.grpc.ForwardingClientCall.SimpleForwardingClientCall;
import io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener;
import io.grpc.testing.TestMethodDescriptors;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
/** Unit tests for {@link ClientInterceptors}. */
@RunWith(JUnit4.class)
public class ClientInterceptorsTest {
@Rule
public final MockitoRule mocks = MockitoJUnit.rule();
@Mock
private Channel channel;
private BaseClientCall call = new BaseClientCall();
private final MethodDescriptor<Void, Void> method = TestMethodDescriptors.voidMethod();
/**
* Sets up mocks.
*/
@Before public void setUp() {
when(channel.newCall(
ArgumentMatchers.<MethodDescriptor<String, Integer>>any(), any(CallOptions.class)))
.thenReturn(call);
}
@Test(expected = NullPointerException.class)
public void npeForNullChannel() {
ClientInterceptors.intercept(null, Arrays.<ClientInterceptor>asList());
}
@Test(expected = NullPointerException.class)
public void npeForNullInterceptorList() {
ClientInterceptors.intercept(channel, (List<ClientInterceptor>) null);
}
@Test(expected = NullPointerException.class)
public void npeForNullInterceptor() {
ClientInterceptors.intercept(channel, (ClientInterceptor) null);
}
@Test
public void noop() {
assertSame(channel, ClientInterceptors.intercept(channel, Arrays.<ClientInterceptor>asList()));
}
@Test
public void channelAndInterceptorCalled() {
ClientInterceptor interceptor =
mock(ClientInterceptor.class, delegatesTo(new NoopInterceptor()));
Channel intercepted = ClientInterceptors.intercept(channel, interceptor);
CallOptions callOptions = CallOptions.DEFAULT;
// First call
assertSame(call, intercepted.newCall(method, callOptions));
verify(channel).newCall(same(method), same(callOptions));
verify(interceptor)
.interceptCall(same(method), same(callOptions), ArgumentMatchers.<Channel>any());
verifyNoMoreInteractions(channel, interceptor);
// Second call
assertSame(call, intercepted.newCall(method, callOptions));
verify(channel, times(2)).newCall(same(method), same(callOptions));
verify(interceptor, times(2))
.interceptCall(same(method), same(callOptions), ArgumentMatchers.<Channel>any());
verifyNoMoreInteractions(channel, interceptor);
}
@Test
public void callNextTwice() {
ClientInterceptor interceptor = new ClientInterceptor() {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions,
Channel next) {
// Calling next twice is permitted, although should only rarely be useful.
assertSame(call, next.newCall(method, callOptions));
return next.newCall(method, callOptions);
}
};
Channel intercepted = ClientInterceptors.intercept(channel, interceptor);
assertSame(call, intercepted.newCall(method, CallOptions.DEFAULT));
verify(channel, times(2)).newCall(same(method), same(CallOptions.DEFAULT));
verifyNoMoreInteractions(channel);
}
@Test
public void ordered() {
final List<String> order = new ArrayList<>();
channel = new Channel() {
@SuppressWarnings("unchecked")
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> newCall(
MethodDescriptor<ReqT, RespT> method, CallOptions callOptions) {
order.add("channel");
return (ClientCall<ReqT, RespT>) call;
}
@Override
public String authority() {
return null;
}
};
ClientInterceptor interceptor1 = new ClientInterceptor() {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions,
Channel next) {
order.add("i1");
return next.newCall(method, callOptions);
}
};
ClientInterceptor interceptor2 = new ClientInterceptor() {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions,
Channel next) {
order.add("i2");
return next.newCall(method, callOptions);
}
};
Channel intercepted = ClientInterceptors.intercept(channel, interceptor1, interceptor2);
assertSame(call, intercepted.newCall(method, CallOptions.DEFAULT));
assertEquals(Arrays.asList("i2", "i1", "channel"), order);
}
@Test
public void orderedForward() {
final List<String> order = new ArrayList<>();
channel = new Channel() {
@SuppressWarnings("unchecked")
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> newCall(
MethodDescriptor<ReqT, RespT> method, CallOptions callOptions) {
order.add("channel");
return (ClientCall<ReqT, RespT>) call;
}
@Override
public String authority() {
return null;
}
};
ClientInterceptor interceptor1 = new ClientInterceptor() {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions,
Channel next) {
order.add("i1");
return next.newCall(method, callOptions);
}
};
ClientInterceptor interceptor2 = new ClientInterceptor() {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions,
Channel next) {
order.add("i2");
return next.newCall(method, callOptions);
}
};
Channel intercepted = ClientInterceptors.interceptForward(channel, interceptor1, interceptor2);
assertSame(call, intercepted.newCall(method, CallOptions.DEFAULT));
assertEquals(Arrays.asList("i1", "i2", "channel"), order);
}
@Test
public void callOptions() {
final CallOptions initialCallOptions = CallOptions.DEFAULT.withDeadlineAfter(100, NANOSECONDS);
final CallOptions newCallOptions = initialCallOptions.withDeadlineAfter(300, NANOSECONDS);
assertNotSame(initialCallOptions, newCallOptions);
ClientInterceptor interceptor =
mock(ClientInterceptor.class, delegatesTo(new ClientInterceptor() {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions,
Channel next) {
return next.newCall(method, newCallOptions);
}
}));
Channel intercepted = ClientInterceptors.intercept(channel, interceptor);
intercepted.newCall(method, initialCallOptions);
verify(interceptor)
.interceptCall(same(method), same(initialCallOptions), ArgumentMatchers.<Channel>any());
verify(channel).newCall(same(method), same(newCallOptions));
}
@Test
public void addOutboundHeaders() {
final Metadata.Key<String> credKey = Metadata.Key.of("Cred", Metadata.ASCII_STRING_MARSHALLER);
ClientInterceptor interceptor = new ClientInterceptor() {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions,
Channel next) {
ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);
return new SimpleForwardingClientCall<ReqT, RespT>(call) {
@Override
public void start(ClientCall.Listener<RespT> responseListener, Metadata headers) {
headers.put(credKey, "abcd");
super.start(responseListener, headers);
}
};
}
};
Channel intercepted = ClientInterceptors.intercept(channel, interceptor);
@SuppressWarnings("unchecked")
ClientCall.Listener<Void> listener = mock(ClientCall.Listener.class);
ClientCall<Void, Void> interceptedCall = intercepted.newCall(method, CallOptions.DEFAULT);
// start() on the intercepted call will eventually reach the call created by the real channel
interceptedCall.start(listener, new Metadata());
// The headers passed to the real channel call will contain the information inserted by the
// interceptor.
assertSame(listener, call.listener);
assertEquals("abcd", call.headers.get(credKey));
}
@Test
public void examineInboundHeaders() {
final List<Metadata> examinedHeaders = new ArrayList<>();
ClientInterceptor interceptor = new ClientInterceptor() {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions,
Channel next) {
ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);
return new SimpleForwardingClientCall<ReqT, RespT>(call) {
@Override
public void start(ClientCall.Listener<RespT> responseListener, Metadata headers) {
super.start(new SimpleForwardingClientCallListener<RespT>(responseListener) {
@Override
public void onHeaders(Metadata headers) {
examinedHeaders.add(headers);
super.onHeaders(headers);
}
}, headers);
}
};
}
};
Channel intercepted = ClientInterceptors.intercept(channel, interceptor);
@SuppressWarnings("unchecked")
ClientCall.Listener<Void> listener = mock(ClientCall.Listener.class);
ClientCall<Void, Void> interceptedCall = intercepted.newCall(method, CallOptions.DEFAULT);
interceptedCall.start(listener, new Metadata());
// Capture the underlying call listener that will receive headers from the transport.
Metadata inboundHeaders = new Metadata();
// Simulate that a headers arrives on the underlying call listener.
call.listener.onHeaders(inboundHeaders);
assertThat(examinedHeaders).contains(inboundHeaders);
}
@Test
public void normalCall() {
ClientInterceptor interceptor = new ClientInterceptor() {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions,
Channel next) {
ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);
return new SimpleForwardingClientCall<ReqT, RespT>(call) { };
}
};
Channel intercepted = ClientInterceptors.intercept(channel, interceptor);
ClientCall<Void, Void> interceptedCall = intercepted.newCall(method, CallOptions.DEFAULT);
assertNotSame(call, interceptedCall);
@SuppressWarnings("unchecked")
ClientCall.Listener<Void> listener = mock(ClientCall.Listener.class);
Metadata headers = new Metadata();
interceptedCall.start(listener, headers);
assertSame(listener, call.listener);
assertSame(headers, call.headers);
interceptedCall.sendMessage(null /*request*/);
assertThat(call.messages).containsExactly((String) null);
interceptedCall.halfClose();
assertTrue(call.halfClosed);
interceptedCall.request(1);
assertThat(call.requests).containsExactly(1);
}
@Test
public void exceptionInStart() {
final Exception error = new Exception("emulated error");
ClientInterceptor interceptor = new ClientInterceptor() {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions,
Channel next) {
ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);
return new CheckedForwardingClientCall<ReqT, RespT>(call) {
@Override
protected void checkedStart(ClientCall.Listener<RespT> responseListener, Metadata headers)
throws Exception {
throw error;
// delegate().start will not be called
}
};
}
};
Channel intercepted = ClientInterceptors.intercept(channel, interceptor);
@SuppressWarnings("unchecked")
ClientCall.Listener<Void> listener = mock(ClientCall.Listener.class);
ClientCall<Void, Void> interceptedCall = intercepted.newCall(method, CallOptions.DEFAULT);
assertNotSame(call, interceptedCall);
interceptedCall.start(listener, new Metadata());
interceptedCall.sendMessage(null /*request*/);
interceptedCall.halfClose();
interceptedCall.request(1);
call.done = true;
ArgumentCaptor<Status> captor = ArgumentCaptor.forClass(Status.class);
verify(listener).onClose(captor.capture(), any(Metadata.class));
assertSame(error, captor.getValue().getCause());
// Make sure nothing bad happens after the exception.
ClientCall<?, ?> noop = ((CheckedForwardingClientCall<?, ?>)interceptedCall).delegate();
// Should not throw, even on bad input
noop.cancel("Cancel for test", null);
noop.start(null, null);
noop.request(-1);
noop.halfClose();
noop.sendMessage(null);
assertFalse(noop.isReady());
}
@Test
public void authorityIsDelegated() {
ClientInterceptor interceptor = new ClientInterceptor() {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions,
Channel next) {
return next.newCall(method, callOptions);
}
};
when(channel.authority()).thenReturn("auth");
Channel intercepted = ClientInterceptors.intercept(channel, interceptor);
assertEquals("auth", intercepted.authority());
}
@Test
public void customOptionAccessible() {
CallOptions.Key<String> customOption = CallOptions.Key.create("custom");
CallOptions callOptions = CallOptions.DEFAULT.withOption(customOption, "value");
ArgumentCaptor<CallOptions> passedOptions = ArgumentCaptor.forClass(CallOptions.class);
ClientInterceptor interceptor =
mock(ClientInterceptor.class, delegatesTo(new NoopInterceptor()));
Channel intercepted = ClientInterceptors.intercept(channel, interceptor);
assertSame(call, intercepted.newCall(method, callOptions));
verify(channel).newCall(same(method), same(callOptions));
verify(interceptor).interceptCall(same(method), passedOptions.capture(), isA(Channel.class));
assertSame("value", passedOptions.getValue().getOption(customOption));
}
private static class NoopInterceptor implements ClientInterceptor {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions, Channel next) {
return next.newCall(method, callOptions);
}
}
private static class BaseClientCall extends ClientCall<String, Integer> {
private boolean started;
private boolean done;
private ClientCall.Listener<Integer> listener;
private Metadata headers;
private List<Integer> requests = new ArrayList<>();
private List<String> messages = new ArrayList<>();
private boolean halfClosed;
@Override
public void start(ClientCall.Listener<Integer> listener, Metadata headers) {
checkNotDone();
started = true;
this.listener = listener;
this.headers = headers;
}
@Override
public void request(int numMessages) {
checkNotDone();
checkStarted();
requests.add(numMessages);
}
@Override
public void cancel(String message, Throwable cause) {
checkNotDone();
}
@Override
public void halfClose() {
checkNotDone();
checkStarted();
this.halfClosed = true;
}
@Override
public void sendMessage(String message) {
checkNotDone();
checkStarted();
messages.add(message);
}
private void checkNotDone() {
if (done) {
throw new IllegalStateException("no more methods should be called");
}
}
private void checkStarted() {
if (!started) {
throw new IllegalStateException("should have called start");
}
}
}
}