-
Notifications
You must be signed in to change notification settings - Fork 418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
executor could take more than once incorrectly #383
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7d80613
Baseline test and force threads to yield.
mjcarroll 6853019
Add timer tracking for executor.
mjcarroll a2c7e9e
Add locking and test happy-path exit conditions.
mjcarroll 38c48d1
Move logic to multi_threaded_executor
mjcarroll d51bf80
Address reviewer feedback by reducing scope of set.
mjcarroll fdcfd01
Expand tolerance on testing.
mjcarroll 1b5d3fd
comment fixup
dhood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright 2018 Open Source Robotics Foundation, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <chrono> | ||
#include <string> | ||
#include <memory> | ||
|
||
#include "rclcpp/exceptions.hpp" | ||
#include "rclcpp/node.hpp" | ||
#include "rclcpp/rclcpp.hpp" | ||
#include "rclcpp/executors.hpp" | ||
|
||
#include "rcl_interfaces/msg/intra_process_message.hpp" | ||
|
||
using namespace std::chrono_literals; | ||
|
||
using rcl_interfaces::msg::IntraProcessMessage; | ||
|
||
class TestMultiThreadedExecutor : public ::testing::Test | ||
{ | ||
protected: | ||
static void SetUpTestCase() | ||
{ | ||
rclcpp::init(0, nullptr); | ||
} | ||
}; | ||
|
||
/* | ||
Test that timers are not taken multiple times when using reentrant callback groups. | ||
*/ | ||
TEST_F(TestMultiThreadedExecutor, timer_over_take) { | ||
#ifdef __linux__ | ||
// This seems to be the most effective way to force the bug to happen on Linux. | ||
// This is unnecessary on MacOS, since the default scheduler causes it. | ||
struct sched_param param; | ||
param.sched_priority = 0; | ||
if (sched_setscheduler(0, SCHED_BATCH, ¶m) != 0) { | ||
perror("sched_setscheduler"); | ||
} | ||
#endif | ||
|
||
bool yield_before_execute = true; | ||
|
||
rclcpp::executors::MultiThreadedExecutor executor( | ||
rclcpp::executor::create_default_executor_arguments(), 2u, yield_before_execute); | ||
|
||
ASSERT_GT(executor.get_number_of_threads(), 1u); | ||
|
||
std::shared_ptr<rclcpp::Node> node = | ||
std::make_shared<rclcpp::Node>("test_multi_threaded_executor_timer_over_take"); | ||
|
||
auto cbg = node->create_callback_group(rclcpp::callback_group::CallbackGroupType::Reentrant); | ||
|
||
rclcpp::Clock system_clock(RCL_STEADY_TIME); | ||
std::mutex last_mutex; | ||
auto last = system_clock.now(); | ||
|
||
std::atomic_int timer_count {0}; | ||
|
||
auto timer_callback = [&timer_count, &executor, &system_clock, &last_mutex, &last]() { | ||
const double PERIOD = 0.1f; | ||
const double TOLERANCE = 0.01f; | ||
|
||
rclcpp::Time now = system_clock.now(); | ||
timer_count++; | ||
|
||
if (timer_count > 5) { | ||
executor.cancel(); | ||
} | ||
|
||
{ | ||
std::lock_guard<std::mutex> lock(last_mutex); | ||
double diff = std::abs((now - last).nanoseconds()) / 1.0e9; | ||
last = now; | ||
|
||
if (diff < PERIOD - TOLERANCE || diff > PERIOD + TOLERANCE) { | ||
executor.cancel(); | ||
ASSERT_TRUE(diff > PERIOD - TOLERANCE); | ||
ASSERT_TRUE(diff < PERIOD + TOLERANCE); | ||
} | ||
} | ||
}; | ||
|
||
auto timer = node->create_wall_timer(100ms, timer_callback, cbg); | ||
executor.add_node(node); | ||
executor.spin(); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to be wrapped in
if(UNIX)
due tosched_setscheduler()
in the test?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added an
#ifdef
around the corresponding sched_setscheduler(), because the bug originally manifested on OSX, so we wanted to test against that. That trick additionally isn't necessary on OSX because the bug shows up with the default scheduler.