Skip to content

Commit

Permalink
Fix windows test
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdebreceni committed Oct 14, 2024
1 parent cf9189c commit 5d50be1
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 57 deletions.
58 changes: 58 additions & 0 deletions core/include/minifi-cpp/utils/TimeUtil.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* 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 <string>
#include <chrono>

namespace org::apache::nifi::minifi::utils::timeutils {

/**
* Mockable clock classes
*/
class Clock {
public:
virtual ~Clock() = default;
virtual std::chrono::milliseconds timeSinceEpoch() const = 0;
virtual bool wait_until(std::condition_variable& cv, std::unique_lock<std::mutex>& lck, std::chrono::milliseconds time, const std::function<bool()>& pred) {
return cv.wait_for(lck, time - timeSinceEpoch(), pred);
}
};

class SteadyClock : public Clock {
public:
std::chrono::milliseconds timeSinceEpoch() const override {
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch());
}

virtual std::chrono::time_point<std::chrono::steady_clock> now() const {
return std::chrono::steady_clock::now();
}
};

std::shared_ptr<SteadyClock> getClock();

// test-only utility to specify what clock to use
void setClock(std::shared_ptr<SteadyClock> clock);

#ifdef WIN32
void dateSetGlobalInstall(const std::string& install);
#endif

} // namespace org::apache::nifi::minifi::utils::timeutils

2 changes: 1 addition & 1 deletion extension-utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ target_link_libraries(minifi-extension-utils PUBLIC minifi-core)
include(RangeV3)
include(Asio)
include(MagicEnum)
list(APPEND CORE_LIBRARIES ZLIB::ZLIB concurrentqueue RapidJSON spdlog Threads::Threads gsl-lite range-v3 expected-lite date::date date::tz asio magic_enum OpenSSL::Crypto OpenSSL::SSL CURL::libcurl RapidJSON)
list(APPEND CORE_LIBRARIES ZLIB::ZLIB concurrentqueue RapidJSON spdlog Threads::Threads gsl-lite range-v3 expected-lite asio magic_enum OpenSSL::Crypto OpenSSL::SSL CURL::libcurl RapidJSON)
if(NOT WIN32)
list(APPEND CORE_LIBRARIES OSSP::libuuid++)
endif()
Expand Down
2 changes: 2 additions & 0 deletions extensions/expression-language/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "utils/OsUtils.h"
#include "expression/Expression.h"
#include "utils/RegexUtils.h"
#include "utils/TimeUtil.h"

