comparison stream_upstream_hash.t @ 816:77359b849cd5

Tests: stream package.
author Andrey Zelenkov <zelenkov@nginx.com>
date Mon, 20 Jul 2015 15:06:09 +0300
parents 824754da4afc
children e9064d691790
comparison
equal deleted inserted replaced
815:9f5f604a840e 816:77359b849cd5
16 16
17 BEGIN { use FindBin; chdir($FindBin::Bin); } 17 BEGIN { use FindBin; chdir($FindBin::Bin); }
18 18
19 use lib 'lib'; 19 use lib 'lib';
20 use Test::Nginx; 20 use Test::Nginx;
21 use Test::Nginx::Stream qw/ stream /;
21 22
22 ############################################################################### 23 ###############################################################################
23 24
24 select STDERR; $| = 1; 25 select STDERR; $| = 1;
25 select STDOUT; $| = 1; 26 select STDOUT; $| = 1;
80 my (%ports, $peer); 81 my (%ports, $peer);
81 82
82 $peer = $opts{peer}; 83 $peer = $opts{peer};
83 84
84 for (1 .. $count) { 85 for (1 .. $count) {
85 if (stream_get($data, $peer) =~ /(\d+)/) { 86 if (stream($peer)->io($data) =~ /(\d+)/) {
86 $ports{$1} = 0 unless defined $ports{$1}; 87 $ports{$1} = 0 unless defined $ports{$1};
87 $ports{$1}++; 88 $ports{$1}++;
88 } 89 }
89 } 90 }
90 91
91 return join ', ', map { $_ . ": " . $ports{$_} } sort keys %ports; 92 return join ', ', map { $_ . ": " . $ports{$_} } sort keys %ports;
92 }
93
94 sub stream_get {
95 my ($data, $peer) = @_;
96
97 my $s = stream_connect($peer);
98 stream_write($s, $data);
99
100 $data = '';
101 while (my $buf = stream_read($s)) {
102 $data .= $buf;
103 }
104 return $data;
105 }
106
107 sub stream_connect {
108 my $peer = shift;
109 my $s = IO::Socket::INET->new(
110 Proto => 'tcp',
111 PeerAddr => $peer || '127.0.0.1:8080'
112 )
113 or die "Can't connect to nginx: $!\n";
114
115 return $s;
116 }
117
118 sub stream_write {
119 my ($s, $message) = @_;
120
121 local $SIG{PIPE} = 'IGNORE';
122
123 $s->blocking(0);
124 while (IO::Select->new($s)->can_write(1.5)) {
125 my $n = $s->syswrite($message);
126 last unless $n;
127 $message = substr($message, $n);
128 last unless length $message;
129 }
130
131 if (length $message) {
132 $s->close();
133 }
134 }
135
136 sub stream_read {
137 my ($s) = @_;
138 my ($buf);
139
140 $s->blocking(0);
141 if (IO::Select->new($s)->can_read(5)) {
142 $s->sysread($buf, 1024);
143 };
144
145 log_in($buf);
146 return $buf;
147 } 93 }
148 94
149 ############################################################################### 95 ###############################################################################
150 96
151 sub stream_daemon { 97 sub stream_daemon {