From 1438cfc6972a6ffb6f0ece37d51de47963a3e5b5 Mon Sep 17 00:00:00 2001 From: Tom Date: Mon, 16 Sep 2019 18:13:18 +0200 Subject: [PATCH] test: add cases wait-for-value and wait-for-predicate --- test/test.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/test/test.cpp b/test/test.cpp index bb922ad..751993c 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -46,6 +46,45 @@ TEST_CASE("condition-get-set") REQUIRE( condition.get() == 42 ); } +TEST_CASE("wait-for-value") +{ + sgnl::AtomicCondition condition(23); + std::future future = + std::async( + std::launch::async, + [&] { condition.wait_for(std::chrono::hours(1000), 42); }); + + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + std::this_thread::yield(); + + condition.set(42); + condition.notify_one(); + future.get(); + + REQUIRE( condition.get() == 42 ); +} + +TEST_CASE("wait-for-predicate") +{ + sgnl::AtomicCondition condition(23); + std::future future = + std::async( + std::launch::async, + [&] { + auto pred = [&]() { return condition.get() == 42; }; + condition.wait_for(std::chrono::hours(1000), pred); + }); + + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + std::this_thread::yield(); + + condition.set(42); + condition.notify_one(); + future.get(); + + REQUIRE( condition.get() == 42 ); +} + TEST_CASE("constructor-thread-blocks-signals") { sgnl::AtomicCondition condition(false);