Skip to content

Commit 45ad68a

Browse files
authored
Add execution context functionality (#1301)
Add execution context classes which provides functionality to cancel operation, setting errors, and execute tasks. Add execution context unit tests. Fix compilation with gcc 11. Relates-To: OLPEDGE-2077 Signed-off-by: Yevhenii Dudnyk <[email protected]>
1 parent 89c5962 commit 45ad68a

File tree

6 files changed

+265
-0
lines changed

6 files changed

+265
-0
lines changed

olp-cpp-sdk-authentication/src/TokenEndpointImpl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
#include "TokenEndpointImpl.h"
2121

22+
#include <thread>
23+
2224
#include <olp/authentication/SignInResult.h>
2325
#include <olp/core/http/HttpStatusCode.h>
2426
#include <olp/core/http/NetworkConstants.h>

olp-cpp-sdk-core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ set(OLP_SDK_LOGGING_HEADERS
153153

154154
set(OLP_SDK_THREAD_HEADERS
155155
./include/olp/core/thread/Atomic.h
156+
./include/olp/core/thread/ExecutionContext.h
156157
./include/olp/core/thread/SyncQueue.h
157158
./include/olp/core/thread/SyncQueue.inl
158159
./include/olp/core/thread/TaskScheduler.h
@@ -342,6 +343,7 @@ set(OLP_SDK_LOGGING_SOURCES
342343
)
343344

344345
set(OLP_SDK_THREAD_SOURCES
346+
./src/thread/ExecutionContext.cpp
345347
./src/thread/PriorityQueueExtended.h
346348
./src/thread/ThreadPoolTaskScheduler.cpp
347349
)
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (C) 2022 HERE Europe B.V.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
#pragma once
21+
22+
#include <utility>
23+
24+
#include <olp/core/client/ApiError.h>
25+
#include <olp/core/client/CancellationContext.h>
26+
27+
namespace olp {
28+
namespace thread {
29+
30+
/// Handles the cancellation and final mechanisms.
31+
class CORE_API ExecutionContext final {
32+
using CancelFuncType = std::function<void()>;
33+
using ExecuteFuncType = std::function<client::CancellationToken()>;
34+
using FailedCallback = std::function<void(client::ApiError)>;
35+
36+
public:
37+
/// A default contructor, initializes the `ExecutionContextImpl` instance.
38+
ExecutionContext();
39+
40+
/**
41+
* @brief Checks whether `CancellationContext` is cancelled.
42+
*
43+
* @return True if `CancellationContext` is cancelled; false otherwise.
44+
*/
45+
bool Cancelled() const;
46+
47+
/// @copydoc CancellationContext::CancelOperation()
48+
void CancelOperation();
49+
50+
/// @copydoc CancellationContext::ExecuteOrCancelled()
51+
void ExecuteOrCancelled(const ExecuteFuncType& execute_fn,
52+
const CancelFuncType& cancel_fn = nullptr);
53+
54+
/**
55+
* @brief Sets the error that is returned in the `Finally`
56+
* method of the execution.
57+
*
58+
* It immediately finishes the task execution and provides an error
59+
* via the `SetFailedCallback` callback of `ExecutionContext`.
60+
*
61+
* @param error The `ApiError` instance containing the error information.
62+
*/
63+
void SetError(client::ApiError error);
64+
65+
/**
66+
* @brief Sets a callback for `SetError`.
67+
*
68+
* @param callback Handles the finalization of the execution
69+
* in case of an error.
70+
*/
71+
void SetFailedCallback(FailedCallback callback);
72+
73+
/**
74+
* @brief Gets the `CancellationContext` object associated
75+
* with this `ExecutionContext` instance.
76+
*
77+
* The caller can use it to cancel the ongoing operation.
78+
*
79+
* @return The `CancellationContext` instance.
80+
*/
81+
client::CancellationContext GetContext() const;
82+
83+
private:
84+
class ExecutionContextImpl;
85+
std::shared_ptr<ExecutionContextImpl> impl_;
86+
};
87+
88+
} // namespace thread
89+
} // namespace olp
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright (C) 2022 HERE Europe B.V.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
#include <olp/core/thread/ExecutionContext.h>
21+
22+
namespace olp {
23+
namespace thread {
24+
25+
class ExecutionContext::ExecutionContextImpl {
26+
public:
27+
bool IsCancelled() const { return context_.IsCancelled(); }
28+
29+
void CancelOperation() { context_.CancelOperation(); }
30+
31+
void ExecuteOrCancelled(const ExecuteFuncType& execute_fn,
32+
const CancelFuncType& cancel_fn) {
33+
context_.ExecuteOrCancelled(execute_fn, cancel_fn);
34+
}
35+
36+
void SetError(client::ApiError error) {
37+
if (failed_callback_) {
38+
failed_callback_(std::move(error));
39+
failed_callback_ = nullptr;
40+
}
41+
}
42+
43+
void SetFailedCallback(FailedCallback callback) {
44+
failed_callback_ = std::move(callback);
45+
}
46+
47+
client::CancellationContext GetContext() const { return context_; }
48+
49+
private:
50+
client::CancellationContext context_;
51+
FailedCallback failed_callback_;
52+
};
53+
54+
ExecutionContext::ExecutionContext()
55+
: impl_(std::make_shared<ExecutionContextImpl>()) {}
56+
57+
void ExecutionContext::SetError(client::ApiError error) {
58+
impl_->SetError(std::move(error));
59+
}
60+
61+
bool ExecutionContext::Cancelled() const { return impl_->IsCancelled(); }
62+
63+
void ExecutionContext::CancelOperation() { return impl_->CancelOperation(); }
64+
65+
void ExecutionContext::ExecuteOrCancelled(const ExecuteFuncType& execute_fn,
66+
const CancelFuncType& cancel_fn) {
67+
impl_->ExecuteOrCancelled(execute_fn, cancel_fn);
68+
}
69+
70+
void ExecutionContext::SetFailedCallback(FailedCallback callback) {
71+
impl_->SetFailedCallback(std::move(callback));
72+
}
73+
74+
client::CancellationContext ExecutionContext::GetContext() const {
75+
return impl_->GetContext();
76+
}
77+
78+
} // namespace thread
79+
} // namespace olp

olp-cpp-sdk-core/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ set(OLP_CPP_SDK_CORE_TESTS_SOURCES
5757
./logging/MessageFormatterTest.cpp
5858
./logging/MockAppender.cpp
5959

60+
./thread/ExecutionContextTest.cpp
6061
./thread/PriorityQueueExtendedTest.cpp
6162
./thread/SyncQueueTest.cpp
6263
./thread/ThreadPoolTaskSchedulerTest.cpp
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (C) 2022 HERE Europe B.V.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
#include <gtest/gtest.h>
21+
22+
#include <olp/core/thread/ExecutionContext.h>
23+
24+
namespace {
25+
using ExecutionContext = olp::thread::ExecutionContext;
26+
27+
TEST(ExecutionContextTest, Cancel) {
28+
ExecutionContext execution_context;
29+
30+
EXPECT_FALSE(execution_context.Cancelled());
31+
execution_context.CancelOperation();
32+
EXPECT_TRUE(execution_context.Cancelled());
33+
}
34+
35+
TEST(ExecutionContextTest, ExecuteOrCancelled) {
36+
ExecutionContext execution_context;
37+
38+
{
39+
SCOPED_TRACE("Execute");
40+
41+
bool executed = false;
42+
bool cancelled = false;
43+
execution_context.ExecuteOrCancelled(
44+
[&executed]() {
45+
executed = true;
46+
return olp::client::CancellationToken();
47+
},
48+
[&cancelled]() { cancelled = true; });
49+
50+
EXPECT_TRUE(executed);
51+
EXPECT_FALSE(cancelled);
52+
}
53+
54+
{
55+
SCOPED_TRACE("Cancel");
56+
57+
bool executed = false;
58+
bool cancelled = false;
59+
execution_context.CancelOperation();
60+
execution_context.ExecuteOrCancelled(
61+
[&]() {
62+
executed = true;
63+
return olp::client::CancellationToken();
64+
},
65+
[&cancelled]() { cancelled = true; });
66+
67+
EXPECT_FALSE(executed);
68+
EXPECT_TRUE(cancelled);
69+
}
70+
}
71+
72+
TEST(ExecutionContextTest, SetFailedCallback) {
73+
ExecutionContext execution_context;
74+
75+
olp::client::ApiError api_error;
76+
execution_context.SetFailedCallback(
77+
[&](olp::client::ApiError error) { api_error = std::move(error); });
78+
execution_context.SetError(olp::client::ApiError::NetworkConnection());
79+
80+
EXPECT_EQ(api_error.GetErrorCode(),
81+
olp::client::ErrorCode::NetworkConnection);
82+
}
83+
84+
TEST(ExecutionContextTest, GetContext) {
85+
ExecutionContext execution_context;
86+
87+
EXPECT_FALSE(execution_context.GetContext().IsCancelled());
88+
execution_context.CancelOperation();
89+
EXPECT_TRUE(execution_context.GetContext().IsCancelled());
90+
}
91+
92+
} // namespace

0 commit comments

Comments
 (0)