annotate stream_tcp_nodelay.t @ 882:e309d9701007

Tests: avoid RST in HTTP/2 proxy_protocol tests. Do not write to a socket that was shut down by the peer. On win32 this can result in WSAECONNABORTED read error before all pending data was delivered.
author Sergey Kandaurov <pluknet@nginx.com>
date Thu, 24 Mar 2016 01:13:01 +0300
parents 940757cf34f5
children f4189a38c3a4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
642
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1 #!/usr/bin/perl
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
2
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
3 # (C) Andrey Zelenkov
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
4 # (C) Sergey Kandaurov
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
5 # (C) Nginx, Inc.
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
6
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
7 # Stream tests for tcp_nodelay.
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
8
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
9 ###############################################################################
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
10
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
11 use warnings;
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
12 use strict;
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
13
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
14 use Test::More;
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
15
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
16 use IO::Select;
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
17
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
18 BEGIN { use FindBin; chdir($FindBin::Bin); }
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
19
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
20 use lib 'lib';
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
21 use Test::Nginx;
816
77359b849cd5 Tests: stream package.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 812
diff changeset
22 use Test::Nginx::Stream qw/ stream /;
642
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
23
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
24 ###############################################################################
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
25
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
26 select STDERR; $| = 1;
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
27 select STDOUT; $| = 1;
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
28
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
29 my $t = Test::Nginx->new()->has(qw/stream/);
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
30
851
940757cf34f5 Tests: more TODO 'phantom event' alerts on Solaris.
Sergey Kandaurov <pluknet@nginx.com>
parents: 816
diff changeset
31 $t->todo_alerts() if $^O eq 'solaris';
940757cf34f5 Tests: more TODO 'phantom event' alerts on Solaris.
Sergey Kandaurov <pluknet@nginx.com>
parents: 816
diff changeset
32
642
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
33 plan(skip_all => 'no tcp_nodelay') unless $t->has_version('1.9.4');
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
34
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
35 $t->plan(2)->write_file_expand('nginx.conf', <<'EOF');
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
36
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
37 %%TEST_GLOBALS%%
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
38
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
39 daemon off;
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
40
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
41 events {
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
42 }
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
43
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
44 stream {
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
45 proxy_buffer_size 1;
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
46 tcp_nodelay off;
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
47
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
48 server {
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
49 listen 127.0.0.1:8081;
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
50 proxy_pass 127.0.0.1:8080;
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
51 }
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
52
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
53 server {
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
54 tcp_nodelay on;
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
55 listen 127.0.0.1:8082;
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
56 proxy_pass 127.0.0.1:8080;
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
57 }
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
58 }
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
59
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
60 EOF
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
61
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
62 $t->run_daemon(\&stream_daemon);
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
63 $t->run()->waitforsocket('127.0.0.1:8080');
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
64
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
65 ###############################################################################
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
66
812
acec03d44976 Tests: close connection on last write in stream_tcp_nodelay.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 711
diff changeset
67 my $str = '1234567890' x 10 . 'F';
816
77359b849cd5 Tests: stream package.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 812
diff changeset
68 my $length = length($str);
642
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
69
816
77359b849cd5 Tests: stream package.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 812
diff changeset
70 is(stream('127.0.0.1:8081')->io($str, length => $length), $str,
77359b849cd5 Tests: stream package.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 812
diff changeset
71 'tcp_nodelay off');
77359b849cd5 Tests: stream package.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 812
diff changeset
72 is(stream('127.0.0.1:8082')->io($str, length => $length), $str,
77359b849cd5 Tests: stream package.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 812
diff changeset
73 'tcp_nodelay on');
642
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
74
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
75 ###############################################################################
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
76
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
77 sub stream_daemon {
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
78 my $server = IO::Socket::INET->new(
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
79 Proto => 'tcp',
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
80 LocalAddr => '127.0.0.1:8080',
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
81 Listen => 5,
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
82 Reuse => 1
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
83 )
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
84 or die "Can't create listening socket: $!\n";
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
85
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
86 my $sel = IO::Select->new($server);
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
87
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
88 local $SIG{PIPE} = 'IGNORE';
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
89
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
90 while (my @ready = $sel->can_read) {
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
91 foreach my $fh (@ready) {
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
92 if ($server == $fh) {
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
93 my $new = $fh->accept;
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
94 $new->autoflush(1);
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
95 $sel->add($new);
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
96
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
97 } elsif (stream_handle_client($fh)) {
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
98 $sel->remove($fh);
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
99 $fh->close;
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
100 }
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
101 }
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
102 }
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
103 }
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
104
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
105 sub stream_handle_client {
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
106 my ($client) = @_;
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
107
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
108 log2c("(new connection $client)");
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
109
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
110 $client->sysread(my $buffer, 65536) or return 1;
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
111
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
112 log2i("$client $buffer");
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
113
812
acec03d44976 Tests: close connection on last write in stream_tcp_nodelay.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 711
diff changeset
114 my $close = $buffer =~ /F/;
acec03d44976 Tests: close connection on last write in stream_tcp_nodelay.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 711
diff changeset
115
642
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
116 log2o("$client $buffer");
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
117
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
118 $client->syswrite($buffer);
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
119
812
acec03d44976 Tests: close connection on last write in stream_tcp_nodelay.t.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 711
diff changeset
120 return $close;
642
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
121 }
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
122
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
123 sub log2i { Test::Nginx::log_core('|| <<', @_); }
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
124 sub log2o { Test::Nginx::log_core('|| >>', @_); }
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
125 sub log2c { Test::Nginx::log_core('||', @_); }
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
126
555e718a70a5 Tests: basic stream tests for tcp_nodelay.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
127 ###############################################################################