Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions rclcpp_async/include/rclcpp_async/task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ struct Task
{
if (this != &o) {
if (handle) {
handle.promise().stop_source.request_stop();
handle.destroy();
}
handle = o.handle;
Expand Down Expand Up @@ -264,6 +265,7 @@ struct Task<void>
{
if (this != &o) {
if (handle) {
handle.promise().stop_source.request_stop();
handle.destroy();
}
handle = o.handle;
Expand Down
47 changes: 47 additions & 0 deletions rclcpp_async/test/test_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,50 @@ TEST(TaskVoid, OperatorBoolAndDone)
EXPECT_TRUE(static_cast<bool>(task2));
EXPECT_TRUE(task2.done());
}

TEST(TaskT, MoveAssignRequestsStop)
{
auto task = returns_42();
// Capture a token (not a reference to stop_source) so the shared stop-state
// outlives the coroutine frame destruction in move-assign.
auto token = task.handle.promise().stop_source.get_token();
EXPECT_FALSE(token.stop_requested());

// Move-assign with empty Task should request_stop on the old handle.
task = Task<int>{};
EXPECT_TRUE(token.stop_requested());
}

TEST(TaskVoid, MoveAssignRequestsStop)
{
auto task = does_nothing();
auto token = task.handle.promise().stop_source.get_token();
EXPECT_FALSE(token.stop_requested());

task = Task<void>{};
EXPECT_TRUE(token.stop_requested());
}

TEST(TaskT, MoveAssignStopCallbackFires)
{
bool callback_fired = false;
auto task = returns_42();
auto token = task.handle.promise().stop_source.get_token();
std::stop_callback cb(token, [&]() { callback_fired = true; });

EXPECT_FALSE(callback_fired);
task = Task<int>{};
EXPECT_TRUE(callback_fired);
}

TEST(TaskVoid, MoveAssignStopCallbackFires)
{
bool callback_fired = false;
auto task = does_nothing();
auto token = task.handle.promise().stop_source.get_token();
std::stop_callback cb(token, [&]() { callback_fired = true; });

EXPECT_FALSE(callback_fired);
task = Task<void>{};
EXPECT_TRUE(callback_fired);
}
Loading