comparison stream_proxy_protocol.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
11 use strict; 11 use strict;
12 12
13 use Test::More; 13 use Test::More;
14 14
15 use IO::Select; 15 use IO::Select;
16 use Socket qw/ CRLF /; 16 use Socket qw/ $CRLF /;
17 17
18 BEGIN { use FindBin; chdir($FindBin::Bin); } 18 BEGIN { use FindBin; chdir($FindBin::Bin); }
19 19
20 use lib 'lib'; 20 use lib 'lib';
21 use Test::Nginx; 21 use Test::Nginx;
22 use Test::Nginx::Stream qw/ stream /;
22 23
23 ############################################################################### 24 ###############################################################################
24 25
25 select STDERR; $| = 1; 26 select STDERR; $| = 1;
26 select STDOUT; $| = 1; 27 select STDOUT; $| = 1;
56 $t->try_run('no stream proxy_protocol')->plan(2); 57 $t->try_run('no stream proxy_protocol')->plan(2);
57 $t->waitforsocket('127.0.0.1:8081'); 58 $t->waitforsocket('127.0.0.1:8081');
58 59
59 ############################################################################### 60 ###############################################################################
60 61
61 my %r = stream_get('close'); 62 my $s = stream();
62 is($r{'data'}, "PROXY TCP4 127.0.0.1 127.0.0.1 $r{'sp'} 8080" . CRLF . "close", 63 my $data = $s->io('close');
63 'protocol on'); 64 my $sp = $s->sockport();
65 is($data, "PROXY TCP4 127.0.0.1 127.0.0.1 $sp 8080${CRLF}close", 'protocol on');
64 66
65 %r = stream_get('close', '127.0.0.1:8082'); 67 is(stream('127.0.0.1:8082')->io('close'), 'close', 'protocol off');
66 is($r{'data'}, 'close', 'protocol off');
67
68 ###############################################################################
69
70 sub stream_get {
71 my ($data, $peer) = @_;
72
73 my $s = stream_connect($peer);
74 my $sockport = $s->sockport();
75 stream_write($s, $data);
76
77 $data = '';
78 while (my $buf = stream_read($s)) {
79 $data .= $buf;
80 }
81
82 return ('data' => $data, 'sp' => $sockport);
83 }
84
85 sub stream_connect {
86 my $peer = shift;
87 my $s = IO::Socket::INET->new(
88 Proto => 'tcp',
89 PeerAddr => $peer || '127.0.0.1:8080'
90 )
91 or die "Can't connect to nginx: $!\n";
92
93 return $s;
94 }
95
96 sub stream_write {
97 my ($s, $message) = @_;
98
99 local $SIG{PIPE} = 'IGNORE';
100
101 $s->blocking(0);
102 while (IO::Select->new($s)->can_write(1.5)) {
103 my $n = $s->syswrite($message);
104 last unless $n;
105 $message = substr($message, $n);
106 last unless length $message;
107 }
108
109 if (length $message) {
110 $s->close();
111 }
112 }
113
114 sub stream_read {
115 my ($s) = @_;
116 my ($buf);
117
118 $s->blocking(0);
119 if (IO::Select->new($s)->can_read(5)) {
120 $s->sysread($buf, 1024);
121 };
122
123 log_in($buf);
124 return $buf;
125 }
126 68
127 ############################################################################### 69 ###############################################################################
128 70
129 sub stream_daemon { 71 sub stream_daemon {
130 my $server = IO::Socket::INET->new( 72 my $server = IO::Socket::INET->new(