forked from ros2/rclcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make unpublished topics unrecorded by default (ros2#968)
* Make unpublished topics unrecorded by default Topics with no publishers do not offer any QoS profiles. This causes the recorder to subscribe with a default QoS, which is often disadvantageous. This change makes subscribing to unpublished topics optional, off by default. Once a publisher is discovered for a topic, discovery picks it up and adds it to the bag. This addresses ros2#967 Signed-off-by: Sean Kelly <skelly@seegrid.com> * Feedback from review Signed-off-by: Sean Kelly <skelly@seegrid.com> * Additional review feedback Signed-off-by: Sean Kelly <skelly@seegrid.com> * Narrow scope of tests and make more deterministic Signed-off-by: Sean Kelly <skelly@seegrid.com> * Fix test expectation for unpublished topics Signed-off-by: Sean Kelly <skelly@seegrid.com> * Add ROSBAG2_TRANSPORT_EXPORT for get_requested_or_available_topics() This is needed to try to fix Windows build Co-authored-by: Sean Kelly <skelly@seegrid.com> Co-authored-by: Michael Orlov <morlovmr@gmail.com>
- Loading branch information
1 parent
4e4619b
commit c6e5290
Showing
12 changed files
with
202 additions
and
7 deletions.
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
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
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
70 changes: 70 additions & 0 deletions
70
rosbag2_transport/test/rosbag2_transport/mock_recorder.hpp
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,70 @@ | ||
// Copyright 2022, Apex.AI | ||
// | ||
// 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. | ||
|
||
#ifndef ROSBAG2_TRANSPORT__MOCK_RECORDER_HPP_ | ||
#define ROSBAG2_TRANSPORT__MOCK_RECORDER_HPP_ | ||
|
||
#include <chrono> | ||
#include <memory> | ||
#include <string> | ||
#include <ratio> | ||
|
||
#include "rosbag2_transport/recorder.hpp" | ||
|
||
class MockRecorder : public rosbag2_transport::Recorder | ||
{ | ||
public: | ||
MockRecorder( | ||
std::shared_ptr<rosbag2_cpp::Writer> writer, | ||
const rosbag2_storage::StorageOptions & storage_options, | ||
const rosbag2_transport::RecordOptions & record_options) | ||
: Recorder(writer, storage_options, record_options) | ||
{} | ||
|
||
template<typename DurationRepT = int64_t, typename DurationT = std::milli> | ||
bool wait_for_topic_to_be_discovered( | ||
const std::string & topic_name_to_wait_for, | ||
std::chrono::duration<DurationRepT, DurationT> timeout = std::chrono::seconds(10)) | ||
{ | ||
bool discovered = false; | ||
using clock = std::chrono::steady_clock; | ||
auto start = clock::now(); | ||
do { | ||
auto topic_names_and_types = this->get_topic_names_and_types(); | ||
for (const auto &[topic_name, topic_types] : topic_names_and_types) { | ||
if (topic_name_to_wait_for == topic_name) { | ||
discovered = true; | ||
break; | ||
} | ||
} | ||
std::this_thread::sleep_for(std::chrono::milliseconds(20)); | ||
} while (!discovered && (clock::now() - start) < timeout); | ||
return discovered; | ||
} | ||
|
||
bool topic_available_for_recording(const std::string & topic_name) | ||
{ | ||
bool available_for_recording = false; | ||
auto topics_to_subscribe = this->get_requested_or_available_topics(); | ||
for (const auto & topic_and_type : topics_to_subscribe) { | ||
if (topic_and_type.first == topic_name) { | ||
available_for_recording = true; | ||
break; | ||
} | ||
} | ||
return available_for_recording; | ||
} | ||
}; | ||
|
||
#endif // ROSBAG2_TRANSPORT__MOCK_RECORDER_HPP_ |
95 changes: 95 additions & 0 deletions
95
rosbag2_transport/test/rosbag2_transport/test_record_all_include_unpublished_topics.cpp
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,95 @@ | ||
// Copyright 2022 Seegrid Corporation. All Rights Reserved. | ||
// | ||
// 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 <gmock/gmock.h> | ||
|
||
#include <chrono> | ||
#include <memory> | ||
#include <string> | ||
#include <utility> | ||
|
||
#include "mock_recorder.hpp" | ||
#include "test_msgs/msg/basic_types.hpp" | ||
#include "test_msgs/message_fixtures.hpp" | ||
#include "rosbag2_test_common/publication_manager.hpp" | ||
#include "record_integration_fixture.hpp" | ||
|
||
using namespace std::chrono_literals; // NOLINT | ||
|
||
TEST_F(RecordIntegrationTestFixture, record_all_include_unpublished_false_ignores_unpublished) | ||
{ | ||
const std::string string_topic = "/string_topic"; | ||
auto node = std::make_shared<rclcpp::Node>("test_string_msg_listener_node"); | ||
auto string_msgs_sub = node->create_subscription<test_msgs::msg::Strings>( | ||
string_topic, 10, [](test_msgs::msg::Strings::ConstSharedPtr) {}); | ||
|
||
rosbag2_transport::RecordOptions record_options = {true, false, {}, "rmw_format", 100ms}; | ||
record_options.include_unpublished_topics = false; | ||
auto recorder = std::make_shared<MockRecorder>(writer_, storage_options_, record_options); | ||
recorder->record(); | ||
start_async_spin(recorder); | ||
|
||
ASSERT_TRUE(recorder->wait_for_topic_to_be_discovered(string_topic)); | ||
ASSERT_FALSE(recorder->topic_available_for_recording(string_topic)); | ||
} | ||
|
||
TEST_F(RecordIntegrationTestFixture, record_all_include_unpublished_true_includes_unpublished) | ||
{ | ||
const std::string string_topic = "/string_topic"; | ||
auto node = std::make_shared<rclcpp::Node>("test_string_msg_listener_node"); | ||
auto string_msgs_sub = node->create_subscription<test_msgs::msg::Strings>( | ||
string_topic, 10, [](test_msgs::msg::Strings::ConstSharedPtr) {}); | ||
|
||
rosbag2_transport::RecordOptions record_options = {true, false, {}, "rmw_format", 100ms}; | ||
record_options.include_unpublished_topics = true; | ||
auto recorder = std::make_shared<MockRecorder>(writer_, storage_options_, record_options); | ||
recorder->record(); | ||
start_async_spin(recorder); | ||
|
||
ASSERT_TRUE(recorder->wait_for_topic_to_be_discovered(string_topic)); | ||
ASSERT_TRUE(recorder->topic_available_for_recording(string_topic)); | ||
} | ||
|
||
TEST_F( | ||
RecordIntegrationTestFixture, | ||
record_all_include_unpublished_false_includes_later_published) | ||
{ | ||
const std::string string_topic = "/string_topic"; | ||
auto node = std::make_shared<rclcpp::Node>("test_string_msg_listener_node"); | ||
auto string_msgs_sub = node->create_subscription<test_msgs::msg::Strings>( | ||
string_topic, 10, [](test_msgs::msg::Strings::ConstSharedPtr) {}); | ||
|
||
rosbag2_transport::RecordOptions record_options = {true, false, {}, "rmw_format", 100ms}; | ||
record_options.include_unpublished_topics = false; | ||
auto recorder = std::make_shared<MockRecorder>(writer_, storage_options_, record_options); | ||
recorder->record(); | ||
start_async_spin(recorder); | ||
|
||
ASSERT_TRUE(recorder->wait_for_topic_to_be_discovered(string_topic)); | ||
ASSERT_FALSE(recorder->topic_available_for_recording(string_topic)); | ||
|
||
// Start up a publisher on our topic *after* the recording has started | ||
auto string_message = get_messages_strings()[0]; | ||
string_message->string_value = "Hello World"; | ||
rosbag2_test_common::PublicationManager pub_manager; | ||
|
||
// Publish 10 messages at a 30ms interval for a steady 300 milliseconds worth of data | ||
pub_manager.setup_publisher( | ||
string_topic, string_message, 10, rclcpp::QoS{rclcpp::KeepAll()}, 30ms); | ||
|
||
ASSERT_TRUE(pub_manager.wait_for_matched(string_topic.c_str())); | ||
pub_manager.run_publishers(); | ||
|
||
ASSERT_TRUE(recorder->topic_available_for_recording(string_topic)); | ||
} |
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