From 40118a188fb05219d1188ff775ce71f66c8cb56a Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Wed, 6 Mar 2024 20:07:41 -0300 Subject: src/hero.mjs: Add simplified implementation of `makePipeReader()` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There are a few limitations of the current approach that I didn't figure out how to solve yet: 1. when stopping the server, mainly for shutting down the Node.js process when running the tests, we need to open and close the pipe in write mode at least once, otherwise it hangs forever. After opening and closing it, the "autoClose" functionality of the `fs.createReadStream()` kicks in, and it closes, otherwise the test hangs forever as Node.js still has open handles. That's why the `server.close()` function has a gratuitous call to `fs.createWriteStream(pipe).end()`: so that when stopping the server, the pipe is always closed at least once; 2. when actually running the application, if one tries an IPC command like: $ echo '{ "action": "config-dump" }' > web.pipe It works, but only for the first time. As this shell line opened the pipe, wrote to it and then closed it, the reader that `makePipeReader()` constructed closed, but didn't open it again. So when running the exact same IPC command for a second time, the shell hangs, as there is no process on the other side listening for the command and consuming the pipe. Shortcoming nº 2 is much more important to be solved, as we can live with shortcoming nª 1 for some time. Both are to be fixed in the near future. --- tests/js/hero.mjs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'tests/js') diff --git a/tests/js/hero.mjs b/tests/js/hero.mjs index 67cf690..14d9a07 100644 --- a/tests/js/hero.mjs +++ b/tests/js/hero.mjs @@ -1378,6 +1378,12 @@ const test_makePipeReaderFn = async t => { const lines = []; const lineFn = x => lines.push(x); const makePipeReader = makePipeReaderFn({ lineFn: null }); + + rmIf(path); + makePipeReader(path); + fs.createWriteStream(path).end(); + + assert.deepEqual(lines, []); }); await t.test("closing on pipe EOF reopens", async () => { @@ -1385,6 +1391,21 @@ const test_makePipeReaderFn = async t => { const lines = []; const lineFn = x => lines.push(x); const makePipeReader = makePipeReaderFn({ lineFn }); + + rmIf(path); + makePipeReader(path); + const writer = fs.createWriteStream(path); + writer.end("first\nsecond\nthird\n"); + return new Promise((resolve, reject) => { + writer.close(() => { + assert.deepEqual(lines, [ + "first", + "second", + "third", + ]); + resolve(); + }); + }); }); }; -- cgit v1.2.3