pub(super) use super::queue::PacketQueue;
mod binding;
mod error;
mod inner;
mod input;
mod output;
mod state;
mod validation;
pub(crate) use binding::{MemoryInputBinding, MemoryOutputBinding};
pub use error::CallbackError;
pub(crate) use inner::{MemoryInputInner, MemoryOutputInner};
pub use input::MemoryInput;
pub use output::MemoryOutput;
#[cfg(test)]
mod tests {
use std::num::NonZeroUsize;
use std::sync::mpsc;
use std::thread;
use super::*;
use crate::{Error, MemoryQueuePolicy, TS_PACKET_SIZE};
fn capacity(packets: usize) -> NonZeroUsize {
NonZeroUsize::new(packets * TS_PACKET_SIZE).expect("nonzero capacity")
}
#[test]
fn closed_and_aborted_input_reject_empty_pushes() {
let input = MemoryInput::new(capacity(1), MemoryQueuePolicy::Drop).expect("input");
input.close();
assert!(matches!(input.push(&[]), Err(Error::QueueClosed { .. })));
let input = MemoryInput::new(capacity(1), MemoryQueuePolicy::Drop).expect("input");
input.abort();
assert!(matches!(input.push(&[]), Err(Error::QueueAborted { .. })));
}
#[test]
fn lease_is_exclusive_until_the_binding_drops() {
let input = MemoryInput::new(capacity(1), MemoryQueuePolicy::Drop).expect("input");
let binding = input.bind().expect("first binding");
assert!(matches!(input.bind(), Err(Error::InvalidState { .. })));
drop(binding);
input.bind().expect("binding after native owner deletion");
}
#[test]
fn close_wakes_a_blocked_output_receiver() {
let output =
MemoryOutput::new(capacity(1), MemoryQueuePolicy::Backpressure).expect("output");
let worker = std::sync::Arc::clone(&output);
let (started, waiting) = mpsc::channel();
let waiter = thread::spawn(move || {
started.send(()).expect("signal waiter start");
worker.recv_shared()
});
waiting.recv().expect("waiter started");
output.close();
assert_eq!(
waiter.join().expect("waiter join").expect("queue result"),
None
);
}
#[test]
fn output_shared_receive_avoids_a_second_payload_copy() {
let output = MemoryOutput::new(capacity(1), MemoryQueuePolicy::Drop).expect("output");
output
.inner
.push_from_callback(&[0x47; TS_PACKET_SIZE])
.expect("callback queue push");
let shared = output
.try_recv_shared()
.expect("queue result")
.expect("queued payload");
assert_eq!(shared.as_ref(), &[0x47; TS_PACKET_SIZE]);
}
}