Mercurial > hg > nginx-tests
comparison stream_proxy.t @ 816:77359b849cd5
Tests: stream package.
author | Andrey Zelenkov <zelenkov@nginx.com> |
---|---|
date | Mon, 20 Jul 2015 15:06:09 +0300 |
parents | 122cd3a82367 |
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; |
47 $t->run_daemon(\&stream_daemon); | 48 $t->run_daemon(\&stream_daemon); |
48 $t->run()->waitforsocket('127.0.0.1:8081'); | 49 $t->run()->waitforsocket('127.0.0.1:8081'); |
49 | 50 |
50 ############################################################################### | 51 ############################################################################### |
51 | 52 |
52 my $s = stream_connect(); | 53 my $s = stream(); |
53 | 54 |
54 stream_write($s, 'foo1'); | 55 is($s->io('foo1', length => 4), 'bar1', 'proxy connection'); |
55 is(stream_read($s), 'bar1', 'proxy connection'); | 56 is($s->io('foo3', length => 4), 'bar3', 'proxy connection again'); |
57 is($s->io('close'), 'close', 'proxy connection close'); | |
58 is($s->io('test'), '', 'proxy connection closed'); | |
56 | 59 |
57 stream_write($s, 'foo3'); | 60 $s = stream(); |
58 is(stream_read($s), 'bar3', 'proxy connection again'); | |
59 | |
60 stream_write($s, 'close'); | |
61 is(stream_read($s), 'close', 'proxy connection close'); | |
62 | |
63 stream_write($s, 'test'); | |
64 is(stream_read($s), '', 'proxy connection closed'); | |
65 | |
66 $s = stream_connect(); | |
67 | 61 |
68 sleep 3; | 62 sleep 3; |
69 | 63 |
70 stream_write($s, 'foo'); | 64 is($s->io('foo', length => 3), 'bar', 'proxy connect timeout'); |
71 is(stream_read($s), 'bar', 'proxy connect timeout'); | |
72 | |
73 ############################################################################### | |
74 | |
75 sub stream_connect { | |
76 my $peer = shift; | |
77 my $s = IO::Socket::INET->new( | |
78 Proto => 'tcp', | |
79 PeerAddr => $peer || '127.0.0.1:8080' | |
80 ) | |
81 or die "Can't connect to nginx: $!\n"; | |
82 | |
83 return $s; | |
84 } | |
85 | |
86 sub stream_write { | |
87 my ($s, $message) = @_; | |
88 | |
89 local $SIG{PIPE} = 'IGNORE'; | |
90 | |
91 $s->blocking(0); | |
92 while (IO::Select->new($s)->can_write(1.5)) { | |
93 my $n = $s->syswrite($message); | |
94 last unless $n; | |
95 $message = substr($message, $n); | |
96 last unless length $message; | |
97 } | |
98 | |
99 if (length $message) { | |
100 $s->close(); | |
101 } | |
102 } | |
103 | |
104 sub stream_read { | |
105 my ($s) = @_; | |
106 my ($buf); | |
107 | |
108 $s->blocking(0); | |
109 if (IO::Select->new($s)->can_read(5)) { | |
110 $s->sysread($buf, 1024); | |
111 }; | |
112 | |
113 log_in($buf); | |
114 return $buf; | |
115 } | |
116 | 65 |
117 ############################################################################### | 66 ############################################################################### |
118 | 67 |
119 sub stream_daemon { | 68 sub stream_daemon { |
120 my $server = IO::Socket::INET->new( | 69 my $server = IO::Socket::INET->new( |