/
NettyClientHandlerTest.java
934 lines (811 loc) · 34.7 KB
/
NettyClientHandlerTest.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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
/*
* 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.netty;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.truth.Truth.assertThat;
import static io.grpc.internal.ClientStreamListener.RpcProgress.PROCESSED;
import static io.grpc.internal.ClientStreamListener.RpcProgress.REFUSED;
import static io.grpc.internal.GrpcUtil.DEFAULT_MAX_MESSAGE_SIZE;
import static io.grpc.netty.Utils.CONTENT_TYPE_GRPC;
import static io.grpc.netty.Utils.CONTENT_TYPE_HEADER;
import static io.grpc.netty.Utils.HTTPS;
import static io.grpc.netty.Utils.HTTP_METHOD;
import static io.grpc.netty.Utils.STATUS_OK;
import static io.grpc.netty.Utils.TE_HEADER;
import static io.grpc.netty.Utils.TE_TRAILERS;
import static io.netty.handler.codec.http2.Http2CodecUtil.DEFAULT_PRIORITY_WEIGHT;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.same;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import com.google.common.base.Stopwatch;
import com.google.common.base.Supplier;
import com.google.common.base.Ticker;
import com.google.common.collect.ImmutableList;
import com.google.common.io.ByteStreams;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.grpc.Attributes;
import io.grpc.Metadata;
import io.grpc.Status;
import io.grpc.StatusException;
import io.grpc.internal.ClientStreamListener;
import io.grpc.internal.ClientStreamListener.RpcProgress;
import io.grpc.internal.ClientTransport;
import io.grpc.internal.ClientTransport.PingCallback;
import io.grpc.internal.GrpcUtil;
import io.grpc.internal.KeepAliveManager;
import io.grpc.internal.ManagedClientTransport;
import io.grpc.internal.StatsTraceContext;
import io.grpc.internal.StreamListener;
import io.grpc.internal.TransportTracer;
import io.grpc.netty.GrpcHttp2HeadersUtils.GrpcHttp2ClientHeadersDecoder;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.channel.EventLoop;
import io.netty.handler.codec.http2.DefaultHttp2Connection;
import io.netty.handler.codec.http2.DefaultHttp2Headers;
import io.netty.handler.codec.http2.Http2Connection;
import io.netty.handler.codec.http2.Http2Error;
import io.netty.handler.codec.http2.Http2Exception;
import io.netty.handler.codec.http2.Http2Headers;
import io.netty.handler.codec.http2.Http2LocalFlowController;
import io.netty.handler.codec.http2.Http2Settings;
import io.netty.handler.codec.http2.Http2Stream;
import io.netty.util.AsciiString;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
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.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
/**
* Tests for {@link NettyClientHandler}.
*/
@RunWith(JUnit4.class)
public class NettyClientHandlerTest extends NettyHandlerTestBase<NettyClientHandler> {
private NettyClientStream.TransportState streamTransportState;
private Http2Headers grpcHeaders;
private long nanoTime; // backs a ticker, for testing ping round-trip time measurement
private int maxHeaderListSize = Integer.MAX_VALUE;
private int streamId = 3;
private ClientTransportLifecycleManager lifecycleManager;
private KeepAliveManager mockKeepAliveManager = null;
private List<String> setKeepaliveManagerFor = ImmutableList.of("cancelShouldSucceed",
"sendFrameShouldSucceed", "channelShutdownShouldCancelBufferedStreams",
"createIncrementsIdsForActualAndBufferdStreams", "dataPingAckIsRecognized");
private Runnable tooManyPingsRunnable = new Runnable() {
@Override public void run() {}
};
@Rule
public TestName testNameRule = new TestName();
@Mock
private ManagedClientTransport.Listener listener;
@Mock
private ClientStreamListener streamListener;
private final Queue<InputStream> streamListenerMessageQueue = new LinkedList<>();
@Override
protected void manualSetUp() throws Exception {
setUp();
}
/**
* Set up for test.
*/
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
doAnswer(
new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
StreamListener.MessageProducer producer =
(StreamListener.MessageProducer) invocation.getArguments()[0];
InputStream message;
while ((message = producer.next()) != null) {
streamListenerMessageQueue.add(message);
}
return null;
}
})
.when(streamListener)
.messagesAvailable(ArgumentMatchers.<StreamListener.MessageProducer>any());
lifecycleManager = new ClientTransportLifecycleManager(listener);
// This mocks the keepalive manager only for there's in which we verify it. For other tests
// it'll be null which will be testing if we behave correctly when it's not present.
if (setKeepaliveManagerFor.contains(testNameRule.getMethodName())) {
mockKeepAliveManager = mock(KeepAliveManager.class);
}
initChannel(new GrpcHttp2ClientHeadersDecoder(GrpcUtil.DEFAULT_MAX_HEADER_LIST_SIZE));
streamTransportState = new TransportStateImpl(
handler(),
channel().eventLoop(),
DEFAULT_MAX_MESSAGE_SIZE,
transportTracer);
streamTransportState.setListener(streamListener);
grpcHeaders = new DefaultHttp2Headers()
.scheme(HTTPS)
.authority(as("www.fake.com"))
.path(as("/fakemethod"))
.method(HTTP_METHOD)
.add(as("auth"), as("sometoken"))
.add(CONTENT_TYPE_HEADER, CONTENT_TYPE_GRPC)
.add(TE_HEADER, TE_TRAILERS);
// Simulate receipt of initial remote settings.
ByteBuf serializedSettings = serializeSettings(new Http2Settings());
channelRead(serializedSettings);
}
@Test
public void cancelBufferedStreamShouldChangeClientStreamStatus() throws Exception {
// Force the stream to be buffered.
receiveMaxConcurrentStreams(0);
// Create a new stream with id 3.
ChannelFuture createFuture = enqueue(
newCreateStreamCommand(grpcHeaders, streamTransportState));
assertEquals(3, streamTransportState.id());
// Cancel the stream.
cancelStream(Status.CANCELLED);
assertTrue(createFuture.isSuccess());
verify(streamListener).closed(eq(Status.CANCELLED), same(PROCESSED), any(Metadata.class));
}
@Test
public void createStreamShouldSucceed() throws Exception {
createStream();
verifyWrite().writeHeaders(eq(ctx()), eq(3), eq(grpcHeaders), eq(0),
eq(DEFAULT_PRIORITY_WEIGHT), eq(false), eq(0), eq(false), any(ChannelPromise.class));
}
@Test
public void cancelShouldSucceed() throws Exception {
createStream();
cancelStream(Status.CANCELLED);
verifyWrite().writeRstStream(eq(ctx()), eq(3), eq(Http2Error.CANCEL.code()),
any(ChannelPromise.class));
verify(mockKeepAliveManager, times(1)).onTransportActive(); // onStreamActive
verify(mockKeepAliveManager, times(1)).onTransportIdle(); // onStreamClosed
verifyNoMoreInteractions(mockKeepAliveManager);
}
@Test
public void cancelDeadlineExceededShouldSucceed() throws Exception {
createStream();
cancelStream(Status.DEADLINE_EXCEEDED);
verifyWrite().writeRstStream(eq(ctx()), eq(3), eq(Http2Error.CANCEL.code()),
any(ChannelPromise.class));
}
@Test
public void cancelWhileBufferedShouldSucceed() throws Exception {
// Force the stream to be buffered.
receiveMaxConcurrentStreams(0);
ChannelFuture createFuture = createStream();
assertFalse(createFuture.isDone());
ChannelFuture cancelFuture = cancelStream(Status.CANCELLED);
assertTrue(cancelFuture.isSuccess());
assertTrue(createFuture.isDone());
assertTrue(createFuture.isSuccess());
}
/**
* Although nobody is listening to an exception should it occur during cancel(), we don't want an
* exception to be thrown because it would negatively impact performance, and we don't want our
* users working around around such performance issues.
*/
@Test
public void cancelTwiceShouldSucceed() throws Exception {
createStream();
cancelStream(Status.CANCELLED);
verifyWrite().writeRstStream(any(ChannelHandlerContext.class), eq(3),
eq(Http2Error.CANCEL.code()), any(ChannelPromise.class));
ChannelFuture future = cancelStream(Status.CANCELLED);
assertTrue(future.isSuccess());
}
@Test
public void cancelTwiceDifferentReasons() throws Exception {
createStream();
cancelStream(Status.DEADLINE_EXCEEDED);
verifyWrite().writeRstStream(eq(ctx()), eq(3), eq(Http2Error.CANCEL.code()),
any(ChannelPromise.class));
ChannelFuture future = cancelStream(Status.CANCELLED);
assertTrue(future.isSuccess());
}
@Test
public void sendFrameShouldSucceed() throws Exception {
createStream();
// Send a frame and verify that it was written.
ChannelFuture future
= enqueue(new SendGrpcFrameCommand(streamTransportState, content(), true));
assertTrue(future.isSuccess());
verifyWrite().writeData(eq(ctx()), eq(3), eq(content()), eq(0), eq(true),
any(ChannelPromise.class));
verify(mockKeepAliveManager, times(1)).onTransportActive(); // onStreamActive
verifyNoMoreInteractions(mockKeepAliveManager);
}
@Test
public void sendForUnknownStreamShouldFail() throws Exception {
ChannelFuture future
= enqueue(new SendGrpcFrameCommand(streamTransportState, content(), true));
assertTrue(future.isDone());
assertFalse(future.isSuccess());
}
@Test
public void inboundShouldForwardToStream() throws Exception {
createStream();
// Read a headers frame first.
Http2Headers headers = new DefaultHttp2Headers().status(STATUS_OK)
.set(CONTENT_TYPE_HEADER, CONTENT_TYPE_GRPC)
.set(as("magic"), as("value"));
ByteBuf headersFrame = headersFrame(3, headers);
channelRead(headersFrame);
ArgumentCaptor<Metadata> captor = ArgumentCaptor.forClass(Metadata.class);
verify(streamListener).headersRead(captor.capture());
assertEquals("value",
captor.getValue().get(Metadata.Key.of("magic", Metadata.ASCII_STRING_MARSHALLER)));
streamTransportState.requestMessagesFromDeframerForTesting(1);
// Create a data frame and then trigger the handler to read it.
ByteBuf frame = grpcDataFrame(3, false, contentAsArray());
channelRead(frame);
InputStream message = streamListenerMessageQueue.poll();
assertArrayEquals(ByteBufUtil.getBytes(content()), ByteStreams.toByteArray(message));
message.close();
assertNull("no additional message expected", streamListenerMessageQueue.poll());
}
@Test
public void receivedGoAwayShouldRefuseLaterStreamId() throws Exception {
ChannelFuture future = enqueue(newCreateStreamCommand(grpcHeaders, streamTransportState));
channelRead(goAwayFrame(streamId - 1));
// This _should_ be REFUSED, but we purposefully use PROCESSED. See comment for
// abruptGoAwayStatusConservative in NettyClientHandler
verify(streamListener).closed(any(Status.class), eq(PROCESSED), any(Metadata.class));
assertTrue(future.isDone());
}
@Test
public void receivedGoAwayShouldNotAffectEarlyStreamId() throws Exception {
ChannelFuture future = enqueue(newCreateStreamCommand(grpcHeaders, streamTransportState));
channelRead(goAwayFrame(streamId));
verify(streamListener, never())
.closed(any(Status.class), any(Metadata.class));
verify(streamListener, never())
.closed(any(Status.class), any(RpcProgress.class), any(Metadata.class));
assertTrue(future.isDone());
}
@Test
public void receivedGoAwayShouldNotAffectRacingQueuedStreamId() throws Exception {
// This command has not actually been executed yet
ChannelFuture future = writeQueue().enqueue(
newCreateStreamCommand(grpcHeaders, streamTransportState), true);
channelRead(goAwayFrame(streamId));
verify(streamListener, never())
.closed(any(Status.class), any(Metadata.class));
verify(streamListener, never())
.closed(any(Status.class), any(RpcProgress.class), any(Metadata.class));
assertTrue(future.isDone());
}
@Test
public void receivedAbruptGoAwayShouldFailRacingQueuedStreamid() throws Exception {
// This command has not actually been executed yet
ChannelFuture future = writeQueue().enqueue(
newCreateStreamCommand(grpcHeaders, streamTransportState), true);
// Read a GOAWAY that indicates our stream can't be sent
channelRead(goAwayFrame(0, 8 /* Cancel */, Unpooled.copiedBuffer("this is a test", UTF_8)));
ArgumentCaptor<Status> captor = ArgumentCaptor.forClass(Status.class);
verify(streamListener).closed(captor.capture(), same(REFUSED),
ArgumentMatchers.<Metadata>notNull());
assertEquals(Status.UNAVAILABLE.getCode(), captor.getValue().getCode());
assertEquals(
"Abrupt GOAWAY closed unsent stream. HTTP/2 error code: CANCEL, "
+ "debug data: this is a test",
captor.getValue().getDescription());
assertTrue(future.isDone());
}
@Test
public void receivedResetWithRefuseCode() throws Exception {
ChannelFuture future = enqueue(newCreateStreamCommand(grpcHeaders, streamTransportState));
channelRead(rstStreamFrame(streamId, (int) Http2Error.REFUSED_STREAM.code() ));
verify(streamListener).closed(any(Status.class), eq(REFUSED), any(Metadata.class));
assertTrue(future.isDone());
}
@Test
public void receivedResetWithCanceCode() throws Exception {
ChannelFuture future = enqueue(newCreateStreamCommand(grpcHeaders, streamTransportState));
channelRead(rstStreamFrame(streamId, (int) Http2Error.CANCEL.code()));
verify(streamListener).closed(any(Status.class), eq(PROCESSED), any(Metadata.class));
assertTrue(future.isDone());
}
@Test
public void receivedGoAwayShouldFailUnknownStreams() throws Exception {
enqueue(newCreateStreamCommand(grpcHeaders, streamTransportState));
// Read a GOAWAY that indicates our stream was never processed by the server.
channelRead(goAwayFrame(0, 8 /* Cancel */, Unpooled.copiedBuffer("this is a test", UTF_8)));
ArgumentCaptor<Status> captor = ArgumentCaptor.forClass(Status.class);
// See comment for abruptGoAwayStatusConservative in NettyClientHandler
verify(streamListener).closed(captor.capture(), same(PROCESSED),
ArgumentMatchers.<Metadata>notNull());
assertEquals(Status.CANCELLED.getCode(), captor.getValue().getCode());
assertEquals(
"Abrupt GOAWAY closed sent stream. HTTP/2 error code: CANCEL, "
+ "debug data: this is a test",
captor.getValue().getDescription());
}
@Test
public void receivedGoAwayShouldFailBufferedStreams() throws Exception {
receiveMaxConcurrentStreams(0);
ChannelFuture future = enqueue(newCreateStreamCommand(grpcHeaders, streamTransportState));
// Read a GOAWAY that indicates our stream was never processed by the server.
channelRead(goAwayFrame(0, 8 /* Cancel */, Unpooled.copiedBuffer("this is a test", UTF_8)));
assertTrue(future.isDone());
assertFalse(future.isSuccess());
Status status = Status.fromThrowable(future.cause());
assertEquals(Status.UNAVAILABLE.getCode(), status.getCode());
assertEquals(
"GOAWAY closed buffered stream. HTTP/2 error code: CANCEL, "
+ "debug data: this is a test",
status.getDescription());
}
@Test
public void receivedGoAwayShouldFailNewStreams() throws Exception {
// Read a GOAWAY that indicates our stream was never processed by the server.
channelRead(goAwayFrame(0, 8 /* Cancel */, Unpooled.copiedBuffer("this is a test", UTF_8)));
// Now try to create a stream.
ChannelFuture future = enqueue(newCreateStreamCommand(grpcHeaders, streamTransportState));
assertTrue(future.isDone());
assertFalse(future.isSuccess());
Status status = Status.fromThrowable(future.cause());
assertEquals(Status.UNAVAILABLE.getCode(), status.getCode());
assertEquals(
"GOAWAY shut down transport. HTTP/2 error code: CANCEL, "
+ "debug data: this is a test",
status.getDescription());
}
// This test is not as useful as it looks, because the HTTP/2 Netty code catches and doesn't
// propagate exceptions during the onGoAwayReceived callback.
@Test
public void receivedGoAway_notUtf8() throws Exception {
// 0xFF is never permitted in UTF-8. 0xF0 should have 3 continuations following, and 0x0a isn't
// a continuation.
channelRead(goAwayFrame(0, 11 /* ENHANCE_YOUR_CALM */,
Unpooled.copiedBuffer(new byte[] {(byte) 0xFF, (byte) 0xF0, (byte) 0x0a})));
}
@Test
public void receivedGoAway_enhanceYourCalmWithoutTooManyPings() throws Exception {
final AtomicBoolean b = new AtomicBoolean();
tooManyPingsRunnable = new Runnable() {
@Override
public void run() {
b.set(true);
}
};
setUp();
channelRead(goAwayFrame(0, 11 /* ENHANCE_YOUR_CALM */,
Unpooled.copiedBuffer("not_many_pings", UTF_8)));
assertFalse(b.get());
}
@Test
public void receivedGoAway_enhanceYourCalmWithTooManyPings() throws Exception {
final AtomicBoolean b = new AtomicBoolean();
tooManyPingsRunnable = new Runnable() {
@Override
public void run() {
b.set(true);
}
};
setUp();
channelRead(goAwayFrame(0, 11 /* ENHANCE_YOUR_CALM */,
Unpooled.copiedBuffer("too_many_pings", UTF_8)));
assertTrue(b.get());
}
@Test
public void receivedGoAway_enhanceYourCalmShouldLogDebugData() throws Exception {
final AtomicReference<LogRecord> logRef = new AtomicReference<>();
Handler handler = new Handler() {
@Override
public void publish(LogRecord record) {
logRef.set(record);
}
@Override
public void flush() {
}
@Override
public void close() throws SecurityException {
}
};
Logger logger = Logger.getLogger(NettyClientHandler.class.getName());
try {
logger.addHandler(handler);
enqueue(newCreateStreamCommand(grpcHeaders, streamTransportState));
channelRead(goAwayFrame(0, 11 /* Enhance your calm */,
Unpooled.copiedBuffer("this is a test", UTF_8)));
assertNotNull(logRef.get());
assertTrue(MessageFormat.format(logRef.get().getMessage(), logRef.get().getParameters())
.contains("Debug data: this is a test"));
} finally {
logger.removeHandler(handler);
}
}
@Test
public void cancelStreamShouldCreateAndThenFailBufferedStream() throws Exception {
receiveMaxConcurrentStreams(0);
enqueue(newCreateStreamCommand(grpcHeaders, streamTransportState));
assertEquals(3, streamTransportState.id());
cancelStream(Status.CANCELLED);
verify(streamListener).closed(eq(Status.CANCELLED), same(PROCESSED), any(Metadata.class));
}
@Test
public void channelShutdownShouldCancelBufferedStreams() throws Exception {
// Force a stream to get added to the pending queue.
receiveMaxConcurrentStreams(0);
ChannelFuture future = enqueue(newCreateStreamCommand(grpcHeaders, streamTransportState));
handler().channelInactive(ctx());
assertTrue(future.isDone());
assertFalse(future.isSuccess());
verify(mockKeepAliveManager, times(1)).onTransportTermination(); // channelInactive
verifyNoMoreInteractions(mockKeepAliveManager);
}
@Test
public void channelShutdownShouldFailInFlightStreams() throws Exception {
createStream();
handler().channelInactive(ctx());
ArgumentCaptor<Status> captor = ArgumentCaptor.forClass(Status.class);
verify(streamListener).closed(captor.capture(), same(PROCESSED),
ArgumentMatchers.<Metadata>notNull());
assertEquals(Status.UNAVAILABLE.getCode(), captor.getValue().getCode());
}
@Test
public void connectionWindowShouldBeOverridden() throws Exception {
flowControlWindow = 1048576; // 1MiB
setUp();
Http2Stream connectionStream = connection().connectionStream();
Http2LocalFlowController localFlowController = connection().local().flowController();
int actualInitialWindowSize = localFlowController.initialWindowSize(connectionStream);
int actualWindowSize = localFlowController.windowSize(connectionStream);
assertEquals(flowControlWindow, actualWindowSize);
assertEquals(flowControlWindow, actualInitialWindowSize);
assertEquals(1048576, actualWindowSize);
}
@Test
public void createIncrementsIdsForActualAndBufferdStreams() throws Exception {
receiveMaxConcurrentStreams(2);
enqueue(newCreateStreamCommand(grpcHeaders, streamTransportState));
assertEquals(3, streamTransportState.id());
streamTransportState = new TransportStateImpl(
handler(),
channel().eventLoop(),
DEFAULT_MAX_MESSAGE_SIZE,
transportTracer);
streamTransportState.setListener(streamListener);
enqueue(newCreateStreamCommand(grpcHeaders, streamTransportState));
assertEquals(5, streamTransportState.id());
streamTransportState = new TransportStateImpl(
handler(),
channel().eventLoop(),
DEFAULT_MAX_MESSAGE_SIZE,
transportTracer);
streamTransportState.setListener(streamListener);
enqueue(newCreateStreamCommand(grpcHeaders, streamTransportState));
assertEquals(7, streamTransportState.id());
verify(mockKeepAliveManager, times(1)).onTransportActive(); // onStreamActive
verifyNoMoreInteractions(mockKeepAliveManager);
}
@Test
public void exhaustedStreamsShouldFail() throws Exception {
streamId = Integer.MAX_VALUE;
setUp();
assertNull(lifecycleManager.getShutdownStatus());
// Create the MAX_INT stream.
ChannelFuture future = createStream();
assertTrue(future.isSuccess());
TransportStateImpl newStreamTransportState = new TransportStateImpl(
handler(),
channel().eventLoop(),
DEFAULT_MAX_MESSAGE_SIZE,
transportTracer);
// This should fail - out of stream IDs.
future = enqueue(newCreateStreamCommand(grpcHeaders, newStreamTransportState));
assertTrue(future.isDone());
assertFalse(future.isSuccess());
Status status = lifecycleManager.getShutdownStatus();
assertNotNull(status);
assertTrue("status does not reference 'exhausted': " + status,
status.getDescription().contains("exhausted"));
}
@Test
public void nonExistentStream() throws Exception {
Status status = Status.INTERNAL.withDescription("zz");
lifecycleManager.notifyShutdown(status);
// Stream creation can race with the transport shutting down, with the create command already
// enqueued.
ChannelFuture future1 = createStream();
future1.await();
assertNotNull(future1.cause());
assertThat(Status.fromThrowable(future1.cause()).getCode()).isEqualTo(status.getCode());
ChannelFuture future2 = enqueue(new CancelClientStreamCommand(streamTransportState, status));
future2.sync();
}
@Test
public void ping() throws Exception {
PingCallbackImpl callback1 = new PingCallbackImpl();
assertEquals(0, transportTracer.getStats().keepAlivesSent);
sendPing(callback1);
assertEquals(1, transportTracer.getStats().keepAlivesSent);
// add'l ping will be added as listener to outstanding operation
PingCallbackImpl callback2 = new PingCallbackImpl();
sendPing(callback2);
assertEquals(1, transportTracer.getStats().keepAlivesSent);
ArgumentCaptor<Long> captor = ArgumentCaptor.forClass(long.class);
verifyWrite().writePing(eq(ctx()), eq(false), captor.capture(),
any(ChannelPromise.class));
// getting a bad ack won't cause the callback to be invoked
long pingPayload = captor.getValue();
// to compute bad payload, read the good payload and subtract one
long badPingPayload = pingPayload - 1;
channelRead(pingFrame(true, badPingPayload));
// operation not complete because ack was wrong
assertEquals(0, callback1.invocationCount);
assertEquals(0, callback2.invocationCount);
nanoTime += 10101;
// reading the proper response should complete the future
channelRead(pingFrame(true, pingPayload));
assertEquals(1, callback1.invocationCount);
assertEquals(10101, callback1.roundTripTime);
assertNull(callback1.failureCause);
// callback2 piggy-backed on same operation
assertEquals(1, callback2.invocationCount);
assertEquals(10101, callback2.roundTripTime);
assertNull(callback2.failureCause);
// now that previous ping is done, next request starts a new operation
callback1 = new PingCallbackImpl();
assertEquals(1, transportTracer.getStats().keepAlivesSent);
sendPing(callback1);
assertEquals(2, transportTracer.getStats().keepAlivesSent);
assertEquals(0, callback1.invocationCount);
}
@Test
public void ping_failsWhenChannelCloses() throws Exception {
PingCallbackImpl callback = new PingCallbackImpl();
sendPing(callback);
assertEquals(0, callback.invocationCount);
handler().channelInactive(ctx());
// ping failed on channel going inactive
assertEquals(1, callback.invocationCount);
assertTrue(callback.failureCause instanceof StatusException);
assertEquals(Status.Code.UNAVAILABLE,
((StatusException) callback.failureCause).getStatus().getCode());
// A failed ping is still counted
assertEquals(1, transportTracer.getStats().keepAlivesSent);
}
@Test
public void oustandingUserPingShouldNotInteractWithDataPing() throws Exception {
createStream();
handler().setAutoTuneFlowControl(true);
PingCallbackImpl callback = new PingCallbackImpl();
assertEquals(0, transportTracer.getStats().keepAlivesSent);
sendPing(callback);
assertEquals(1, transportTracer.getStats().keepAlivesSent);
ArgumentCaptor<Long> captor = ArgumentCaptor.forClass(long.class);
verifyWrite().writePing(eq(ctx()), eq(false), captor.capture(), any(ChannelPromise.class));
long payload = captor.getValue();
channelRead(grpcDataFrame(3, false, contentAsArray()));
long pingData = handler().flowControlPing().payload();
channelRead(pingFrame(true, pingData));
assertEquals(1, handler().flowControlPing().getPingReturn());
assertEquals(0, callback.invocationCount);
channelRead(pingFrame(true, payload));
assertEquals(1, handler().flowControlPing().getPingReturn());
assertEquals(1, callback.invocationCount);
assertEquals(1, transportTracer.getStats().keepAlivesSent);
}
@Test
public void bdpPingAvoidsTooManyPingsOnSpecialServers() throws Exception {
// gRPC servers limit PINGs based on what they _send_. Some servers limit PINGs based on what is
// _received_.
createStream();
handler().setAutoTuneFlowControl(true);
Http2Headers headers = new DefaultHttp2Headers().status(STATUS_OK)
.set(CONTENT_TYPE_HEADER, CONTENT_TYPE_GRPC);
channelRead(headersFrame(3, headers));
channelRead(dataFrame(3, false, content()));
verifyWrite().writePing(eq(ctx()), eq(false), eq(1234L), any(ChannelPromise.class));
channelRead(pingFrame(true, 1234));
channelRead(dataFrame(3, false, content()));
verifyWrite(times(2)).writePing(eq(ctx()), eq(false), eq(1234L), any(ChannelPromise.class));
channelRead(pingFrame(true, 1234));
channelRead(dataFrame(3, false, content()));
// No ping was sent
verifyWrite(times(2)).writePing(eq(ctx()), eq(false), eq(1234L), any(ChannelPromise.class));
}
@Test
public void bdpPingAllowedAfterSendingData() throws Exception {
// gRPC servers limit PINGs based on what they _send_. Some servers limit PINGs based on what is
// _received_.
flowControlWindow = 64 * 1024;
manualSetUp();
createStream();
handler().setAutoTuneFlowControl(true);
ByteBuf content = Unpooled.buffer(64 * 1024 + 1024);
content.writerIndex(content.capacity());
ChannelFuture future
= enqueue(new SendGrpcFrameCommand(streamTransportState, content, false));
assertFalse(future.isDone()); // flow control limits send
Http2Headers headers = new DefaultHttp2Headers().status(STATUS_OK)
.set(CONTENT_TYPE_HEADER, CONTENT_TYPE_GRPC);
channelRead(headersFrame(3, headers));
channelRead(dataFrame(3, false, content()));
verifyWrite().writePing(eq(ctx()), eq(false), eq(1234L), any(ChannelPromise.class));
channelRead(pingFrame(true, 1234));
channelRead(dataFrame(3, false, content()));
verifyWrite(times(2)).writePing(eq(ctx()), eq(false), eq(1234L), any(ChannelPromise.class));
channelRead(pingFrame(true, 1234));
channelRead(dataFrame(3, false, content()));
// No ping was sent
verifyWrite(times(2)).writePing(eq(ctx()), eq(false), eq(1234L), any(ChannelPromise.class));
channelRead(windowUpdate(0, 2024));
channelRead(windowUpdate(3, 2024));
assertTrue(future.isDone());
assertTrue(future.isSuccess());
// But now one is sent
channelRead(dataFrame(3, false, content()));
verifyWrite(times(3)).writePing(eq(ctx()), eq(false), eq(1234L), any(ChannelPromise.class));
}
@Override
public void dataPingAckIsRecognized() throws Exception {
super.dataPingAckIsRecognized();
verify(mockKeepAliveManager, times(1)).onTransportActive(); // onStreamActive
// onHeadersRead, onDataRead, onPingAckRead
verify(mockKeepAliveManager, times(3)).onDataReceived();
verifyNoMoreInteractions(mockKeepAliveManager);
}
@Test
public void exceptionCaughtShouldCloseConnection() throws Exception {
handler().exceptionCaught(ctx(), new RuntimeException("fake exception"));
// TODO(nmittler): EmbeddedChannel does not currently invoke the channelInactive processing,
// so exceptionCaught() will not close streams properly in this test.
// Once https://github.com/netty/netty/issues/4316 is resolved, we should also verify that
// any open streams are closed properly.
assertFalse(channel().isOpen());
}
@Override
protected void makeStream() throws Exception {
createStream();
// The tests in NettyServerHandlerTest expect the header to already be read, since they work on
// both client- and server-side.
Http2Headers headers = new DefaultHttp2Headers().status(STATUS_OK)
.set(CONTENT_TYPE_HEADER, CONTENT_TYPE_GRPC);
ByteBuf headersFrame = headersFrame(3, headers);
channelRead(headersFrame);
}
@CanIgnoreReturnValue
private ChannelFuture sendPing(PingCallback callback) {
return enqueue(new SendPingCommand(callback, MoreExecutors.directExecutor()));
}
private void receiveMaxConcurrentStreams(int max) throws Exception {
ByteBuf serializedSettings = serializeSettings(new Http2Settings().maxConcurrentStreams(max));
channelRead(serializedSettings);
}
@CanIgnoreReturnValue
private ChannelFuture createStream() throws Exception {
ChannelFuture future = enqueue(newCreateStreamCommand(grpcHeaders, streamTransportState));
return future;
}
@CanIgnoreReturnValue
private ChannelFuture cancelStream(Status status) throws Exception {
return enqueue(new CancelClientStreamCommand(streamTransportState, status));
}
@Override
protected NettyClientHandler newHandler() throws Http2Exception {
Http2Connection connection = new DefaultHttp2Connection(false);
// Create and close a stream previous to the nextStreamId.
Http2Stream stream = connection.local().createStream(streamId - 2, true);
stream.close();
final Ticker ticker = new Ticker() {
@Override
public long read() {
return nanoTime;
}
};
Supplier<Stopwatch> stopwatchSupplier = new Supplier<Stopwatch>() {
@Override
public Stopwatch get() {
return Stopwatch.createUnstarted(ticker);
}
};
return NettyClientHandler.newHandler(
connection,
frameReader(),
frameWriter(),
lifecycleManager,
mockKeepAliveManager,
false,
flowControlWindow,
maxHeaderListSize,
stopwatchSupplier,
tooManyPingsRunnable,
transportTracer,
Attributes.EMPTY,
"someauthority");
}
@Override
protected WriteQueue initWriteQueue() {
handler().startWriteQueue(channel());
return handler().getWriteQueue();
}
private AsciiString as(String string) {
return new AsciiString(string);
}
private static CreateStreamCommand newCreateStreamCommand(
Http2Headers headers, NettyClientStream.TransportState stream) {
return new CreateStreamCommand(headers, stream, true, false);
}
private static class PingCallbackImpl implements ClientTransport.PingCallback {
int invocationCount;
long roundTripTime;
Throwable failureCause;
@Override
public void onSuccess(long roundTripTimeNanos) {
invocationCount++;
this.roundTripTime = roundTripTimeNanos;
}
@Override
public void onFailure(Throwable cause) {
invocationCount++;
this.failureCause = cause;
}
}
private static class TransportStateImpl extends NettyClientStream.TransportState {
public TransportStateImpl(
NettyClientHandler handler,
EventLoop eventLoop,
int maxMessageSize,
TransportTracer transportTracer) {
super(
handler,
eventLoop,
maxMessageSize,
StatsTraceContext.NOOP,
transportTracer,
"methodName");
}
@Override
protected Status statusFromFailedFuture(ChannelFuture f) {
return Utils.statusFromThrowable(f.cause());
}
}
}