# HG changeset patch # User Dmitry Volyntsev # Date 1633962894 0 # Node ID 9e0347f4df11fed38ecd2d6c96bd60e1e9134f20 # Parent 9fc3b428b18a0a9c677e67a897345966ac3fc6a0 Tests: added js tests for async functions. diff --git a/js_async.t b/js_async.t --- a/js_async.t +++ b/js_async.t @@ -37,6 +37,7 @@ http { js_set $test_async set_timeout; js_set $context_var context_var; + js_set $test_set_rv_var set_rv_var; js_include test.js; @@ -44,6 +45,10 @@ http { listen 127.0.0.1:8080; server_name localhost; + location /njs { + js_content test_njs; + } + location /async_var { return 200 $test_async; } @@ -71,12 +76,24 @@ http { sendfile_max_chunk 5; js_content limit_rate; } + + location /async_content { + js_content async_content; + } + + location /set_rv_var { + return 200 $test_set_rv_var; + } } } EOF $t->write_file('test.js', < {resolve(x)}).then(v => v).then(v => v); + } + + async function async_content(r) { + const a1 = await pr('A'); + const a2 = await pr('B'); + + r.return(200, `retval: \${a1 + a2}`); + } + + async function set_rv_var(r) { + const a1 = await pr(10); + const a2 = await pr(20); + + r.setReturnValue(`retval: \${a1 + a2}`); + } + EOF -$t->try_run('no njs available')->plan(7); +$t->try_run('no njs available')->plan(9); ############################################################################### @@ -171,6 +206,15 @@ like(http_get('/set_timeout_data'), qr/1 like(http_get('/shared_ctx?a=xxx'), qr/H: xxx/, 'shared context'); like(http_get('/limit_rate'), qr/A{50}/, 'limit_rate'); +TODO: { +local $TODO = 'not yet' + unless http_get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.7.0'; + +like(http_get('/async_content'), qr/retval: AB/, 'async content'); +like(http_get('/set_rv_var'), qr/retval: 30/, 'set return value variable'); + +} + http_get('/async_var'); $t->stop(); diff --git a/stream_js.t b/stream_js.t --- a/stream_js.t +++ b/stream_js.t @@ -67,6 +67,7 @@ stream { js_set $js_unk js_unk; js_set $js_req_line js_req_line; js_set $js_sess_unk js_sess_unk; + js_set $js_async js_async; js_include test.js; @@ -184,6 +185,11 @@ stream { js_filter js_filter_except; proxy_pass 127.0.0.1:8090; } + + server { + listen 127.0.0.1:8100; + return $js_async; + } } EOF @@ -356,10 +362,21 @@ EOF s.on('unknown', function() {}); } + function pr(x) { + return new Promise(resolve => {resolve(x)}).then(v => v).then(v => v); + } + + async function js_async(s) { + const a1 = await pr(10); + const a2 = await pr(20); + + s.setReturnValue(`retval: \${a1 + a2}`); + } + EOF $t->run_daemon(\&stream_daemon, port(8090)); -$t->try_run('no stream njs available')->plan(22); +$t->try_run('no stream njs available')->plan(23); $t->waitforsocket('127.0.0.1:' . port(8090)); ############################################################################### @@ -392,6 +409,14 @@ stream('127.0.0.1:' . port(8097))->io('x stream('127.0.0.1:' . port(8098))->io('x'); stream('127.0.0.1:' . port(8099))->io('x'); +TODO: { +local $TODO = 'not yet' + unless get('/njs') =~ /^([.0-9]+)$/m && $1 ge '0.7.0'; + +is(stream('127.0.0.1:' . port(8100))->read(), 'retval: 30', 'js_async'); + +} + $t->stop(); ok(index($t->read_file('error.log'), 'SEE-THIS') > 0, 'stream js log');