Skip to content

Commit c3b1385

Browse files
committed
Add more complex test for mixing node and python await.
1 parent 92adb18 commit c3b1385

File tree

4 files changed

+274
-2
lines changed

4 files changed

+274
-2
lines changed

source/tests/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ add_subdirectory(metacall_node_fail_load_leak_test)
150150
add_subdirectory(metacall_node_typescript_test)
151151
add_subdirectory(metacall_node_python_async_after_destroy_test)
152152
add_subdirectory(metacall_node_python_await_test)
153+
add_subdirectory(metacall_node_python_await_extended_test)
153154
add_subdirectory(metacall_node_python_exception_test)
154155
add_subdirectory(metacall_node_clear_mem_test)
155156
add_subdirectory(metacall_node_async_resources_test)
@@ -190,9 +191,9 @@ add_subdirectory(metacall_python_relative_path_test)
190191
add_subdirectory(metacall_python_without_functions_test)
191192
add_subdirectory(metacall_python_builtins_test)
192193
add_subdirectory(metacall_python_async_test)
193-
# TODO: add_subdirectory(metacall_python_await_test)
194+
# TODO: add_subdirectory(metacall_python_await_test) # TODO: Implement metacall_await in Python Port
194195
add_subdirectory(metacall_python_exception_test)
195-
# TODO: add_subdirectory(metacall_python_node_await_test)
196+
# TODO: add_subdirectory(metacall_python_node_await_test) # TODO: Implement metacall_await in Python Port
196197
add_subdirectory(metacall_python_without_env_vars_test)
197198
add_subdirectory(metacall_map_test)
198199
add_subdirectory(metacall_map_await_test)
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# Check if this loader is enabled
2+
if(NOT OPTION_BUILD_LOADERS OR NOT OPTION_BUILD_LOADERS_NODE OR NOT OPTION_BUILD_LOADERS_PY OR NOT OPTION_BUILD_PORTS OR NOT OPTION_BUILD_PORTS_NODE)
3+
return()
4+
endif()
5+
6+
#
7+
# Executable name and options
8+
#
9+
10+
# Target name
11+
set(target metacall-node-python-await-extended-test)
12+
message(STATUS "Test ${target}")
13+
14+
#
15+
# Compiler warnings
16+
#
17+
18+
include(Warnings)
19+
20+
#
21+
# Compiler security
22+
#
23+
24+
include(SecurityFlags)
25+
26+
#
27+
# Sources
28+
#
29+
30+
set(include_path "${CMAKE_CURRENT_SOURCE_DIR}/include/${target}")
31+
set(source_path "${CMAKE_CURRENT_SOURCE_DIR}/source")
32+
33+
set(sources
34+
${source_path}/main.cpp
35+
${source_path}/metacall_node_python_await_extended_test.cpp
36+
)
37+
38+
# Group source files
39+
set(header_group "Header Files (API)")
40+
set(source_group "Source Files")
41+
source_group_by_path(${include_path} "\\\\.h$|\\\\.hpp$"
42+
${header_group} ${headers})
43+
source_group_by_path(${source_path} "\\\\.cpp$|\\\\.c$|\\\\.h$|\\\\.hpp$"
44+
${source_group} ${sources})
45+
46+
#
47+
# Create executable
48+
#
49+
50+
# Build executable
51+
add_executable(${target}
52+
${sources}
53+
)
54+
55+
# Create namespaced alias
56+
add_executable(${META_PROJECT_NAME}::${target} ALIAS ${target})
57+
58+
#
59+
# Project options
60+
#
61+
62+
set_target_properties(${target}
63+
PROPERTIES
64+
${DEFAULT_PROJECT_OPTIONS}
65+
FOLDER "${IDE_FOLDER}"
66+
)
67+
68+
#
69+
# Include directories
70+
#
71+
72+
target_include_directories(${target}
73+
PRIVATE
74+
${DEFAULT_INCLUDE_DIRECTORIES}
75+
${PROJECT_BINARY_DIR}/source/include
76+
)
77+
78+
#
79+
# Libraries
80+
#
81+
82+
target_link_libraries(${target}
83+
PRIVATE
84+
${DEFAULT_LIBRARIES}
85+
86+
GTest
87+
88+
${META_PROJECT_NAME}::metacall
89+
)
90+
91+
#
92+
# Compile definitions
93+
#
94+
95+
target_compile_definitions(${target}
96+
PRIVATE
97+
${DEFAULT_COMPILE_DEFINITIONS}
98+
99+
# NodeJS Port path
100+
METACALL_NODE_PORT_PATH="${CMAKE_SOURCE_DIR}/source/ports/node_port/index.js"
101+
)
102+
103+
#
104+
# Compile options
105+
#
106+
107+
target_compile_options(${target}
108+
PRIVATE
109+
${DEFAULT_COMPILE_OPTIONS}
110+
)
111+
112+
#
113+
# Linker options
114+
#
115+
116+
target_link_libraries(${target}
117+
PRIVATE
118+
${DEFAULT_LINKER_OPTIONS}
119+
)
120+
121+
#
122+
# Define test
123+
#
124+
125+
add_test(NAME ${target}
126+
COMMAND $<TARGET_FILE:${target}>
127+
)
128+
129+
#
130+
# Define dependencies
131+
#
132+
133+
add_dependencies(${target}
134+
node_loader
135+
py_loader
136+
)
137+
138+
#
139+
# Define test properties
140+
#
141+
142+
set_property(TEST ${target}
143+
PROPERTY LABELS ${target}
144+
)
145+
146+
include(TestEnvironmentVariables)
147+
148+
test_environment_variables(${target}
149+
""
150+
${TESTS_ENVIRONMENT_VARIABLES}
151+
${PY_DEBUG_ENVIRONMENT_VARIABLES}
152+
)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* MetaCall Library by Parra Studios
3+
* A library for providing a foreign function interface calls.
4+
*
5+
* Copyright (C) 2016 - 2024 Vicente Eduardo Ferrer Garcia <[email protected]>
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
#include <gtest/gtest.h>
22+
23+
int main(int argc, char *argv[])
24+
{
25+
::testing::InitGoogleTest(&argc, argv);
26+
27+
return RUN_ALL_TESTS();
28+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* MetaCall Library by Parra Studios
3+
* A library for providing a foreign function interface calls.
4+
*
5+
* Copyright (C) 2016 - 2024 Vicente Eduardo Ferrer Garcia <[email protected]>
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
#include <gtest/gtest.h>
22+
23+
#include <metacall/metacall.h>
24+
#include <metacall/metacall_loaders.h>
25+
#include <metacall/metacall_value.h>
26+
27+
class metacall_node_python_await_extended_test : public testing::Test
28+
{
29+
public:
30+
};
31+
32+
TEST_F(metacall_node_python_await_extended_test, DefaultConstructor)
33+
{
34+
metacall_print_info();
35+
36+
ASSERT_EQ((int)0, (int)metacall_initialize());
37+
38+
/* NodeJS & Python */
39+
#if defined(OPTION_BUILD_LOADERS_NODE) && defined(OPTION_BUILD_LOADERS_PY)
40+
{
41+
static const char buffer[] =
42+
/* NodeJS */
43+
"const { metacall_await, metacall_load_from_memory, metacall_inspect } = require('" METACALL_NODE_PORT_PATH "');\n"
44+
"const util = require('util')\n"
45+
"metacall_load_from_memory('py', `"
46+
/* Python */
47+
"import sys\n"
48+
"import threading\n"
49+
"async def python_simple(n):\n"
50+
"\tprint('inside python_simple', threading.current_thread().ident, ':', n)\n"
51+
"\tsys.stdout.flush()\n"
52+
"\treturn n\n"
53+
// "import asyncio\n"
54+
// "async def python_simple(n):\n"
55+
// " await asyncio.sleep(1)\n"
56+
// " return n\n"
57+
"`);\n"
58+
/* Debug */
59+
"console.log('--------------------------------------------------------------------')\n"
60+
"console.log(util.inspect(metacall_inspect(), false, null, true))\n"
61+
"console.log('--------------------------------------------------------------------')\n"
62+
/* NodeJS Check */
63+
"const size = 10000;\n"
64+
"let buffer = new SharedArrayBuffer(4);\n"
65+
"let int32 = new Int32Array(buffer);\n"
66+
"Atomics.store(int32, 0, 0);\n"
67+
"process.on('exit', () => {\n"
68+
" if (Atomics.load(int32, 0) != size) {\n"
69+
" process.exit(3);\n"
70+
" }\n"
71+
"});\n"
72+
/* NodeJS Promise */
73+
"for (let i = 0; i < size; i++) {\n"
74+
" metacall_await('python_simple', 32).then(v => {\n"
75+
" console.log('RESULT:', v, Atomics.load(int32, 0));\n"
76+
" if (v !== 32) {\n"
77+
" process.exit(1);\n"
78+
" }\n"
79+
" Atomics.add(int32, 0, 1);\n"
80+
" }).catch(v => {\n"
81+
" console.log('ERROR:', v);\n"
82+
" process.exit(2);\n"
83+
" });\n"
84+
"}\n";
85+
86+
ASSERT_EQ((int)0, (int)metacall_load_from_memory("node", buffer, sizeof(buffer), NULL));
87+
}
88+
#endif /* OPTION_BUILD_LOADERS_NODE && OPTION_BUILD_LOADERS_PY */
89+
90+
EXPECT_EQ((int)0, (int)metacall_destroy());
91+
}

0 commit comments

Comments
 (0)