use std::sync::Arc;
use proptest::{prelude::*, test_runner::TestCaseError};
use super::{ConnectionId, SessionRegistry};
use crate::push::PushQueues;
#[tokio::test]
async fn get_evicts_dead_entry_from_registry() {
let registry = SessionRegistry::<u8>::default();
let id = ConnectionId::new(1);
let (queues, handle) = PushQueues::<u8>::builder()
.high_capacity(1)
.low_capacity(1)
.build()
.expect("failed to build PushQueues");
assert!(registry.insert(id, &handle));
drop(handle);
drop(queues);
assert!(registry.get(&id).is_none(), "dead handle must not upgrade");
assert!(
registry.entries.is_empty(),
"failed lookup must evict the dead entry rather than leave it to prune"
);
}
fn live_handle() -> (PushQueues<u8>, crate::push::PushHandle<u8>) {
PushQueues::<u8>::builder()
.high_capacity(1)
.low_capacity(1)
.build()
.expect("failed to build PushQueues")
}
#[tokio::test]
async fn prune_retains_live_and_evicts_dead_entries() {
let registry = SessionRegistry::<u8>::default();
let live_id = ConnectionId::new(11);
let dead_id = ConnectionId::new(12);
let (_live_queues, live) = live_handle();
assert!(registry.insert(live_id, &live));
let (dead_queues, dead) = live_handle();
assert!(registry.insert(dead_id, &dead));
drop(dead);
drop(dead_queues);
assert_eq!(
registry.entries.len(),
2,
"both entries must be present before pruning"
);
assert!(
registry.entries.contains_key(&live_id),
"live entry must be registered"
);
assert!(
registry.entries.contains_key(&dead_id),
"dead entry must be registered"
);
registry.prune();
assert_eq!(
registry.entries.len(),
1,
concat!(
"prune must leave exactly one entry: an inverted predicate would ",
"also leave one"
)
);
assert!(
registry.entries.contains_key(&live_id),
"prune must retain the live entry"
);
assert!(
!registry.entries.contains_key(&dead_id),
"prune must evict the dead entry"
);
assert!(
registry.get(&live_id).is_some(),
"live handle must still upgrade after pruning"
);
assert_eq!(registry.active_ids(), vec![live_id]);
}
#[tokio::test]
async fn allocation_skips_an_identifier_a_live_entry_occupies() {
let registry = SessionRegistry::<u8>::default();
let manual_id = ConnectionId::new(1);
let (_manual_queues, manual_handle) = live_handle();
assert!(registry.insert(manual_id, &manual_handle));
let allocated = registry.allocate_connection_id();
assert_eq!(allocated, ConnectionId::new(2));
assert!(
registry.get(&manual_id).is_some(),
"the manual entry must survive the allocation"
);
}
#[tokio::test]
async fn insert_refuses_to_replace_a_live_entry() {
let registry = SessionRegistry::<u8>::default();
let id = ConnectionId::new(7);
let (mut first_queues, first) = live_handle();
let (_second_queues, second) = live_handle();
assert!(registry.insert(id, &first));
assert!(!registry.insert(id, &second));
let published = registry.get(&id).expect("the first handle stays live");
published
.push_high_priority(11)
.await
.expect("the first lane has room");
let (_, value) = first_queues.recv().await.expect("recv failed");
assert_eq!(
value, 11,
"the published handle must still address the first connection"
);
}
#[tokio::test]
async fn allocation_and_publication_claim_one_identifier() {
let registry = SessionRegistry::<u8>::default();
let manual_id = ConnectionId::new(1);
let (mut manual_queues, manual_handle) = live_handle();
assert!(registry.insert(manual_id, &manual_handle));
let (mut server_queues, server_handle) = live_handle();
let published = registry.allocate_and_publish(&server_handle);
assert_eq!(published, ConnectionId::new(2));
let addressed = registry
.get(&published)
.expect("the published entry must address the connection");
addressed
.push_high_priority(21)
.await
.expect("the published lane has room");
assert_eq!(
server_queues.recv().await.expect("recv failed").1,
21,
"the published identifier must address this connection"
);
let manual = registry
.get(&manual_id)
.expect("the manual entry must survive the publication");
manual
.push_high_priority(22)
.await
.expect("the manual lane has room");
assert_eq!(
manual_queues.recv().await.expect("recv failed").1,
22,
"the manual identifier must still address the application's handle"
);
}
#[tokio::test]
async fn a_competing_insert_in_the_claim_window_makes_it_retry() {
let registry = SessionRegistry::<u8>::default();
let (mut manual_queues, manual_handle) = live_handle();
let (mut connection_queues, connection_handle) = live_handle();
let mut took_window = false;
let published = registry.claim_next(|candidate| {
if !took_window {
took_window = true;
assert!(
registry.insert(candidate, &manual_handle),
"the competing insert must take the freshly allocated candidate"
);
}
registry.insert(candidate, &connection_handle)
});
let manual_id = ConnectionId::new(1);
assert_eq!(
published,
ConnectionId::new(2),
"the claim must retry past the identifier the competing insert took"
);
let addressed = registry
.get(&published)
.expect("the published entry must address the connection");
addressed
.push_high_priority(41)
.await
.expect("the published lane has room");
assert_eq!(
connection_queues.recv().await.expect("recv failed").1,
41,
"the published identifier must address this connection"
);
let manual = registry
.get(&manual_id)
.expect("the manual entry must survive the claim");
manual
.push_high_priority(42)
.await
.expect("the manual lane has room");
assert_eq!(
manual_queues.recv().await.expect("recv failed").1,
42,
"the identifier the manual insert took must still address its handle"
);
}
#[tokio::test]
async fn teardown_removes_only_the_entry_it_published() {
let registry = SessionRegistry::<u8>::default();
let id = ConnectionId::new(5);
let (_ending_queues, ending) = live_handle();
assert!(registry.insert(id, &ending));
registry.remove(&id);
let (_manual_queues, manual) = live_handle();
assert!(registry.insert(id, &manual));
assert!(
!registry.remove_owned(id, &ending),
"a later handle under the identifier must not be removed"
);
assert!(
registry.get(&id).is_some(),
"the application's handle must survive the ending connection's teardown"
);
assert!(
registry.remove_owned(id, &manual),
"removal must succeed while the entry holds the connection's handle"
);
assert!(registry.get(&id).is_none());
}
#[tokio::test]
async fn insert_replaces_a_dead_entry() {
let registry = SessionRegistry::<u8>::default();
let id = ConnectionId::new(9);
let (_dead_queues, dead) = live_handle();
assert!(registry.insert(id, &dead));
drop(dead);
let (_live_queues, live) = live_handle();
assert!(registry.insert(id, &live));
assert!(registry.get(&id).is_some());
}
#[test]
fn connection_ids_start_at_one_and_increase() {
let registry = SessionRegistry::<u8>::new();
assert_eq!(registry.allocate_connection_id(), ConnectionId::new(1));
assert_eq!(registry.allocate_connection_id(), ConnectionId::new(2));
assert_eq!(registry.allocate_connection_id(), ConnectionId::new(3));
}
#[test]
fn default_registry_allocates_ids_from_one() {
let registry = SessionRegistry::<u8>::default();
assert_eq!(registry.allocate_connection_id(), ConnectionId::new(1));
}
fn is_strictly_increasing(ids: &[u64]) -> bool {
ids.windows(2)
.all(|pair| matches!(pair, [earlier, later] if earlier < later))
}
proptest! {
#[test]
fn allocation_is_unique_and_strictly_increasing(requested in 1usize..=512) {
let registry = SessionRegistry::<u8>::new();
let ids: Vec<u64> = (0..requested)
.map(|_| registry.allocate_connection_id().as_u64())
.collect();
prop_assert_eq!(
ids.first().copied(),
Some(1),
"the first identifier must be 1"
);
prop_assert!(
is_strictly_increasing(&ids),
"identifiers must increase strictly: {:?}",
ids
);
}
}
proptest! {
#![proptest_config(ProptestConfig { cases: 32, ..ProptestConfig::default() })]
#[test]
fn concurrent_allocation_keeps_ids_unique(
threads in 1usize..=8,
per_thread in 1usize..=32,
) {
let registry = Arc::new(SessionRegistry::<u8>::new());
let workers: Vec<_> = (0..threads)
.map(|_| {
let shared = Arc::clone(®istry);
std::thread::spawn(move || {
(0..per_thread)
.map(|_| shared.allocate_connection_id().as_u64())
.collect::<Vec<_>>()
})
})
.collect();
let mut all = Vec::with_capacity(threads * per_thread);
for worker in workers {
let run = worker
.join()
.map_err(|_| TestCaseError::fail("an allocation thread panicked"))?;
prop_assert!(
is_strictly_increasing(&run),
"one thread's identifiers must increase strictly: {:?}",
run
);
all.extend(run);
}
all.sort_unstable();
prop_assert_eq!(all.len(), threads * per_thread);
prop_assert_eq!(
all.first().copied(),
Some(1),
"the lowest identifier must be 1"
);
prop_assert!(
is_strictly_increasing(&all),
"identifiers handed to different threads must be unique: {:?}",
all
);
}
}