1+ #include " condy/async_operations.hpp"
2+ #include " condy/execution.hpp"
3+ #include " condy/runtime.hpp"
4+ #include " condy/sender_operations.hpp"
5+ #include < doctest/doctest.h>
6+ #include < exec/when_any.hpp>
7+
8+ namespace ex = stdexec;
9+
10+ TEST_CASE (" test execution - schedule" ) {
11+ condy::Runtime runtime;
12+ std::thread::id runtime_thread_id;
13+ std::thread runtime_thread ([&] {
14+ runtime_thread_id = std::this_thread::get_id ();
15+ runtime.run ();
16+ });
17+
18+ auto scheduler = condy::get_scheduler (runtime);
19+
20+ bool executed = false ;
21+ ex::sender auto sender = ex::schedule (scheduler) | ex::then ([&] {
22+ executed = true ;
23+ return std::this_thread::get_id ();
24+ });
25+
26+ auto [thread_id] = ex::sync_wait (sender).value ();
27+ REQUIRE (executed);
28+ REQUIRE (thread_id == runtime_thread_id);
29+ REQUIRE (runtime_thread_id != std::this_thread::get_id ());
30+
31+ runtime.allow_exit ();
32+ runtime_thread.join ();
33+ }
34+
35+ TEST_CASE (" test execution - sender" ) {
36+ condy::Runtime runtime;
37+ std::thread runtime_thread ([&] { runtime.run (); });
38+
39+ auto scheduler = condy::get_scheduler (runtime);
40+
41+ bool executed = false ;
42+ ex::sender auto sender = ex::schedule (scheduler) | ex::let_value ([&] {
43+ executed = true ;
44+ return condy::async_nop ();
45+ });
46+
47+ auto [r] = ex::sync_wait (sender).value ();
48+ REQUIRE (executed);
49+ REQUIRE (r == 0 );
50+
51+ runtime.allow_exit ();
52+ runtime_thread.join ();
53+ }
54+
55+ TEST_CASE (" test execution - when_all" ) {
56+ condy::Runtime runtime;
57+ std::thread runtime_thread ([&] { runtime.run (); });
58+
59+ auto scheduler = condy::get_scheduler (runtime);
60+
61+ bool executed1 = false ;
62+ bool executed2 = false ;
63+
64+ auto sender1 = ex::schedule (scheduler) | ex::then ([&] {
65+ executed1 = true ;
66+ return 42 ;
67+ });
68+ auto sender2 = ex::schedule (scheduler) | ex::then ([&] {
69+ executed2 = true ;
70+ return 0 ;
71+ });
72+
73+ auto when_all_sender = ex::when_all (sender1, sender2);
74+ auto [r1, r2] = ex::sync_wait (when_all_sender).value ();
75+
76+ REQUIRE (executed1);
77+ REQUIRE (executed2);
78+ REQUIRE (r1 == 42 );
79+ REQUIRE (r2 == 0 );
80+
81+ runtime.allow_exit ();
82+ runtime_thread.join ();
83+ }
84+
85+ TEST_CASE (" test execution - when_any" ) {
86+ condy::Runtime runtime;
87+ std::thread runtime_thread ([&] { runtime.run (); });
88+
89+ auto scheduler = condy::get_scheduler (runtime);
90+
91+ __kernel_timespec ts = {
92+ .tv_sec = 60ll * 60ll ,
93+ .tv_nsec = 0 ,
94+ };
95+ auto sender = ex::schedule (scheduler) | ex::let_value ([&] {
96+ return exec::when_any (condy::async_timeout (&ts, 0 , 0 ),
97+ condy::async_nop ());
98+ });
99+
100+ auto [r] = ex::sync_wait (sender).value ();
101+ REQUIRE (r == 0 );
102+
103+ runtime.allow_exit ();
104+ runtime_thread.join ();
105+ }
106+
107+ TEST_CASE (" test execution - when_any with different thread" ) {
108+ condy::Runtime runtime1, runtime2;
109+ std::thread thread1 ([&] { runtime1.run (); });
110+ std::thread thread2 ([&] { runtime2.run (); });
111+
112+ auto scheduler1 = condy::get_scheduler (runtime1);
113+ auto scheduler2 = condy::get_scheduler (runtime2);
114+
115+ __kernel_timespec ts = {
116+ .tv_sec = 60ll * 60ll ,
117+ .tv_nsec = 0 ,
118+ };
119+ auto sender1 = ex::schedule (scheduler1) | ex::let_value ([&] {
120+ return condy::async_timeout (&ts, 0 , 0 );
121+ });
122+ auto sender2 = ex::schedule (scheduler2) | ex::then ([] { return 42 ; });
123+ auto when_any_sender = exec::when_any (sender1, sender2);
124+ auto [r] = ex::sync_wait (when_any_sender).value ();
125+ REQUIRE (r == 42 );
126+
127+ runtime1.allow_exit ();
128+ runtime2.allow_exit ();
129+ thread1.join ();
130+ thread2.join ();
131+ }
132+
133+ TEST_CASE (" test execution - condy when_all" ) {
134+ using condy::operators::operator &&;
135+
136+ condy::Runtime runtime;
137+ std::thread runtime_thread ([&] { runtime.run (); });
138+
139+ auto scheduler = condy::get_scheduler (runtime);
140+
141+ bool executed = false ;
142+ ex::sender auto sender = ex::schedule (scheduler) | ex::let_value ([&] {
143+ executed = true ;
144+ return condy::async_nop () &&
145+ condy::async_nop () &&
146+ condy::async_nop ();
147+ });
148+
149+ auto [r1, r2, r3] = ex::sync_wait (sender).value ();
150+ REQUIRE (executed);
151+ REQUIRE (r1 == 0 );
152+ REQUIRE (r2 == 0 );
153+ REQUIRE (r3 == 0 );
154+
155+ runtime.allow_exit ();
156+ runtime_thread.join ();
157+ }
0 commit comments