comparison stream_proxy.t @ 555:ba6cc90e3d67

Tests: basic stream proxy tests.
author Sergey Kandaurov <pluknet@nginx.com>
date Wed, 22 Apr 2015 16:06:44 +0300
parents
children 824754da4afc
comparison
equal deleted inserted replaced
554:4cc65166509e 555:ba6cc90e3d67
1 #!/usr/bin/perl
2
3 # (C) Sergey Kandaurov
4 # (C) Nginx, Inc.
5
6 # Tests for stream proxy module.
7
8 ###############################################################################
9
10 use warnings;
11 use strict;
12
13 use Test::More;
14
15 use IO::Select;
16
17 BEGIN { use FindBin; chdir($FindBin::Bin); }
18
19 use lib 'lib';
20 use Test::Nginx;
21
22 ###############################################################################
23
24 select STDERR; $| = 1;
25 select STDOUT; $| = 1;
26
27 my $t = Test::Nginx->new()->has(qw/stream/)->plan(4)
28 ->write_file_expand('nginx.conf', <<'EOF');
29
30 %%TEST_GLOBALS%%
31
32 daemon off;
33
34 events {
35 }
36
37 stream {
38 server {
39 listen 127.0.0.1:8080;
40 proxy_pass 127.0.0.1:8081;
41 proxy_connect_timeout 1s;
42 }
43 }
44
45 EOF
46
47 $t->run_daemon(\&stream_daemon);
48 $t->run()->waitforsocket('127.0.0.1:8081');
49
50 ###############################################################################
51
52 my $s = stream_connect();
53
54 stream_write($s, 'foo1');
55 is(stream_read($s), 'bar1', 'proxy connection');
56
57 stream_write($s, 'foo3');
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 ###############################################################################
67
68 sub stream_connect {
69 my $peer = shift;
70 my $s = IO::Socket::INET->new(
71 Proto => 'tcp',
72 PeerAddr => $peer || '127.0.0.1:8080'
73 )
74 or die "Can't connect to nginx: $!\n";
75
76 return $s;
77 }
78
79 sub stream_write {
80 my ($s, $message) = @_;
81
82 local $SIG{PIPE} = 'IGNORE';
83
84 $s->blocking(0);
85 while (IO::Select->new($s)->can_write(1.5)) {
86 my $n = $s->syswrite($message);
87 last unless $n;
88 $message = substr($message, $n);
89 last unless length $message;
90 }
91
92 if (length $message) {
93 $s->close();
94 }
95 }
96
97 sub stream_read {
98 my ($s) = @_;
99 my ($buf);
100
101 $s->blocking(0);
102 if (IO::Select->new($s)->can_read(3)) {
103 $s->sysread($buf, 1024);
104 };
105
106 log_in($buf);
107 return $buf;
108 }
109
110 ###############################################################################
111
112 sub stream_daemon {
113 my $server = IO::Socket::INET->new(
114 Proto => 'tcp',
115 LocalAddr => '127.0.0.1:8081',
116 Listen => 5,
117 Reuse => 1
118 )
119 or die "Can't create listening socket: $!\n";
120
121 my $sel = IO::Select->new($server);
122
123 local $SIG{PIPE} = 'IGNORE';
124
125 while (my @ready = $sel->can_read) {
126 foreach my $fh (@ready) {
127 if ($server == $fh) {
128 my $new = $fh->accept;
129 $new->autoflush(1);
130 $sel->add($new);
131
132 } elsif (stream_handle_client($fh)) {
133 $sel->remove($fh);
134 $fh->close;
135 }
136 }
137 }
138 }
139
140 sub stream_handle_client {
141 my ($client) = @_;
142
143 log2c("(new connection $client)");
144
145 $client->sysread(my $buffer, 65536) or return 1;
146
147 log2i("$client $buffer");
148
149 $buffer =~ s/foo/bar/g;
150
151 log2o("$client $buffer");
152
153 $client->syswrite($buffer);
154
155 return $buffer =~ /close/;
156 }
157
158 sub log2i { Test::Nginx::log_core('|| <<', @_); }
159 sub log2o { Test::Nginx::log_core('|| >>', @_); }
160 sub log2c { Test::Nginx::log_core('||', @_); }
161
162 ###############################################################################