diff --git a/lib/pal/PAL.cpp b/lib/pal/PAL.cpp index 94a71b4a0..8356e3648 100644 --- a/lib/pal/PAL.cpp +++ b/lib/pal/PAL.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -137,32 +138,23 @@ namespace PAL_NS_BEGIN { #define DBG_BUFFER_LEN 2048 -#ifndef _WIN32 -#define DEBUG_LOG_NULL "/dev/null" -#else -#define DEBUG_LOG_NULL "NUL" -#endif - - bool isLoggingInited = false; - #ifdef HAVE_MAT_LOGGING std::recursive_mutex debugLogMutex; std::string debugLogPath; std::unique_ptr debugLogStream; + bool isLoggingInited = false; + uint64_t debugLogEpoch = 0; - bool log_init(bool isTraceEnabled, const std::string& traceFolderPath) + bool log_init(bool isTraceEnabled, const std::string& traceFolderPath, uint64_t& epoch) { + std::lock_guard lock(debugLogMutex); + epoch = 0; if (!isTraceEnabled) { + isLoggingInited = false; return false; } - bool result = true; - if (debugLogStream != nullptr) - { - return result; - } - // Check if the path exists #if defined(_WIN32) || defined(_WIN64) DWORD fileAttr = GetFileAttributesA(traceFolderPath.c_str()); @@ -175,29 +167,46 @@ namespace PAL_NS_BEGIN { if (!pathExists || containsParentDirectory) { + isLoggingInited = false; return false; } - debugLogMutex.lock(); - debugLogPath = traceFolderPath; - if (debugLogPath.back() != '/' && debugLogPath.back() != '\\') + std::string requestedPath = traceFolderPath; + if (requestedPath.back() != '/' && requestedPath.back() != '\\') { - debugLogPath += "/"; + requestedPath += "/"; } - debugLogPath += "mat-debug-"; - debugLogPath += std::to_string(MAT::GetCurrentProcessId()); - debugLogPath += ".log"; + requestedPath += "mat-debug-"; + requestedPath += std::to_string(MAT::GetCurrentProcessId()); + requestedPath += ".log"; - debugLogStream = std::unique_ptr(new std::fstream()); - debugLogStream->open(debugLogPath, std::fstream::out); - if (!debugLogStream->is_open()) + if (!debugLogStream || debugLogPath != requestedPath) { - // If file cannot be created, log to /dev/null - debugLogStream->open(DEBUG_LOG_NULL); - result = false; + isLoggingInited = false; + debugLogStream.reset(); + debugLogPath = std::move(requestedPath); + auto stream = std::unique_ptr(new std::fstream()); + stream->open(debugLogPath, std::fstream::out); + if (!stream->is_open()) + { + return false; + } + debugLogStream = std::move(stream); } - debugLogMutex.unlock(); - return result; + + if (++debugLogEpoch == 0) + { + ++debugLogEpoch; + } + epoch = debugLogEpoch; + isLoggingInited = true; + return true; + } + + bool log_init(bool isTraceEnabled, const std::string& traceFolderPath) + { + uint64_t epoch; + return log_init(isTraceEnabled, traceFolderPath, epoch); } const std::unique_ptr& getDebugLogStream() noexcept @@ -212,21 +221,28 @@ namespace PAL_NS_BEGIN { void log_done() { - debugLogMutex.lock(); - if (debugLogStream) + std::lock_guard lock(debugLogMutex); + debugLogStream.reset(); + isLoggingInited = false; + } + + void log_done(uint64_t epoch) + { + std::lock_guard lock(debugLogMutex); + if (epoch != 0 && epoch == debugLogEpoch) { - debugLogStream = nullptr; + debugLogStream.reset(); isLoggingInited = false; } - debugLogMutex.unlock(); } #else - bool log_init(bool /*isTraceEnabled*/, const std::string& /*traceFolderPath*/) + bool log_init(bool /*isTraceEnabled*/, const std::string& /*traceFolderPath*/, uint64_t& epoch) { + epoch = 0; return false; } - void log_done() + void log_done(uint64_t /*epoch*/) { } #endif @@ -362,6 +378,7 @@ namespace PAL_NS_BEGIN { std::shared_ptr PlatformAbstractionLayer::getDefaultTaskDispatcher() { + std::lock_guard lock(m_stateLock); if (!m_taskDispatcher) { // Default implementation of task dispatcher is a single-threaded worker thread task queue @@ -567,43 +584,53 @@ namespace PAL_NS_BEGIN { void PlatformAbstractionLayer::registerSemanticContext(ISemanticContext* context) { - if (m_DeviceInformation != nullptr) + std::shared_ptr deviceInformation; + std::shared_ptr systemInformation; + std::shared_ptr networkInformation; + { + std::lock_guard lock(m_stateLock); + deviceInformation = m_DeviceInformation; + systemInformation = m_SystemInformation; + networkInformation = m_NetworkInformation; + } + + if (deviceInformation != nullptr) { - const auto& deviceId = m_DeviceInformation->GetDeviceId(); + const auto& deviceId = deviceInformation->GetDeviceId(); #ifdef MATSDK_DISABLE_DEVICE_ID if (!deviceId.empty()) #endif context->SetDeviceId(deviceId); - context->SetDeviceModel(m_DeviceInformation->GetModel()); - context->SetDeviceMake(m_DeviceInformation->GetManufacturer()); + context->SetDeviceModel(deviceInformation->GetModel()); + context->SetDeviceMake(deviceInformation->GetManufacturer()); } - if (m_SystemInformation != nullptr) + if (systemInformation != nullptr) { // Get SystemInfo common fields - context->SetOsVersion(m_SystemInformation->GetOsMajorVersion()); - context->SetOsName(m_SystemInformation->GetOsName()); - context->SetOsBuild(m_SystemInformation->GetOsFullVersion()); - context->SetDeviceClass(m_SystemInformation->GetDeviceClass()); + context->SetOsVersion(systemInformation->GetOsMajorVersion()); + context->SetOsName(systemInformation->GetOsName()); + context->SetOsBuild(systemInformation->GetOsFullVersion()); + context->SetDeviceClass(systemInformation->GetDeviceClass()); // AppInfo fields - context->SetAppId(m_SystemInformation->GetAppId()); - context->SetAppVersion(m_SystemInformation->GetAppVersion()); - context->SetAppLanguage(m_SystemInformation->GetAppLanguage()); + context->SetAppId(systemInformation->GetAppId()); + context->SetAppVersion(systemInformation->GetAppVersion()); + context->SetAppLanguage(systemInformation->GetAppLanguage()); // UserInfo fields. - context->SetUserLanguage(m_SystemInformation->GetUserLanguage()); - context->SetUserTimeZone(m_SystemInformation->GetUserTimeZone()); - //context->SetUserAdvertisingId(m_SystemInformation->GetUserAdvertisingId()); + context->SetUserLanguage(systemInformation->GetUserLanguage()); + context->SetUserTimeZone(systemInformation->GetUserTimeZone()); + //context->SetUserAdvertisingId(systemInformation->GetUserAdvertisingId()); - context->SetCommercialId(m_SystemInformation->GetCommercialId()); + context->SetCommercialId(systemInformation->GetCommercialId()); } - if (m_NetworkInformation != nullptr) + if (networkInformation != nullptr) { // Get NetworkInfo common fields - context->SetNetworkProvider(m_NetworkInformation->GetNetworkProvider()); - context->SetNetworkCost(m_NetworkInformation->GetNetworkCost()); - context->SetNetworkType(m_NetworkInformation->GetNetworkType()); + context->SetNetworkProvider(networkInformation->GetNetworkProvider()); + context->SetNetworkCost(networkInformation->GetNetworkCost()); + context->SetNetworkType(networkInformation->GetNetworkType()); } } @@ -637,53 +664,129 @@ namespace PAL_NS_BEGIN { void PlatformAbstractionLayer::initialize(IRuntimeConfig& configuration) { - if (m_palStarted.fetch_add(1) == 0) { + std::unique_lock lock(m_stateLock); + m_stateChanged.wait(lock, [this] { return !m_initializing; }); + if (m_palStarted > 0) + { + const size_t count = ++m_palStarted; + lock.unlock(); + LOG_INFO("Already initialized: %zu", count); + (void)count; + return; + } + m_initializing = true; + } + + // Platform provider factories can invoke callbacks that access PAL. + uint64_t logEpoch = 0; + std::shared_ptr systemInformation; + std::shared_ptr deviceInformation; + std::shared_ptr networkInformation; +#if HAVE_EXCEPTIONS + try + { +#endif std::string traceFolderPath = MAT::GetTempDirectory(); if (configuration.HasConfig(CFG_STR_TRACE_FOLDER_PATH)) { traceFolderPath = static_cast(configuration[CFG_STR_TRACE_FOLDER_PATH]); } - detail::isLoggingInited = detail::log_init(configuration[CFG_BOOL_ENABLE_TRACE], traceFolderPath); + detail::log_init(configuration[CFG_BOOL_ENABLE_TRACE], traceFolderPath, logEpoch); LOG_TRACE("Initializing..."); - m_SystemInformation = SystemInformationImpl::Create(configuration); - m_DeviceInformation = DeviceInformationImpl::Create(configuration); - m_NetworkInformation = NetworkInformationImpl::Create(configuration); - LOG_INFO("Initialized"); + systemInformation = SystemInformationImpl::Create(configuration); + deviceInformation = DeviceInformationImpl::Create(configuration); + networkInformation = NetworkInformationImpl::Create(configuration); +#if HAVE_EXCEPTIONS } - else + catch (...) + { + networkInformation.reset(); + deviceInformation.reset(); + systemInformation.reset(); + detail::log_done(logEpoch); + { + std::lock_guard lock(m_stateLock); + m_initializing = false; + } + m_stateChanged.notify_all(); + throw; + } +#endif + { - LOG_INFO("Already initialized: %d", m_palStarted.load()); + std::lock_guard lock(m_stateLock); + m_SystemInformation = std::move(systemInformation); + m_DeviceInformation = std::move(deviceInformation); + m_NetworkInformation = std::move(networkInformation); + m_logEpoch = logEpoch; + m_palStarted = 1; + m_initializing = false; } + m_stateChanged.notify_all(); + LOG_INFO("Initialized"); + } + + std::shared_ptr PlatformAbstractionLayer::GetNetworkInformation() const noexcept + { + std::lock_guard lock(m_stateLock); + return m_NetworkInformation; + } + + std::shared_ptr PlatformAbstractionLayer::GetDeviceInformation() const noexcept + { + std::lock_guard lock(m_stateLock); + return m_DeviceInformation; } - std::shared_ptr PlatformAbstractionLayer::GetNetworkInformation() const noexcept { return m_NetworkInformation; } - std::shared_ptr PlatformAbstractionLayer::GetDeviceInformation() const noexcept { return m_DeviceInformation; } - std::shared_ptr PlatformAbstractionLayer::GetSystemInformation() const noexcept { return m_SystemInformation; } + std::shared_ptr PlatformAbstractionLayer::GetSystemInformation() const noexcept + { + std::lock_guard lock(m_stateLock); + return m_SystemInformation; + } void PlatformAbstractionLayer::shutdown() { - if (m_palStarted == 0) + std::shared_ptr taskDispatcher; + std::shared_ptr systemInformation; + std::shared_ptr deviceInformation; + std::shared_ptr networkInformation; + uint64_t logEpoch = 0; { - LOG_ERROR("PAL is already shutdown!"); - return; - } + std::unique_lock lock(m_stateLock); + m_stateChanged.wait(lock, [this] { return !m_initializing; }); + if (m_palStarted == 0) + { + lock.unlock(); + LOG_ERROR("PAL is already shutdown!"); + return; + } + if (--m_palStarted > 0) + { + const size_t count = m_palStarted; + lock.unlock(); + LOG_INFO("Shutting down: %zu", count); + (void)count; + return; + } - if (m_palStarted.fetch_sub(1) == 1) - { - LOG_TRACE("Shutting down..."); - if (m_taskDispatcher) { m_taskDispatcher = nullptr; } - if (m_SystemInformation) { m_SystemInformation = nullptr; } - if (m_DeviceInformation) { m_DeviceInformation = nullptr; } - if (m_NetworkInformation) { m_NetworkInformation = nullptr; } - LOG_INFO("Shut down"); - detail::log_done(); - } - else - { - LOG_INFO("Shutting down: %d", m_palStarted.load()); + taskDispatcher = std::move(m_taskDispatcher); + systemInformation = std::move(m_SystemInformation); + deviceInformation = std::move(m_DeviceInformation); + networkInformation = std::move(m_NetworkInformation); + logEpoch = m_logEpoch; + m_logEpoch = 0; } + + LOG_TRACE("Shutting down..."); + // Releasing the last dispatcher reference can join a worker that re-enters PAL. + taskDispatcher.reset(); + systemInformation.reset(); + deviceInformation.reset(); + networkInformation.reset(); + LOG_INFO("Shut down"); + detail::log_done(logEpoch); } #ifndef HAVE_MAT_UTC diff --git a/lib/pal/PAL.hpp b/lib/pal/PAL.hpp index f79cdd080..209d1ad6c 100644 --- a/lib/pal/PAL.hpp +++ b/lib/pal/PAL.hpp @@ -111,7 +111,11 @@ namespace PAL_NS_BEGIN MATSDK_LOG_DECL_COMPONENT_CLASS(); private: - volatile std::atomic m_palStarted { 0 }; + mutable std::mutex m_stateLock; + std::condition_variable m_stateChanged; + bool m_initializing = false; + size_t m_palStarted = 0; + uint64_t m_logEpoch = 0; std::shared_ptr m_taskDispatcher; std::shared_ptr m_SystemInformation; std::shared_ptr m_NetworkInformation; @@ -243,9 +247,11 @@ namespace PAL_NS_BEGIN namespace detail { bool log_init(bool isTraceEnabled, const std::string& traceFolderPath); + bool log_init(bool isTraceEnabled, const std::string& traceFolderPath, uint64_t& epoch); const std::unique_ptr& getDebugLogStream() noexcept; const std::string& getDebugLogPath() noexcept; void log_done(); + void log_done(uint64_t epoch); } #endif diff --git a/tests/functests/BasicFuncTests.cpp b/tests/functests/BasicFuncTests.cpp index 7f231b07b..a66d03c28 100644 --- a/tests/functests/BasicFuncTests.cpp +++ b/tests/functests/BasicFuncTests.cpp @@ -44,27 +44,31 @@ namespace PAL_NS_BEGIN static long GetPalRefCount() { - return PAL::GetPAL().m_palStarted.load(); + auto& pal = PAL::GetPAL(); + std::lock_guard lock(pal.m_stateLock); + return static_cast(pal.m_palStarted); }; static std::shared_ptr GetTaskDispatcher() { - return PAL::GetPAL().m_taskDispatcher; + auto& pal = PAL::GetPAL(); + std::lock_guard lock(pal.m_stateLock); + return pal.m_taskDispatcher; }; static std::shared_ptr GetSystemInformation() { - return PAL::GetPAL().m_SystemInformation; + return PAL::GetPAL().GetSystemInformation(); } static std::shared_ptr GetNetworkInformation() { - return PAL::GetPAL().m_NetworkInformation; + return PAL::GetPAL().GetNetworkInformation(); } static std::shared_ptr GetDeviceInformation() { - return PAL::GetPAL().m_DeviceInformation; + return PAL::GetPAL().GetDeviceInformation(); } }; } diff --git a/tests/no-exceptions/no-exceptions-smoke.cpp b/tests/no-exceptions/no-exceptions-smoke.cpp index d85307af3..e7d7a3fc0 100644 --- a/tests/no-exceptions/no-exceptions-smoke.cpp +++ b/tests/no-exceptions/no-exceptions-smoke.cpp @@ -5,7 +5,9 @@ #include "LogConfiguration.hpp" #include "CorrelationVector.hpp" #include "TransmitProfiles.hpp" +#include "config/RuntimeConfig_Default.hpp" #include "http/HttpResponseDecoder.hpp" +#include "pal/PAL.hpp" #include "utils/ZlibUtils.hpp" #include @@ -91,6 +93,22 @@ namespace int main() { + ILogConfiguration logConfiguration; + RuntimeConfig_Default runtimeConfiguration(logConfiguration); + PAL::PlatformAbstractionLayer pal; + pal.initialize(runtimeConfiguration); + pal.initialize(runtimeConfiguration); + check(pal.GetSystemInformation() && pal.GetDeviceInformation() && pal.GetNetworkInformation(), + "PAL initialization must publish all providers"); +#ifdef MATSDK_DISABLE_DEVICE_ID + check(pal.GetDeviceInformation()->GetDeviceId().empty(), "native device ID must remain disabled"); +#endif + pal.shutdown(); + check(pal.GetSystemInformation() != nullptr, "PAL must retain providers until its final shutdown"); + pal.shutdown(); + check(!pal.GetSystemInformation() && !pal.GetDeviceInformation() && !pal.GetNetworkInformation(), + "PAL final shutdown must release its providers"); + const char* invalidConfigurations[] = {nullptr, "", "[", "not-json", "null", "1", "\"text\"", "[]", "[{}]"}; for (const char* input : invalidConfigurations) { diff --git a/tests/unittests/PalTests.cpp b/tests/unittests/PalTests.cpp index 9d4ba3891..ca0ec9add 100644 --- a/tests/unittests/PalTests.cpp +++ b/tests/unittests/PalTests.cpp @@ -4,12 +4,16 @@ // #include "common/Common.hpp" +#include "config/RuntimeConfig_Default.hpp" +#include "pal/PAL.hpp" #include "pal/PseudoRandomGenerator.hpp" #include "pal/TaskDispatcher.hpp" #include "pal/WorkerThread.hpp" +#include "NullObjects.hpp" #include "Version.hpp" #include +#include #include #include #include @@ -22,13 +26,12 @@ #include #include #include +#include +#include #ifdef HAVE_MAT_LOGGING -#include "pal/PAL.hpp" #include #include -#include -#include #include #if defined(_WIN32) || defined(_WIN64) #include @@ -40,6 +43,35 @@ using namespace PAL::detail; using namespace testing; +namespace PAL_NS_BEGIN { +#ifdef HAVE_MAT_LOGGING + namespace detail + { + extern std::recursive_mutex debugLogMutex; + extern std::string debugLogPath; + extern std::unique_ptr debugLogStream; + extern bool isLoggingInited; + extern uint64_t debugLogEpoch; + } +#endif + + class PALTest + { + public: + static bool HasDispatcher(const PlatformAbstractionLayer& pal) + { + std::lock_guard lock(pal.m_stateLock); + return static_cast(pal.m_taskDispatcher); + } + + static bool IsInitializing(const PlatformAbstractionLayer& pal) + { + std::lock_guard lock(pal.m_stateLock); + return pal.m_initializing; + } + }; +} PAL_NS_END + #if defined(_WIN32) || defined(_WIN64) namespace PAL_NS_BEGIN { std::string formatWindowsOsFullVersion( @@ -51,7 +83,384 @@ namespace PAL_NS_BEGIN { } PAL_NS_END #endif -class PalTests : public Test {}; +namespace +{ +#ifdef HAVE_MAT_LOGGING + class ScopedLogState + { + public: + ScopedLogState() + { + std::lock_guard lock(PAL::detail::debugLogMutex); + m_path = std::move(PAL::detail::debugLogPath); + m_stream = std::move(PAL::detail::debugLogStream); + m_enabled = PAL::detail::isLoggingInited; + m_epoch = PAL::detail::debugLogEpoch; + PAL::detail::isLoggingInited = false; + } + + ~ScopedLogState() + { + std::lock_guard lock(PAL::detail::debugLogMutex); + PAL::detail::debugLogStream = std::move(m_stream); + PAL::detail::debugLogPath = std::move(m_path); + PAL::detail::debugLogEpoch = m_epoch; + PAL::detail::isLoggingInited = m_enabled; + } + + private: + std::string m_path; + std::unique_ptr m_stream; + uint64_t m_epoch = 0; + bool m_enabled = false; + }; +#endif + + class BlockingRuntimeConfig : public MAT::RuntimeConfig_Default + { + public: + BlockingRuntimeConfig( + MAT::ILogConfiguration& configuration, + std::promise& entered, + std::shared_future release) + : RuntimeConfig_Default(configuration), m_entered(entered), m_release(std::move(release)) + { + } + + bool HasConfig(const char* key) override + { + if (!m_blocked.exchange(true)) + { + m_entered.set_value(); + m_release.wait(); + } + return RuntimeConfig_Default::HasConfig(key); + } + + private: + std::promise& m_entered; + std::shared_future m_release; + std::atomic m_blocked { false }; + }; + +#if HAVE_EXCEPTIONS + class ThrowOnceRuntimeConfig : public MAT::RuntimeConfig_Default + { + public: + explicit ThrowOnceRuntimeConfig(MAT::ILogConfiguration& configuration) + : RuntimeConfig_Default(configuration) + { + } + + MAT::Variant& operator[](const char* key) override + { + if (m_throw && std::string(key) == CFG_BOOL_ENABLE_NET_DETECT) + { + m_throw = false; + throw std::runtime_error("PAL initialization failed"); + } + return RuntimeConfig_Default::operator[](key); + } + + private: + bool m_throw = true; + }; +#endif + + class ReinitializePalTask + { + public: + ReinitializePalTask( + PAL::PlatformAbstractionLayer& pal, + MAT::IRuntimeConfig& configuration, + std::shared_future release, + std::atomic& shouldInitialize, + std::promise& started, + std::promise& finished, + std::shared_ptr& replacement) + : m_pal(pal), + m_configuration(configuration), + m_release(std::move(release)), + m_shouldInitialize(shouldInitialize), + m_started(started), + m_finished(finished), + m_replacement(replacement) + { + } + + void Run() + { + m_started.set_value(); + m_release.wait(); + if (m_shouldInitialize.load()) + { + m_pal.initialize(m_configuration); + m_replacement = m_pal.getDefaultTaskDispatcher(); + m_pal.shutdown(); + } + m_finished.set_value(); + } + + private: + PAL::PlatformAbstractionLayer& m_pal; + MAT::IRuntimeConfig& m_configuration; + std::shared_future m_release; + std::atomic& m_shouldInitialize; + std::promise& m_started; + std::promise& m_finished; + std::shared_ptr& m_replacement; + }; + + class SignalTask + { + public: + explicit SignalTask(std::promise& signal) : m_signal(signal) {} + void Run() { m_signal.set_value(); } + + private: + std::promise& m_signal; + }; + + class BlockingTask + { + public: + BlockingTask(std::promise& started, std::shared_future release) + : m_started(started), m_release(std::move(release)) + { + } + + void Run() + { + m_started.set_value(); + m_release.wait(); + } + + private: + std::promise& m_started; + std::shared_future m_release; + }; + + class BlockingSemanticContext : public MAT::NullContext + { + public: + BlockingSemanticContext(std::promise& entered, std::shared_future release) + : m_entered(entered), m_release(std::move(release)) + { + } + + void SetDeviceId(const std::string& value) override + { + deviceId = value; + sawDeviceId = true; + } + + void SetDeviceModel(const std::string& value) override + { + m_entered.set_value(); + m_release.wait(); + deviceModel = value; + sawDeviceModel = true; + } + + void SetOsName(const std::string& value) override + { + osName = value; + sawOsName = true; + } + + void SetNetworkProvider(const std::string&) override + { + sawNetworkProvider = true; + } + + std::string deviceId; + std::string deviceModel; + std::string osName; + bool sawDeviceId = false; + bool sawDeviceModel = false; + bool sawOsName = false; + bool sawNetworkProvider = false; + + private: + std::promise& m_entered; + std::shared_future m_release; + }; +} + +class PalTests : public Test +{ +#ifdef HAVE_MAT_LOGGING + ScopedLogState m_logState; +#endif +}; + +TEST_F(PalTests, ConcurrentInitializationPublishesCompleteState) +{ + MAT::ILogConfiguration logConfiguration; + logConfiguration[CFG_BOOL_ENABLE_TRACE] = false; + std::promise firstEntered; + std::promise releaseFirst; + auto firstEnteredFuture = firstEntered.get_future(); + BlockingRuntimeConfig configuration(logConfiguration, firstEntered, releaseFirst.get_future().share()); + PAL::PlatformAbstractionLayer pal; + + struct Observation + { + std::shared_ptr system; + std::shared_ptr device; + std::shared_ptr network; + std::shared_ptr dispatcher; + }; + std::array observations; + auto initialize = [&](size_t index) { + pal.initialize(configuration); + observations[index].system = pal.GetSystemInformation(); + observations[index].device = pal.GetDeviceInformation(); + observations[index].network = pal.GetNetworkInformation(); + observations[index].dispatcher = pal.getDefaultTaskDispatcher(); + }; + + std::thread first([&] { initialize(0); }); + if (firstEnteredFuture.wait_for(std::chrono::seconds(5)) != std::future_status::ready) + { + releaseFirst.set_value(); + first.join(); + pal.shutdown(); + FAIL() << "The first initializer did not enter the configuration"; + } + + std::promise secondStarted; + std::promise secondFinished; + auto secondStartedFuture = secondStarted.get_future(); + auto secondFinishedFuture = secondFinished.get_future(); + std::thread second([&] { + secondStarted.set_value(); + initialize(1); + secondFinished.set_value(); + }); + secondStartedFuture.wait(); + + auto getter = std::async(std::launch::async, [&] { return pal.GetSystemInformation(); }); + const auto getterStatus = getter.wait_for(std::chrono::seconds(2)); + const auto secondStatus = secondFinishedFuture.wait_for(std::chrono::milliseconds(250)); + releaseFirst.set_value(); + first.join(); + second.join(); + + EXPECT_EQ(getterStatus, std::future_status::ready); + EXPECT_EQ(getter.get(), nullptr); + EXPECT_EQ(secondStatus, std::future_status::timeout); + EXPECT_THAT(observations[0].system, NotNull()); + EXPECT_THAT(observations[0].device, NotNull()); + EXPECT_THAT(observations[0].network, NotNull()); + EXPECT_THAT(observations[0].dispatcher, NotNull()); + EXPECT_EQ(observations[0].system, observations[1].system); + EXPECT_EQ(observations[0].device, observations[1].device); + EXPECT_EQ(observations[0].network, observations[1].network); + EXPECT_EQ(observations[0].dispatcher, observations[1].dispatcher); + + std::promise releaseShutdown; + auto shutdownGate = releaseShutdown.get_future().share(); + std::thread shutdown1([&] { shutdownGate.wait(); pal.shutdown(); }); + std::thread shutdown2([&] { shutdownGate.wait(); pal.shutdown(); }); + releaseShutdown.set_value(); + shutdown1.join(); + shutdown2.join(); + EXPECT_EQ(pal.GetSystemInformation(), nullptr); + EXPECT_EQ(pal.GetDeviceInformation(), nullptr); + EXPECT_EQ(pal.GetNetworkInformation(), nullptr); + EXPECT_FALSE(PAL::PALTest::HasDispatcher(pal)); +} + +#if HAVE_EXCEPTIONS +TEST_F(PalTests, FailedInitializationCanBeRetried) +{ + MAT::ILogConfiguration logConfiguration; + logConfiguration[CFG_BOOL_ENABLE_TRACE] = false; + ThrowOnceRuntimeConfig configuration(logConfiguration); + PAL::PlatformAbstractionLayer pal; + + EXPECT_THROW(pal.initialize(configuration), std::runtime_error); + ASSERT_FALSE(PAL::PALTest::IsInitializing(pal)); + EXPECT_FALSE(pal.GetSystemInformation()); + EXPECT_FALSE(pal.GetDeviceInformation()); + EXPECT_FALSE(pal.GetNetworkInformation()); + pal.initialize(configuration); + EXPECT_THAT(pal.GetSystemInformation(), NotNull()); + pal.shutdown(); +} +#endif + +TEST_F(PalTests, RetainedDispatcherSurvivesFinalShutdown) +{ + MAT::ILogConfiguration logConfiguration; + logConfiguration[CFG_BOOL_ENABLE_TRACE] = false; + MAT::RuntimeConfig_Default configuration(logConfiguration); + PAL::PlatformAbstractionLayer pal; + pal.initialize(configuration); + auto dispatcher = pal.getDefaultTaskDispatcher(); + pal.shutdown(); + + std::promise ran; + auto ranFuture = ran.get_future(); + SignalTask task(ran); + PAL::dispatchTask(dispatcher.get(), &task, &SignalTask::Run); + EXPECT_EQ(ranFuture.wait_for(std::chrono::seconds(5)), std::future_status::ready); + dispatcher.reset(); +} + +TEST_F(PalTests, WorkerCanReinitializeWhileFinalShutdownReleasesDispatcher) +{ + MAT::ILogConfiguration logConfiguration; + logConfiguration[CFG_BOOL_ENABLE_TRACE] = false; + MAT::RuntimeConfig_Default configuration(logConfiguration); + PAL::PlatformAbstractionLayer pal; + pal.initialize(configuration); + auto dispatcher = pal.getDefaultTaskDispatcher(); + + std::promise workerStarted; + std::promise releaseWorker; + std::promise workerFinished; + auto startedFuture = workerStarted.get_future(); + auto finishedFuture = workerFinished.get_future(); + std::atomic shouldInitialize { false }; + std::shared_ptr replacement; + ReinitializePalTask task( + pal, configuration, releaseWorker.get_future().share(), shouldInitialize, + workerStarted, workerFinished, replacement); + PAL::dispatchTask(dispatcher.get(), &task, &ReinitializePalTask::Run); + if (startedFuture.wait_for(std::chrono::seconds(5)) != std::future_status::ready) + { + releaseWorker.set_value(); + dispatcher.reset(); + pal.shutdown(); + FAIL() << "The worker did not start"; + } + dispatcher.reset(); + + std::thread shutdown([&] { pal.shutdown(); }); + std::promise dispatcherRetired; + auto retiredFuture = dispatcherRetired.get_future(); + std::thread observer([&] { + const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(5); + while (PAL::PALTest::HasDispatcher(pal) && std::chrono::steady_clock::now() < deadline) + { + std::this_thread::yield(); + } + dispatcherRetired.set_value(!PAL::PALTest::HasDispatcher(pal)); + }); + const bool retired = + retiredFuture.wait_for(std::chrono::seconds(5)) == std::future_status::ready && retiredFuture.get(); + shouldInitialize.store(retired); + releaseWorker.set_value(); + observer.join(); + const auto finished = finishedFuture.wait_for(std::chrono::seconds(5)); + shutdown.join(); + + EXPECT_TRUE(retired); + EXPECT_EQ(finished, std::future_status::ready); + EXPECT_THAT(replacement, NotNull()); +} TEST_F(PalTests, UuidGeneration) { @@ -189,24 +598,50 @@ TEST_F(PalTests, MonotonicTime) TEST_F(PalTests, SemanticContextPopulation) { - /* MockISemanticContext context; - - - EXPECT_CALL(context, SetAppId(Not(IsEmpty()))).WillOnce(DoDefault()); - EXPECT_CALL(context, SetAppVersion(_)).WillOnce(DoDefault()); - EXPECT_CALL(context, SetAppLanguage(_)).WillOnce(DoDefault()); - - EXPECT_CALL(context, SetUserLanguage(_)).WillOnce(DoDefault()); - EXPECT_CALL(context, SetUserTimeZone(Not(IsEmpty()))).WillOnce(DoDefault()); - //EXPECT_CALL(context, SetUserAdvertisingId(_)).WillOnce(DoDefault()); - - - PAL::registerSemanticContext(&context); + MAT::ILogConfiguration logConfiguration; + logConfiguration[CFG_BOOL_ENABLE_TRACE] = false; + MAT::RuntimeConfig_Default configuration(logConfiguration); + PAL::PlatformAbstractionLayer pal; + pal.initialize(configuration); + + ASSERT_TRUE(pal.GetDeviceInformation()); + ASSERT_TRUE(pal.GetSystemInformation()); + ASSERT_TRUE(pal.GetNetworkInformation()); + const auto expectedDeviceId = pal.GetDeviceInformation()->GetDeviceId(); + const auto expectedDeviceModel = pal.GetDeviceInformation()->GetModel(); + const auto expectedOsName = pal.GetSystemInformation()->GetOsName(); + + std::promise entered; + std::promise release; + auto enteredFuture = entered.get_future(); + BlockingSemanticContext context(entered, release.get_future().share()); + std::thread populate([&] { pal.registerSemanticContext(&context); }); + if (enteredFuture.wait_for(std::chrono::seconds(5)) != std::future_status::ready) + { + release.set_value(); + populate.join(); + pal.shutdown(); + FAIL() << "Semantic-context population did not reach the device callback"; + } - PAL::sleep(500); + auto shutdown = std::async(std::launch::async, [&] { pal.shutdown(); }); + const auto shutdownStatus = shutdown.wait_for(std::chrono::seconds(2)); + release.set_value(); + populate.join(); + shutdown.get(); - PAL::unregisterSemanticContext(&context); - */ + EXPECT_EQ(shutdownStatus, std::future_status::ready); +#ifdef MATSDK_DISABLE_DEVICE_ID + EXPECT_EQ(context.sawDeviceId, !expectedDeviceId.empty()); +#else + EXPECT_TRUE(context.sawDeviceId); +#endif + EXPECT_EQ(context.deviceId, expectedDeviceId); + EXPECT_TRUE(context.sawDeviceModel); + EXPECT_EQ(context.deviceModel, expectedDeviceModel); + EXPECT_TRUE(context.sawOsName); + EXPECT_EQ(context.osName, expectedOsName); + EXPECT_TRUE(context.sawNetworkProvider); } TEST_F(PalTests, SdkVersion) @@ -840,6 +1275,7 @@ TEST_F(PalTests, WorkerThreadSelfJoinSurvivesConcurrentFinalRelease) class LogInitTest : public Test { protected: + ScopedLogState m_logState; std::string validPath = "valid/path/"; void SetUp() override @@ -937,4 +1373,125 @@ TEST_F(LogInitTest, LogInitPathWithoutTrailingBackslash) EXPECT_TRUE(PAL::detail::getDebugLogStream()->is_open()); } +TEST_F(LogInitTest, OlderEpochCannotCloseCurrentLog) +{ + uint64_t oldEpoch = 0; + uint64_t currentEpoch = 0; + ASSERT_TRUE(log_init(true, validPath, oldEpoch)); + ASSERT_TRUE(log_init(true, validPath, currentEpoch)); + EXPECT_NE(oldEpoch, currentEpoch); + + log_done(oldEpoch); + ASSERT_TRUE(getDebugLogStream()); + EXPECT_TRUE(getDebugLogStream()->is_open()); + log_done(currentEpoch); + EXPECT_FALSE(getDebugLogStream()); +} + +TEST_F(LogInitTest, PalGenerationSurvivesOlderShutdown) +{ + MAT::ILogConfiguration firstLogConfiguration; + firstLogConfiguration[CFG_BOOL_ENABLE_TRACE] = true; + firstLogConfiguration[CFG_STR_TRACE_FOLDER_PATH] = validPath; + MAT::RuntimeConfig_Default firstConfiguration(firstLogConfiguration); + PAL::PlatformAbstractionLayer pal; + pal.initialize(firstConfiguration); + ASSERT_TRUE(getDebugLogStream()); + const std::string oldPath = getDebugLogPath(); + + auto dispatcher = pal.getDefaultTaskDispatcher(); + std::promise started; + std::promise release; + auto startedFuture = started.get_future(); + BlockingTask task(started, release.get_future().share()); + PAL::dispatchTask(dispatcher.get(), &task, &BlockingTask::Run); + if (startedFuture.wait_for(std::chrono::seconds(5)) != std::future_status::ready) + { + release.set_value(); + dispatcher.reset(); + pal.shutdown(); + FAIL() << "The worker did not start"; + } + dispatcher.reset(); + + std::thread shutdown([&] { pal.shutdown(); }); + const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(5); + while (PAL::PALTest::HasDispatcher(pal) && std::chrono::steady_clock::now() < deadline) + { + std::this_thread::yield(); + } + if (PAL::PALTest::HasDispatcher(pal)) + { + release.set_value(); + shutdown.join(); + FAIL() << "The old dispatcher was not retired"; + } + + const auto separator = validPath.find_last_of("/\\", validPath.size() - 2); + const std::string otherDirectory = validPath.substr(0, separator + 1); + MAT::ILogConfiguration secondLogConfiguration; + secondLogConfiguration[CFG_BOOL_ENABLE_TRACE] = true; + secondLogConfiguration[CFG_STR_TRACE_FOLDER_PATH] = otherDirectory; + MAT::RuntimeConfig_Default secondConfiguration(secondLogConfiguration); + pal.initialize(secondConfiguration); + const std::string newPath = getDebugLogPath(); + release.set_value(); + shutdown.join(); + + EXPECT_NE(newPath, oldPath); + ASSERT_TRUE(getDebugLogStream()); + EXPECT_TRUE(getDebugLogStream()->is_open()); + EXPECT_EQ(getDebugLogPath(), newPath); + pal.shutdown(); + std::remove(oldPath.c_str()); +} + +TEST_F(LogInitTest, NewEpochUsesItsConfiguredPath) +{ + uint64_t oldEpoch = 0; + uint64_t currentEpoch = 0; + ASSERT_TRUE(log_init(true, validPath, oldEpoch)); + const std::string oldPath = getDebugLogPath(); + const auto separator = validPath.find_last_of("/\\", validPath.size() - 2); + ASSERT_NE(separator, std::string::npos); + const std::string otherDirectory = validPath.substr(0, separator + 1); + + const bool opened = log_init(true, otherDirectory, currentEpoch); + EXPECT_TRUE(opened); + if (opened) + { + EXPECT_NE(getDebugLogPath(), oldPath); + log_done(oldEpoch); + EXPECT_TRUE(getDebugLogStream()); + } + std::remove(oldPath.c_str()); +} + +TEST_F(LogInitTest, FailedOpenIsNotReusedAsSuccessfulLogging) +{ + uint64_t epoch = 0; + ASSERT_TRUE(log_init(true, validPath, epoch)); + const std::string blockedPath = getDebugLogPath(); + log_done(epoch); + ASSERT_EQ(std::remove(blockedPath.c_str()), 0); +#if defined(_WIN32) || defined(_WIN64) + ASSERT_TRUE(CreateDirectoryA(blockedPath.c_str(), nullptr)); +#else + ASSERT_EQ(mkdir(blockedPath.c_str(), 0777), 0); +#endif + + uint64_t firstEpoch = 0; + uint64_t secondEpoch = 0; + EXPECT_FALSE(log_init(true, validPath, firstEpoch)); + EXPECT_FALSE(log_init(true, validPath, secondEpoch)); + EXPECT_EQ(firstEpoch, 0u); + EXPECT_EQ(secondEpoch, 0u); + log_done(); +#if defined(_WIN32) || defined(_WIN64) + EXPECT_TRUE(RemoveDirectoryA(blockedPath.c_str())); +#else + EXPECT_EQ(rmdir(blockedPath.c_str()), 0); +#endif +} + #endif