Skip to content

Commit 18784ab

Browse files
Reduce flakiness in TaskContext tests
Introduce sequence of operations in test on Condition instead of sleep. Relates-To: OLPEDGE-909 Signed-off-by: Diachenko Mykahilo <[email protected]>
1 parent 1fa9c00 commit 18784ab

File tree

2 files changed

+20
-47
lines changed

2 files changed

+20
-47
lines changed

olp-cpp-sdk-dataservice-read/src/TaskContext.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ class TaskContext {
4343

4444
void Execute() const { impl_->Execute(); }
4545

46-
void BlockingCancel(
46+
bool BlockingCancel(
4747
std::chrono::milliseconds timeout = std::chrono::seconds(60)) const {
48-
impl_->BlockingCancel(timeout);
48+
return impl_->BlockingCancel(timeout);
4949
}
5050

5151
client::CancellationToken CancelToken() const { return impl_->CancelToken(); }
@@ -71,7 +71,7 @@ class TaskContext {
7171
public:
7272
virtual ~Impl() = default;
7373
virtual void Execute() = 0;
74-
virtual void BlockingCancel(std::chrono::milliseconds timeout) = 0;
74+
virtual bool BlockingCancel(std::chrono::milliseconds timeout) = 0;
7575
virtual client::CancellationToken CancelToken() = 0;
7676
};
7777

@@ -128,9 +128,9 @@ class TaskContext {
128128
state_.store(State::COMPLETED);
129129
}
130130

131-
void BlockingCancel(std::chrono::milliseconds timeout) override {
131+
bool BlockingCancel(std::chrono::milliseconds timeout) override {
132132
if (state_.load() == State::COMPLETED) {
133-
return;
133+
return true;
134134
}
135135

136136
// Cancel operation and wait for notification
@@ -141,7 +141,7 @@ class TaskContext {
141141
execute_func_ = nullptr;
142142
}
143143

144-
condition_.Wait(timeout);
144+
return condition_.Wait(timeout);
145145
}
146146

147147
client::CancellationToken CancelToken() override {

olp-cpp-sdk-dataservice-read/tests/TaskContextTest.cpp

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "TaskContext.h"
2121

2222
#include <gtest/gtest.h>
23+
#include <olp/core/client/Condition.h>
2324

2425
namespace {
2526

@@ -31,6 +32,8 @@ using Response = ApiResponse<ResponseType, ApiError>;
3132
using ExecuteFunc = std::function<Response(CancellationContext)>;
3233
using Callback = std::function<void(Response)>;
3334

35+
const auto kWaitTime = std::chrono::seconds(2);
36+
3437
class TaskContextTestable : public TaskContext {
3538
public:
3639
std::function<void(void)> notify;
@@ -115,23 +118,7 @@ TEST(TaskContextTest, ExecuteSimple) {
115118
}
116119

117120
TEST(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

204181
TEST(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

Comments
 (0)