diff js_async.t @ 1738:9e0347f4df11

Tests: added js tests for async functions.
author Dmitry Volyntsev <xeioex@nginx.com>
date Mon, 11 Oct 2021 14:34:54 +0000
parents 9d8b100a6ce3
children 18ac4d9e5a2a
line wrap: on
line diff
--- 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', <<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 @@ EOF
         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('/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();