diff options
author | Niall Douglas (s [underscore] sourceforge {at} nedprod [dot] com) <spamtrap@nedprod.com> | 2017-09-26 16:27:21 +0300 |
---|---|---|
committer | Niall Douglas (s [underscore] sourceforge {at} nedprod [dot] com) <spamtrap@nedprod.com> | 2017-09-26 16:28:15 +0300 |
commit | 25d7169b711bbd25c465034fadfe7e2fb4509beb (patch) | |
tree | 1320099aa2edf2f040e428b4d28497681530e07f /test/tests/coroutines.cpp | |
parent | 8e1729b4843159ad483d12f7ee78eace56ee01b9 (diff) |
Fixed racy hang in coroutines unit test.
Diffstat (limited to 'test/tests/coroutines.cpp')
-rw-r--r-- | test/tests/coroutines.cpp | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/test/tests/coroutines.cpp b/test/tests/coroutines.cpp index 3a570d51..c5fa3065 100644 --- a/test/tests/coroutines.cpp +++ b/test/tests/coroutines.cpp @@ -94,23 +94,31 @@ static inline void TestPostSelfToRunCoroutines() afio::io_service service; std::atomic<bool> ready(false); auto runthreadid = QUICKCPPLIB_NAMESPACE::utils::thread::this_thread_id(); - auto coroutine = [&]() -> std::future<void> { - auto thisthreadid = QUICKCPPLIB_NAMESPACE::utils::thread::this_thread_id(); - BOOST_CHECK(thisthreadid != runthreadid); - ready = true; - co_await afio::io_service::awaitable_post_to_self(service); - thisthreadid = QUICKCPPLIB_NAMESPACE::utils::thread::this_thread_id(); - BOOST_CHECK(thisthreadid == runthreadid); + auto coroutinethread = [&]() -> void { + auto coroutine = [&]() -> std::future<void> { + auto thisthreadid = QUICKCPPLIB_NAMESPACE::utils::thread::this_thread_id(); + BOOST_CHECK(thisthreadid != runthreadid); + ready = true; + co_await afio::io_service::awaitable_post_to_self(service); + thisthreadid = QUICKCPPLIB_NAMESPACE::utils::thread::this_thread_id(); + BOOST_CHECK(thisthreadid == runthreadid); + // std::cout << "Coroutine exiting" << std::endl; + ready = false; + }; + // std::cout << "Thread waiting on coroutine" << std::endl; + coroutine().get(); + // std::cout << "Thread exiting" << std::endl; }; - auto asynch = std::async(std::launch::async, coroutine); + auto asynch = std::async(std::launch::async, coroutinethread); while(!ready) { std::this_thread::yield(); } - while(!service.run()) - ; - auto r = asynch.get(); - r.get(); + while(ready) + { + service.run().value(); + } + asynch.get(); #endif } |