2424#include < metacall/metacall_loaders.h>
2525
2626#include < atomic>
27- #include < chrono>
28- #include < thread>
2927
3028std::atomic_bool callback_result (false );
3129std::atomic_bool signal_result (false );
@@ -86,17 +84,24 @@ TEST_F(metacall_node_signal_handler_test, DefaultConstructor)
8684
8785 const char buffer[] =
8886 " const { metacall } = require('" METACALL_NODE_PORT_PATH " ');\n "
87+ " let notified = false;\n "
88+ " const notify = () => {\n "
89+ " if (notified === true) return;\n "
90+ " notified = true;\n "
91+ " metacall('c_callback');\n "
92+ " };\n "
8993 " const cp = require('child_process');\n "
9094 " let sp = cp.spawn('ps');\n "
9195 " sp.stdout.on('data', data => {\n "
9296 " console.log('node: stdout: ' + data.toString());\n "
9397 " });\n "
94- " sp.on('exit', (code, signal) => {\n "
95- " console.log(`node: child process exited with code ${code}`);\n "
96- " });\n "
9798 " process.on('SIGCHLD', () => {\n "
9899 " console.log(`node: Received SIGCHLD signal in process`);\n "
99- " metacall('c_callback');\n "
100+ " notify()\n "
101+ " });\n "
102+ " sp.on('exit', code => {\n "
103+ " console.log(`node: child process exited with code ${code}`);\n "
104+ " notify();\n " /* Sometimes exit event gets received before SIGCHLD, so we notify anyway to c_callback */
100105 " });\n " ;
101106
102107 EXPECT_EQ ((int )0 , (int )metacall_load_from_memory (" node" , buffer, sizeof (buffer), NULL ));
@@ -122,11 +127,6 @@ TEST_F(metacall_node_signal_handler_test, DefaultConstructor)
122127 metacall_allocator_destroy (allocator);
123128 }
124129
125- /* Apparently it seems to fail randomly due to a race condition between processes
126- and I do not want to implement a wait mechanism because this is just a PoC.
127- TODO: I am not sure but could this be related to the destroy mechanism of NodeJS? We should review it */
128- std::this_thread::sleep_for (std::chrono::seconds (10 ));
129-
130130 EXPECT_EQ ((int )0 , (int )metacall_destroy ());
131131
132132 EXPECT_EQ ((bool )callback_result.load (), (bool )true );
0 commit comments