kokkos-execution 0.0.1
Loading...
Searching...
No Matches
test_upon_stopped.cpp
Go to the documentation of this file.
1#include "gtest/gtest.h"
2
4PRAGMA_DIAGNOSTIC_PUSH
6#include "exec/single_thread_context.hpp"
7#include "exec/static_thread_pool.hpp"
8PRAGMA_DIAGNOSTIC_POP
9
11
22
23namespace Tests {
24
26using env_with_stop_token_t = stdexec::env<stdexec::prop<stdexec::get_stop_token_t, stdexec::inplace_stop_token>>;
27
29template <typename Sndr>
31 static_assert(!stdexec::sends_stopped<Sndr, stdexec::env<>>);
32 static_assert(stdexec::sends_stopped<Sndr, env_with_stop_token_t>);
33
34 return true;
35}
36
38template <bool Stop, stdexec::sender SchdSndr>
39void test_stop_before_start(SchdSndr&& schd_sndr) {
40 stdexec::inplace_stop_source iss; // NOLINT(misc-const-correctness)
41
42 if constexpr (Stop)
43 iss.request_stop();
44
45 unsigned int value = 0;
46
47 auto sndr_then = std::forward<SchdSndr>(schd_sndr) | stdexec::then([&value]() noexcept {
48 value += 12;
49 return 666;
50 });
51
53
54 static_assert(
55 stdexec::get_completion_signatures<decltype(sndr_then), stdexec::env<>>()
56 == stdexec::completion_signatures<stdexec::set_value_t(int)>{});
57
58 auto sndr_upon_stopped = std::move(sndr_then) | stdexec::upon_stopped([&value]() noexcept {
59 value += 56;
60 return 42;
61 });
62
63 static_assert(
64 stdexec::get_completion_signatures<decltype(sndr_then), stdexec::env<>>()
65 == stdexec::completion_signatures<stdexec::set_value_t(int)>{});
66
68 static_assert(STDEXEC::__never_sends<stdexec::set_stopped_t, decltype(sndr_upon_stopped), env_with_stop_token_t>);
69
70 auto sndr = std::move(sndr_upon_stopped)
71 | stdexec::write_env(stdexec::prop{stdexec::get_stop_token, iss.get_token()});
72
73 const auto res = stdexec::sync_wait(std::move(sndr));
74
75 ASSERT_TRUE(res.has_value());
76 EXPECT_EQ(std::get<0>(*res), Stop ? 42 : 666);
77 ASSERT_EQ(value, Stop ? 56 : 12);
78}
79
80template <typename T>
81requires requires(T& arg) {
82 { arg.get_scheduler() } -> stdexec::scheduler;
83}
84class UponStoppedTest : public testing::Test { };
85
86TYPED_TEST_SUITE_P(UponStoppedTest);
87
89 static_assert(test_stopped_advertised_iff_stop_token<decltype(stdexec::schedule(
90 std::declval<TypeParam>().get_scheduler()))>());
91
92 TypeParam scheduler_provider{};
93
94 test_stop_before_start<true>(stdexec::schedule(scheduler_provider.get_scheduler()));
95 test_stop_before_start<false>(stdexec::schedule(scheduler_provider.get_scheduler()));
96}
97
98REGISTER_TYPED_TEST_SUITE_P(UponStoppedTest, stop_before_start);
99
100using UponStoppedTestTypes = testing::Types<exec::static_thread_pool, exec::single_thread_context>;
101
102INSTANTIATE_TYPED_TEST_SUITE_P(UponStoppedTestInstantiation, UponStoppedTest, UponStoppedTestTypes);
103
104} // namespace Tests
#define KOKKOS_EXECUTION_STDEXEC_PRAGMA_DIAGNOSTIC_IGNORED
Basic list of ignored diagnostics when including anything from stdexec.
void test_stop_before_start(SchdSndr &&schd_sndr)
stdexec::env< stdexec::prop< stdexec::get_stop_token_t, stdexec::inplace_stop_token > > env_with_stop_token_t
An environment queryable for stdexec::get_stop_token_t.
testing::Types< exec::static_thread_pool, exec::single_thread_context > UponStoppedTestTypes
consteval bool test_stopped_advertised_iff_stop_token()