Tests: added js tests for async functions.
diff --git a/js_async.t b/js_async.t
index 49169e2..3c817ea 100644
--- a/js_async.t
+++ b/js_async.t
@@ -37,6 +37,7 @@
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 @@
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 @@
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', <<EOF);
+ function test_njs(r) {
+ r.return(200, njs.version);
+ }
+
function set_timeout(r) {
var timerId = setTimeout(timeout_cb_r, 5, r, 0);
clearTimeout(timerId);
@@ -159,9 +176,27 @@
setTimeout(limit_rate_cb, 1000, r);
}
+ function pr(x) {
+ return new Promise(resolve => {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('/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
index 3f40939..2b22381 100644
--- a/stream_js.t
+++ b/stream_js.t
@@ -67,6 +67,7 @@
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 @@
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 @@
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(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');