comparison grpc.t @ 1700:c903c0a3f302

Tests: added grpc GOAWAY tests.
author Sergey Kandaurov <pluknet@nginx.com>
date Thu, 17 Jun 2021 12:45:21 +0300
parents 202d8feedad1
children 99a9b8b50f21
comparison
equal deleted inserted replaced
1699:202d8feedad1 1700:c903c0a3f302
22 22
23 select STDERR; $| = 1; 23 select STDERR; $| = 1;
24 select STDOUT; $| = 1; 24 select STDOUT; $| = 1;
25 25
26 my $t = Test::Nginx->new()->has(qw/http rewrite http_v2 grpc/) 26 my $t = Test::Nginx->new()->has(qw/http rewrite http_v2 grpc/)
27 ->has(qw/upstream_keepalive/)->plan(138); 27 ->has(qw/upstream_keepalive/)->plan(146);
28 28
29 $t->write_file_expand('nginx.conf', <<'EOF'); 29 $t->write_file_expand('nginx.conf', <<'EOF');
30 30
31 %%TEST_GLOBALS%% 31 %%TEST_GLOBALS%%
32 32
240 is($frame->{length}, 3, 'setting updated - DATA length'); 240 is($frame->{length}, 3, 'setting updated - DATA length');
241 is($frame->{flags}, 1, 'setting updated - DATA flags'); 241 is($frame->{flags}, 1, 'setting updated - DATA flags');
242 $frames = $f->{http_end}(); 242 $frames = $f->{http_end}();
243 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; 243 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
244 is($frame->{headers}{'x-connection'}, $c, 'keepalive 3 - connection reuse'); 244 is($frame->{headers}{'x-connection'}, $c, 'keepalive 3 - connection reuse');
245
246 undef $f;
247 $f = grpc();
248
249 # upstream keepalive - GOAWAY, current request aborted
250
251 $f->{http_start}('/KeepAlive');
252 $f->{data}('Hello');
253 $frames = $f->{http_end}();
254 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
255 ok($c = $frame->{headers}{'x-connection'}, 'keepalive 4 - connection');
256
257 $f->{http_start}('/KeepAlive', reuse => 1);
258 $f->{goaway}(0, 0, 5);
259 $f->{data}('Hello');
260 $frames = $f->{http_end}();
261 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
262 is($frame->{headers}{':status'}, 502, 'keepalive 4 - GOAWAY aborted request');
263
264 $f->{http_start}('/KeepAlive');
265 $f->{data}('Hello');
266 $frames = $f->{http_end}();
267 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
268 cmp_ok($frame->{headers}{'x-connection'}, '>', $c, 'keepalive 4 - closed');
269
270 undef $f;
271 $f = grpc();
272
273 # upstream keepalive - disabled with a higher GOAWAY Last-Stream-ID
274
275 $f->{http_start}('/KeepAlive');
276 $f->{goaway}(0, 3, 5);
277 $f->{data}('Hello');
278 $frames = $f->{http_end}();
279 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
280 ok($c = $frame->{headers}{'x-connection'}, 'keepalive 5 - GOAWAY next stream');
281
282 TODO: {
283 local $TODO = 'not yet' unless $t->has_version('1.21.1');
284
285 $f->{http_start}('/KeepAlive');
286 $f->{data}('Hello');
287 $frames = $f->{http_end}();
288 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
289 cmp_ok($frame->{headers}{'x-connection'}, '>', $c, 'keepalive 5 - closed');
290
291 }
292
293 undef $f;
294 $f = grpc();
295
296 # upstream keepalive - GOAWAY in grpc filter, current stream aborted
297
298 $f->{http_start}('/KeepAlive');
299 $f->{data}('Hello');
300 $frames = $f->{http_end}(grpc_filter_goaway => [0, 0, 5]);
301 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
302 ok($c = $frame->{headers}{'x-connection'}, 'keepalive 6 - connection');
303 ($frame) = grep { $_->{type} eq "RST_STREAM" } @$frames;
304 ok($frame, 'keepalive 6 - grpc filter GOAWAY aborted stream');
305
306 $f->{http_start}('/KeepAlive');
307 $f->{data}('Hello');
308 $frames = $f->{http_end}();
309 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
310 cmp_ok($frame->{headers}{'x-connection'}, '>', $c, 'keepalive 6 - closed');
245 311
246 undef $f; 312 undef $f;
247 $f = grpc(); 313 $f = grpc();
248 314
249 # various header compression formats 315 # various header compression formats
662 alarm(0); 728 alarm(0);
663 }; 729 };
664 alarm(0); 730 alarm(0);
665 if ($@) { 731 if ($@) {
666 log_in("died: $@"); 732 log_in("died: $@");
733 # connection could be unexpectedly reused
734 goto reused if $client;
667 return undef; 735 return undef;
668 } 736 }
669 737
670 log2c("(new connection $client)"); 738 log2c("(new connection $client)");
671 $n++; 739 $n++;
674 742
675 $c = Test::Nginx::HTTP2->new(1, socket => $client, 743 $c = Test::Nginx::HTTP2->new(1, socket => $client,
676 pure => 1, preface => "") or return; 744 pure => 1, preface => "") or return;
677 } 745 }
678 746
747 reused:
679 my $frames = $c->read(all => [{ fin => 4 }]); 748 my $frames = $c->read(all => [{ fin => 4 }]);
680 749
681 if (!$extra{reuse}) { 750 if (!$extra{reuse}) {
682 $c->h2_settings(0); 751 $c->h2_settings(0);
683 $c->h2_settings(1); 752 $c->h2_settings(1);
704 $f->{update_sid} = sub { 773 $f->{update_sid} = sub {
705 $c->h2_window(shift, $sid); 774 $c->h2_window(shift, $sid);
706 }; 775 };
707 $f->{settings} = sub { 776 $f->{settings} = sub {
708 $c->h2_settings(@_); 777 $c->h2_settings(@_);
778 };
779 $f->{goaway} = sub {
780 $c->h2_goaway(@_);
709 }; 781 };
710 $f->{http_end} = sub { 782 $f->{http_end} = sub {
711 my (%extra) = @_; 783 my (%extra) = @_;
712 my $h = [ 784 my $h = [
713 { name => ':status', value => '200', 785 { name => ':status', value => '200',
721 $c->new_stream({ body_more => 1, headers => $h, %extra }, $sid); 793 $c->new_stream({ body_more => 1, headers => $h, %extra }, $sid);
722 $c->h2_body('Hello world', { body_more => 1, 794 $c->h2_body('Hello world', { body_more => 1,
723 body_padding => $extra{body_padding} }); 795 body_padding => $extra{body_padding} });
724 $c->h2_settings(0, %{$extra{grpc_filter_settings}}) 796 $c->h2_settings(0, %{$extra{grpc_filter_settings}})
725 if $extra{grpc_filter_settings}; 797 if $extra{grpc_filter_settings};
798 $c->h2_goaway(@{$extra{grpc_filter_goaway}})
799 if $extra{grpc_filter_goaway};
726 $c->new_stream({ headers => [ 800 $c->new_stream({ headers => [
727 { name => 'grpc-status', value => '0', 801 { name => 'grpc-status', value => '0',
728 mode => 2, huff => 1 }, 802 mode => 2, huff => 1 },
729 { name => 'grpc-message', value => '', 803 { name => 'grpc-message', value => '',
730 mode => 2, huff => 1 }, 804 mode => 2, huff => 1 },
731 ]}, $sid); 805 ]}, $sid);
732 806
807 return $s->read(all => [{ type => 'RST_STREAM' }])
808 if $extra{grpc_filter_goaway};
733 return $s->read(all => [{ fin => 1 }]); 809 return $s->read(all => [{ fin => 1 }]);
734 }; 810 };
735 $f->{http_pres} = sub { 811 $f->{http_pres} = sub {
736 my (%extra) = @_; 812 my (%extra) = @_;
737 $s->h2_settings(0, 0x4 => 8192); 813 $s->h2_settings(0, 0x4 => 8192);