24
24
#include < metacall/metacall_loaders.h>
25
25
26
26
#include < atomic>
27
- #include < chrono>
28
- #include < thread>
29
27
30
28
std::atomic_bool callback_result (false );
31
29
std::atomic_bool signal_result (false );
@@ -86,17 +84,24 @@ TEST_F(metacall_node_signal_handler_test, DefaultConstructor)
86
84
87
85
const char buffer[] =
88
86
" 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 "
89
93
" const cp = require('child_process');\n "
90
94
" let sp = cp.spawn('ps');\n "
91
95
" sp.stdout.on('data', data => {\n "
92
96
" console.log('node: stdout: ' + data.toString());\n "
93
97
" });\n "
94
- " sp.on('exit', (code, signal) => {\n "
95
- " console.log(`node: child process exited with code ${code}`);\n "
96
- " });\n "
97
98
" process.on('SIGCHLD', () => {\n "
98
99
" 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 */
100
105
" });\n " ;
101
106
102
107
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)
122
127
metacall_allocator_destroy (allocator);
123
128
}
124
129
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
-
130
130
EXPECT_EQ ((int )0 , (int )metacall_destroy ());
131
131
132
132
EXPECT_EQ ((bool )callback_result.load (), (bool )true );
0 commit comments