Mercurial > hg > nginx-tests
comparison stream_tcp_nodelay.t @ 642:555e718a70a5
Tests: basic stream tests for tcp_nodelay.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Tue, 11 Aug 2015 20:17:30 +0300 |
parents | |
children | 824754da4afc |
comparison
equal
deleted
inserted
replaced
641:45d89d9c38a9 | 642:555e718a70a5 |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Andrey Zelenkov | |
4 # (C) Sergey Kandaurov | |
5 # (C) Nginx, Inc. | |
6 | |
7 # Stream tests for tcp_nodelay. | |
8 | |
9 ############################################################################### | |
10 | |
11 use warnings; | |
12 use strict; | |
13 | |
14 use Test::More; | |
15 | |
16 use IO::Select; | |
17 | |
18 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
19 | |
20 use lib 'lib'; | |
21 use Test::Nginx; | |
22 | |
23 ############################################################################### | |
24 | |
25 select STDERR; $| = 1; | |
26 select STDOUT; $| = 1; | |
27 | |
28 my $t = Test::Nginx->new()->has(qw/stream/); | |
29 | |
30 plan(skip_all => 'no tcp_nodelay') unless $t->has_version('1.9.4'); | |
31 | |
32 $t->plan(2)->write_file_expand('nginx.conf', <<'EOF'); | |
33 | |
34 %%TEST_GLOBALS%% | |
35 | |
36 daemon off; | |
37 | |
38 events { | |
39 } | |
40 | |
41 stream { | |
42 proxy_buffer_size 1; | |
43 tcp_nodelay off; | |
44 | |
45 server { | |
46 listen 127.0.0.1:8081; | |
47 proxy_pass 127.0.0.1:8080; | |
48 } | |
49 | |
50 server { | |
51 tcp_nodelay on; | |
52 listen 127.0.0.1:8082; | |
53 proxy_pass 127.0.0.1:8080; | |
54 } | |
55 } | |
56 | |
57 EOF | |
58 | |
59 $t->run_daemon(\&stream_daemon); | |
60 $t->run()->waitforsocket('127.0.0.1:8080'); | |
61 | |
62 ############################################################################### | |
63 | |
64 my $str = '1234567890' x 10; | |
65 | |
66 is(stream_get($str, '127.0.0.1:8081'), $str, 'tcp_nodelay off'); | |
67 is(stream_get($str, '127.0.0.1:8082'), $str, 'tcp_nodelay on'); | |
68 | |
69 ############################################################################### | |
70 | |
71 sub stream_get { | |
72 my ($data, $peer) = @_; | |
73 my $data_length = length $data; | |
74 | |
75 my $s = stream_connect($peer); | |
76 stream_write($s, $data); | |
77 | |
78 $data = ''; | |
79 while (length $data < $data_length) { | |
80 my $buf = stream_read($s); | |
81 $data .= $buf; | |
82 } | |
83 return $data; | |
84 } | |
85 | |
86 sub stream_connect { | |
87 my $peer = shift; | |
88 my $s = IO::Socket::INET->new( | |
89 Proto => 'tcp', | |
90 PeerAddr => $peer || '127.0.0.1:8080' | |
91 ) | |
92 or die "Can't connect to nginx: $!\n"; | |
93 | |
94 return $s; | |
95 } | |
96 | |
97 sub stream_write { | |
98 my ($s, $message) = @_; | |
99 | |
100 local $SIG{PIPE} = 'IGNORE'; | |
101 | |
102 $s->blocking(0); | |
103 while (IO::Select->new($s)->can_write(1.5)) { | |
104 my $n = $s->syswrite($message); | |
105 last unless $n; | |
106 $message = substr($message, $n); | |
107 last unless length $message; | |
108 } | |
109 | |
110 if (length $message) { | |
111 $s->close(); | |
112 } | |
113 } | |
114 | |
115 sub stream_read { | |
116 my ($s) = @_; | |
117 my ($buf); | |
118 | |
119 $s->blocking(0); | |
120 if (IO::Select->new($s)->can_read(3)) { | |
121 $s->sysread($buf, 1024); | |
122 }; | |
123 | |
124 log_in($buf); | |
125 return $buf; | |
126 } | |
127 | |
128 ############################################################################### | |
129 | |
130 sub stream_daemon { | |
131 my $server = IO::Socket::INET->new( | |
132 Proto => 'tcp', | |
133 LocalAddr => '127.0.0.1:8080', | |
134 Listen => 5, | |
135 Reuse => 1 | |
136 ) | |
137 or die "Can't create listening socket: $!\n"; | |
138 | |
139 my $sel = IO::Select->new($server); | |
140 | |
141 local $SIG{PIPE} = 'IGNORE'; | |
142 | |
143 while (my @ready = $sel->can_read) { | |
144 foreach my $fh (@ready) { | |
145 if ($server == $fh) { | |
146 my $new = $fh->accept; | |
147 $new->autoflush(1); | |
148 $sel->add($new); | |
149 | |
150 } elsif (stream_handle_client($fh)) { | |
151 $sel->remove($fh); | |
152 $fh->close; | |
153 } | |
154 } | |
155 } | |
156 } | |
157 | |
158 sub stream_handle_client { | |
159 my ($client) = @_; | |
160 | |
161 log2c("(new connection $client)"); | |
162 | |
163 $client->sysread(my $buffer, 65536) or return 1; | |
164 | |
165 log2i("$client $buffer"); | |
166 | |
167 log2o("$client $buffer"); | |
168 | |
169 $client->syswrite($buffer); | |
170 | |
171 return 0; | |
172 } | |
173 | |
174 sub log2i { Test::Nginx::log_core('|| <<', @_); } | |
175 sub log2o { Test::Nginx::log_core('|| >>', @_); } | |
176 sub log2c { Test::Nginx::log_core('||', @_); } | |
177 | |
178 ############################################################################### |