#[path = "../examples/keyed/shape.rs"]
mod shape;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use rustorb_dds::{
DataReader, DataReaderQos, DataWriterQos, DomainParticipant, DurabilityKind, DurabilityService,
History, InstanceState, ParticipantConfig, Reliability, ResourceLimits,
};
use shape::Shape;
const WAIT: Duration = Duration::from_secs(10);
fn config(domain: u32) -> ParticipantConfig {
let mut c = ParticipantConfig::localhost(domain);
c.announce_period = Duration::from_millis(200);
c.writer.heartbeat_period = Duration::from_millis(50);
c
}
fn participant(domain: u32) -> DomainParticipant {
DomainParticipant::with_config(config(domain)).unwrap()
}
fn persistent_participant(domain: u32, dir: &Path) -> DomainParticipant {
let mut c = config(domain);
c.persistence_directory = Some(dir.to_path_buf());
DomainParticipant::with_config(c).unwrap()
}
fn temp_dir(name: &str) -> PathBuf {
let d = std::env::temp_dir().join(format!("rustorb-dds-test-{name}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&d);
d
}
fn shape(id: u32, index: u32) -> Shape {
Shape {
id,
index,
message: format!("shape {id}"),
}
}
fn writer_qos(durability: DurabilityKind) -> DataWriterQos {
DataWriterQos {
history: History::KeepLast(3),
durability,
durability_service: DurabilityService {
history: History::KeepLast(2),
..DurabilityService::default()
},
..DataWriterQos::default()
}
}
fn reader_qos(durability: DurabilityKind) -> DataReaderQos {
DataReaderQos {
reliability: Reliability::reliable(),
history: History::KeepAll,
durability,
..DataReaderQos::default()
}
}
fn reader(p: &DomainParticipant, topic: &str, durability: DurabilityKind) -> DataReader<Shape> {
p.create_subscriber()
.unwrap()
.create_datareader_with_qos(
&p.create_topic::<Shape>(topic).unwrap(),
reader_qos(durability),
)
.unwrap()
}
fn take(reader: &DataReader<Shape>, n: usize, wait: Duration) -> (Vec<(u32, u32)>, usize) {
let deadline = Instant::now() + wait;
let mut got = Vec::new();
let mut disposed = 0;
while got.len() < n && Instant::now() < deadline {
if reader.wait_for_data(Duration::from_millis(50)) {
for s in reader.take() {
match s.data {
Some(d) => got.push((d.id, d.index)),
None if s.info.instance_state == InstanceState::NotAliveDisposed => {
disposed += 1
}
None => {}
}
}
}
}
got.sort();
(got, disposed)
}
#[test]
fn transient_samples_outlive_the_writer() {
let a = participant(24);
let b = participant(24);
let topic = a.create_topic::<Shape>("transient").unwrap();
let writer = a
.create_publisher()
.unwrap()
.create_datawriter_with_qos(&topic, writer_qos(DurabilityKind::Transient))
.unwrap();
for i in 1..=5 {
writer.write(&shape(1, i)).unwrap();
writer.write(&shape(2, i)).unwrap();
}
drop(writer);
let expected = vec![(1, 4), (1, 5), (2, 4), (2, 5)];
let remote = reader(&b, "transient", DurabilityKind::Transient);
assert_eq!(take(&remote, 4, WAIT), (expected.clone(), 0));
let local = reader(&a, "transient", DurabilityKind::TransientLocal);
assert_eq!(take(&local, 4, WAIT), (expected, 0));
let volatile = reader(&b, "transient", DurabilityKind::Volatile);
assert_eq!(take(&volatile, 1, Duration::from_millis(500)).0, vec![]);
assert!(take(&remote, 1, Duration::from_millis(300)).0.is_empty());
for s in remote.read() {
assert_eq!(s.info.instance_state, InstanceState::Alive);
}
}
#[test]
fn disposed_instances_go_after_the_cleanup_delay() {
let a = participant(85);
let b = participant(85);
let topic = a.create_topic::<Shape>("cleanup").unwrap();
let mut qos = writer_qos(DurabilityKind::Transient);
qos.durability_service.service_cleanup_delay = Duration::from_secs(2);
qos.durability_service.resource_limits = ResourceLimits {
max_instances: Some(2),
..ResourceLimits::default()
};
let writer = a
.create_publisher()
.unwrap()
.create_datawriter_with_qos(&topic, qos)
.unwrap();
for id in 1..=3 {
writer.write(&shape(id, 1)).unwrap();
}
writer.dispose(&shape(3, 0)).unwrap();
drop(writer);
let early = reader(&a, "cleanup", DurabilityKind::Transient);
let (got, disposed) = take(&early, 2, WAIT);
let deadline = Instant::now() + Duration::from_millis(300);
let mut disposed = disposed;
while disposed == 0 && Instant::now() < deadline {
disposed += take(&early, 1, Duration::from_millis(50)).1;
}
assert_eq!(got, vec![(2, 1), (3, 1)]);
assert_eq!(disposed, 1);
std::thread::sleep(Duration::from_millis(2500));
let late = reader(&b, "cleanup", DurabilityKind::Transient);
assert_eq!(take(&late, 2, Duration::from_secs(2)), (vec![(2, 1)], 0));
}
#[test]
fn persistent_samples_outlive_the_program() {
let dir = temp_dir("persistent");
{
let a = persistent_participant(86, &dir);
let topic = a.create_topic::<Shape>("persistent").unwrap();
let writer = a
.create_publisher()
.unwrap()
.create_datawriter_with_qos(&topic, writer_qos(DurabilityKind::Persistent))
.unwrap();
for i in 1..=4 {
writer.write(&shape(1, i)).unwrap();
}
writer.write(&shape(2, 1)).unwrap();
writer.dispose(&shape(2, 0)).unwrap();
}
let b = persistent_participant(86, &dir);
let c = participant(86);
let early = reader(&b, "persistent", DurabilityKind::TransientLocal);
let topic = b.create_topic::<Shape>("persistent").unwrap();
let publisher = b.create_publisher().unwrap();
let writer = publisher
.create_datawriter_with_qos(&topic, writer_qos(DurabilityKind::Persistent))
.unwrap();
let (got, disposed) = take(&early, 4, Duration::from_secs(1));
assert_eq!(got, vec![(1, 2), (1, 3), (1, 4), (2, 1)]);
assert_eq!(disposed + take(&early, 1, Duration::from_millis(200)).1, 1);
let late = reader(&c, "persistent", DurabilityKind::Persistent);
assert_eq!(take(&late, 4, WAIT).0, vec![(1, 2), (1, 3), (1, 4), (2, 1)]);
let other = publisher
.create_datawriter_with_qos(&topic, writer_qos(DurabilityKind::Persistent))
.unwrap();
other.write(&shape(7, 1)).unwrap();
writer.write(&shape(1, 5)).unwrap();
drop(other);
drop(writer);
drop(publisher);
drop(early);
drop(late);
drop(b);
let d = persistent_participant(86, &dir);
let topic = d.create_topic::<Shape>("persistent").unwrap();
let publisher = d.create_publisher().unwrap();
let _w1 = publisher
.create_datawriter_with_qos(&topic, writer_qos(DurabilityKind::Persistent))
.unwrap();
let _w2 = publisher
.create_datawriter_with_qos(&topic, writer_qos(DurabilityKind::Persistent))
.unwrap();
let late = reader(&c, "persistent", DurabilityKind::Persistent);
assert_eq!(
take(&late, 5, WAIT).0,
vec![(1, 3), (1, 4), (1, 5), (2, 1), (7, 1)]
);
drop(d);
let _ = std::fs::remove_dir_all(&dir);
}