Skip to content

Commit

Permalink
Fix usage of rclcpp::ok with a non-default context (#352)
Browse files Browse the repository at this point in the history
* Fix usage of rclcpp::ok with a non-default context

The current implementation calls `rclcpp::ok` without any arguments,
which amounts to verifying that the global default context is valid. In
the case where a node is added to a custom context, and the global
context is not used, `rclcpp::ok` without any arguments will always
return `false` since the global context has never been initialized. To
fix it, pass to rclcpp the context that's available via the node's base
interface.

* Add a test for custom context
  • Loading branch information
haudren-woven authored Jun 26, 2024
1 parent 2730d50 commit 19f6480
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ class Updater : public DiagnosticTaskVector
*/
void update()
{
if (rclcpp::ok()) {
if (rclcpp::ok(base_interface_->get_context())) {
bool warn_nohwid = hwid_.empty();

std::vector<diagnostic_msgs::msg::DiagnosticStatus> status_vec;
Expand Down
36 changes: 36 additions & 0 deletions diagnostic_updater/test/diagnostic_updater_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,42 @@ TEST(DiagnosticUpdater, testUpdaterAsNodeClassMember) {
SUCCEED();
}

class SaveIfCalled : public diagnostic_updater::DiagnosticTask
{
public:
SaveIfCalled()
: DiagnosticTask("SaveIfCalled") {}

void run(diagnostic_updater::DiagnosticStatusWrapper & s)
{
s.summary(0, "Have been called!");
has_been_called_ = true;
}

bool has_been_called() const
{
return has_been_called_;
}

private:
bool has_been_called_ = false;
};


TEST(DiagnosticUpdater, testCustomContext) {
auto context = std::make_shared<rclcpp::Context>();
context->init(0, nullptr, rclcpp::InitOptions());

auto node =
std::make_shared<rclcpp::Node>("CustomContextNode", rclcpp::NodeOptions().context(context));
diagnostic_updater::Updater updater(node);
SaveIfCalled save_if_called;
updater.add(save_if_called);
updater.force_update();
ASSERT_TRUE(save_if_called.has_been_called());
context->shutdown("End test");
}

TEST(DiagnosticUpdater, testDiagnosticStatusWrapperKeyValuePairs) {
diagnostic_updater::DiagnosticStatusWrapper stat;

Expand Down

0 comments on commit 19f6480

Please sign in to comment.