comparison stream_upstream_least_conn.t @ 816:77359b849cd5

Tests: stream package.
author Andrey Zelenkov <zelenkov@nginx.com>
date Mon, 20 Jul 2015 15:06:09 +0300
parents b8db75ef1f00
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;
60 61
61 is(many('.', 10), '8081: 5, 8082: 5', 'balanced'); 62 is(many('.', 10), '8081: 5, 8082: 5', 'balanced');
62 63
63 my @sockets; 64 my @sockets;
64 for (1 .. 2) { 65 for (1 .. 2) {
65 my $s = stream_connect(); 66 my $s = stream();
66 stream_write($s, 'w'); 67 $s->write('w');
67 push @sockets, $s; 68 push @sockets, $s;
68 } 69 }
69 70
70 select undef, undef, undef, 0.2; 71 select undef, undef, undef, 0.2;
71 72
73 74
74 ############################################################################### 75 ###############################################################################
75 76
76 sub many { 77 sub many {
77 my ($data, $count, %opts) = @_; 78 my ($data, $count, %opts) = @_;
78 my (%ports, $peer); 79 my (%ports);
79
80 $peer = $opts{peer};
81 80
82 for (1 .. $count) { 81 for (1 .. $count) {
83 if (stream_get($data, $peer) =~ /(\d+)/) { 82 if (stream()->io($data) =~ /(\d+)/) {
84 $ports{$1} = 0 unless defined $ports{$1}; 83 $ports{$1} = 0 unless defined $ports{$1};
85 $ports{$1}++; 84 $ports{$1}++;
86 } 85 }
87 } 86 }
88 87
89 return join ', ', map { $_ . ": " . $ports{$_} } sort keys %ports; 88 return join ', ', map { $_ . ": " . $ports{$_} } sort keys %ports;
90 }
91
92 sub stream_get {
93 my ($data, $peer) = @_;
94
95 my $s = stream_connect($peer);
96 stream_write($s, $data);
97
98 $data = '';
99 while (my $buf = stream_read($s)) {
100 $data .= $buf;
101 }
102 return $data;
103 }
104
105 sub stream_connect {
106 my $peer = shift;
107 my $s = IO::Socket::INET->new(
108 Proto => 'tcp',
109 PeerAddr => $peer || '127.0.0.1:8080'
110 )
111 or die "Can't connect to nginx: $!\n";
112
113 return $s;
114 }
115
116 sub stream_write {
117 my ($s, $message) = @_;
118
119 local $SIG{PIPE} = 'IGNORE';
120
121 $s->blocking(0);
122 while (IO::Select->new($s)->can_write(1.5)) {
123 my $n = $s->syswrite($message);
124 last unless $n;
125 $message = substr($message, $n);
126 last unless length $message;
127 }
128
129 if (length $message) {
130 $s->close();
131 }
132 }
133
134 sub stream_read {
135 my ($s) = @_;
136 my ($buf);
137
138 $s->blocking(0);
139 if (IO::Select->new($s)->can_read(5)) {
140 $s->sysread($buf, 1024);
141 };
142
143 log_in($buf);
144 return $buf;
145 } 89 }
146 90
147 ############################################################################### 91 ###############################################################################
148 92
149 sub stream_daemon { 93 sub stream_daemon {