From 78f50617690ce322d5ee1124b69e9c7705cab6a6 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Fri, 8 Jul 2022 09:59:02 +0200 Subject: [PATCH 1/8] Refs #15118. Adding overload on RTPSReader. Signed-off-by: Miguel Company --- include/fastdds/rtps/reader/RTPSReader.h | 3 +++ src/cpp/rtps/reader/RTPSReader.cpp | 19 +++++++++++++++++++ .../fastdds/rtps/reader/RTPSReader.h | 2 ++ 3 files changed, 24 insertions(+) diff --git a/include/fastdds/rtps/reader/RTPSReader.h b/include/fastdds/rtps/reader/RTPSReader.h index a0c8def98ca..a2e083ae151 100644 --- a/include/fastdds/rtps/reader/RTPSReader.h +++ b/include/fastdds/rtps/reader/RTPSReader.h @@ -238,6 +238,9 @@ class RTPSReader RTPS_DllAPI uint64_t get_unread_count() const; + RTPS_DllAPI uint64_t get_unread_count( + bool mark_as_read); + /** * @return True if the reader expects Inline QOS. */ diff --git a/src/cpp/rtps/reader/RTPSReader.cpp b/src/cpp/rtps/reader/RTPSReader.cpp index e7e513b6bc6..43323a0620a 100644 --- a/src/cpp/rtps/reader/RTPSReader.cpp +++ b/src/cpp/rtps/reader/RTPSReader.cpp @@ -367,6 +367,25 @@ uint64_t RTPSReader::get_unread_count() const return total_unread_; } +uint64_t RTPSReader::get_unread_count( + bool mark_as_read) +{ + std::unique_lock lock(mp_mutex); + uint64_t ret_val = total_unread_; + + if (mark_as_read) + { + for (auto it = mp_history->changesBegin(); it != mp_history->changesEnd(); ++it) + { + CacheChange_t* change = *it; + change->isRead = false; + } + + total_unread_ = 0; + } + return total_unread_; +} + bool RTPSReader::is_datasharing_compatible_with( const WriterProxyData& wdata) { diff --git a/test/mock/rtps/RTPSReader/fastdds/rtps/reader/RTPSReader.h b/test/mock/rtps/RTPSReader/fastdds/rtps/reader/RTPSReader.h index 944fa031112..fbf00b745d7 100644 --- a/test/mock/rtps/RTPSReader/fastdds/rtps/reader/RTPSReader.h +++ b/test/mock/rtps/RTPSReader/fastdds/rtps/reader/RTPSReader.h @@ -115,6 +115,8 @@ class RTPSReader : public Endpoint MOCK_METHOD0(get_unread_count, uint64_t()); + MOCK_METHOD1(get_unread_count, uint64_t(bool)); + MOCK_METHOD1(set_content_filter, void (eprosima::fastdds::rtps::IReaderDataFilter* filter)); // *INDENT-ON* From b50d2acc5e15b0a73031ac42a2b22c8f193451d8 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Fri, 8 Jul 2022 10:00:45 +0200 Subject: [PATCH 2/8] Refs #15118. Adding method on DataReaderHistory. Signed-off-by: Miguel Company --- .../fastdds/subscriber/history/DataReaderHistory.cpp | 6 ++++++ .../fastdds/subscriber/history/DataReaderHistory.hpp | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/cpp/fastdds/subscriber/history/DataReaderHistory.cpp b/src/cpp/fastdds/subscriber/history/DataReaderHistory.cpp index d936f820529..7b531c2dc74 100644 --- a/src/cpp/fastdds/subscriber/history/DataReaderHistory.cpp +++ b/src/cpp/fastdds/subscriber/history/DataReaderHistory.cpp @@ -545,6 +545,12 @@ bool DataReaderHistory::get_next_deadline( return true; } +uint64_t DataReaderHistory::get_unread_count( + bool mark_as_read) +{ + return mp_reader->get_unread_count(mark_as_read); +} + std::pair DataReaderHistory::lookup_instance( const InstanceHandle_t& handle, bool exact) const diff --git a/src/cpp/fastdds/subscriber/history/DataReaderHistory.hpp b/src/cpp/fastdds/subscriber/history/DataReaderHistory.hpp index 916ef0fb139..49532c92b4f 100644 --- a/src/cpp/fastdds/subscriber/history/DataReaderHistory.hpp +++ b/src/cpp/fastdds/subscriber/history/DataReaderHistory.hpp @@ -214,6 +214,16 @@ class DataReaderHistory : public eprosima::fastrtps::rtps::ReaderHistory InstanceHandle_t& handle, std::chrono::steady_clock::time_point& next_deadline_us); + /** + * Get the number of samples pending to be read. + * + * @param mark_as_read Whether the unread samples should be marked as read or not. + * + * @return the number of samples on the reader history that have never been read. + */ + uint64_t get_unread_count( + bool mark_as_read); + /** * @brief Get the list of changes corresponding to an instance handle. * @param handle The handle to the instance. From 04196989c42f4cf5a3f429ba2b3328124aa62a5b Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Fri, 8 Jul 2022 10:02:26 +0200 Subject: [PATCH 3/8] Refs #15118. Changed method on DataReaderImpl. Signed-off-by: Miguel Company --- src/cpp/fastdds/subscriber/DataReader.cpp | 2 +- src/cpp/fastdds/subscriber/DataReaderImpl.cpp | 5 +++-- src/cpp/fastdds/subscriber/DataReaderImpl.hpp | 9 +++++++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/cpp/fastdds/subscriber/DataReader.cpp b/src/cpp/fastdds/subscriber/DataReader.cpp index 2bdaa331100..00a2adb14e2 100644 --- a/src/cpp/fastdds/subscriber/DataReader.cpp +++ b/src/cpp/fastdds/subscriber/DataReader.cpp @@ -252,7 +252,7 @@ ReturnCode_t DataReader::get_first_untaken_info( uint64_t DataReader::get_unread_count() const { - return impl_->get_unread_count(); + return impl_->get_unread_count(false); } const GUID_t& DataReader::guid() diff --git a/src/cpp/fastdds/subscriber/DataReaderImpl.cpp b/src/cpp/fastdds/subscriber/DataReaderImpl.cpp index 238fc342c90..6173aadd4e3 100644 --- a/src/cpp/fastdds/subscriber/DataReaderImpl.cpp +++ b/src/cpp/fastdds/subscriber/DataReaderImpl.cpp @@ -710,9 +710,10 @@ ReturnCode_t DataReaderImpl::get_first_untaken_info( return ReturnCode_t::RETCODE_NO_DATA; } -uint64_t DataReaderImpl::get_unread_count() const +uint64_t DataReaderImpl::get_unread_count( + bool mark_as_read) { - return reader_ ? reader_->get_unread_count() : 0; + return reader_ ? history_.get_unread_count(mark_as_read) : 0; } const GUID_t& DataReaderImpl::guid() const diff --git a/src/cpp/fastdds/subscriber/DataReaderImpl.hpp b/src/cpp/fastdds/subscriber/DataReaderImpl.hpp index 5fff3989327..26d6450256d 100644 --- a/src/cpp/fastdds/subscriber/DataReaderImpl.hpp +++ b/src/cpp/fastdds/subscriber/DataReaderImpl.hpp @@ -196,9 +196,14 @@ class DataReaderImpl SampleInfo* info); /** - * @return the number of samples pending to be read. + * Get the number of samples pending to be read. + * + * @param mark_as_read Whether the unread samples should be marked as read or not. + * + * @return the number of samples on the reader history that have never been read. */ - uint64_t get_unread_count() const; + uint64_t get_unread_count( + bool mark_as_read); /** * Get associated GUID From 15e3f810ae41900fb75f03c130ffed77e4e79770 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Fri, 8 Jul 2022 10:02:58 +0200 Subject: [PATCH 4/8] Refs #15118. Adding overload on DataReader. Signed-off-by: Miguel Company --- include/fastdds/dds/subscriber/DataReader.hpp | 10 ++++++++++ src/cpp/fastdds/subscriber/DataReader.cpp | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/include/fastdds/dds/subscriber/DataReader.hpp b/include/fastdds/dds/subscriber/DataReader.hpp index d27297945b9..9b7a634110b 100644 --- a/include/fastdds/dds/subscriber/DataReader.hpp +++ b/include/fastdds/dds/subscriber/DataReader.hpp @@ -809,6 +809,16 @@ class DataReader : public DomainEntity */ RTPS_DllAPI uint64_t get_unread_count() const; + /** + * Get the number of samples pending to be read. + * + * @param mark_as_read Whether the unread samples should be marked as read or not. + * + * @return the number of samples on the reader history that have never been read. + */ + RTPS_DllAPI uint64_t get_unread_count( + bool mark_as_read) const; + /** * Get associated GUID. * diff --git a/src/cpp/fastdds/subscriber/DataReader.cpp b/src/cpp/fastdds/subscriber/DataReader.cpp index 00a2adb14e2..80d2483542c 100644 --- a/src/cpp/fastdds/subscriber/DataReader.cpp +++ b/src/cpp/fastdds/subscriber/DataReader.cpp @@ -255,6 +255,12 @@ uint64_t DataReader::get_unread_count() const return impl_->get_unread_count(false); } +uint64_t DataReader::get_unread_count( + bool mark_as_read) const +{ + return impl_->get_unread_count(mark_as_read); +} + const GUID_t& DataReader::guid() { return impl_->guid(); From bc1383bc1e80266a5481982f21e05a4307d7f98f Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Mon, 11 Jul 2022 07:44:18 +0200 Subject: [PATCH 5/8] Refs #15118. Fixed returned value on RTPSReader. Signed-off-by: Miguel Company --- src/cpp/rtps/reader/RTPSReader.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cpp/rtps/reader/RTPSReader.cpp b/src/cpp/rtps/reader/RTPSReader.cpp index 43323a0620a..0c6d5afb657 100644 --- a/src/cpp/rtps/reader/RTPSReader.cpp +++ b/src/cpp/rtps/reader/RTPSReader.cpp @@ -383,7 +383,7 @@ uint64_t RTPSReader::get_unread_count( total_unread_ = 0; } - return total_unread_; + return ret_val; } bool RTPSReader::is_datasharing_compatible_with( From b5e160865da14ad08e63581165496bfa1fe8ed0f Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Wed, 13 Jul 2022 10:15:36 +0200 Subject: [PATCH 6/8] Refs #15118. Added unit test. Signed-off-by: Miguel Company --- .../dds/subscriber/DataReaderTests.cpp | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/test/unittest/dds/subscriber/DataReaderTests.cpp b/test/unittest/dds/subscriber/DataReaderTests.cpp index 64024a7a6a3..fca05988f80 100644 --- a/test/unittest/dds/subscriber/DataReaderTests.cpp +++ b/test/unittest/dds/subscriber/DataReaderTests.cpp @@ -1550,6 +1550,70 @@ TEST_F(DataReaderTests, read_unread) } } +/* + * This test checks the behaviour of the two overloads of get_unread_count. + */ +TEST_F(DataReaderTests, get_unread_count) +{ + static const Duration_t time_to_wait(0, 100 * 1000 * 1000); + static constexpr int32_t num_samples = 10; + static constexpr uint64_t num_samples_check = static_cast(num_samples); + + const ReturnCode_t& ok_code = ReturnCode_t::RETCODE_OK; + + DataWriterQos writer_qos = DATAWRITER_QOS_DEFAULT; + writer_qos.history().kind = KEEP_LAST_HISTORY_QOS; + writer_qos.history().depth = num_samples; + writer_qos.publish_mode().kind = SYNCHRONOUS_PUBLISH_MODE; + writer_qos.reliability().kind = RELIABLE_RELIABILITY_QOS; + + DataReaderQos reader_qos = DATAREADER_QOS_DEFAULT; + reader_qos.reliability().kind = RELIABLE_RELIABILITY_QOS; + reader_qos.history().kind = KEEP_ALL_HISTORY_QOS; + reader_qos.resource_limits().max_instances = 1; + reader_qos.resource_limits().max_samples_per_instance = num_samples; + reader_qos.resource_limits().max_samples = 3 * num_samples; + + create_instance_handles(); + create_entities(nullptr, reader_qos, SUBSCRIBER_QOS_DEFAULT, writer_qos); + + FooType data; + data.index(0); + data.message()[1] = '\0'; + + // Send a bunch of samples + for (char i = 0; i < num_samples; ++i) + { + data.message()[0] = i + '0'; + EXPECT_EQ(ok_code, data_writer_->write(&data, handle_ok_)); + } + + // Reader should have 10 unread samples + + // There are unread samples, so wait_for_unread should be ok + EXPECT_TRUE(data_reader_->wait_for_unread_message(time_to_wait)); + + // Calling get_unread_count() several times should always return the same value + for (char i = 0; i < num_samples; ++i) + { + EXPECT_EQ(num_samples_check, data_reader_->get_unread_count()); + } + + // Calling get_unread_count(false) several times should always return the same value + for (char i = 0; i < num_samples; ++i) + { + EXPECT_EQ(num_samples_check, data_reader_->get_unread_count(false)); + } + + // Calling get_unread_count(true) once will return the correct value + EXPECT_EQ(num_samples_check, data_reader_->get_unread_count(true)); + + // All variants should then return 0 + EXPECT_EQ(0, data_reader_->get_unread_count(true)); + EXPECT_EQ(0, data_reader_->get_unread_count(false)); + EXPECT_EQ(0, data_reader_->get_unread_count()); +} + template void lookup_instance_test( DataType& data, From f7ad4d399ea848decdf6c8793f1ada025d30537b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Gonz=C3=A1lez=20Moreno?= Date: Wed, 13 Jul 2022 12:21:42 +0200 Subject: [PATCH 7/8] Refs #14940. Fix error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ricardo González Moreno --- src/cpp/rtps/reader/RTPSReader.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/cpp/rtps/reader/RTPSReader.cpp b/src/cpp/rtps/reader/RTPSReader.cpp index 0c6d5afb657..62a57a3f709 100644 --- a/src/cpp/rtps/reader/RTPSReader.cpp +++ b/src/cpp/rtps/reader/RTPSReader.cpp @@ -375,13 +375,16 @@ uint64_t RTPSReader::get_unread_count( if (mark_as_read) { - for (auto it = mp_history->changesBegin(); it != mp_history->changesEnd(); ++it) + for (auto it = mp_history->changesBegin(); 0 < total_unread_ && it != mp_history->changesEnd(); ++it) { CacheChange_t* change = *it; - change->isRead = false; + if (!change->isRead) + { + change->isRead = true; + --total_unread_; + } } - - total_unread_ = 0; + assert(0 == total_unread_); } return ret_val; } From 4249f34f2579bdc0c0358d05af7068136cfa6c3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Gonz=C3=A1lez=20Moreno?= Date: Wed, 13 Jul 2022 13:31:01 +0200 Subject: [PATCH 8/8] Refs #14940. Improve test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ricardo González Moreno --- test/unittest/dds/subscriber/DataReaderTests.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/unittest/dds/subscriber/DataReaderTests.cpp b/test/unittest/dds/subscriber/DataReaderTests.cpp index fca05988f80..37445cbdce8 100644 --- a/test/unittest/dds/subscriber/DataReaderTests.cpp +++ b/test/unittest/dds/subscriber/DataReaderTests.cpp @@ -1599,15 +1599,25 @@ TEST_F(DataReaderTests, get_unread_count) EXPECT_EQ(num_samples_check, data_reader_->get_unread_count()); } + SampleInfo sample_info; + ASSERT_EQ(ReturnCode_t::RETCODE_OK, data_reader_->get_first_untaken_info(&sample_info)); + ASSERT_EQ(SampleStateKind::NOT_READ_SAMPLE_STATE, sample_info.sample_state); + // Calling get_unread_count(false) several times should always return the same value for (char i = 0; i < num_samples; ++i) { EXPECT_EQ(num_samples_check, data_reader_->get_unread_count(false)); } + ASSERT_EQ(ReturnCode_t::RETCODE_OK, data_reader_->get_first_untaken_info(&sample_info)); + ASSERT_EQ(SampleStateKind::NOT_READ_SAMPLE_STATE, sample_info.sample_state); + // Calling get_unread_count(true) once will return the correct value EXPECT_EQ(num_samples_check, data_reader_->get_unread_count(true)); + ASSERT_EQ(ReturnCode_t::RETCODE_OK, data_reader_->get_first_untaken_info(&sample_info)); + ASSERT_EQ(SampleStateKind::READ_SAMPLE_STATE, sample_info.sample_state); + // All variants should then return 0 EXPECT_EQ(0, data_reader_->get_unread_count(true)); EXPECT_EQ(0, data_reader_->get_unread_count(false));