2020#include " TaskContext.h"
2121
2222#include < gtest/gtest.h>
23+ #include < olp/core/client/Condition.h>
2324
2425namespace {
2526
@@ -31,6 +32,8 @@ using Response = ApiResponse<ResponseType, ApiError>;
3132using ExecuteFunc = std::function<Response(CancellationContext)>;
3233using Callback = std::function<void (Response)>;
3334
35+ const auto kWaitTime = std::chrono::seconds(2 );
36+
3437class TaskContextTestable : public TaskContext {
3538 public:
3639 std::function<void (void )> notify;
@@ -115,23 +118,7 @@ TEST(TaskContextTest, ExecuteSimple) {
115118}
116119
117120TEST (TaskContextTest, BlockingCancel) {
118- std::mutex execution_mutex, cancelation_mutex;
119- std::condition_variable execution_cv, cancelation_cv;
120-
121- auto execution_wait = [&]() {
122- std::unique_lock<std::mutex> lock (execution_mutex);
123- EXPECT_EQ (execution_cv.wait_for (lock, std::chrono::seconds (2 )),
124- std::cv_status::no_timeout);
125- };
126-
127- auto cancelation_wait = [&]() {
128- std::unique_lock<std::mutex> lock (cancelation_mutex);
129- EXPECT_EQ (cancelation_cv.wait_for (lock, std::chrono::seconds (2 )),
130- std::cv_status::no_timeout);
131- };
132-
133121 ExecuteFunc func = [&](CancellationContext c) -> Response {
134- execution_wait ();
135122 EXPECT_TRUE (c.IsCancelled ());
136123 return std::string (" Success" );
137124 };
@@ -142,21 +129,11 @@ TEST(TaskContextTest, BlockingCancel) {
142129
143130 TaskContext context = TaskContext::Create (func, callback);
144131
145- std::thread execute_thread ([&]() { context. Execute (); } );
132+ EXPECT_FALSE (context. BlockingCancel ( std::chrono::seconds ( 0 )) );
146133
147- std::thread cancel_thread ([&]() {
148- cancelation_wait ();
149- context.BlockingCancel ();
150- });
134+ std::thread cancel_thread ([&]() { EXPECT_TRUE (context.BlockingCancel ()); });
151135
152- // wait threads to start and reach the task execution
153- std::this_thread::sleep_for (std::chrono::milliseconds (10 ));
154- // reach cancel
155- cancelation_cv.notify_one ();
156- // wait till execution starts
157- std::this_thread::sleep_for (std::chrono::milliseconds (10 ));
158- // check for conditions and continue execution
159- execution_cv.notify_one ();
136+ std::thread execute_thread ([&]() { context.Execute (); });
160137
161138 execute_thread.join ();
162139 cancel_thread.join ();
@@ -202,13 +179,12 @@ TEST(TaskContextTest, BlockingCancelIsWaiting) {
202179}
203180
204181TEST (TaskContextTest, CancelToken) {
205- std::mutex execution_mutex ;
206- std::condition_variable execution_cv ;
182+ Condition continue_execution ;
183+ Condition execution_started ;
207184
208185 auto execution_wait = [&]() {
209- std::unique_lock<std::mutex> lock (execution_mutex);
210- EXPECT_EQ (execution_cv.wait_for (lock, std::chrono::seconds (2 )),
211- std::cv_status::no_timeout);
186+ execution_started.Notify ();
187+ EXPECT_TRUE (continue_execution.Wait (kWaitTime ));
212188 };
213189
214190 ExecuteFunc func = [&](CancellationContext c) -> Response {
@@ -225,14 +201,11 @@ TEST(TaskContextTest, CancelToken) {
225201
226202 std::thread execute_thread ([&]() { context.Execute (); });
227203
228- auto token = context.CancelToken ();
204+ EXPECT_TRUE (execution_started.Wait ());
205+
206+ context.CancelToken ().cancel ();
229207
230- // wait till execution starts
231- std::this_thread::sleep_for (std::chrono::milliseconds (10 ));
232- // cancel operation
233- token.cancel ();
234- // check for conditions and continue execution
235- execution_cv.notify_one ();
208+ continue_execution.Notify ();
236209
237210 execute_thread.join ();
238211
0 commit comments