From 89f1c684d73ce275671f2cf5c312dd75af1f18d8 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Wed, 21 Oct 2020 03:39:05 +0300 Subject: add Shared{Memory,Object} classes --- test/unit_tests/shmem.cpp | 72 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 test/unit_tests/shmem.cpp (limited to 'test/unit_tests') diff --git a/test/unit_tests/shmem.cpp b/test/unit_tests/shmem.cpp new file mode 100644 index 0000000..26f1389 --- /dev/null +++ b/test/unit_tests/shmem.cpp @@ -0,0 +1,72 @@ +// Copyright (c) 2020 Egor Tensin +// This file is part of the "winapi-common" project. +// For details, see https://github.com/egor-tensin/winapi-common. +// Distributed under the MIT License. + +#include + +#include +#include + +#include +#include +#include + +using namespace winapi; + +namespace { + +BOOST_CONSTEXPR_OR_CONST auto shmem_name = "test-data-struct"; + +BOOST_CONSTEXPR_OR_CONST int main_data = -1; +BOOST_CONSTEXPR_OR_CONST int setter1_data = 69; +BOOST_CONSTEXPR_OR_CONST int setter2_data = 420; + +struct DataStruct { + std::mutex mtx; + std::condition_variable cv; + int data; +}; + +void setter1_main() { + const auto data_struct = SharedObject::open(shmem_name); + std::unique_lock lck{data_struct->mtx}; + BOOST_TEST_MESSAGE(data_struct.ptr()); + + data_struct->cv.wait(lck, [&]() { return data_struct->data == main_data; }); + data_struct->data = setter1_data; + data_struct->cv.notify_all(); +} + +void setter2_main() { + auto data_struct = SharedObject::open(shmem_name); + std::unique_lock lck{data_struct->mtx}; + BOOST_TEST_MESSAGE(data_struct.ptr()); + + data_struct->cv.wait(lck, [&]() { return data_struct->data == setter1_data; }); + data_struct->data = setter2_data; + data_struct->cv.notify_all(); +} + +} // namespace + +BOOST_AUTO_TEST_SUITE(shmem_tests) + +BOOST_AUTO_TEST_CASE(basic) { + auto data_struct = SharedObject::create(shmem_name); + + std::thread setter1{&setter1_main}; + std::thread setter2{&setter2_main}; + { + std::lock_guard lck{data_struct->mtx}; + data_struct->data = main_data; + } + data_struct->cv.notify_all(); + + setter1.join(); + setter2.join(); + + BOOST_TEST(data_struct->data == setter2_data); +} + +BOOST_AUTO_TEST_SUITE_END() -- cgit v1.2.3