Mercurial > hg > nginx-tests
comparison stream_unix.t @ 828:68d5a68eb938
Tests: basic stream tests with unix socket.
author | Andrey Zelenkov <zelenkov@nginx.com> |
---|---|
date | Wed, 27 Jan 2016 16:11:00 +0300 |
parents | |
children | e9064d691790 |
comparison
equal
deleted
inserted
replaced
827:df3ab213fb0f | 828:68d5a68eb938 |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Andrey Zelenkov | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Simple tests for stream with unix socket. | |
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 use Test::Nginx::Stream qw/ stream /; | |
20 | |
21 ############################################################################### | |
22 | |
23 select STDERR; $| = 1; | |
24 select STDOUT; $| = 1; | |
25 | |
26 eval { require IO::Socket::UNIX; }; | |
27 plan(skip_all => 'IO::Socket::UNIX not installed') if $@; | |
28 | |
29 my $t = Test::Nginx->new()->has(qw/stream unix/)->plan(2); | |
30 | |
31 $t->write_file_expand('nginx.conf', <<'EOF'); | |
32 | |
33 %%TEST_GLOBALS%% | |
34 | |
35 daemon off; | |
36 | |
37 events { | |
38 } | |
39 | |
40 stream { | |
41 upstream u { | |
42 server unix:%%TESTDIR%%/unix.sock; | |
43 } | |
44 | |
45 server { | |
46 listen 127.0.0.1:8080; | |
47 proxy_pass unix:%%TESTDIR%%/unix.sock; | |
48 } | |
49 | |
50 server { | |
51 listen 127.0.0.1:8081; | |
52 proxy_pass u; | |
53 } | |
54 } | |
55 | |
56 EOF | |
57 | |
58 my $path = $t->testdir() . '/unix.sock'; | |
59 | |
60 $t->run_daemon(\&stream_daemon, $path); | |
61 $t->run(); | |
62 | |
63 # wait for unix socket to appear | |
64 | |
65 for (1 .. 50) { | |
66 last if -S $path; | |
67 select undef, undef, undef, 0.1; | |
68 } | |
69 | |
70 ############################################################################### | |
71 | |
72 my $str = 'SEE-THIS'; | |
73 | |
74 is(stream('127.0.0.1:8080')->io($str), $str, 'proxy'); | |
75 is(stream('127.0.0.1:8081')->io($str), $str, 'upstream'); | |
76 | |
77 ############################################################################### | |
78 | |
79 sub stream_daemon { | |
80 my $server = IO::Socket::UNIX->new( | |
81 Proto => 'tcp', | |
82 Local => shift, | |
83 Listen => 5, | |
84 Reuse => 1 | |
85 ) | |
86 or die "Can't create listening socket: $!\n"; | |
87 | |
88 local $SIG{PIPE} = 'IGNORE'; | |
89 | |
90 while (my $client = $server->accept()) { | |
91 $client->autoflush(1); | |
92 | |
93 log2c("(new connection $client)"); | |
94 | |
95 $client->sysread(my $buffer, 65536) or next; | |
96 | |
97 log2i("$client $buffer"); | |
98 | |
99 log2o("$client $buffer"); | |
100 | |
101 $client->syswrite($buffer); | |
102 | |
103 close $client; | |
104 } | |
105 } | |
106 | |
107 sub log2i { Test::Nginx::log_core('|| <<', @_); } | |
108 sub log2o { Test::Nginx::log_core('|| >>', @_); } | |
109 sub log2c { Test::Nginx::log_core('||', @_); } | |
110 | |
111 ############################################################################### |