Skip to content

Commit

Permalink
Add build of shared_mem lib, add UT
Browse files Browse the repository at this point in the history
  • Loading branch information
ben committed Aug 17, 2024
1 parent 03eec44 commit c6ac8f4
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 7 deletions.
7 changes: 3 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)

Expand All @@ -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()
6 changes: 6 additions & 0 deletions sh_mem/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required (VERSION 3.0)

project (shared_mem)

ADD_LIBRARY( shared_mem STATIC
ProcCommunicator.cpp SharedMemoryReceiver.cpp SharedMemorySender.cpp )
2 changes: 1 addition & 1 deletion sh_mem/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <chrono>
#include <mutex>

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()
Expand Down
6 changes: 4 additions & 2 deletions unit_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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()
Expand Down
79 changes: 79 additions & 0 deletions unit_tests/MessagingTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "ProcCommunicator.h"
#include <iostream>
#include <thread>
#include <chrono>
#include <mutex>
#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<int> 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();
}

0 comments on commit c6ac8f4

Please sign in to comment.