41
41
#include " PubSubParticipant.hpp"
42
42
#include " PubSubReader.hpp"
43
43
#include " PubSubWriter.hpp"
44
+ #include < fastdds/dds/builtin/topic/PublicationBuiltinTopicData.hpp>
45
+ #include < fastdds/rtps/writer/WriterDiscoveryStatus.hpp>
44
46
45
47
using namespace eprosima ::fastdds;
46
48
@@ -1879,35 +1881,100 @@ TEST(DDSDataWriter, transport_priority_mutable)
1879
1881
1880
1882
// ----------- Procedure -----------
1881
1883
1882
- // 1. Create a disabled `DataWriter` with default QoS.
1884
+ // 1. Create a participant with a custom listener that captures the transport priority
1885
+ // of the discovered writer.
1886
+ struct CustomListener : public fdds ::DomainParticipantListener
1887
+ {
1888
+ void on_data_writer_discovery (
1889
+ fdds::DomainParticipant* /* participant */ ,
1890
+ rtps::WriterDiscoveryStatus reason,
1891
+ const fdds::PublicationBuiltinTopicData& info,
1892
+ bool & should_be_ignored) override
1893
+ {
1894
+ std::lock_guard<std::mutex> lock (mtx_);
1895
+
1896
+ if (reason == rtps::WriterDiscoveryStatus::DISCOVERED_WRITER)
1897
+ {
1898
+ // Capture the transport priority of the discovered writer.
1899
+ ASSERT_TRUE (info.transport_priority .has_value ());
1900
+ discovered_transport_priority_ = info.transport_priority ->value ;
1901
+ }
1902
+ else if (reason == rtps::WriterDiscoveryStatus::CHANGED_QOS_WRITER)
1903
+ {
1904
+ // Capture the updated transport priority of the writer.
1905
+ ASSERT_TRUE (info.transport_priority .has_value ());
1906
+ updated_transport_priority_ = info.transport_priority ->value ;
1907
+ cv_.notify_all ();
1908
+ }
1909
+
1910
+ should_be_ignored = false ;
1911
+ }
1912
+
1913
+ void wait_update ()
1914
+ {
1915
+ std::unique_lock<std::mutex> lock (mtx_);
1916
+ // Wait for the transport priority to be updated.
1917
+ cv_.wait (lock, [this ]
1918
+ {
1919
+ return updated_transport_priority_ != 0 ;
1920
+ });
1921
+ }
1922
+
1923
+ void check_expectations () const
1924
+ {
1925
+ std::lock_guard<std::mutex> lock (mtx_);
1926
+ int32_t prio = PRIORITY_2;
1927
+ EXPECT_EQ (discovered_transport_priority_, prio);
1928
+ prio = PRIORITY_3;
1929
+ EXPECT_EQ (updated_transport_priority_, prio);
1930
+ }
1931
+
1932
+ private:
1933
+
1934
+ mutable std::mutex mtx_;
1935
+ std::condition_variable cv_;
1936
+ int32_t discovered_transport_priority_ = 0 ;
1937
+ int32_t updated_transport_priority_ = 0 ;
1938
+ };
1939
+
1940
+ CustomListener custom_listener;
1941
+ auto factory = fdds::DomainParticipantFactory::get_shared_instance ();
1942
+ fdds::DomainId_t domain_id = static_cast <fdds::DomainId_t>(GET_PID ()) % 230 ;
1943
+ fdds::DomainParticipant* participant = factory->create_participant (
1944
+ domain_id, fdds::PARTICIPANT_QOS_DEFAULT, &custom_listener, fdds::StatusMask::none ());
1945
+
1946
+ // 2. Create a disabled `DataWriter` with default QoS.
1883
1947
fdds::PublisherQos pub_qos{};
1884
1948
pub_qos.entity_factory ().autoenable_created_entities = false ;
1885
1949
writer.publisher_qos (pub_qos).init ();
1886
1950
fdds::DataWriter& data_writer = writer.get_native_writer ();
1887
1951
1888
- // 2 . Get the writer's current QoS into `current_qos` and save the transport priority into `transport_priority_1`.
1952
+ // 3 . Get the writer's current QoS into `current_qos` and save the transport priority into `transport_priority_1`.
1889
1953
auto current_qos = data_writer.get_qos ();
1890
1954
int32_t transport_priority_1 = current_qos.transport_priority ().value ;
1891
1955
1892
- // 3 . Update the transport priority in `current_qos` to `PRIORITY_2` and set the `DataWriter` QoS to `current_qos`.
1956
+ // 4 . Update the transport priority in `current_qos` to `PRIORITY_2` and set the `DataWriter` QoS to `current_qos`.
1893
1957
current_qos.transport_priority ().value = PRIORITY_2;
1894
1958
EXPECT_EQ (data_writer.set_qos (current_qos), fdds::RETCODE_OK);
1895
1959
1896
- // 4 . Get the writer's current QoS into `current_qos` and save the transport priority into `transport_priority_2`.
1960
+ // 5 . Get the writer's current QoS into `current_qos` and save the transport priority into `transport_priority_2`.
1897
1961
current_qos = data_writer.get_qos ();
1898
1962
int32_t transport_priority_2 = current_qos.transport_priority ().value ;
1899
1963
1900
- // 5 . Enable the `DataWriter`.
1964
+ // 6 . Enable the `DataWriter`.
1901
1965
EXPECT_EQ (data_writer.enable (), fdds::RETCODE_OK);
1902
1966
1903
- // 6 . Update the transport priority in `current_qos` to `PRIORITY_3` and set the `DataWriter` QoS to `current_qos`.
1967
+ // 7 . Update the transport priority in `current_qos` to `PRIORITY_3` and set the `DataWriter` QoS to `current_qos`.
1904
1968
current_qos.transport_priority ().value = PRIORITY_3;
1905
1969
EXPECT_EQ (data_writer.set_qos (current_qos), fdds::RETCODE_OK);
1906
1970
1907
- // 7 . Get the writer's current QoS into `current_qos` and save the transport priority into `transport_priority_3`.
1971
+ // 8 . Get the writer's current QoS into `current_qos` and save the transport priority into `transport_priority_3`.
1908
1972
current_qos = data_writer.get_qos ();
1909
1973
int32_t transport_priority_3 = current_qos.transport_priority ().value ;
1910
1974
1975
+ // 9. Wait for the participant to discover the `DataWriter` and update the transport priority.
1976
+ custom_listener.wait_update ();
1977
+
1911
1978
// ----------- Assertions -----------
1912
1979
1913
1980
// 1. `transport_priority_1` equals `0`.
@@ -1918,6 +1985,14 @@ TEST(DDSDataWriter, transport_priority_mutable)
1918
1985
1919
1986
// 3. `transport_priority_3` equals `PRIORITY_3`.
1920
1987
EXPECT_EQ (transport_priority_3, PRIORITY_3);
1988
+
1989
+ // 4. The other participant discovered and updated transport priorities
1990
+ // are equal to `PRIORITY_2` and `PRIORITY_3`, respectively.
1991
+ custom_listener.check_expectations ();
1992
+
1993
+ // ----------- Cleanup -----------
1994
+
1995
+ factory->delete_participant (participant);
1921
1996
}
1922
1997
1923
1998
/* *
0 commit comments