From: Ammar Faizi <[email protected]>
To: Alviro Iskandar Setiawan <[email protected]>
Cc: Ammar Faizi <[email protected]>,
Muhammad Rizki <[email protected]>,
Kanna Scarlet <[email protected]>,
GNU/Weeb Mailing List <[email protected]>
Subject: [PATCH v1 16/22] tests/js/ring: Add JavaScript class wrapper example
Date: Sun, 21 Aug 2022 18:24:47 +0700 [thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
Just an example writing a wrapper to simplify the SQ/CQ mechanism.
Signed-off-by: Ammar Faizi <[email protected]>
---
tests/js/ring.js | 89 ++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 89 insertions(+)
diff --git a/tests/js/ring.js b/tests/js/ring.js
index 6752fe1..372c6e6 100644
--- a/tests/js/ring.js
+++ b/tests/js/ring.js
@@ -164,6 +164,94 @@ function test_simple_http_post_set_header()
assert(h.ch.read_ret() === ss.length);
}
+class CHNet {
+ constructor(cr, url, method) {
+ this.ch = chnet.create_net();
+ this.ch.set_url(url);
+ this.ch.set_method(method);
+ this.cr = cr;
+ }
+
+ prep_read(read_size) {
+ let ring = this.cr.ring;
+ let sqe = ring.get_sqe();
+ if (!sqe)
+ return false;
+ sqe.prep_read(this.ch, read_size);
+ sqe.set_user_data(this);
+ return true;
+ }
+
+ set_user_data(data) {
+ this.udata = data;
+ }
+
+ get_user_data() {
+ return this.udata;
+ }
+
+ set_header(key, val, overwrite = true) {
+ this.ch.set_request_header(key, val, overwrite);
+ }
+};
+
+class ChromiumNet {
+ constructor() {
+ this.ring = chnet.create_ring(4096);
+ }
+
+ create(url, method = "GET") {
+ return new CHNet(this, url, method);
+ }
+
+ for_each_cqe(cb = null) {
+ let ret = this.ring.for_each_cqe(function (cqe) {
+
+ if (cb)
+ cb(cqe);
+
+ let ch = cqe.user_data;
+ let ret = cqe.res;
+ let udata = ch.udata;
+
+ if (ret < 0 && ch.onerror)
+ ch.onerror(ch, ret, udata);
+ else
+ ch.onreadcomplete(ch, ret, udata);
+ });
+ this.ring.cq_advance(ret);
+ return ret;
+ }
+};
+
+function test_classes()
+{
+ const UA = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36";
+ const NR_REQ = 10;
+
+ let cr = new ChromiumNet;
+ let ch_arr = [];
+ let i, j, ch;
+
+ for (i = 0; i < NR_REQ; i++) {
+ ch = cr.create("http://127.0.0.1:8000/index.php?action=hello");
+ ch.set_header("User-Agent", UA);
+ ch.prep_read(4096);
+ ch.set_user_data(i);
+ ch.onerror = function (cr, ret, udata) {
+ console.log(`Req: ${udata} returned error: ${cr.ch.get_error()}`);
+ };
+ ch.onreadcomplete = function (cr, ret, udata) {
+ console.log(`Req: ${udata} returned ${ret} bytes: ${cr.ch.read_buf()}`);
+ };
+ ch_arr[i] = ch;
+ }
+
+ cr.ring.submit_sqe();
+ cr.ring.wait_cqe(1);
+ cr.for_each_cqe();
+}
+
function main()
{
test_nop();
@@ -171,6 +259,7 @@ function main()
test_simple_http();
test_simple_http_set_header();
test_simple_http_post_set_header();
+ test_classes();
}
main();
--
Ammar Faizi
next prev parent reply other threads:[~2022-08-21 11:25 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-21 11:24 [PATCH v1 00/22] ncns updates Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 01/22] chnet: Add initial request body support Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 02/22] chnet: node: Add set_user_data support on SQE Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 03/22] tests/js/ring: Update the unit test to utilize set_user_data Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 04/22] binding.gyp: Add `-ggdb3` flag for better debugging experience Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 05/22] binding.gyp: Add `-Wno-enum-constexpr-conversion` flag Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 06/22] chnet: node: Add set_method function to set HTTP method Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 07/22] chnet: node: Add get_error function to return the error string Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 08/22] chnet: node: Add set_payload function to set HTTP req body Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 09/22] tests/js/ring: Add simple HTTP POST request example in NodeJS Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 10/22] chnet: Split construct URL req creation into a new function Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 11/22] chnet: Add set request header support Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 12/22] chnet: node: Fix unused variable warning Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 13/22] chnet: node: Add set request header function in NodeJS Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 14/22] tests/js/ring: Add more set header function test Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 15/22] chnet: node: Don't use static counter for data ID Ammar Faizi
2022-08-21 11:24 ` Ammar Faizi [this message]
2022-08-21 11:24 ` [PATCH v1 17/22] chnet: Initial chunked request body support Ammar Faizi
2022-08-21 22:13 ` Alviro Iskandar Setiawan
2022-08-22 3:08 ` Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 18/22] chnet: Rework the chunked request body interface Ammar Faizi
2022-08-21 22:20 ` Alviro Iskandar Setiawan
2022-08-21 11:24 ` [PATCH v1 19/22] chnet: ring: Refactor the ring completely Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 20/22] chnet: Use busy-waiting for signal waiter Ammar Faizi
2022-08-21 22:29 ` Alviro Iskandar Setiawan
2022-08-21 11:24 ` [PATCH v1 21/22] chnet: ring: Bump max_entry to 2G Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 22/22] tests/cpp: Delete basic.cpp as it's no longer relevant Ammar Faizi
2022-08-21 22:21 ` Alviro Iskandar Setiawan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox