szaszm commented on a change in pull request #1211:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1211#discussion_r746474071



##########
File path: libminifi/test/unit/ExtensionVerificationTests.cpp
##########
@@ -0,0 +1,98 @@
+/**
+ * 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.
+ */
+
+#define CUSTOM_EXTENSION_INIT
+#undef NDEBUG
+
+#include <filesystem>
+#include "../TestBase.h"
+#include "agent/agent_version.h"
+#include "core/extension/Utils.h"
+#include "utils/IntegrationTestUtils.h"
+
+using namespace std::literals;
+
+#if defined(WIN32)
+const std::string extension_file = "extension.dll";
+#elif defined(__APPLE__)
+const std::string extension_file = "libextension.dylib";
+#else
+const std::string extension_file = "libextension.so";
+#endif
+
+struct Fixture : public TestController {
+  Fixture() {
+    extension_ = std::filesystem::path(createTempDirectory()) / extension_file;
+  }
+  std::filesystem::path extension_;
+  static inline std::shared_ptr<logging::Logger> 
logger_{core::logging::LoggerFactory<Fixture>::getLogger()};

Review comment:
       I think this should be in an anonymous namespace instead of being 
inline. `inline` works, too, as long as the definitions of all Fixture classes 
and all logger_ members are identical in a given program, but I think it's 
better practice to hide what doesn't need to be exposed.

##########
File path: cmake/Extensions.cmake
##########
@@ -22,10 +22,31 @@ define_property(GLOBAL PROPERTY EXTENSION-OPTIONS
 
 set_property(GLOBAL PROPERTY EXTENSION-OPTIONS "")
 
+set(extension-build-info-file 
"${CMAKE_CURRENT_BINARY_DIR}/ExtensionBuildInfo.cpp")
+file(GENERATE OUTPUT ${extension-build-info-file}
+    CONTENT "\
+    #include \"utils/Export.h\"\n\
+    #ifdef BUILD_ID_VARIABLE_NAME\n\
+    EXTENSIONAPI extern const char* const BUILD_ID_VARIABLE_NAME = 
\"__EXTENSION_BUILD_IDENTIFIER_BEGIN__${BUILD_IDENTIFIER}__EXTENSION_BUILD_IDENTIFIER_END__\";\n\
+    #else\n\
+    static_assert(false, \"BUILD_ID_VARIABLE_NAME is not defined\");\n\
+    #endif\n")

Review comment:
       I think this is a hack. We shouldn't have to open the extension as a 
binary file, search for a prefix and extract the ID as text. Why don't 
extensions define a function that takes a build identifier and returns whether 
the extension is compatible with the agent or not? The agent could call this 
function after dlopen, before the extension's init.

##########
File path: libminifi/include/core/extension/Utils.h
##########
@@ -0,0 +1,113 @@
+/**
+ * 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.
+ */
+
+#pragma once
+
+#include <optional>
+#include <functional>
+#include <chrono>
+#include <utility>
+#include <memory>
+#include <string>
+
+#include "utils/gsl.h"
+#include "utils/file/FileView.h"
+#include "utils/StringUtils.h"
+
+namespace org::apache::nifi::minifi::core::extension::internal {
+
+struct Timer {
+  explicit Timer(std::function<void(int)> cb): cb_(std::move(cb)) {}
+  ~Timer() {
+    auto end = std::chrono::steady_clock::now();
+    int elapsed = 
gsl::narrow<int>(std::chrono::duration_cast<std::chrono::milliseconds>(end - 
start_).count());
+    cb_(elapsed);
+  }
+  std::chrono::steady_clock::time_point 
start_{std::chrono::steady_clock::now()};
+  std::function<void(int)> cb_;
+};
+
+struct LibraryDescriptor {
+  std::string name;
+  std::filesystem::path dir;
+  std::string filename;
+
+  [[nodiscard]]
+  bool verify(const std::shared_ptr<logging::Logger>& logger) const {
+    auto path = getFullPath();
+    Timer timer{[&] (int ms) {
+      logger->log_error("Verification for '%s' took %d ms", path.string(), ms);
+    }};

Review comment:
       You may want to use the date library duration formatting utilities for 
simpler code. https://howardhinnant.github.io/date/date.html#duration_io
   ```suggestion
       const Timer timer{[&](const std::chrono::milliseconds elapsed) {
           using date::operator<<;
           core::logging::LOG_ERROR(logger) << "Verification for '" << path << 
"' took " << elapsed;
       }};
   ```
   
   see full example: https://godbolt.org/z/xW9Phe3f9
   other changes:
   - Timer doesn't need to erase the callback type, it can be just templated 
without any issues. I used the invocable concept, but it works without concept 
checking, too, by just using the `typename` keyword instead.
   - const (use as you prefer, I prefer as much const as reasonably possible)
   - narrowing conversion is no longer needed after the change, the only 
precision loss happening is in `duration_cast`




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to