details:   https://hg.nginx.org/njs/rev/e4105b65d105
branches:  
changeset: 2234:e4105b65d105
user:      Dmitry Volyntsev <xei...@nginx.com>
date:      Wed Nov 15 15:08:18 2023 -0800
description:
Tests: added console test for stream module.

diffstat:

 nginx/t/stream_js_console.t |  142 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 142 insertions(+), 0 deletions(-)

diffs (146 lines):

diff -r 0213cb43bfce -r e4105b65d105 nginx/t/stream_js_console.t
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/nginx/t/stream_js_console.t       Wed Nov 15 15:08:18 2023 -0800
@@ -0,0 +1,142 @@
+#!/usr/bin/perl
+
+# (C) Dmitry Volyntsev
+# (C) Nginx, Inc.
+
+# Tests for stream njs module, console object.
+
+###############################################################################
+
+use warnings;
+use strict;
+
+use Test::More;
+use Socket qw/ CRLF /;
+
+BEGIN { use FindBin; chdir($FindBin::Bin); }
+
+use lib 'lib';
+use Test::Nginx;
+use Test::Nginx::Stream qw/ stream /;
+
+###############################################################################
+
+select STDERR; $| = 1;
+select STDOUT; $| = 1;
+
+my $t = Test::Nginx->new()->has(qw/stream/)
+       ->write_file_expand('nginx.conf', <<'EOF');
+
+%%TEST_GLOBALS%%
+
+daemon off;
+
+events {
+}
+
+stream {
+    %%TEST_GLOBALS_STREAM%%
+
+    js_import test.js;
+
+
+    server {
+        listen       127.0.0.1:8080;
+
+        js_preread test.log;
+
+        proxy_pass 127.0.0.1:8090;
+    }
+
+    server {
+        listen       127.0.0.1:8081;
+
+        js_preread test.timer;
+
+        proxy_pass 127.0.0.1:8090;
+    }
+}
+
+EOF
+
+$t->write_file('test.js', <<EOF);
+    function log(s) {
+        s.on('upload', function (data) {
+            if (data.length > 0) {
+                s.off('upload');
+                data = Buffer.from(data, 'base64');
+                const object = JSON.parse(data);
+                console.log(object);
+                s.allow();
+            }
+        });
+    }
+
+    function timer(s) {
+        s.on('upload', function (data) {
+            if (data.length > 0) {
+                s.off('upload');
+                console.time('foo');
+                setTimeout(function() {
+                    console.timeEnd('foo');
+                    s.allow();
+                }, 7);
+            }
+        });
+    }
+
+    export default { log, timer };
+EOF
+
+$t->run_daemon(\&stream_daemon, port(8090));
+$t->try_run('no njs console')->plan(4);
+$t->waitforsocket('127.0.0.1:' . port(8090));
+
+###############################################################################
+
+is(stream('127.0.0.1:' . port(8080))->io('eyJhIjpbIkIiLCJDIl19'),
+       'eyJhIjpbIkIiLCJDIl19', 'log test');
+is(stream('127.0.0.1:' . port(8081))->io('timer'), 'timer', 'timer test');
+
+$t->stop();
+
+like($t->read_file('error.log'), qr/\[info\].*js: \{a:\['B','C'\]\}/,
+       'console.log with object');
+like($t->read_file('error.log'), qr/\[info\].*js: foo: \d+\.\d\d\d\d\d\dms/,
+       'console.time foo');
+
+###############################################################################
+
+sub stream_daemon {
+       my $server = IO::Socket::INET->new(
+               Proto => 'tcp',
+               LocalAddr => '127.0.0.1:' . port(8090),
+               Listen => 5,
+               Reuse => 1
+       )
+               or die "Can't create listening socket: $!\n";
+
+       local $SIG{PIPE} = 'IGNORE';
+
+       while (my $client = $server->accept()) {
+               $client->autoflush(1);
+
+               log2c("(new connection $client)");
+
+               $client->sysread(my $buffer, 65536) or next;
+
+               log2i("$client $buffer");
+
+               log2o("$client $buffer");
+
+               $client->syswrite($buffer);
+
+               close $client;
+       }
+}
+
+sub log2i { Test::Nginx::log_core('|| <<', @_); }
+sub log2o { Test::Nginx::log_core('|| >>', @_); }
+sub log2c { Test::Nginx::log_core('||', @_); }
+
+###############################################################################
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
https://mailman.nginx.org/mailman/listinfo/nginx-devel

Reply via email to