From 9cba4108ba24bbb2f3c0dd25ec68ba438faf1023 Mon Sep 17 00:00:00 2001 From: Yonle Date: Sun, 12 May 2024 10:10:56 +0700 Subject: [PATCH] cli: add testworker Signed-off-by: Yonle --- bostr_cli.js | 24 +++++++++++++++++++----- testworker.js | 25 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 testworker.js diff --git a/bostr_cli.js b/bostr_cli.js index 92fc24b..69ebf45 100755 --- a/bostr_cli.js +++ b/bostr_cli.js @@ -12,11 +12,12 @@ function showHelp() { console.log( "Usage: bostr [command] (argv)\n" + "Available command:\n" + - " makeconf [conffile] - Make config file\n" + - " checkconf [conffile] - Check config file\n" + - " start [conffile] - Run bostr with specified config\n" + - " hexconverter [nip19] - Convert NIP-19 string to hex\n" + - " help - Show this help text\n\n" + + " makeconf [conffile] - Make config file\n" + + " checkconf [conffile] - Check config file\n" + + " start [conffile] - Run bostr with specified config\n" + + " hexconverter [nip19] - Convert NIP-19 string to hex\n" + + " testworker [conffile] - Test worker with specified bostr config\n" + + " help - Show this help text\n\n" + "Software is licensed under BSD-3-Clause\n" + "https://github.com/Yonle/bostr" ); @@ -71,6 +72,19 @@ switch (argv[0]) { process.env.BOSTR_CONFIG_PATH = readPath(argv[1]); require("./index.js"); break; + case "testworker": + if (!argv[1]) return console.log("Usage: bostr testworker [conffile]"); + if (!fs.existsSync(argv[1])) { + console.error("Config not exists."); + return process.exit(254); + } + + process.env.BOSTR_CONFIG_PATH = readPath(argv[1]); + + console.log("Press CTRL + C to stop the test."); + console.log("Using config:", process.env.BOSTR_CONFIG_PATH); + require("./testworker.js"); + break; case "hexconverter": if (!argv[1]) return console.log("Usage: bostr hexconverter [npub|nsec|....] ...."); for (const i of argv.slice(1)) { diff --git a/testworker.js b/testworker.js new file mode 100644 index 0000000..6ca5888 --- /dev/null +++ b/testworker.js @@ -0,0 +1,25 @@ +const { Worker } = require("worker_threads"); +const worker = new Worker(__dirname + "/worker_bouncer.js", { name: "Bostr (worker)" }); + +let id = null; + +worker.on('message', msg => { + console.log(msg); + if (!id && msg.id) { + id = msg.id; + + worker.postMessage({ + type: "req", + id, + sid: "testing", + filters: [{ kinds: [1] }] + }); + } +}); + +worker.on("online", _ => { + worker.postMessage({ + type: "getsess", + data: { ip: "[TestWorker]", ident: "TestWorker" } + }) +});