diff --git a/test/stop_on_request_test.cpp b/test/stop_on_request_test.cpp index 4a0da62e..26474c48 100644 --- a/test/stop_on_request_test.cpp +++ b/test/stop_on_request_test.cpp @@ -18,10 +18,8 @@ #include #include #include -#include #include #include -#include #include #include #include @@ -31,6 +29,51 @@ #include +#if !UNIFEX_NO_COROUTINES + +# include +# include + +TEST(StopOnRequest, MultiThreadedCancellations) { + constexpr size_t iterations = 10; + constexpr size_t numSources = 5; + unifex::inplace_stop_source stopSources[iterations * numSources]; + + auto makeTask = [](unifex::inplace_stop_source& stopSource) + -> unifex::nothrow_task { + stopSource.request_stop(); + co_return; + }; + + bool wasCancelled = false; + + for (size_t i = 0; i < iterations; i++) { + std::vector> tasks; + unifex::single_thread_context threads[numSources]; + for (size_t j = 0; j < numSources; j++) { + tasks.emplace_back(unifex::on( + threads[j].get_scheduler(), + makeTask(stopSources[i * numSources + j]))); + } + auto cancellationSender = unifex::stop_on_request( + stopSources[i * numSources].get_token(), + stopSources[i * numSources + 1].get_token(), + stopSources[i * numSources + 2].get_token(), + stopSources[i * numSources + 3].get_token(), + stopSources[i * numSources + 4].get_token()) | + unifex::let_done([&]() { + wasCancelled = true; + return unifex::just(); + }); + unifex::sync_wait(unifex::when_all( + unifex::when_all_range(std::move(tasks)), cancellationSender)); + EXPECT_TRUE(wasCancelled); + wasCancelled = false; + } +} + +#endif // !UNIFEX_NO_COROUTINES + // Dummy stop source and token class to test callback construction exception // handling namespace { @@ -246,44 +289,6 @@ TEST(StopOnRequest, ReceiverAndStopSourceCancellationsBeforeConstruction) { EXPECT_TRUE(wasCancelled); } -TEST(StopOnRequest, MultiThreadedCancellations) { - constexpr size_t iterations = 10; - constexpr size_t numSources = 5; - unifex::inplace_stop_source stopSources[iterations * numSources]; - - auto makeTask = [](unifex::inplace_stop_source& stopSource) - -> unifex::nothrow_task { - stopSource.request_stop(); - co_return; - }; - - bool wasCancelled = false; - - for (size_t i = 0; i < iterations; i++) { - std::vector> tasks; - unifex::single_thread_context threads[numSources]; - for (size_t j = 0; j < numSources; j++) { - tasks.emplace_back(unifex::on( - threads[j].get_scheduler(), - makeTask(stopSources[i * numSources + j]))); - } - auto cancellationSender = unifex::stop_on_request( - stopSources[i * numSources].get_token(), - stopSources[i * numSources + 1].get_token(), - stopSources[i * numSources + 2].get_token(), - stopSources[i * numSources + 3].get_token(), - stopSources[i * numSources + 4].get_token()) | - unifex::let_done([&]() { - wasCancelled = true; - return unifex::just(); - }); - unifex::sync_wait(unifex::when_all( - unifex::when_all_range(std::move(tasks)), cancellationSender)); - EXPECT_TRUE(wasCancelled); - wasCancelled = false; - } -} - TEST(StopOnRequest, StopAfterComplete) { unifex::inplace_stop_source externalStopSource;