Skip to content

Commit c0c6445

Browse files
authored
Add check if an operation was cancelled (#1332)
Add checking if the download operation was cancelled after getting a result and not wait for all operations to be done. Relates-To: OLPEDGE-2746 Signed-off-by: Yevhenii Dudnyk <[email protected]>
1 parent 849fbd4 commit c0c6445

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ class DownloadItemsJob {
8484
if (response.IsSuccessful()) {
8585
requests_succeeded_++;
8686
} else {
87+
if (response.GetError().GetErrorCode() ==
88+
olp::client::ErrorCode::Cancelled &&
89+
user_callback_) {
90+
auto user_callback = std::move(user_callback_);
91+
if (user_callback) {
92+
user_callback({{client::ErrorCode::Cancelled, "Cancelled"}});
93+
}
94+
return;
95+
}
96+
8797
requests_failed_++;
8898
}
8999

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,18 @@ class QueryMetadataJob {
112112
}
113113
}
114114

115+
if (canceled_) {
116+
download_job_->OnPrefetchCompleted(
117+
{{client::ErrorCode::Cancelled, "Cancelled"}});
118+
return;
119+
}
120+
115121
if (!--query_count_) {
116122
if (CheckIfFail()) {
117123
download_job_->OnPrefetchCompleted(query_errors_.front());
118124
return;
119125
}
120126

121-
if (canceled_) {
122-
download_job_->OnPrefetchCompleted(
123-
{{client::ErrorCode::Cancelled, "Cancelled"}});
124-
return;
125-
}
126-
127127
if (filter_) {
128128
query_result_ = filter_(std::move(query_result_));
129129
}

tests/integration/olp-cpp-sdk-dataservice-read/VersionedLayerClientPrefetchPartitionsTest.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,62 @@ TEST_F(VersionedLayerClientPrefetchPartitionsTest, PrefetchPartitionsFails) {
216216
}
217217
}
218218

219+
TEST_F(VersionedLayerClientPrefetchPartitionsTest, PrefetchPartitionCancelled) {
220+
auto partitions_count = 1;
221+
auto client =
222+
read::VersionedLayerClient(kCatalogHrn, kLayer, boost::none, settings_);
223+
auto partitions = GeneratePartitionIds(partitions_count);
224+
auto partitions_response =
225+
ReadDefaultResponses::GeneratePartitionsResponse(partitions_count);
226+
const auto request =
227+
read::PrefetchPartitionsRequest().WithPartitionIds(partitions);
228+
{
229+
SCOPED_TRACE("Get version fails");
230+
231+
ExpectVersionRequest(
232+
http::NetworkResponse().WithStatus(HttpStatusCode::BAD_REQUEST));
233+
234+
auto future = client.PrefetchPartitions(request).GetFuture();
235+
ASSERT_NE(future.wait_for(std::chrono::seconds(kTimeout)),
236+
std::future_status::timeout);
237+
238+
auto response = future.get();
239+
ASSERT_FALSE(response.IsSuccessful());
240+
}
241+
{
242+
SCOPED_TRACE("Query partition fails");
243+
ExpectVersionRequest();
244+
ExpectQueryPartitionsRequest(
245+
partitions, partitions_response,
246+
http::NetworkResponse().WithStatus(HttpStatusCode::BAD_REQUEST));
247+
248+
auto future = client.PrefetchPartitions(request).GetFuture();
249+
ASSERT_NE(future.wait_for(std::chrono::seconds(kTimeout)),
250+
std::future_status::timeout);
251+
252+
auto response = future.get();
253+
ASSERT_FALSE(response.IsSuccessful());
254+
}
255+
{
256+
SCOPED_TRACE("Get data cancelled");
257+
ExpectQueryPartitionsRequest(partitions, partitions_response);
258+
259+
ExpectBlobRequest(
260+
partitions_response.GetPartitions().begin()->GetDataHandle(), "data",
261+
http::NetworkResponse().WithStatus(
262+
static_cast<int>(http::ErrorCode::CANCELLED_ERROR)));
263+
264+
auto test_request = client.PrefetchPartitions(request);
265+
auto future = test_request.GetFuture();
266+
ASSERT_NE(future.wait_for(std::chrono::seconds(kTimeout)),
267+
std::future_status::timeout);
268+
269+
auto response = future.get();
270+
ASSERT_FALSE(response.IsSuccessful());
271+
ASSERT_EQ(response.GetError().GetErrorCode(), client::ErrorCode::Cancelled);
272+
}
273+
}
274+
219275
TEST_F(VersionedLayerClientPrefetchPartitionsTest, PrefetchBatchFails) {
220276
// Should result in two metadata query
221277
constexpr auto partitions_count = 200u;

0 commit comments

Comments
 (0)