#ifdef WIN32
#pragma comment(lib, "wldap32.lib" )
Expand Down Expand Up @@ -1634,6 +1635,7 @@ Expression Expression::make_aggregate(const std::function<Value(const Parameters

#ifdef WIN32
void dateSetInstall(const std::string& install) {
utils::timeutils::dateSetInstall(install);
date::set_install(install);
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion extensions/systemd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ include(${CMAKE_SOURCE_DIR}/extensions/ExtensionHeader.txt)

add_minifi_library(minifi-systemd SHARED "ConsumeJournald.cpp" "libwrapper/LibWrapper.cpp" "libwrapper/DlopenWrapper.cpp")

target_link_libraries(minifi-systemd ${LIBMINIFI} Threads::Threads date::date)
target_link_libraries(minifi-systemd ${LIBMINIFI} Threads::Threads)

register_extension(minifi-systemd "SYSTEMD EXTENSIONS" SYSTEMD-EXTENSIONS "Enabled log collection from journald" "extensions/systemd/tests")
register_extension_linter(minifi-systemd-extension-linter)
11 changes: 9 additions & 2 deletions libminifi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ endif()

set(TLS_SOURCES "src/utils/tls/*.cpp" "src/io/tls/*.cpp")

file(GLOB SOURCES "src/agent/*.cpp" "src/properties/*.cpp" "src/utils/file/*.cpp" "src/sitetosite/*.cpp" "src/core/logging/*.cpp" "src/core/logging/internal/*.cpp" "src/core/logging/alert/*.cpp" "src/core/state/*.cpp" "src/core/state/nodes/*.cpp" "src/c2/protocols/*.cpp" "src/c2/triggers/*.cpp" "src/c2/*.cpp" "src/io/*.cpp" ${SOCKET_SOURCES} ${TLS_SOURCES} "src/core/controller/*.cpp" "src/controllers/*.cpp" "src/core/*.cpp" "src/core/repository/*.cpp" "src/core/flow/*.cpp" "src/core/json/*.cpp" "src/core/yaml/*.cpp" "src/core/reporting/*.cpp" "src/core/extension/*.cpp" "src/serialization/*.cpp" "src/provenance/*.cpp" "src/utils/*.cpp" "src/*.cpp")
file(GLOB SOURCES
"src/agent/*.cpp" "src/properties/*.cpp" "src/utils/file/*.cpp" "src/sitetosite/*.cpp"
"src/core/logging/*.cpp" "src/core/logging/internal/*.cpp" "src/core/logging/alert/*.cpp"
"src/core/state/*.cpp" "src/core/state/nodes/*.cpp" "src/c2/protocols/*.cpp" "src/c2/triggers/*.cpp"
"src/c2/*.cpp" "src/io/*.cpp" ${SOCKET_SOURCES} ${TLS_SOURCES} "src/core/controller/*.cpp"
"src/controllers/*.cpp" "src/core/*.cpp" "src/core/repository/*.cpp" "src/core/flow/*.cpp"
"src/core/json/*.cpp" "src/core/yaml/*.cpp" "src/core/reporting/*.cpp" "src/core/extension/*.cpp"
"src/serialization/*.cpp" "src/provenance/*.cpp" "src/utils/*.cpp" "src/*.cpp" "src/http/*.cpp")
# manually add this as it might not yet be present when this executes
list(APPEND SOURCES "${CMAKE_CURRENT_BINARY_DIR}/agent_version.cpp")

Expand Down Expand Up @@ -76,7 +83,7 @@ endif()
include(RangeV3)
include(Asio)
include(MagicEnum)
list(APPEND LIBMINIFI_LIBRARIES minifi-utils yaml-cpp ZLIB::ZLIB concurrentqueue RapidJSON spdlog Threads::Threads gsl-lite libsodium range-v3 expected-lite date::date date::tz asio magic_enum OpenSSL::Crypto OpenSSL::SSL CURL::libcurl RapidJSON)
list(APPEND LIBMINIFI_LIBRARIES minifi-utils yaml-cpp ZLIB::ZLIB concurrentqueue RapidJSON spdlog Threads::Threads gsl-lite libsodium range-v3 expected-lite asio magic_enum OpenSSL::Crypto OpenSSL::SSL CURL::libcurl RapidJSON)
if(NOT WIN32)
list(APPEND LIBMINIFI_LIBRARIES OSSP::libuuid++)
endif()
Expand Down
24 changes: 24 additions & 0 deletions libminifi/src/http/HTTPClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 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 "http/HTTPClient.h"
#include "core/Resource.h"

namespace org::apache::nifi::minifi::http {

REGISTER_RESOURCE(HTTPClient, InternalResource);

} // namespace org::apache::nifi::minifi::http
48 changes: 48 additions & 0 deletions libminifi/src/utils/TimeUtil.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* 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 "utils/TimeUtil.h"
#ifdef WIN32
#include "date/tz.h"
#endif

namespace org::apache::nifi::minifi::utils::timeutils {

static std::mutex global_clock_mtx;
static std::shared_ptr<SteadyClock> global_clock{std::make_shared<SteadyClock>()};

std::shared_ptr<SteadyClock> getClock() {
std::lock_guard lock(global_clock_mtx);
return global_clock;
}

// test-only utility to specify what clock to use
void setClock(std::shared_ptr<SteadyClock> clock) {
std::lock_guard lock(global_clock_mtx);
global_clock = std::move(clock);
}

#ifdef WIN32
void dateSetGlobalInstall(const std::string& install) {
date::set_install(install);
}
#endif


} // namespace org::apache::nifi::minifi::utils::timeutils

6 changes: 4 additions & 2 deletions libminifi/test/unit/CronTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
#include "utils/Cron.h"
#include "date/date.h"
#include "date/tz.h"
#include "utils/TimeUtil.h"

using std::chrono::system_clock;
using std::chrono::seconds;
using org::apache::nifi::minifi::utils::Cron;
namespace timeutils = org::apache::nifi::minifi::utils::timeutils;


void checkNext(const std::string& expr, const date::zoned_time<seconds>& from, const date::zoned_time<seconds>& next) {
Expand Down Expand Up @@ -160,7 +162,7 @@ TEST_CASE("Cron::calculateNextTrigger", "[cron]") {
using namespace date::literals; // NOLINT(google-build-using-namespace)
using namespace std::literals::chrono_literals;
#ifdef WIN32
date::set_install(TZ_DATA_DIR);
timeutils::dateSetInstall(TZ_DATA_DIR);
#endif

checkNext("0/15 * 1-4 * * ?",
Expand Down Expand Up @@ -446,7 +448,7 @@ TEST_CASE("Cron::calculateNextTrigger with timezones", "[cron]") {
using namespace date::literals; // NOLINT(google-build-using-namespace)
using namespace std::literals::chrono_literals;
#ifdef WIN32
date::set_install(TZ_DATA_DIR);
timeutils::dateSetInstall(TZ_DATA_DIR);
#endif

const std::vector<std::string> time_zones{ "Europe/Berlin", "Asia/Seoul", "America/Los_Angeles", "Asia/Singapore", "UTC" };
Expand Down
1 change: 0 additions & 1 deletion libminifi/test/unit/SchedulingAgentTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ class SchedulingAgentTestFixture {

#ifdef WIN32
minifi::utils::timeutils::dateSetInstall(TZ_DATA_DIR);
date::set_install(TZ_DATA_DIR);
#endif
}

Expand Down
36 changes: 1 addition & 35 deletions utils/include/utils/TimeUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <memory>

#include "StringUtils.h"
#include "minifi-cpp/utils/TimeUtil.h"

// libc++ doesn't define operator<=> on durations, and apparently the operator rewrite rules don't automagically make one
#if defined(_LIBCPP_VERSION)
Expand Down Expand Up @@ -76,41 +77,6 @@ inline uint64_t getTimeNano() {
return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}

/**
* Mockable clock classes
*/
class Clock {
public:
virtual ~Clock() = default;
virtual std::chrono::milliseconds timeSinceEpoch() const = 0;
virtual bool wait_until(std::condition_variable& cv, std::unique_lock<std::mutex>& lck, std::chrono::milliseconds time, const std::function<bool()>& pred) {
return cv.wait_for(lck, time - timeSinceEpoch(), pred);
}
};

class SystemClock : public Clock {
public:
std::chrono::milliseconds timeSinceEpoch() const override {
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
}
};

class SteadyClock : public Clock {
public:
std::chrono::milliseconds timeSinceEpoch() const override {
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch());
}

virtual std::chrono::time_point<std::chrono::steady_clock> now() const {
return std::chrono::steady_clock::now();
}
};

std::shared_ptr<SteadyClock> getClock();

// test-only utility to specify what clock to use
void setClock(std::shared_ptr<SteadyClock> clock);

inline std::string getTimeStr(std::chrono::system_clock::time_point tp) {
std::ostringstream stream;
date::to_stream(stream, TIME_FORMAT, std::chrono::floor<std::chrono::milliseconds>(tp));
Expand Down
2 changes: 0 additions & 2 deletions utils/src/http/HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,4 @@ void HTTPClient::CurlMimeFree::operator()(curl_mime* curl_mime) const {
curl_mime_free(curl_mime);
}

REGISTER_RESOURCE(HTTPClient, InternalResource);

} // namespace org::apache::nifi::minifi::http
14 changes: 1 addition & 13 deletions utils/src/utils/TimeUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,6 @@
namespace org::apache::nifi::minifi::utils::timeutils {

using namespace std::literals::chrono_literals;
static std::mutex global_clock_mtx;
static std::shared_ptr<SteadyClock> global_clock{std::make_shared<SteadyClock>()};

std::shared_ptr<SteadyClock> getClock() {
std::lock_guard lock(global_clock_mtx);
return global_clock;
}

// test-only utility to specify what clock to use
void setClock(std::shared_ptr<SteadyClock> clock) {
std::lock_guard lock(global_clock_mtx);
global_clock = std::move(clock);
}

std::optional<std::chrono::system_clock::time_point> parseRfc3339(const std::string& str) {
std::istringstream stream(str);
Expand Down Expand Up @@ -76,6 +63,7 @@ std::optional<std::chrono::system_clock::time_point> parseRfc3339(const std::str
// date::set_install can point to the TZDATA location, but it has to be called from each library/executable that wants to use timezones
void dateSetInstall(const std::string& install) {
date::set_install(install);
dateSetGlobalInstall(install);
}
#endif

Expand Down

0 comments on commit 5d50be1

Please sign in to comment.