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


##########
libminifi/test/libtest/unit/TestUtils.h:
##########
@@ -0,0 +1,223 @@
+/**
+ *
+ * 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 <cassert>
+#include <chrono>
+#include <filesystem>
+#include <fstream>
+#include <memory>
+#include <string>
+#include <unordered_set>
+#include <utility>
+#include <vector>
+
+#include "utils/file/FileUtils.h"
+#include "utils/Environment.h"
+#include "utils/Id.h"
+#include "utils/TimeUtil.h"
+#include "TestBase.h"
+
+#include "rapidjson/document.h"
+#include "asio.hpp"
+#include "asio/ssl.hpp"
+#include "utils/net/Ssl.h"
+
+using namespace std::literals::chrono_literals;
+
+#undef GetObject  // windows.h #defines GetObject = GetObjectA or GetObjectW, 
which conflicts with rapidjson
+#include "Connection.h"
+#include "utils/FlowFileQueue.h"
+#include "Catch.h"
+
+#define FIELD_ACCESSOR(field) \
+  template<typename T> \
+  static auto get_##field(T&& instance) -> 
decltype((std::forward<T>(instance).field)) { \
+    return std::forward<T>(instance).field; \
+  }
+
+#define METHOD_ACCESSOR(method) \
+  template<typename T, typename ...Args> \
+  static auto call_##method(T&& instance, Args&& ...args) -> 
decltype((std::forward<T>(instance).method(std::forward<Args>(args)...))) { \
+    return std::forward<T>(instance).method(std::forward<Args>(args)...); \
+  }
+
+namespace org::apache::nifi::minifi::test::utils {
+
+std::filesystem::path putFileToDir(const std::filesystem::path& dir_path, 
const std::filesystem::path& file_name, const std::string& content);
+std::string getFileContent(const std::filesystem::path& file_name);
+
+void makeFileOrDirectoryNotWritable(const std::filesystem::path& file_name);
+void makeFileOrDirectoryWritable(const std::filesystem::path& file_name);
+
+minifi::utils::Identifier generateUUID();
+
+class ManualClock : public minifi::utils::timeutils::SteadyClock {
+ public:
+  [[nodiscard]] std::chrono::milliseconds timeSinceEpoch() const override {
+    std::lock_guard lock(mtx_);
+    return time_;
+  }
+
+  [[nodiscard]] std::chrono::time_point<std::chrono::steady_clock> now() const 
override {
+    return std::chrono::steady_clock::time_point{timeSinceEpoch()};
+  }
+
+  void advance(std::chrono::milliseconds elapsed_time);
+  bool wait_until(std::condition_variable& cv, std::unique_lock<std::mutex>& 
lck, std::chrono::milliseconds time, const std::function<bool()>& pred) 
override;
+
+ private:
+  mutable std::mutex mtx_;
+  std::unordered_set<std::condition_variable*> cvs_;
+  std::chrono::milliseconds time_{0};
+};
+
+template <class Rep, class Period, typename Fun>
+bool verifyEventHappenedInPollTime(
+    const std::chrono::duration<Rep, Period>& wait_duration,
+    Fun&& check,
+    std::chrono::microseconds check_interval = std::chrono::milliseconds(100)) 
{
+  std::chrono::steady_clock::time_point wait_end = 
std::chrono::steady_clock::now() + wait_duration;
+  do {
+    if (std::forward<Fun>(check)()) {
+      return true;
+    }
+    std::this_thread::sleep_for(check_interval);
+  } while (std::chrono::steady_clock::now() < wait_end);
+  return false;
+}
+
+template <class Rep, class Period, typename ...String>
+bool verifyLogLinePresenceInPollTime(const std::chrono::duration<Rep, Period>& 
wait_duration, String&&... patterns) {
+  auto check = [&patterns...] {
+    const std::string logs = LogTestController::getInstance().getLogs();
+    return ((logs.find(patterns) != std::string::npos) && ...);
+  };
+  return verifyEventHappenedInPollTime(wait_duration, check);
+}
+
+template <class Rep, class Period, typename ...String>
+bool verifyLogLineVariantPresenceInPollTime(const std::chrono::duration<Rep, 
Period>& wait_duration, String&&... patterns) {
+  auto check = [&patterns...] {
+    const std::string logs = LogTestController::getInstance().getLogs();
+    return ((logs.find(patterns) != std::string::npos) || ...);
+  };
+  return verifyEventHappenedInPollTime(wait_duration, check);
+}
+
+namespace internal {
+struct JsonContext {
+  const JsonContext *parent{nullptr};
+  std::string_view member;
+
+  std::string path() const {
+    if (!parent) {
+      return "/";
+    }
+    return minifi::utils::string::join_pack(parent->path(), member, "/");
+  }
+};
+}  // namespace internal
+
+#define REQUIRE_WARN(cond, msg) if (!(cond)) {WARN(msg); REQUIRE(cond);}
+
+// carries out a loose match on objects, i.e. it doesn't matter if the
+// actual object has extra fields than expected
+void matchJSON(const internal::JsonContext& ctx, const rapidjson::Value& 
actual, const rapidjson::Value& expected, bool strict = false);
+void verifyJSON(const std::string& actual_str, const std::string& 
expected_str, bool strict = false);
+
+template<typename T>
+class ExceptionSubStringMatcher : public Catch::Matchers::MatcherBase<T> {
+ public:
+  explicit ExceptionSubStringMatcher(std::vector<std::string> 
exception_substr) :
+      possible_exception_substrs_(std::move(exception_substr)) {}
+
+  bool match(T const& script_exception) const override {
+    for (auto& possible_exception_substr : possible_exception_substrs_) {
+      if (std::string(script_exception.what()).find(possible_exception_substr) 
!= std::string::npos)
+        return true;
+    }
+    return false;
+  }

Review Comment:
   I'm not too extreme about "no raw loops", but this is a perfect candidate 
for replacement with higher level abstractions. C++23 `string`/`string_view` 
`contains` would be even better, but unfortunately we can't use that yet.
   ```suggestion
     bool match(T const& script_exception) const override {
       return std::any_of(std::begin(possible_exception_substrs_), 
std::end(possible_exception_substrs_), [what = 
std::string_view{script_exception.what()}](const auto& 
possible_exception_substr) {
         return what.find(possible_exception_substr) != std::string_view::npos;
       };
     }
   ```



-- 
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