Skip to content

Commit

Permalink
Add unit test to make sure that snapshot writing in the new file
Browse files Browse the repository at this point in the history
Co-authored-by: Clemens Mühlbacher <[email protected]>
Signed-off-by: Michael Orlov <[email protected]>
  • Loading branch information
MichaelOrlov and cmuehlbacher committed Oct 27, 2024
1 parent e46c7f0 commit 169c6b4
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions rosbag2_cpp/test/rosbag2_cpp/test_sequential_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,91 @@ TEST_F(SequentialWriterTest, snapshot_mode_zero_cache_size_throws_exception)
EXPECT_THROW(writer_->open(storage_options_, {rmw_format, rmw_format}), std::runtime_error);
}

TEST_F(SequentialWriterTest, snapshot_writes_with_splitting)
{
storage_options_.max_bagfile_size = 0;
storage_options_.max_cache_size = 200;
storage_options_.snapshot_mode = true;

// Expect a single write call when the snapshot is triggered
EXPECT_CALL(
*storage_, write(
An<const std::vector<std::shared_ptr<const rosbag2_storage::SerializedBagMessage>> &>())
).Times(1);

ON_CALL(
*storage_,
write(An<std::shared_ptr<const rosbag2_storage::SerializedBagMessage>>())).WillByDefault(
[this](std::shared_ptr<const rosbag2_storage::SerializedBagMessage>) {
fake_storage_size_ += 1;
});

ON_CALL(*storage_, get_bagfile_size).WillByDefault(
[this]() {
return fake_storage_size_.load();
});

ON_CALL(*metadata_io_, write_metadata).WillByDefault(
[this](const std::string &, const rosbag2_storage::BagMetadata & metadata) {
fake_metadata_ = metadata;
});

ON_CALL(*storage_, get_relative_file_path).WillByDefault(
[this]() {
return fake_storage_uri_;
});

auto sequential_writer = std::make_unique<rosbag2_cpp::writers::SequentialWriter>(
std::move(storage_factory_), converter_factory_, std::move(metadata_io_));
writer_ = std::make_unique<rosbag2_cpp::Writer>(std::move(sequential_writer));

std::vector<std::string> closed_files;
std::vector<std::string> opened_files;
rosbag2_cpp::bag_events::WriterEventCallbacks callbacks;
callbacks.write_split_callback =
[&closed_files, &opened_files](rosbag2_cpp::bag_events::BagSplitInfo & info) {
closed_files.emplace_back(info.closed_file);
opened_files.emplace_back(info.opened_file);
};
writer_->add_event_callbacks(callbacks);

std::string rmw_format = "rmw_format";

std::string msg_content = "Hello";
auto msg_length = msg_content.length();
auto message = std::make_shared<rosbag2_storage::SerializedBagMessage>();
message->topic_name = "test_topic";
message->serialized_data =
rosbag2_storage::make_serialized_message(msg_content.c_str(), msg_length);

writer_->open(storage_options_, {rmw_format, rmw_format});
writer_->create_topic({0u, "test_topic", "test_msgs/BasicTypes", "", {}, ""});

for (size_t i = 0; i < 100; i++) {
writer_->write(message);
}
writer_->take_snapshot();

EXPECT_THAT(closed_files.size(), 1);
EXPECT_THAT(opened_files.size(), 1);

if (!((closed_files.size() == opened_files.size()) && (opened_files.size() == 1))) {
// Output debug info
for (size_t i = 0; i < opened_files.size(); i++) {
std::cout << "opened_file[" << i << "] = '" << opened_files[i] <<
"'; closed_file[" << i << "] = '" << closed_files[i] << "';" << std::endl;
}
}

ASSERT_EQ(opened_files.size(), 1);
ASSERT_EQ(closed_files.size(), 1);

auto expected_closed = fs::path(storage_options_.uri) / (bag_base_dir_ + "_0");
auto expected_opened = fs::path(storage_options_.uri) / (bag_base_dir_ + "_1");
ASSERT_STREQ(closed_files[0].c_str(), expected_closed.generic_string().c_str());
ASSERT_STREQ(opened_files[0].c_str(), expected_opened.generic_string().c_str());
}

TEST_F(SequentialWriterTest, split_event_calls_callback)
{
const uint64_t max_bagfile_size = 3;
Expand Down

0 comments on commit 169c6b4

Please sign in to comment.