From c6ac8f4abe5713264b43d6590d939a0fd516e601 Mon Sep 17 00:00:00 2001 From: ben Date: Sat, 17 Aug 2024 19:39:48 +0300 Subject: [PATCH] Add build of shared_mem lib, add UT --- CMakeLists.txt | 7 ++-- sh_mem/CMakeLists.txt | 6 +++ sh_mem/main.cpp | 2 +- unit_tests/CMakeLists.txt | 6 ++- unit_tests/MessagingTest.cpp | 79 ++++++++++++++++++++++++++++++++++++ 5 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 sh_mem/CMakeLists.txt create mode 100644 unit_tests/MessagingTest.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d347d9..c3aa69e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,8 @@ IF (UNIX OR APPLE) include_directories(${SQLite3_INCLUDE_DIRS} ${JPEG_INCLUDE_DIR}) ENDIF() +add_subdirectory(sh_mem) + set(AM_SOURCE_FILES analyze/AffinityComparer.cpp analyze/ThresholdDiffChecker.cpp @@ -37,9 +39,6 @@ set(AM_SOURCE_FILES extraction/BmpExtractor.cpp extraction/MultipleExtractor.cpp common/Timers.hpp - sh_mem/ProcCommunicator.cpp - sh_mem/SharedMemoryReceiver.cpp - sh_mem/SharedMemorySender.cpp AmApi.cpp ) @@ -51,7 +50,7 @@ ENDIF() add_executable(aquamarine main.cpp) -target_link_libraries(aquamarine aquamarine_lib ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries(aquamarine aquamarine_lib shared_mem ${CMAKE_THREAD_LIBS_INIT}) IF (UNIX OR APPLE) target_link_libraries(aquamarine ${SQLite3_LIBRARIES} ${JPEG_LIBRARIES} ) ENDIF() diff --git a/sh_mem/CMakeLists.txt b/sh_mem/CMakeLists.txt new file mode 100644 index 0000000..f93db28 --- /dev/null +++ b/sh_mem/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required (VERSION 3.0) + +project (shared_mem) + +ADD_LIBRARY( shared_mem STATIC + ProcCommunicator.cpp SharedMemoryReceiver.cpp SharedMemorySender.cpp ) \ No newline at end of file diff --git a/sh_mem/main.cpp b/sh_mem/main.cpp index 0df2c01..0b0273f 100644 --- a/sh_mem/main.cpp +++ b/sh_mem/main.cpp @@ -4,7 +4,7 @@ #include #include -static const std::string shared_mem_name{"/sh_mem5"}; +static const std::string shared_mem_name{"/shmsh1"}; // Function to be executed in a separate thread void backgroundTask() diff --git a/unit_tests/CMakeLists.txt b/unit_tests/CMakeLists.txt index 4733470..4357d00 100644 --- a/unit_tests/CMakeLists.txt +++ b/unit_tests/CMakeLists.txt @@ -27,7 +27,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) #include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${SQLite3_INCLUDE_DIRS} ${JPEG_INCLUDE_DIR}) -include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${AM_INC}) +include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${AM_INC} ${AM_INC}/sh_mem) IF (UNIX) include_directories(${SQLite3_INCLUDE_DIRS} ${JPEG_INCLUDE_DIR}) ENDIF() @@ -44,6 +44,8 @@ FetchContent_MakeAvailable(googletest) file(GLOB UT_SOURCES "*.cpp") +add_subdirectory(../sh_mem ${CMAKE_BINARY_DIR}/sh_mem) + add_executable(aquamarine_ut ${AM_SRC}/extraction/BmpExtractor.cpp ${AM_SRC}/extraction/JpgExtractor.cpp @@ -61,7 +63,7 @@ add_executable(aquamarine_ut ${AM_SRC}/database/DataBaseCommunicator.cpp ${UT_SOURCES} ) -target_link_libraries(aquamarine_ut gtest_main ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries(aquamarine_ut gtest_main shared_mem ${CMAKE_THREAD_LIBS_INIT}) IF (UNIX) target_link_libraries(aquamarine_ut ${SQLite3_LIBRARIES} ${JPEG_LIBRARIES} ) ENDIF() diff --git a/unit_tests/MessagingTest.cpp b/unit_tests/MessagingTest.cpp new file mode 100644 index 0000000..85cdb63 --- /dev/null +++ b/unit_tests/MessagingTest.cpp @@ -0,0 +1,79 @@ +#include "ProcCommunicator.h" +#include +#include +#include +#include +#include "gtest/gtest.h" +static const std::string shared_mem_name{"/sh_mem_100"}; + +// Function to be executed in a separate thread +void backgroundTask() +{ + std::cout << "Background task started...\n"; + ProcCommunicator *slave = new ProcCommunicator(false, true, shared_mem_name); + Message *res = nullptr; + + int counter = 0; + + while (counter < 2) + { + Message msg{777, MessageType::HANDSHAKE_OK}; + std::cout << "Background Slave rec\n"; + Message *res = slave->receive(); + msg.id = res->id; + slave->send(&msg); + counter++; + } + + delete slave; + std::cout << "Background task completed.\n"; +} + +void backgroundTaskMasterMaster() +{ + std::cout << "Background Master task started...\n"; + ProcCommunicator master(true, true, shared_mem_name); + Message msg_hand{1, MessageType::HANDSHAKE}; + + int counter = 0; + + while (counter < 1) + {std::cout << "Background Master 1send\n"; + master.send(&msg_hand); + std::cout << "Background Master rec\n"; + auto msg_resp = master.receive(); + //std::cout << "m 1 =" << msg_resp->id << std::endl; + EXPECT_TRUE(msg_resp->id==1); + + counter++; + } + + std::cout << "Background Master task completed.\n"; +} + +TEST(MessagingTest, TestMultiMasterMode) +{ + std::cout << "Main thread starts...\n"; + std::vector vec1{1, 2, 3, 4, 5}; + + Message msg_hand{2, MessageType::HANDSHAKE}; + ProcCommunicator master(true, true, shared_mem_name); + + std::thread worker(backgroundTask); + std::thread worker_master(backgroundTaskMasterMaster); + + // std::this_thread::sleep_for(std::chrono::seconds(5)); + int counter = 0; + + while (counter < 1) + { + master.send(&msg_hand); + auto msg_resp = master.receive(); + EXPECT_TRUE(msg_resp->id==2); + counter++; + } + if (worker.joinable()) + worker.join(); + if (worker_master.joinable()) + worker_master.join(); +} \ No newline at end of file