Mercurial > hg > nginx-tests
comparison upstream_least_conn.t @ 438:60888e2c3f5a
Tests: new http_start() and http_end() functions.
When used together, they allow to break an http request into two separate
send/receive phases and are used to run long requests asynchronously.
An http() "start" extra flag introduced as a convenience shortcut.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Fri, 18 Jul 2014 13:19:55 +0400 |
parents | 98d9b06b087b |
children | b86c05516e65 |
comparison
equal
deleted
inserted
replaced
437:8b4a6b8691eb | 438:60888e2c3f5a |
---|---|
14 use Socket qw/ CRLF /; | 14 use Socket qw/ CRLF /; |
15 | 15 |
16 BEGIN { use FindBin; chdir($FindBin::Bin); } | 16 BEGIN { use FindBin; chdir($FindBin::Bin); } |
17 | 17 |
18 use lib 'lib'; | 18 use lib 'lib'; |
19 use Test::Nginx; | 19 use Test::Nginx qw/ :DEFAULT http_end /; |
20 | 20 |
21 ############################################################################### | 21 ############################################################################### |
22 | 22 |
23 select STDERR; $| = 1; | 23 select STDERR; $| = 1; |
24 select STDOUT; $| = 1; | 24 select STDOUT; $| = 1; |
86 sub parallel { | 86 sub parallel { |
87 my ($uri, $count, %opts) = @_; | 87 my ($uri, $count, %opts) = @_; |
88 my (@sockets, %ports); | 88 my (@sockets, %ports); |
89 | 89 |
90 for (1 .. $count) { | 90 for (1 .. $count) { |
91 push(@sockets, http_start($uri)); | 91 push(@sockets, http_get($uri, start => 1)); |
92 select undef, undef, undef, 0.1; | 92 select undef, undef, undef, 0.1; |
93 } | 93 } |
94 | 94 |
95 for (1 .. $count) { | 95 for (1 .. $count) { |
96 if (http_end(pop(@sockets)) =~ /X-Port: (\d+)/) { | 96 if (http_end(pop(@sockets)) =~ /X-Port: (\d+)/) { |
98 $ports{$1}++; | 98 $ports{$1}++; |
99 } | 99 } |
100 } | 100 } |
101 | 101 |
102 return join ', ', map { $_ . ": " . $ports{$_} } sort keys %ports; | 102 return join ', ', map { $_ . ": " . $ports{$_} } sort keys %ports; |
103 } | |
104 | |
105 sub http_start { | |
106 my ($uri) = @_; | |
107 | |
108 my $s; | |
109 my $request = "GET $uri HTTP/1.0" . CRLF . CRLF; | |
110 | |
111 eval { | |
112 local $SIG{ALRM} = sub { die "timeout\n" }; | |
113 local $SIG{PIPE} = sub { die "sigpipe\n" }; | |
114 alarm(3); | |
115 $s = IO::Socket::INET->new( | |
116 Proto => 'tcp', | |
117 PeerAddr => '127.0.0.1:8080' | |
118 ); | |
119 log_out($request); | |
120 $s->print($request); | |
121 alarm(0); | |
122 }; | |
123 alarm(0); | |
124 if ($@) { | |
125 log_in("died: $@"); | |
126 return undef; | |
127 } | |
128 return $s; | |
129 } | |
130 | |
131 sub http_end { | |
132 my ($s) = @_; | |
133 my $reply; | |
134 | |
135 eval { | |
136 local $SIG{ALRM} = sub { die "timeout\n" }; | |
137 local $SIG{PIPE} = sub { die "sigpipe\n" }; | |
138 alarm(3); | |
139 local $/; | |
140 $reply = $s->getline(); | |
141 log_in($reply); | |
142 alarm(0); | |
143 }; | |
144 alarm(0); | |
145 if ($@) { | |
146 log_in("died: $@"); | |
147 return undef; | |
148 } | |
149 return $reply; | |
150 } | 103 } |
151 | 104 |
152 ############################################################################### | 105 ############################################################################### |
153 | 106 |
154 sub http_daemon { | 107 sub http_daemon { |