use std::time::{Duration, Instant};
use rustdds::{policy, DomainParticipant, QosPolicyBuilder, TopicKind};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
struct Ping {
seq: u32,
}
#[test]
fn late_writer_can_reach_early_reader() {
let participant_a = DomainParticipant::new(51).unwrap();
let qos = QosPolicyBuilder::new()
.reliability(policy::Reliability::Reliable {
max_blocking_time: rustdds::Duration::from_secs(1),
})
.durability(policy::Durability::Volatile)
.history(policy::History::KeepAll)
.build();
let topic_a = participant_a
.create_topic(
"late_endpoint_test_topic".to_string(),
"Ping".to_string(),
&qos,
TopicKind::NoKey,
)
.unwrap();
let subscriber = participant_a.create_subscriber(&qos).unwrap();
let mut reader = subscriber
.create_datareader_no_key_cdr::<Ping>(&topic_a, None)
.unwrap();
let participant_b = DomainParticipant::new(51).unwrap();
std::thread::sleep(Duration::from_secs(3));
let topic_b = participant_b
.create_topic(
"late_endpoint_test_topic".to_string(),
"Ping".to_string(),
&qos,
TopicKind::NoKey,
)
.unwrap();
let publisher = participant_b.create_publisher(&qos).unwrap();
let writer = publisher
.create_datawriter_no_key_cdr::<Ping>(&topic_b, None)
.unwrap();
std::thread::sleep(Duration::from_secs(2));
writer.write(Ping { seq: 42 }, None).unwrap();
let deadline = Instant::now() + Duration::from_secs(5);
while Instant::now() < deadline {
if let Ok(Some(sample)) = reader.take_next_sample() {
assert_eq!(sample.into_value().seq, 42);
return; }
std::thread::sleep(Duration::from_millis(50));
}
panic!("late writer's data never arrived at the early reader within 5 seconds");
}
#[test]
fn late_reader_can_receive_from_early_writer() {
let participant_a = DomainParticipant::new(52).unwrap();
let qos = QosPolicyBuilder::new()
.reliability(policy::Reliability::Reliable {
max_blocking_time: rustdds::Duration::from_secs(1),
})
.durability(policy::Durability::Volatile)
.history(policy::History::KeepAll)
.build();
let topic_a = participant_a
.create_topic(
"late_endpoint_test_topic_2".to_string(),
"Ping".to_string(),
&qos,
TopicKind::NoKey,
)
.unwrap();
let publisher = participant_a.create_publisher(&qos).unwrap();
let writer = publisher
.create_datawriter_no_key_cdr::<Ping>(&topic_a, None)
.unwrap();
let participant_b = DomainParticipant::new(52).unwrap();
std::thread::sleep(Duration::from_secs(3));
let topic_b = participant_b
.create_topic(
"late_endpoint_test_topic_2".to_string(),
"Ping".to_string(),
&qos,
TopicKind::NoKey,
)
.unwrap();
let subscriber = participant_b.create_subscriber(&qos).unwrap();
let mut reader = subscriber
.create_datareader_no_key_cdr::<Ping>(&topic_b, None)
.unwrap();
std::thread::sleep(Duration::from_secs(2));
writer.write(Ping { seq: 99 }, None).unwrap();
let deadline = Instant::now() + Duration::from_secs(5);
while Instant::now() < deadline {
if let Ok(Some(sample)) = reader.take_next_sample() {
assert_eq!(sample.into_value().seq, 99);
return; }
std::thread::sleep(Duration::from_millis(50));
}
panic!("early writer's data never arrived at the late reader within 5 seconds");
}