-
Notifications
You must be signed in to change notification settings - Fork 52
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
Add tests for user-defined signal handler #215
Merged
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# generated from test_rclcpp/test/test_executable_output.py.in | ||
# generated code does not contain a copyright notice | ||
|
||
from launch import LaunchDescriptor | ||
from launch.exit_handler import primary_exit_handler | ||
from launch.launcher import DefaultLauncher | ||
from launch.output_handler import ConsoleOutput | ||
|
||
from launch_testing import create_handler | ||
|
||
|
||
def @TEST_NAME@(): | ||
ld = LaunchDescriptor() | ||
|
||
output_handlers = [ConsoleOutput()] | ||
|
||
shutdown_trigger_handler = create_handler( | ||
'@TEST_EXECUTABLE_NAME@', | ||
ld, | ||
'@TEST_EXECUTABLE_TRIGGER_SHUTDOWN_OUTPUT@', | ||
exit_on_match=True, | ||
filtered_rmw_implementation='@rmw_implementation@', | ||
) | ||
output_handlers.append(shutdown_trigger_handler) | ||
|
||
output_check_handler = create_handler( | ||
'@TEST_EXECUTABLE_NAME@', | ||
ld, | ||
'@TEST_EXECUTABLE_EXPECTED_OUTPUT@', | ||
exit_on_match=False, | ||
filtered_rmw_implementation='@rmw_implementation@', | ||
) | ||
output_handlers.append(output_check_handler) | ||
|
||
ld.add_process( | ||
cmd=['@TEST_EXECUTABLE@'], | ||
name='@TEST_EXECUTABLE_NAME@', | ||
exit_handler=primary_exit_handler, | ||
output_handlers=output_handlers, | ||
) | ||
|
||
launcher = DefaultLauncher() | ||
launcher.add_launch_descriptor(ld) | ||
rc = launcher.launch() | ||
|
||
assert rc == 0, "The launch file failed with exit code '" + str(rc) + "'. " | ||
|
||
output_check_handler.check() | ||
|
||
|
||
if __name__ == '__main__': | ||
@TEST_NAME@() |
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,54 @@ | ||
// Copyright 2017 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 <iostream> | ||
#include <memory> | ||
|
||
#include "rclcpp/rclcpp.hpp" | ||
|
||
using namespace std::chrono_literals; | ||
|
||
void sigintHandler(int sig) | ||
{ | ||
(void)sig; | ||
printf("Custom sigint handler called.\n"); | ||
} | ||
|
||
int main(int argc, char ** argv) | ||
{ | ||
// This exectuable is used in combination with a script that interrupts it at different times. | ||
// Its purpose is to test that a user-defined signal handler gets called even when the rclcpp | ||
// signal handler is/has been in use. | ||
setvbuf(stdout, NULL, _IONBF, BUFSIZ); | ||
|
||
// Override default sigint handler | ||
signal(SIGINT, sigintHandler); | ||
printf("Registered custom signal handler.\n"); | ||
|
||
rclcpp::init(argc, argv); | ||
printf("Called rclcpp::init.\n"); | ||
|
||
printf("Waiting to give an opportunity for interrupt...\n"); | ||
std::this_thread::sleep_for(5s); | ||
|
||
printf("Calling rclcpp::shutdown...\n"); | ||
rclcpp::shutdown(); | ||
printf("Called rclcpp::shutdown.\n"); | ||
|
||
printf("Waiting to give an opportunity for interrupt...\n"); | ||
std::this_thread::sleep_for(5s); | ||
|
||
printf("Exiting.\n"); | ||
return 0; | ||
} |
8 changes: 8 additions & 0 deletions
8
test_rclcpp/test/test_signal_handler_after_shutdown__expected_output.txt
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,8 @@ | ||
Registered custom signal handler. | ||
Called rclcpp::init. | ||
Waiting to give an opportunity for interrupt... | ||
Calling rclcpp::shutdown... | ||
Called rclcpp::shutdown. | ||
Waiting to give an opportunity for interrupt... | ||
Custom sigint handler called. | ||
Exiting. |
6 changes: 6 additions & 0 deletions
6
test_rclcpp/test/test_signal_handler_after_shutdown__trigger_shutdown.txt
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,6 @@ | ||
Registered custom signal handler. | ||
Called rclcpp::init. | ||
Waiting to give an opportunity for interrupt... | ||
Calling rclcpp::shutdown... | ||
Called rclcpp::shutdown. | ||
Waiting to give an opportunity for interrupt... |
9 changes: 9 additions & 0 deletions
9
test_rclcpp/test/test_signal_handler_before_shutdown__expected_output.txt
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,9 @@ | ||
Registered custom signal handler. | ||
Called rclcpp::init. | ||
Waiting to give an opportunity for interrupt... | ||
signal_handler(2) | ||
Custom sigint handler called. | ||
Calling rclcpp::shutdown... | ||
Called rclcpp::shutdown. | ||
Waiting to give an opportunity for interrupt... | ||
Exiting. |
3 changes: 3 additions & 0 deletions
3
test_rclcpp/test/test_signal_handler_before_shutdown__trigger_shutdown.txt
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,3 @@ | ||
Registered custom signal handler. | ||
Called rclcpp::init. | ||
Waiting to give an opportunity for interrupt... |
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.
Couldn't this test be done without any output comparison? The signal handler could set a global variable which could be checked in the main function afterwards. I think that would simplify 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.
Just checking the global variable you wouldn't know if the user-defined signal handler has been called "directly" or from within the rclcpp signal handler.
Checking for the presence/absence of the
signal_handler(2)
output from rclcpp we can infer this without any changes to rclcpp. If we think it's useful in other contexts, we can expose a flag from rclcpp and switch the test to check both variables. Otherwise, for the purpose of this test the console checking can provide that extra info.