comparison stream_proxy_half_close.t @ 1735:8bdf548487f6

Tests: proxy_half_close tests in the stream module.
author Sergey Kandaurov <pluknet@nginx.com>
date Wed, 15 Sep 2021 17:01:40 +0300
parents
children 2a0a6035a1af
comparison
equal deleted inserted replaced
1734:f7170c7acf3a 1735:8bdf548487f6
1 #!/usr/bin/perl
2
3 # (C) Sergey Kandaurov
4 # (C) Nginx, Inc.
5
6 # Tests for stream proxy_half_close directive.
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/);
28
29 $t->write_file_expand('nginx.conf', <<'EOF');
30
31 %%TEST_GLOBALS%%
32
33 daemon off;
34
35 events {
36 }
37
38 stream {
39 %%TEST_GLOBALS_STREAM%%
40
41 server {
42 listen 127.0.0.1:8080;
43 proxy_pass 127.0.0.1:8081;
44
45 proxy_half_close on;
46 }
47 }
48
49 EOF
50
51 $t->try_run('no proxy_half_close')->plan(2);
52
53 ###############################################################################
54
55 my ($s, $u) = pair(8080, 8081);
56 shutdown($u, 1);
57 is(proxy($s, $u, 'SEE'), 'SEE', 'half close upstream');
58
59 ($s, $u) = pair(8080, 8081);
60 shutdown($s, 1);
61 is(proxy($u, $s, 'SEE'), 'SEE', 'half close client');
62
63 ###############################################################################
64
65 sub pair {
66 my ($server, $backend) = @_;
67
68 my $listen = IO::Socket::INET->new(
69 LocalHost => '127.0.0.1:' . port($backend),
70 Listen => 5,
71 Reuse => 1,
72 )
73 or die "Can't listen on $server: $!\n";
74
75 my $connect = IO::Socket::INET->new(
76 Proto => 'tcp',
77 PeerHost => '127.0.0.1:' . port($server),
78 )
79 or die "Can't connect to $server: $!\n";
80
81 my $accept = $listen->accept() if IO::Select->new($listen)->can_read(3);
82
83 return $connect, $accept;
84 }
85
86 sub proxy {
87 my ($from, $to, $msg) = @_;
88 proxy_from($from, $msg);
89 return proxy_to($to);
90 }
91
92 sub proxy_from {
93 my ($s, $msg) = @_;
94
95 local $SIG{PIPE} = 'IGNORE';
96
97 while (IO::Select->new($s)->can_write(5)) {
98 my $n = $s->syswrite($msg);
99 log_out(substr($msg, 0, $n));
100 last unless $n;
101
102 $msg = substr($msg, $n);
103 last unless length $msg;
104 }
105 }
106
107 sub proxy_to {
108 my ($s) = @_;
109 my $buf;
110
111 $s->sysread($buf, 1024) if IO::Select->new($s)->can_read(5);
112
113 log_in($buf);
114 return $buf;
115 }
116
117 ###############################################################################