Skip to content

Commit 426c502

Browse files
committed
Fixed test CancelPendingRequestsCatalog
Made PendingRequests::CancelPendingRequests() to first cancel all requests then check for completion Now ErrorCode::RequestTimeout is propagated properly to user Resolves: OLPEDGE-678 Signed-off-by: Serhii Lozynskyi <[email protected]>
1 parent a958ac0 commit 426c502

File tree

8 files changed

+69
-60
lines changed

8 files changed

+69
-60
lines changed

olp-cpp-sdk-core/include/olp/core/client/PendingRequests.h

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,18 @@ class PendingRequests final {
4040
~PendingRequests();
4141

4242
/**
43-
* @brief Cancels all pending requests.
43+
* @brief Cancels all pending tasks
44+
* @note This call does not wait for the tasks to finalize, use
45+
* CancelAllAndWait() to also wait for the tasks to finalize..
4446
* @return True on success
4547
*/
46-
bool CancelPendingRequests();
48+
bool CancelAll();
49+
50+
/**
51+
* @brief Cancels all pending tasks and waits for all beeing finalized.
52+
* @return True on success
53+
*/
54+
bool CancelAllAndWait();
4755

4856
/**
4957
* @brief Generates a placehoder for request cancellation token and returns a
@@ -82,11 +90,15 @@ class PendingRequests final {
8290

8391
private:
8492
int64_t key_ = 0;
85-
std::unordered_map<int64_t, client::CancellationToken> requests_map_;
86-
std::unordered_set<client::TaskContext, client::TaskContextHash>
87-
task_contexts_;
93+
using RequestMap = std::unordered_map<int64_t, client::CancellationToken>;
94+
using ContextMap =
95+
std::unordered_set<client::TaskContext, client::TaskContextHash>;
96+
RequestMap requests_map_;
97+
ContextMap task_contexts_;
98+
8899
std::mutex requests_lock_;
89100
};
90101

91102
} // namespace client
92-
} // namespace olp
103+
} // namespace olp
104+

olp-cpp-sdk-core/include/olp/core/client/TaskContext.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ class TaskContext {
150150
auto response = function(context_);
151151
// Cancel could occur during function execution, in that case we ignore
152152
// the response.
153-
if (!context_.IsCancelled()) {
153+
if (!context_.IsCancelled() ||
154+
(!response.IsSuccessful() &&
155+
response.GetError().GetErrorCode() == ErrorCode::RequestTimeout)) {
154156
user_response = std::move(response);
155157
}
156158
}
@@ -170,7 +172,9 @@ class TaskContext {
170172
}
171173

172174
// Cancel operation and wait for notification
173-
context_.CancelOperation();
175+
if (!context_.IsCancelled()) {
176+
context_.CancelOperation();
177+
}
174178

175179
{
176180
std::lock_guard<std::mutex> lock(mutex_);

olp-cpp-sdk-core/src/client/PendingRequests.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919

2020
#include <olp/core/client/PendingRequests.h>
21-
2221
#include <olp/core/client/TaskContext.h>
2322
#include <olp/core/logging/Log.h>
2423

@@ -33,17 +32,35 @@ PendingRequests::PendingRequests(){};
3332

3433
PendingRequests::~PendingRequests(){};
3534

36-
bool PendingRequests::CancelPendingRequests() {
37-
requests_lock_.lock();
38-
// Create local copy of the requests to cancel
39-
auto contexts = std::move(task_contexts_);
40-
auto requests_map = requests_map_;
35+
bool PendingRequests::CancelAll() {
36+
RequestMap requests_map;
37+
ContextMap contexts;
38+
{
39+
std::lock_guard<std::mutex> lk(requests_lock_);
40+
requests_map = requests_map_;
41+
contexts = task_contexts_;
42+
}
4143

42-
requests_lock_.unlock();
4344
for (auto& pair : requests_map) {
4445
pair.second.cancel();
4546
}
4647

48+
for (auto context : contexts) {
49+
context.CancelToken().cancel();
50+
}
51+
52+
return true;
53+
}
54+
55+
bool PendingRequests::CancelAllAndWait() {
56+
CancelAll();
57+
58+
ContextMap contexts;
59+
{
60+
std::lock_guard<std::mutex> lk(requests_lock_);
61+
contexts = std::move(task_contexts_);
62+
}
63+
4764
for (auto context : contexts) {
4865
if (!context.BlockingCancel()) {
4966
OLP_SDK_LOG_WARNING(kLogTag, "Timeout, when waiting on BlockingCancel");

olp-cpp-sdk-core/tests/client/PendingRequestsTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ TEST(PendingRequestsTest, RemoveMissingKeyWillFail) {
3636
EXPECT_FALSE(pending_request.Remove(0));
3737
}
3838

39-
TEST(PendingRequestsTest, CancelAllPendingRequest) {
39+
TEST(PendingRequestsTest, CancelAll) {
4040
PendingRequests pending_request;
4141
auto key = pending_request.GenerateRequestPlaceholder();
4242
bool cancelled = false;
4343
auto token = olp::client::CancellationToken([&]() { cancelled = true; });
4444
EXPECT_TRUE(pending_request.Insert(token, key));
45-
EXPECT_TRUE(pending_request.CancelPendingRequests());
45+
EXPECT_TRUE(pending_request.CancelAll());
4646
EXPECT_TRUE(cancelled);
4747
}
4848

olp-cpp-sdk-dataservice-read/src/CatalogClientImpl.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <olp/core/client/OlpClientSettingsFactory.h>
2424
#include <olp/core/client/PendingRequests.h>
2525
#include <olp/core/logging/Log.h>
26+
2627
#include "Common.h"
2728
#include "PrefetchTilesProvider.h"
2829
#include "repositories/ApiRepository.h"
@@ -55,11 +56,13 @@ CatalogClientImpl::CatalogClientImpl(HRN catalog, OlpClientSettings settings)
5556
pending_requests_ = std::make_shared<client::PendingRequests>();
5657
}
5758

58-
CatalogClientImpl::~CatalogClientImpl() { CancelPendingRequests(); }
59+
CatalogClientImpl::~CatalogClientImpl() {
60+
pending_requests_->CancelAllAndWait();
61+
}
5962

6063
bool CatalogClientImpl::CancelPendingRequests() {
6164
OLP_SDK_LOG_TRACE(kLogTag, "CancelPendingRequests");
62-
return pending_requests_->CancelPendingRequests();
65+
return pending_requests_->CancelAll();
6366
}
6467

6568
CancellationToken CatalogClientImpl::GetCatalog(

olp-cpp-sdk-dataservice-read/src/VersionedLayerClientImpl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ VersionedLayerClientImpl::VersionedLayerClientImpl(
7777
}
7878

7979
VersionedLayerClientImpl::~VersionedLayerClientImpl() {
80-
pending_requests_->CancelPendingRequests();
80+
pending_requests_->CancelAllAndWait();
8181
}
8282

8383
client::CancellationToken VersionedLayerClientImpl::GetPartitions(

olp-cpp-sdk-dataservice-read/src/VolatileLayerClientImpl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ VolatileLayerClientImpl::VolatileLayerClientImpl(
6868
}
6969

7070
VolatileLayerClientImpl::~VolatileLayerClientImpl() {
71-
pending_requests_->CancelPendingRequests();
71+
pending_requests_->CancelAllAndWait();
7272
}
7373

7474
client::CancellationToken VolatileLayerClientImpl::GetPartitions(

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

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include <olp/core/logging/Log.h>
2626
#include <olp/core/porting/make_unique.h>
2727
#include <olp/dataservice/read/CatalogClient.h>
28+
#include <olp/dataservice/read/model/Catalog.h>
29+
2830
#include "CatalogClientTestBase.h"
2931
#include "HttpResponses.h"
3032

@@ -416,30 +418,26 @@ TEST_P(CatalogClientTest, GetCatalog403CacheClear) {
416418
ASSERT_FALSE(catalog_response.IsSuccessful());
417419
}
418420

419-
TEST_P(CatalogClientTest, DISABLED_CancelPendingRequestsCatalog) {
421+
TEST_P(CatalogClientTest, CancelPendingRequestsCatalog) {
420422
olp::client::HRN hrn(GetTestCatalog());
421423
testing::InSequence s;
422-
std::vector<std::shared_ptr<std::promise<void>>> waits;
423424
std::vector<std::shared_ptr<std::promise<void>>> pauses;
424425

425426
auto catalog_client = std::make_unique<CatalogClient>(hrn, settings_);
426427
auto catalog_request = CatalogRequest().WithFetchOption(OnlineOnly);
427428
auto version_request = CatalogVersionRequest().WithFetchOption(OnlineOnly);
428429

429430
// Make a few requests
430-
auto wait_for_cancel1 = std::make_shared<std::promise<void>>();
431-
auto pause_for_cancel1 = std::make_shared<std::promise<void>>();
432-
auto wait_for_cancel2 = std::make_shared<std::promise<void>>();
433-
auto pause_for_cancel2 = std::make_shared<std::promise<void>>();
431+
auto wait_for_cancel = std::make_shared<std::promise<void>>();
432+
auto pause_for_cancel = std::make_shared<std::promise<void>>();
434433

435434
{
436435
olp::http::RequestId request_id;
437436
NetworkCallback send_mock;
438437
CancelCallback cancel_mock;
439438

440-
std::tie(request_id, send_mock, cancel_mock) =
441-
GenerateNetworkMockActions(wait_for_cancel1, pause_for_cancel1,
442-
{200, HTTP_RESPONSE_LOOKUP_CONFIG});
439+
std::tie(request_id, send_mock, cancel_mock) = GenerateNetworkMockActions(
440+
wait_for_cancel, pause_for_cancel, {200, HTTP_RESPONSE_LOOKUP_CONFIG});
443441

444442
EXPECT_CALL(*network_mock_,
445443
Send(IsGetRequest(URL_LOOKUP_CONFIG), _, _, _, _))
@@ -450,44 +448,19 @@ TEST_P(CatalogClientTest, DISABLED_CancelPendingRequestsCatalog) {
450448
.WillOnce(testing::Invoke(std::move(cancel_mock)));
451449
}
452450

453-
{
454-
olp::http::RequestId request_id;
455-
NetworkCallback send_mock;
456-
CancelCallback cancel_mock;
457-
458-
std::tie(request_id, send_mock, cancel_mock) =
459-
GenerateNetworkMockActions(wait_for_cancel2, pause_for_cancel2,
460-
{200, HTTP_RESPONSE_LOOKUP_METADATA});
461-
462-
EXPECT_CALL(*network_mock_,
463-
Send(IsGetRequest(URL_LOOKUP_METADATA), _, _, _, _))
464-
.Times(1)
465-
.WillOnce(testing::Invoke(std::move(send_mock)));
466-
467-
EXPECT_CALL(*network_mock_, Cancel(request_id))
468-
.WillOnce(testing::Invoke(std::move(cancel_mock)));
469-
}
470-
471-
waits.push_back(wait_for_cancel1);
472-
pauses.push_back(pause_for_cancel1);
473451
auto catalog_future = catalog_client->GetCatalog(catalog_request);
474-
475-
waits.push_back(wait_for_cancel2);
476-
pauses.push_back(pause_for_cancel2);
477452
auto version_future = catalog_client->GetLatestVersion(version_request);
478453

479-
for (auto wait : waits) {
480-
wait->get_future().get();
481-
}
454+
// We are using only one thread so we can only have one network request
455+
// active. So just wait for it.
456+
wait_for_cancel->get_future().get();
457+
482458
// Cancel them all
483459
catalog_client->CancelPendingRequests();
484-
for (auto pause : pauses) {
485-
pause->set_value();
486-
}
460+
pause_for_cancel->set_value();
487461

488462
// Verify they are all cancelled
489463
CatalogResponse catalog_response = catalog_future.GetFuture().get();
490-
491464
ASSERT_FALSE(catalog_response.IsSuccessful())
492465
<< ApiErrorToString(catalog_response.GetError());
493466

0 commit comments

Comments
 (0)