Welcome! Log In Create A New Profile

Advanced

[njs] Tests: added promise support for unit tests subrequest method.

Dmitry Volyntsev
December 03, 2021 08:58AM
details: https://hg.nginx.org/njs/rev/cd87a113829d
branches:
changeset: 1757:cd87a113829d
user: Dmitry Volyntsev <xeioex@nginx.com>
date: Fri Dec 03 13:55:12 2021 +0000
description:
Tests: added promise support for unit tests subrequest method.

diffstat:

src/test/njs_externals_test.c | 46 +++++++++++++++++++++++++++++++++---------
src/test/njs_externals_test.h | 1 +
src/test/njs_unit_test.c | 17 ++++++++++++---
3 files changed, 50 insertions(+), 14 deletions(-)

diffs (134 lines):

diff -r b27244641dbf -r cd87a113829d src/test/njs_externals_test.c
--- a/src/test/njs_externals_test.c Fri Dec 03 13:55:11 2021 +0000
+++ b/src/test/njs_externals_test.c Fri Dec 03 13:55:12 2021 +0000
@@ -378,9 +378,27 @@ njs_unit_test_r_method(njs_vm_t *vm, njs


static njs_int_t
+njs_unit_test_promise_trampoline(njs_vm_t *vm, njs_value_t *args,
+ njs_uint_t nargs, njs_index_t unused)
+{
+ njs_function_t *callback;
+
+ callback = njs_value_function(njs_argument(args, 1));
+
+ if (callback != NULL) {
+ return njs_vm_call(vm, callback, njs_argument(args, 2), 1);
+ }
+
+ return NJS_OK;
+}
+
+
+static njs_int_t
njs_unit_test_r_subrequest(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t unused)
{
+ njs_int_t ret;
+ njs_value_t retval, *argument, *select;
njs_vm_event_t vm_event;
njs_function_t *callback;
njs_external_ev_t *ev;
@@ -393,9 +411,19 @@ njs_unit_test_r_subrequest(njs_vm_t *vm,
return NJS_ERROR;
}

- callback = njs_value_function(njs_arg(args, nargs, 1));
+ ev = njs_mp_alloc(vm->mem_pool, sizeof(njs_external_ev_t));
+ if (ev == NULL) {
+ njs_memory_error(vm);
+ return NJS_ERROR;
+ }
+
+ ret = njs_vm_promise_create(vm, &retval, &ev->callbacks[0]);
+ if (ret != NJS_OK) {
+ return NJS_ERROR;
+ }
+
+ callback = njs_vm_function_alloc(vm, njs_unit_test_promise_trampoline);
if (callback == NULL) {
- njs_type_error(vm, "argument is not callable");
return NJS_ERROR;
}

@@ -405,22 +433,20 @@ njs_unit_test_r_subrequest(njs_vm_t *vm,
return NJS_ERROR;
}

- ev = njs_mp_alloc(vm->mem_pool, sizeof(njs_external_ev_t));
- if (ev == NULL) {
- njs_memory_error(vm);
- return NJS_ERROR;
- }
+ argument = njs_arg(args, nargs, 1);
+ select = njs_arg(args, nargs, 2);

ev->vm_event = vm_event;
ev->data = r;
- ev->nargs = 1;
- njs_value_assign(&ev->args[0], njs_argument(args, 0));
+ ev->nargs = 2;
+ njs_value_assign(&ev->args[0], &ev->callbacks[!!njs_bool(select)]);
+ njs_value_assign(&ev->args[1], argument);

env = vm->external;

njs_queue_insert_tail(&env->events, &ev->link);

- njs_set_undefined(&vm->retval);
+ njs_vm_retval_set(vm, njs_value_arg(&retval));

return NJS_OK;
}
diff -r b27244641dbf -r cd87a113829d src/test/njs_externals_test.h
--- a/src/test/njs_externals_test.h Fri Dec 03 13:55:11 2021 +0000
+++ b/src/test/njs_externals_test.h Fri Dec 03 13:55:12 2021 +0000
@@ -19,6 +19,7 @@ typedef struct {
void *data;
njs_uint_t nargs;
njs_value_t args[3];
+ njs_value_t callbacks[2];
njs_queue_link_t link;
} njs_external_ev_t;

diff -r b27244641dbf -r cd87a113829d src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c Fri Dec 03 13:55:11 2021 +0000
+++ b/src/test/njs_unit_test.c Fri Dec 03 13:55:12 2021 +0000
@@ -21015,8 +21015,15 @@ static njs_unit_test_t njs_externals_te

{ njs_str("let obj = { a: 1, b: 2};"
"function cb(r) { r.retval(obj.a); }"
- "$r.subrequest(reply => cb(reply))"),
- njs_str("1") },
+ "$r.subrequest($r)"
+ ".then(reply => cb(reply))"),
+ njs_str("1") },
+
+ { njs_str("let obj = { a: 1, b: 2};"
+ "function cb(r, select) { r.retval(obj[select]); }"
+ "$r.subrequest('b')"
+ ".then(select => cb($r, select))"),
+ njs_str("2") },
};


@@ -21025,7 +21032,8 @@ static njs_unit_test_t njs_async_handle
{ njs_str("globalThis.main = (function() {"
" function cb(r) { r.retval(1); }"
" function handler(r) {"
- " r.subrequest(reply => cb(reply));"
+ " r.subrequest(r)"
+ " .then(reply => cb(reply))"
" };"
" return {handler};"
"})();"
@@ -21036,7 +21044,8 @@ static njs_unit_test_t njs_async_handle
" let obj = { a: 1, b: 2};"
" function cb(r) { r.retval(obj.a); }"
" function handler(r) {"
- " r.subrequest(reply => cb(reply));"
+ " r.subrequest(r)"
+ " .then(reply => cb(reply))"
" };"
" return {handler};"
"})();"
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[njs] Tests: added promise support for unit tests subrequest method.

Dmitry Volyntsev 224 December 03, 2021 08:58AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 54
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready