comparison stream_limit_conn.t @ 619:58a67c40a761

Tests: stream limit conn tests.
author Andrey Zelenkov <zelenkov@nginx.com>
date Mon, 29 Jun 2015 21:40:30 +0300
parents
children 5048b8f0fedd
comparison
equal deleted inserted replaced
618:2d9f5f439598 619:58a67c40a761
1 #!/usr/bin/perl
2
3 # (C) Andrey Zelenkov
4 # (C) Nginx, Inc.
5
6 # Tests for stream limit_conn module.
7
8 ###############################################################################
9
10 use warnings;
11 use strict;
12
13 use Test::More;
14
15 BEGIN { use FindBin; chdir($FindBin::Bin); }
16
17 use lib 'lib';
18 use Test::Nginx;
19
20 ###############################################################################
21
22 select STDERR; $| = 1;
23 select STDOUT; $| = 1;
24
25 my $t = Test::Nginx->new()->has(qw/http stream stream_limit_conn shmem/)
26 ->write_file_expand('nginx.conf', <<'EOF');
27
28 %%TEST_GLOBALS%%
29
30 daemon off;
31
32 events {
33 }
34
35 stream {
36 limit_conn_zone $binary_remote_addr zone=zone:1m;
37 limit_conn_zone $binary_remote_addr zone=zone2:1m;
38
39 server {
40 listen 127.0.0.1:8080;
41 proxy_pass 127.0.0.1:8084;
42 limit_conn zone 1;
43 }
44
45 server {
46 listen 127.0.0.1:8085;
47 proxy_pass 127.0.0.1:8084;
48 limit_conn zone 5;
49 }
50
51 server {
52 listen 127.0.0.1:8081;
53 proxy_pass 127.0.0.1:8084;
54 limit_conn zone2 1;
55 }
56
57 server {
58 listen 127.0.0.1:8082;
59 proxy_pass 127.0.0.1:8080;
60 limit_conn zone2 1;
61 }
62
63 server {
64 listen 127.0.0.1:8083;
65 proxy_pass 127.0.0.1:8080;
66 limit_conn zone 1;
67 }
68 }
69
70 http {
71 %%TEST_GLOBALS_HTTP%%
72
73 server {
74 listen 127.0.0.1:8084;
75 server_name localhost;
76
77 location / { }
78 }
79 }
80
81 EOF
82
83 $t->write_file('index.html', '');
84 $t->try_run('no stream limit_conn')->plan(8);
85
86 ###############################################################################
87
88 like(get(), qr/200 OK/, 'passed');
89
90 # same and other zones
91
92 my $s = http(<<EOF, start => 1, sleep => 0.2);
93 GET / HTTP/1.0
94 EOF
95
96 ok($s, 'long connection');
97
98 is(get(), undef, 'rejected same zone');
99 like(get('127.0.0.1:8081'), qr/200 OK/, 'passed different zone');
100 like(get('127.0.0.1:8085'), qr/200 OK/, 'passed same zone unlimited');
101
102 ok(http(<<EOF, socket => $s), 'long connection closed');
103 Host: localhost
104
105 EOF
106
107 # zones proxy chain
108
109 like(get('127.0.0.1:8082'), qr/200 OK/, 'passed proxy');
110 is(get('127.0.0.1:8083'), undef, 'rejected proxy');
111
112 ###############################################################################
113
114 sub get {
115 my $peer = shift;
116
117 my $r = http_get('/', socket => getconn($peer));
118 if (!$r) {
119 $r = undef;
120 }
121
122 return $r;
123 }
124
125 sub getconn {
126 my $peer = shift;
127 my $s = IO::Socket::INET->new(
128 Proto => 'tcp',
129 PeerAddr => $peer || '127.0.0.1:8080'
130 )
131 or die "Can't connect to nginx: $!\n";
132
133 return $s;
134 }
135
136 ###############################################################################