szaszm commented on code in PR #1504:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1504#discussion_r1121673547


##########
extensions/python/tests/CMakeLists.txt:
##########
@@ -0,0 +1,79 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+file(GLOB EXECUTESCRIPT_PYTHON_TESTS 
"TestExecuteScriptProcessorWithPythonScript.cpp" "PythonScriptEngineTests.cpp")
+file(GLOB EXECUTEPYTHONPROCESSOR_UNIT_TESTS "ExecutePythonProcessorTests.cpp" 
"PythonManifestTests.cpp")
+file(GLOB PY_SOURCES  "python/*.cpp")
+include(GenericPython)
+
+SET(EXTENSIONS_TEST_COUNT 0)
+
+FOREACH(testfile ${EXECUTESCRIPT_PYTHON_TESTS})
+    get_filename_component(testfilename "${testfile}" NAME_WE)
+    add_executable("${testfilename}" "${testfile}")
+    target_include_directories(${testfilename} PRIVATE BEFORE 
"${CMAKE_SOURCE_DIR}/extensions/python")
+    target_include_directories(${testfilename} PRIVATE BEFORE 
"${CMAKE_SOURCE_DIR}/extensions/standard-processors")
+    target_include_directories(${testfilename} PRIVATE BEFORE 
"${CMAKE_SOURCE_DIR}/libminifi/test/")
+    add_definitions(-DPYTHON_SUPPORT)
+    target_link_libraries(${testfilename} minifi-standard-processors 
minifi-python-script-extension minifi-script-extension ${CATCH_MAIN_LIB} 
Python::Python)
+    createTests("${testfilename}")
+    MATH(EXPR EXTENSIONS_TEST_COUNT "${EXTENSIONS_TEST_COUNT}+1")
+    add_test(NAME "${testfilename}" COMMAND "${testfilename}" 
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
+
+    if(EXTENSIONS_TEST_COUNT EQUAL 1)
+        add_custom_command(
+                TARGET "${testfilename}"
+                POST_BUILD
+                COMMAND ${CMAKE_COMMAND} -E copy_directory
+                
"${CMAKE_SOURCE_DIR}/extensions/python/tests/test_python_scripts"
+                "$<TARGET_FILE_DIR:${testfilename}>/test_python_scripts")
+    endif()
+ENDFOREACH()
+
+FOREACH(testfile ${EXECUTEPYTHONPROCESSOR_UNIT_TESTS})
+    get_filename_component(testfilename "${testfile}" NAME_WE)
+    add_executable("${testfilename}" "${testfile}")
+
+    include_directories(${PYTHON_INCLUDE_DIR})
+    include_directories(python)
+    add_definitions(-DPYTHON_SUPPORT)

Review Comment:
   Can we change this to target-based, to avoid polluting the global compile 
flags?
   ```suggestion
       target_include_directories("${testfilename}" PRIVATE 
"${PYTHON_INCLUDE_DIR}")
       target_include_directories("${testfilename}" PRIVATE python)
       target_compile_definitions("${testfilename}" PYTHON_SUPPORT)
   ```



##########
extensions/python/tests/CMakeLists.txt:
##########
@@ -0,0 +1,79 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+file(GLOB EXECUTESCRIPT_PYTHON_TESTS 
"TestExecuteScriptProcessorWithPythonScript.cpp" "PythonScriptEngineTests.cpp")
+file(GLOB EXECUTEPYTHONPROCESSOR_UNIT_TESTS "ExecutePythonProcessorTests.cpp" 
"PythonManifestTests.cpp")
+file(GLOB PY_SOURCES  "python/*.cpp")
+include(GenericPython)
+
+SET(EXTENSIONS_TEST_COUNT 0)
+
+FOREACH(testfile ${EXECUTESCRIPT_PYTHON_TESTS})
+    get_filename_component(testfilename "${testfile}" NAME_WE)
+    add_executable("${testfilename}" "${testfile}")
+    target_include_directories(${testfilename} PRIVATE BEFORE 
"${CMAKE_SOURCE_DIR}/extensions/python")
+    target_include_directories(${testfilename} PRIVATE BEFORE 
"${CMAKE_SOURCE_DIR}/extensions/standard-processors")
+    target_include_directories(${testfilename} PRIVATE BEFORE 
"${CMAKE_SOURCE_DIR}/libminifi/test/")
+    add_definitions(-DPYTHON_SUPPORT)
+    target_link_libraries(${testfilename} minifi-standard-processors 
minifi-python-script-extension minifi-script-extension ${CATCH_MAIN_LIB} 
Python::Python)
+    createTests("${testfilename}")
+    MATH(EXPR EXTENSIONS_TEST_COUNT "${EXTENSIONS_TEST_COUNT}+1")
+    add_test(NAME "${testfilename}" COMMAND "${testfilename}" 
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
+
+    if(EXTENSIONS_TEST_COUNT EQUAL 1)
+        add_custom_command(
+                TARGET "${testfilename}"
+                POST_BUILD
+                COMMAND ${CMAKE_COMMAND} -E copy_directory
+                
"${CMAKE_SOURCE_DIR}/extensions/python/tests/test_python_scripts"
+                "$<TARGET_FILE_DIR:${testfilename}>/test_python_scripts")
+    endif()
+ENDFOREACH()
+
+FOREACH(testfile ${EXECUTEPYTHONPROCESSOR_UNIT_TESTS})
+    get_filename_component(testfilename "${testfile}" NAME_WE)
+    add_executable("${testfilename}" "${testfile}")
+
+    include_directories(${PYTHON_INCLUDE_DIR})
+    include_directories(python)
+    add_definitions(-DPYTHON_SUPPORT)
+
+    target_include_directories(${testfilename} PRIVATE BEFORE 
"${CMAKE_SOURCE_DIR}/extensions/python")
+    target_include_directories(${testfilename} PRIVATE BEFORE 
"${CMAKE_SOURCE_DIR}/extensions/standard-processors")

Review Comment:
   Why do python processor tests depend on the standard processors?



##########
extensions/python/tests/PythonScriptEngineTests.cpp:
##########
@@ -0,0 +1,73 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "TestBase.h"
+#include "Catch.h"
+#include "Utils.h"
+#include "PythonScriptEngine.h"
+
+namespace org::apache::nifi::minifi::extensions::python {
+
+using minifi::test::utils::ExceptionSubStringMatcher;
+
+TEST_CASE("PythonScriptEngine errors during eval", "[pythonscriptengineeval]") 
{
+  python::PythonScriptEngine engine;
+  REQUIRE_NOTHROW(engine.eval("print('foo')"));
+  REQUIRE_THROWS_MATCHES(engine.eval("shout('foo')"), PythonScriptException, 
ExceptionSubStringMatcher<PythonScriptException>({"NameError: name 'shout' is 
not defined"}));
+  REQUIRE_THROWS_MATCHES(engine.eval(" print('foo')"), PythonScriptException, 
ExceptionSubStringMatcher<PythonScriptException>({"IndentationError: unexpected 
indent (<string>, line 2)"}));
+}
+
+TEST_CASE("GilScopedAcquire should lock threads properly", 
"[pythonscriptengineeval]") {
+  python::PythonScriptEngine engine;
+
+  for (int i = 0; i < 10; ++i) {
+    DYNAMIC_SECTION("Iteration: " << i) {
+      std::vector<std::string_view> messages;
+      auto sleeping_thread = std::thread([&] {
+        using namespace std::literals::chrono_literals;
+        python::GlobalInterpreterLock gil_lock;
+        messages.emplace_back("Before sleep");
+        std::this_thread::sleep_for(100ms);
+        messages.emplace_back("First thread");
+      });
+      std::this_thread::sleep_for(15ms);
+

Review Comment:
   Instead of sleep, you can use a barrier to synchronize the threads, and 
deterministically avoid starting the second thread before the first locks.
   ```suggestion
         std::barrier sync{2};
         auto sleeping_thread = std::thread([&] {
           using namespace std::literals::chrono_literals;
           python::GlobalInterpreterLock gil_lock;
           sync.arrive();
           messages.emplace_back("Before sleep");
           std::this_thread::sleep_for(100ms);
           messages.emplace_back("First thread");
         });
         sync.arrive_and_wait();
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@nifi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to