mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-07 01:06:45 +08:00
continue adding event ut
This commit is contained in:
parent
7ec99c3394
commit
f930dfe34d
@ -36,7 +36,7 @@ LIBIMP_EXPORT ::LIBIMP::result<void> evt_set(evt_t) noexcept;
|
|||||||
LIBIMP_EXPORT ::LIBIMP::result<bool> evt_wait(evt_t, std::int64_t ms) noexcept;
|
LIBIMP_EXPORT ::LIBIMP::result<bool> evt_wait(evt_t, std::int64_t ms) noexcept;
|
||||||
|
|
||||||
/// \brief Waits until one or all of the specified objects are in the signaled state or the time-out interval elapses.
|
/// \brief Waits until one or all of the specified objects are in the signaled state or the time-out interval elapses.
|
||||||
LIBIMP_EXPORT ::LIBIMP::result<bool> evt_wait(::LIBIMP::span<evt_t>, std::int64_t ms) noexcept;
|
LIBIMP_EXPORT ::LIBIMP::result<bool> evt_wait(::LIBIMP::span<evt_t const>, std::int64_t ms) noexcept;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief The event object.
|
* \brief The event object.
|
||||||
|
|||||||
@ -1,8 +1,6 @@
|
|||||||
|
|
||||||
#include "libimp/log.h"
|
#include "libimp/log.h"
|
||||||
|
|
||||||
#include "libimp/detect_plat.h"
|
|
||||||
|
|
||||||
#include "libimp/detect_plat.h"
|
#include "libimp/detect_plat.h"
|
||||||
#if defined(LIBIMP_OS_WIN)
|
#if defined(LIBIMP_OS_WIN)
|
||||||
# include "libipc/platform/win/event_impl.h"
|
# include "libipc/platform/win/event_impl.h"
|
||||||
|
|||||||
@ -42,7 +42,10 @@ bool is_valid(evt_t evt) noexcept {
|
|||||||
result<evt_t> evt_open(std::string name) noexcept {
|
result<evt_t> evt_open(std::string name) noexcept {
|
||||||
LIBIMP_LOG_();
|
LIBIMP_LOG_();
|
||||||
auto t_name = detail::to_tstring(name);
|
auto t_name = detail::to_tstring(name);
|
||||||
auto h = ::CreateEvent(detail::get_sa(), FALSE, FALSE, t_name.c_str());
|
auto h = ::CreateEvent(detail::get_sa(),
|
||||||
|
/*bManualReset*/ FALSE,
|
||||||
|
/*bInitialState*/FALSE,
|
||||||
|
/*lpName*/ t_name.c_str());
|
||||||
if (h == NULL) {
|
if (h == NULL) {
|
||||||
auto err = sys::error();
|
auto err = sys::error();
|
||||||
log.error("failed: CreateEvent(FALSE, FALSE, ", name, "). error = ", err);
|
log.error("failed: CreateEvent(FALSE, FALSE, ", name, "). error = ", err);
|
||||||
@ -119,7 +122,7 @@ result<bool> evt_wait(evt_t evt, std::int64_t ms) noexcept {
|
|||||||
* \brief Waits until one or all of the specified objects are in the signaled state or the time-out interval elapses.
|
* \brief Waits until one or all of the specified objects are in the signaled state or the time-out interval elapses.
|
||||||
* \see https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-waitformultipleobjects
|
* \see https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-waitformultipleobjects
|
||||||
*/
|
*/
|
||||||
result<bool> evt_wait(::LIBIMP::span<evt_t> evts, std::int64_t ms) noexcept {
|
result<bool> evt_wait(::LIBIMP::span<evt_t const> evts, std::int64_t ms) noexcept {
|
||||||
LIBIMP_LOG_();
|
LIBIMP_LOG_();
|
||||||
if (evts.empty()) {
|
if (evts.empty()) {
|
||||||
log.error("evts handle is empty.");
|
log.error("evts handle is empty.");
|
||||||
|
|||||||
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
#include <libipc/event.h>
|
#include <libipc/event.h>
|
||||||
@ -10,10 +12,88 @@ TEST(event, open_close) {
|
|||||||
ASSERT_TRUE(ipc::evt_close(*evt));
|
ASSERT_TRUE(ipc::evt_close(*evt));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(event, wait_timeout) {
|
||||||
|
auto evt = ipc::evt_open("test");
|
||||||
|
ASSERT_TRUE(evt);
|
||||||
|
ASSERT_FALSE(*ipc::evt_wait(*evt, 0));
|
||||||
|
ASSERT_TRUE(ipc::evt_close(*evt));
|
||||||
|
}
|
||||||
|
|
||||||
TEST(event, set_wait) {
|
TEST(event, set_wait) {
|
||||||
auto evt = ipc::evt_open("test");
|
auto evt = ipc::evt_open("test");
|
||||||
ASSERT_TRUE(evt);
|
ASSERT_TRUE(evt);
|
||||||
ASSERT_TRUE(ipc::evt_set(*evt));
|
ASSERT_TRUE(ipc::evt_set(*evt));
|
||||||
ASSERT_TRUE(ipc::evt_wait(*evt, 0));
|
ASSERT_TRUE(*ipc::evt_wait(*evt, 0));
|
||||||
ASSERT_TRUE(ipc::evt_close(*evt));
|
ASSERT_TRUE(ipc::evt_close(*evt));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(event, unicast) {
|
||||||
|
auto evt = ipc::evt_open("test");
|
||||||
|
ASSERT_TRUE(evt);
|
||||||
|
int success = 0;
|
||||||
|
auto wait = [evt, &success] {
|
||||||
|
if (*ipc::evt_wait(*evt, 200)) ++success;
|
||||||
|
};
|
||||||
|
auto th1 = std::thread(wait);
|
||||||
|
auto th2 = std::thread(wait);
|
||||||
|
auto th3 = std::thread(wait);
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||||
|
ASSERT_TRUE(ipc::evt_set(*evt));
|
||||||
|
th1.join();
|
||||||
|
th2.join();
|
||||||
|
th3.join();
|
||||||
|
ASSERT_TRUE(ipc::evt_close(*evt));
|
||||||
|
ASSERT_EQ(success, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(event, wait_multi_timeout) {
|
||||||
|
auto evt1 = ipc::evt_open("test1");
|
||||||
|
ASSERT_TRUE(evt1);
|
||||||
|
auto evt2 = ipc::evt_open("test2");
|
||||||
|
ASSERT_TRUE(evt2);
|
||||||
|
auto evt3 = ipc::evt_open("test3");
|
||||||
|
ASSERT_TRUE(evt3);
|
||||||
|
ASSERT_FALSE(*ipc::evt_wait(imp::make_span({*evt1, *evt2, *evt3}), 0));
|
||||||
|
ASSERT_TRUE(ipc::evt_close(*evt1));
|
||||||
|
ASSERT_TRUE(ipc::evt_close(*evt2));
|
||||||
|
ASSERT_TRUE(ipc::evt_close(*evt3));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(event, set_wait_multi) {
|
||||||
|
auto evt1 = ipc::evt_open("test1");
|
||||||
|
ASSERT_TRUE(evt1);
|
||||||
|
auto evt2 = ipc::evt_open("test2");
|
||||||
|
ASSERT_TRUE(evt2);
|
||||||
|
auto evt3 = ipc::evt_open("test3");
|
||||||
|
ASSERT_TRUE(evt3);
|
||||||
|
ASSERT_TRUE(ipc::evt_set(*evt2));
|
||||||
|
ASSERT_TRUE(*ipc::evt_wait(imp::make_span({*evt1, *evt2, *evt3}), 0));
|
||||||
|
ASSERT_TRUE(ipc::evt_close(*evt1));
|
||||||
|
ASSERT_TRUE(ipc::evt_close(*evt2));
|
||||||
|
ASSERT_TRUE(ipc::evt_close(*evt3));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(event, unicast_multi) {
|
||||||
|
auto evt1 = ipc::evt_open("test1");
|
||||||
|
ASSERT_TRUE(evt1);
|
||||||
|
auto evt2 = ipc::evt_open("test2");
|
||||||
|
ASSERT_TRUE(evt2);
|
||||||
|
auto evt3 = ipc::evt_open("test3");
|
||||||
|
ASSERT_TRUE(evt3);
|
||||||
|
int success = 0;
|
||||||
|
auto wait = [evt1, evt2, evt3, &success] {
|
||||||
|
if (*ipc::evt_wait(imp::make_span({*evt1, *evt2, *evt3}), 200)) ++success;
|
||||||
|
};
|
||||||
|
auto th1 = std::thread(wait);
|
||||||
|
auto th2 = std::thread(wait);
|
||||||
|
auto th3 = std::thread(wait);
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||||
|
ASSERT_TRUE(ipc::evt_set(*evt3));
|
||||||
|
th1.join();
|
||||||
|
th2.join();
|
||||||
|
th3.join();
|
||||||
|
ASSERT_TRUE(ipc::evt_close(*evt1));
|
||||||
|
ASSERT_TRUE(ipc::evt_close(*evt2));
|
||||||
|
ASSERT_TRUE(ipc::evt_close(*evt3));
|
||||||
|
ASSERT_EQ(success, 1);
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user