comparison stream_js_exit.t @ 1661:69d71a15d407

Tests: added exit hook tests for js stream.
author Dmitry Volyntsev <xeioex@nginx.com>
date Sat, 06 Mar 2021 11:09:54 +0000
parents
children 7f1579e4372a
comparison
equal deleted inserted replaced
1660:068c30e9d2c6 1661:69d71a15d407
1 #!/usr/bin/perl
2
3 # (C) Dmitry Volyntsev
4 # (C) Nginx, Inc.
5
6 # Tests for stream njs module, exit hook.
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 my $t = Test::Nginx->new()->has(qw/http stream/)
27 ->write_file_expand('nginx.conf', <<'EOF');
28
29 %%TEST_GLOBALS%%
30
31 daemon off;
32
33 events {
34 }
35
36 http {
37 %%TEST_GLOBALS_HTTP%%
38
39 js_import test.js;
40
41 server {
42 listen 127.0.0.1:8080;
43 server_name localhost;
44
45 location /njs {
46 js_content test.njs;
47 }
48 }
49 }
50
51 stream {
52 %%TEST_GLOBALS_STREAM%%
53
54 js_import test.js;
55
56 server {
57 listen 127.0.0.1:8081;
58 js_access test.access;
59 js_filter test.filter;
60 proxy_pass 127.0.0.1:8090;
61 }
62
63 server {
64 listen 127.0.0.1:8082;
65 js_access test.access;
66 proxy_pass 127.0.0.1:1;
67 }
68 }
69
70 EOF
71
72 my $p = port(8080);
73
74 $t->write_file('test.js', <<EOF);
75 function test_njs(r) {
76 r.return(200, njs.version);
77 }
78
79 function access(s) {
80 njs.on('exit', () => {
81 var v = s.variables;
82 var c = `\${v.bytes_received}/\${v.bytes_sent}`;
83 var u = `\${v.upstream_bytes_received}/\${v.upstream_bytes_sent}`;
84 s.error(`s:\${s.status} C: \${c} U: \${u}`);
85 });
86
87 s.allow();
88 }
89
90 function filter(s) {
91 s.on('upload', (data, flags) => {
92 s.send(`@\${data}`, flags);
93 });
94
95 s.on('download', (data, flags) => {
96 s.send(data.slice(2), flags);
97 });
98 }
99
100 export default {njs: test_njs, access, filter};
101 EOF
102
103 $t->try_run('no stream njs available')->plan(2);
104
105 $t->run_daemon(\&stream_daemon, port(8090));
106 $t->waitforsocket('127.0.0.1:' . port(8090));
107
108 ###############################################################################
109
110 local $TODO = 'not yet'
111 unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.5.2';
112
113 stream('127.0.0.1:' . port(8081))->io('###');
114 stream('127.0.0.1:' . port(8082))->io('###');
115
116 $t->stop();
117
118 ok(index($t->read_file('error.log'), 's:200 C: 3/6 U: 8/4') > 0, 'normal');
119 ok(index($t->read_file('error.log'), 's:502 C: 0/0 U: 0/0') > 0, 'failed conn');
120
121 ###############################################################################
122
123 sub stream_daemon {
124 my $server = IO::Socket::INET->new(
125 Proto => 'tcp',
126 LocalAddr => '127.0.0.1:' . port(8090),
127 Listen => 5,
128 Reuse => 1
129 )
130 or die "Can't create listening socket: $!\n";
131
132 local $SIG{PIPE} = 'IGNORE';
133
134 while (my $client = $server->accept()) {
135 $client->autoflush(1);
136
137 log2c("(new connection $client)");
138
139 $client->sysread(my $buffer, 65536) or next;
140
141 log2i("$client $buffer");
142
143 log2o("$client $buffer");
144
145 $client->syswrite($buffer);
146 $client->syswrite($buffer);
147
148 close $client;
149 }
150 }
151
152 sub log2i { Test::Nginx::log_core('|| <<', @_); }
153 sub log2o { Test::Nginx::log_core('|| >>', @_); }
154 sub log2c { Test::Nginx::log_core('||', @_); }
155
156 ###############################################################################