Skip to content

Commit

Permalink
fix: cmakes changes, headers and memeber names
Browse files Browse the repository at this point in the history
  • Loading branch information
LucioDonda committed Aug 13, 2024
1 parent 2e93ebd commit 05591d7
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/agent/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
13 changes: 6 additions & 7 deletions src/agent/queue/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion src/agent/queue/include/queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/agent/queue/include/sqlitestorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 0 additions & 7 deletions src/agent/queue/src/queue.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
#include <any>
#include <chrono>
#include <condition_variable>
#include <iostream>
#include <memory>
#include <mutex>
#include <queue>
#include <stop_token>
#include <unordered_map>
#include <utility>
#include <vector>

#include "queue.hpp"

Expand Down
6 changes: 3 additions & 3 deletions src/agent/queue/src/sqlitestorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ void SQLiteStorage::InitializeTable(const std::string& tableName)
void SQLiteStorage::waitForDatabaseAccess()
{
std::unique_lock<std::mutex> 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<std::mutex> lock(m_mutex);
m_db_in_use = false;
m_dbInUse = false;
m_cv.notify_one();
}

Expand Down
43 changes: 15 additions & 28 deletions src/agent/queue/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
GTest::gtest
GTest::gtest_main
GTest::gmock
GTest::gmock_main)
2 changes: 1 addition & 1 deletion src/agent/queue/tests/queue_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 05591d7

Please sign in to comment.