From 05591d70286fd7c51ec59b04c2639779e114f6e1 Mon Sep 17 00:00:00 2001 From: LucioDonda Date: Tue, 13 Aug 2024 11:58:00 -0300 Subject: [PATCH] fix: cmakes changes, headers and memeber names --- src/agent/CMakeLists.txt | 2 +- src/agent/queue/CMakeLists.txt | 13 ++++---- src/agent/queue/include/queue.hpp | 2 +- src/agent/queue/include/sqlitestorage.h | 6 ++-- src/agent/queue/src/queue.cpp | 7 ---- src/agent/queue/src/sqlitestorage.cpp | 6 ++-- src/agent/queue/tests/CMakeLists.txt | 43 +++++++++---------------- src/agent/queue/tests/queue_test.cpp | 2 +- 8 files changed, 30 insertions(+), 51 deletions(-) diff --git a/src/agent/CMakeLists.txt b/src/agent/CMakeLists.txt index 51cda998b9..af39fd2116 100644 --- a/src/agent/CMakeLists.txt +++ b/src/agent/CMakeLists.txt @@ -20,6 +20,7 @@ set(SOURCES add_subdirectory(agent_info) add_subdirectory(communicator) add_subdirectory(configuration_parser) +add_subdirectory(queue) find_package(OpenSSL REQUIRED) find_package(Boost REQUIRED COMPONENTS asio beast) @@ -31,5 +32,4 @@ target_link_libraries(Agent PUBLIC ConfigurationParser Communicator AgentInfo PR if(BUILD_TESTS) enable_testing() add_subdirectory(tests) - add_subdirectory(queue) endif() diff --git a/src/agent/queue/CMakeLists.txt b/src/agent/queue/CMakeLists.txt index f8e4283a01..bf7c6aa090 100644 --- a/src/agent/queue/CMakeLists.txt +++ b/src/agent/queue/CMakeLists.txt @@ -1,26 +1,25 @@ cmake_minimum_required(VERSION 3.22) -project(queue LANGUAGES CXX) +project(AgentQueue LANGUAGES CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED True) set(CMAKE_CXX_FLAGS "-Wall -Wextra -Wunused -pthread") -include_directories(${CMAKE_SOURCE_DIR}/include) - find_package(SQLiteCpp REQUIRED) find_package(nlohmann_json REQUIRED) find_package(fmt REQUIRED) -find_package(Boost REQUIRED COMPONENTS asio beast) +find_package(Boost REQUIRED COMPONENTS asio) -add_library(queue +add_library(AgentQueue src/sqlitestorage.cpp src/queue.cpp ) -target_include_directories(queue PRIVATE include ${SQLiteCpp_INCLUDE_DIRS}) -target_link_libraries(queue PRIVATE SQLiteCpp nlohmann_json::nlohmann_json fmt::fmt Boost::asio) +target_include_directories(AgentQueue PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include PRIVATE ${SQLiteCpp_INCLUDE_DIRS}) +target_link_libraries(AgentQueue PUBLIC nlohmann_json::nlohmann_json Boost::asio PRIVATE SQLiteCpp nlohmann_json::nlohmann_json fmt::fmt Boost::asio) if(BUILD_TESTS) + enable_testing() add_subdirectory(tests) endif() diff --git a/src/agent/queue/include/queue.hpp b/src/agent/queue/include/queue.hpp index f400aa8ab6..5390fe1d17 100644 --- a/src/agent/queue/include/queue.hpp +++ b/src/agent/queue/include/queue.hpp @@ -17,7 +17,7 @@ #include "sqlitestorage.h" // TODO: move to a configuration setting -constexpr int DEFAULT_MAX = 10; +constexpr int DEFAULT_MAX = 10000; constexpr int DEFAULT_TIMEOUT_S = 3; // Factory class diff --git a/src/agent/queue/include/sqlitestorage.h b/src/agent/queue/include/sqlitestorage.h index bbc4b6c53d..e01d085de0 100644 --- a/src/agent/queue/include/sqlitestorage.h +++ b/src/agent/queue/include/sqlitestorage.h @@ -126,11 +126,11 @@ class SQLiteStorage : public Persistence /// Mutex to ensure thread-safe operations. std::mutex m_mutex; - /// @brief condition variable to wait for database access + /// @brief condition variable to wait for database access. std::condition_variable m_cv; - // TODO: should it be atomic? - bool m_db_in_use = false; + // @brief flag for notifying the use of the db. + bool m_dbInUse = false; }; #endif // SQLITE_STORAGE_H diff --git a/src/agent/queue/src/queue.cpp b/src/agent/queue/src/queue.cpp index 9052c3ccb1..0b4ea0062c 100644 --- a/src/agent/queue/src/queue.cpp +++ b/src/agent/queue/src/queue.cpp @@ -1,14 +1,7 @@ -#include #include -#include #include -#include -#include -#include #include -#include #include -#include #include "queue.hpp" diff --git a/src/agent/queue/src/sqlitestorage.cpp b/src/agent/queue/src/sqlitestorage.cpp index fde8f4a8e5..855d209e75 100644 --- a/src/agent/queue/src/sqlitestorage.cpp +++ b/src/agent/queue/src/sqlitestorage.cpp @@ -46,14 +46,14 @@ void SQLiteStorage::InitializeTable(const std::string& tableName) void SQLiteStorage::waitForDatabaseAccess() { std::unique_lock lock(m_mutex); - m_cv.wait(lock, [this] { return !m_db_in_use; }); - m_db_in_use = true; + m_cv.wait(lock, [this] { return !m_dbInUse; }); + m_dbInUse = true; } void SQLiteStorage::releaseDatabaseAccess() { std::lock_guard lock(m_mutex); - m_db_in_use = false; + m_dbInUse = false; m_cv.notify_one(); } diff --git a/src/agent/queue/tests/CMakeLists.txt b/src/agent/queue/tests/CMakeLists.txt index aea65975ff..8af9802bdc 100644 --- a/src/agent/queue/tests/CMakeLists.txt +++ b/src/agent/queue/tests/CMakeLists.txt @@ -1,36 +1,23 @@ find_package(GTest REQUIRED) -# find_package(SQLiteCpp REQUIRED) -# find_package(nlohmann_json REQUIRED) -include_directories(../include) -include_directories(${GTEST_INCLUDE_DIRS}) -include_directories(${SQLiteCpp_INCLUDE_DIRS}) +# AgentQueue tests +add_executable(test_AgentQueue queue_test.cpp) -#TODO: is this reusable ? -# file(GLOB QUEUE_UNIT_TEST_SRC "*.cpp") +target_link_libraries(test_AgentQueue PUBLIC + AgentQueue + GTest::gtest + GTest::gtest_main + GTest::gmock + GTest::gmock_main) -add_executable(test_queue - queue_test.cpp - ../src/sqlitestorage.cpp - ) -target_link_libraries(test_queue - ${GTEST_LIBRARIES} - ${GTEST_MAIN_LIBRARIES} - queue - SQLiteCpp - nlohmann_json::nlohmann_json - fmt::fmt - Boost::asio) - -# Create a test executable for SQLiteStorage tests -add_executable(test_sqlitestorage - sqlitestorage_test.cpp - ../src/sqlitestorage.cpp) +# SQLiteStorage tests +add_executable(test_sqlitestorage sqlitestorage_test.cpp) target_link_libraries(test_sqlitestorage - ${GTEST_LIBRARIES} - ${GTEST_MAIN_LIBRARIES} + AgentQueue SQLiteCpp - nlohmann_json::nlohmann_json - fmt::fmt) \ No newline at end of file + GTest::gtest + GTest::gtest_main + GTest::gmock + GTest::gmock_main) diff --git a/src/agent/queue/tests/queue_test.cpp b/src/agent/queue/tests/queue_test.cpp index c706c16b90..944c0e0a27 100644 --- a/src/agent/queue/tests/queue_test.cpp +++ b/src/agent/queue/tests/queue_test.cpp @@ -578,7 +578,7 @@ TEST_F(QueueTest, FifoOrderCheck) EXPECT_EQ(singleMessage.data.at("data"), R"({"Data" : "for STATEFUL)" + std::to_string(++i) + R"("})"); } - // Kepp the order of the message: FIFO + // Keep the order of the message: FIFO for (int i : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) { auto messageReceived = queue.getNextN(messageType, 1);