use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::{Duration, SystemTime};
use dustnet_core::protocol::{MAX_LIVE_UPDATE_SIZE, ProtocolError};
use tokio::sync::{mpsc, oneshot, watch};
use crate::{
ServerAllocationSite, SubscriptionBudget, SubscriptionLease, read_subscription_content,
reject_allocation_at,
};
const SETTLE: Duration = Duration::from_millis(50);
const RECONCILE: Duration = Duration::from_secs(1);
const MAX_WATCHED_FILES: usize = 256;
const WAKE_SLOTS: usize = 64;
fn max_shared_content_len() -> usize {
MAX_LIVE_UPDATE_SIZE.saturating_sub("UPDATE \n\n".len())
}
#[derive(Debug)]
pub(crate) struct LiveGeneration {
pub(crate) text: String,
_content_lease: SubscriptionLease,
}
impl LiveGeneration {
#[cfg(test)]
pub(crate) fn empty_for_tests(budget: &SubscriptionBudget) -> Result<Arc<Self>, ProtocolError> {
Self::empty(budget)
}
fn empty(budget: &SubscriptionBudget) -> Result<Arc<Self>, ProtocolError> {
Ok(Arc::new(Self {
text: String::new(),
_content_lease: budget.reserve(0)?,
}))
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct FileStamp {
len: u64,
modified: Option<SystemTime>,
inode: u64,
}
impl FileStamp {
fn of(metadata: &std::fs::Metadata) -> Self {
#[cfg(unix)]
let inode = {
use std::os::unix::fs::MetadataExt;
metadata.ino()
};
#[cfg(not(unix))]
let inode = 0;
Self {
len: metadata.len(),
modified: metadata.modified().ok(),
inode,
}
}
}
#[derive(Debug)]
pub(crate) enum WatchEvent {
Touched(PathBuf),
Rescan,
SourceFailed,
}
pub(crate) trait ChangeSource: Send {
fn watch_dir(&mut self, dir: &Path) -> Result<(), ProtocolError>;
fn unwatch_dir(&mut self, dir: &Path);
}
#[derive(Default)]
pub(crate) struct ManualChangeSource;
impl ChangeSource for ManualChangeSource {
fn watch_dir(&mut self, _dir: &Path) -> Result<(), ProtocolError> {
Ok(())
}
fn unwatch_dir(&mut self, _dir: &Path) {}
}
enum WatchDirCommand {
Watch(PathBuf),
Unwatch(PathBuf),
}
pub(crate) struct NotifyChangeSource {
commands: std::sync::mpsc::Sender<WatchDirCommand>,
watched: HashMap<PathBuf, usize>,
}
impl NotifyChangeSource {
pub(crate) fn new(events: mpsc::Sender<WatchEvent>) -> Option<Self> {
let (commands, requests) = std::sync::mpsc::channel::<WatchDirCommand>();
let spawned = std::thread::Builder::new()
.name("dustnet-live-watch".to_owned())
.spawn(move || {
let sink = events.clone();
let handler = move |result: Result<notify::Event, notify::Error>| {
let event = match result {
Ok(event) => match event.paths.first() {
Some(path) => WatchEvent::Touched(path.clone()),
None => WatchEvent::Rescan,
},
Err(_) => WatchEvent::SourceFailed,
};
if sink.try_send(event).is_err() {
let _ = sink.try_send(WatchEvent::Rescan);
}
};
let mut watcher = match notify::recommended_watcher(handler) {
Ok(watcher) => watcher,
Err(error) => {
tracing::warn!(
%error,
"filesystem notification unavailable; \
live regions fall back to periodic reconciliation"
);
let _ = events.try_send(WatchEvent::SourceFailed);
return;
}
};
use notify::Watcher;
while let Ok(command) = requests.recv() {
match command {
WatchDirCommand::Watch(dir) => {
if let Err(error) =
watcher.watch(&dir, notify::RecursiveMode::NonRecursive)
{
tracing::warn!(
dir = %dir.display(),
%error,
"watching this directory failed; \
its live regions fall back to reconciliation"
);
}
}
WatchDirCommand::Unwatch(dir) => {
let _ = watcher.unwatch(&dir);
}
}
}
});
match spawned {
Ok(_) => Some(Self {
commands,
watched: HashMap::new(),
}),
Err(error) => {
tracing::warn!(
%error,
"could not start the live-region watcher thread; \
live regions fall back to periodic reconciliation"
);
None
}
}
}
}
impl ChangeSource for NotifyChangeSource {
fn watch_dir(&mut self, dir: &Path) -> Result<(), ProtocolError> {
if let Some(count) = self.watched.get_mut(dir) {
*count = count.saturating_add(1);
return Ok(());
}
let mut key = PathBuf::new();
key.try_reserve_exact(dir.as_os_str().len()).map_err(|_| {
ProtocolError::ResourceExhausted {
requested: dir.as_os_str().len(),
}
})?;
key.push(dir);
if self
.commands
.send(WatchDirCommand::Watch(key.clone()))
.is_err()
{
return Err(ProtocolError::InvalidMessage(
"the live-region watcher thread has stopped".into(),
));
}
self.watched.insert(key, 1);
Ok(())
}
fn unwatch_dir(&mut self, dir: &Path) {
let Some(count) = self.watched.get_mut(dir) else {
return;
};
*count = count.saturating_sub(1);
if *count > 0 {
return;
}
if let Some((key, _)) = self.watched.remove_entry(dir) {
let _ = self.commands.send(WatchDirCommand::Unwatch(key));
}
}
}
struct WatchedFile {
updates: watch::Sender<Arc<LiveGeneration>>,
wakes: Vec<mpsc::Sender<()>>,
stamp: Option<FileStamp>,
dirty: bool,
_entry_lease: SubscriptionLease,
}
enum WatchCommand {
Attach {
path: PathBuf,
wake: mpsc::Sender<()>,
owner_key: u64,
reply: oneshot::Sender<Result<AttachedWatch, ProtocolError>>,
},
}
pub(crate) struct AttachedWatch {
pub(crate) current: Arc<LiveGeneration>,
pub(crate) updates: watch::Receiver<Arc<LiveGeneration>>,
}
#[derive(Clone)]
pub(crate) struct LiveWatcher {
commands: mpsc::Sender<WatchCommand>,
}
impl LiveWatcher {
pub(crate) async fn attach(
&self,
path: PathBuf,
wake: mpsc::Sender<()>,
owner_key: u64,
) -> Result<AttachedWatch, ProtocolError> {
let (reply, answer) = oneshot::channel();
self.commands
.send(WatchCommand::Attach {
path,
wake,
owner_key,
reply,
})
.await
.map_err(|_| ProtocolError::ConnectionClosed)?;
answer.await.map_err(|_| ProtocolError::ConnectionClosed)?
}
}
pub(crate) struct WatchRegistry {
files: HashMap<PathBuf, WatchedFile>,
budget: SubscriptionBudget,
source: Box<dyn ChangeSource>,
events: mpsc::Receiver<WatchEvent>,
commands: mpsc::Receiver<WatchCommand>,
sequence: u64,
source_ended: bool,
settle: Duration,
reconcile: Duration,
#[cfg(test)]
reads: usize,
}
impl WatchRegistry {
pub(crate) fn new(
budget: SubscriptionBudget,
source: Box<dyn ChangeSource>,
events: mpsc::Receiver<WatchEvent>,
) -> (Self, LiveWatcher) {
let (command_tx, commands) = mpsc::channel(256);
(
Self {
files: HashMap::new(),
budget,
source,
events,
commands,
sequence: 0,
source_ended: false,
settle: SETTLE,
reconcile: RECONCILE,
#[cfg(test)]
reads: 0,
},
LiveWatcher {
commands: command_tx,
},
)
}
#[cfg(test)]
pub(crate) fn with_intervals(mut self, settle: Duration, reconcile: Duration) -> Self {
self.settle = settle;
self.reconcile = reconcile;
self
}
#[cfg(test)]
pub(crate) fn reads(&self) -> usize {
self.reads
}
pub(crate) async fn run(mut self, mut shutdown: watch::Receiver<bool>) {
let mut settle = tokio::time::interval(self.settle);
settle.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
let mut reconcile = tokio::time::interval(self.reconcile);
reconcile.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
loop {
self.reap();
tokio::select! {
command = self.commands.recv() => match command {
Some(command) => self.apply_command(command).await,
None => break,
},
event = self.events.recv(), if !self.source_ended => match event {
Some(event) => self.mark(event),
None => {
self.source_ended = true;
self.mark_all_dirty();
tracing::warn!(
"live-region change source ended; \
falling back to periodic reconciliation"
);
}
},
_ = settle.tick(), if self.any_dirty() => self.refresh_dirty().await,
_ = reconcile.tick() => {
self.mark_all_dirty();
self.refresh_dirty().await;
}
changed = shutdown.changed() => {
if changed.is_err() || *shutdown.borrow() { break; }
}
}
}
}
fn mark(&mut self, event: WatchEvent) {
match event {
WatchEvent::Touched(path) => {
let mut placed = false;
if let Some(entry) = self.files.get_mut(&path) {
entry.dirty = true;
placed = true;
} else {
for (watched, entry) in self.files.iter_mut() {
if watched.parent() == Some(path.as_path())
|| watched.parent() == path.parent()
{
entry.dirty = true;
placed = true;
}
}
}
if !placed {
self.mark_all_dirty();
}
}
WatchEvent::Rescan => self.mark_all_dirty(),
WatchEvent::SourceFailed => {
tracing::warn!(
"live-region change source failed; \
falling back to periodic reconciliation"
);
self.mark_all_dirty();
}
}
}
fn mark_all_dirty(&mut self) {
for entry in self.files.values_mut() {
entry.dirty = true;
}
}
fn any_dirty(&self) -> bool {
self.files.values().any(|entry| entry.dirty)
}
fn reap(&mut self) {
let mut dropped: Vec<PathBuf> = Vec::new();
for (path, entry) in self.files.iter() {
if entry.updates.receiver_count() == 0 {
dropped.push(path.clone());
}
}
for path in dropped {
self.files.remove(&path);
if let Some(parent) = path.parent() {
let still_needed = self
.files
.keys()
.any(|watched| watched.parent() == Some(parent));
if !still_needed {
self.source.unwatch_dir(parent);
}
}
}
}
async fn apply_command(&mut self, command: WatchCommand) {
match command {
WatchCommand::Attach {
path,
wake,
owner_key,
reply,
} => {
let result = self.attach(path, wake, owner_key).await;
let _ = reply.send(result);
}
}
}
async fn attach(
&mut self,
path: PathBuf,
wake: mpsc::Sender<()>,
owner_key: u64,
) -> Result<AttachedWatch, ProtocolError> {
if !self.files.contains_key(&path) {
if self.files.len() >= MAX_WATCHED_FILES {
return Err(ProtocolError::ResourceExhausted {
requested: self.files.len(),
});
}
self.install(&path, owner_key)?;
}
let Some(entry) = self.files.get_mut(&path) else {
return Err(ProtocolError::ResourceExhausted { requested: 0 });
};
if entry.wakes.len() == entry.wakes.capacity()
&& (reject_allocation_at(ServerAllocationSite::WakeSlots, owner_key)
|| entry.wakes.try_reserve(1).is_err())
{
return Err(ProtocolError::ResourceExhausted {
requested: entry.wakes.capacity().saturating_add(1),
});
}
let updates = entry.updates.subscribe();
self.refresh_if_changed(&path).await;
let Some(entry) = self.files.get_mut(&path) else {
return Err(ProtocolError::ResourceExhausted { requested: 0 });
};
entry.wakes.push(wake);
let current = {
let guard = entry.updates.borrow();
Arc::clone(&guard)
};
Ok(AttachedWatch { current, updates })
}
fn install(&mut self, path: &Path, owner_key: u64) -> Result<(), ProtocolError> {
if reject_allocation_at(ServerAllocationSite::WatchEntry, owner_key) {
return Err(ProtocolError::ResourceExhausted {
requested: path.as_os_str().len(),
});
}
let mut key = PathBuf::new();
key.try_reserve_exact(path.as_os_str().len()).map_err(|_| {
ProtocolError::ResourceExhausted {
requested: path.as_os_str().len(),
}
})?;
key.push(path);
if reject_allocation_at(ServerAllocationSite::WakeSlots, owner_key) {
return Err(ProtocolError::ResourceExhausted {
requested: WAKE_SLOTS,
});
}
let mut wakes: Vec<mpsc::Sender<()>> = Vec::new();
wakes
.try_reserve_exact(WAKE_SLOTS)
.map_err(|_| ProtocolError::ResourceExhausted {
requested: WAKE_SLOTS,
})?;
let entry_bytes = key
.capacity()
.checked_add(WAKE_SLOTS.saturating_mul(size_of::<mpsc::Sender<()>>()))
.ok_or(ProtocolError::ResourceExhausted {
requested: usize::MAX,
})?;
let entry_lease = self.budget.reserve(entry_bytes)?;
if let Some(parent) = path.parent()
&& let Err(error) = self.source.watch_dir(parent)
{
tracing::warn!(
path = %path.display(),
%error,
"watching this directory failed; \
live updates for it fall back to periodic reconciliation"
);
}
let (updates, _) = watch::channel(LiveGeneration::empty(&self.budget)?);
self.files.insert(
key,
WatchedFile {
updates,
wakes,
stamp: None,
dirty: true,
_entry_lease: entry_lease,
},
);
Ok(())
}
async fn refresh_dirty(&mut self) {
let dirty: Vec<PathBuf> = self
.files
.iter()
.filter(|(_, entry)| entry.dirty)
.map(|(path, _)| path.clone())
.collect();
for path in dirty {
if let Some(entry) = self.files.get_mut(&path) {
entry.dirty = false;
}
self.refresh_if_changed(&path).await;
}
}
async fn refresh_if_changed(&mut self, path: &Path) {
let metadata = match tokio::fs::metadata(path).await {
Ok(metadata) if metadata.is_file() => metadata,
_ => return,
};
let stamp = FileStamp::of(&metadata);
let Some(entry) = self.files.get(path) else {
return;
};
if entry.stamp == Some(stamp) {
return;
}
let content_len = usize::try_from(metadata.len()).unwrap_or(usize::MAX);
if content_len > max_shared_content_len() {
tracing::warn!(
path = %path.display(),
bytes = content_len,
max = max_shared_content_len(),
"live region not published: file exceeds the maximum update size"
);
if let Some(entry) = self.files.get_mut(path) {
entry.stamp = Some(stamp);
}
return;
}
let peak = match content_len
.checked_add(1)
.and_then(|s| s.checked_add(content_len))
{
Some(peak) => peak,
None => return,
};
let mut lease = match self.budget.reserve(peak) {
Ok(lease) => lease,
Err(_) => return,
};
let owner_key = crate::subscription_owner_key(&path.to_string_lossy(), "");
let text = match read_subscription_content(path, content_len, owner_key, &mut lease).await {
Ok(text) => text,
Err(_) => return,
};
#[cfg(test)]
{
self.reads = self.reads.saturating_add(1);
}
lease.shrink_to(text.capacity());
self.sequence = self.sequence.saturating_add(1);
let sequence = self.sequence;
let Some(entry) = self.files.get_mut(path) else {
return;
};
entry.stamp = Some(stamp);
if entry.updates.borrow().text == text {
return;
}
let bytes = text.len();
let subscribers = entry.updates.receiver_count();
entry.updates.send_replace(Arc::new(LiveGeneration {
text,
_content_lease: lease,
}));
entry.wakes.retain(|wake| !wake.is_closed());
for wake in &entry.wakes {
let _ = wake.try_send(());
}
tracing::info!(
path = %path.display(),
bytes,
sequence,
subscribers,
"live region updated"
);
}
#[cfg(test)]
pub(crate) async fn attach_now(
&mut self,
path: PathBuf,
wake: mpsc::Sender<()>,
owner_key: u64,
) -> Result<AttachedWatch, ProtocolError> {
self.attach(path, wake, owner_key).await
}
#[cfg(test)]
pub(crate) fn reap_now(&mut self) {
self.reap();
}
#[cfg(test)]
pub(crate) fn subscribers_of(&self, path: &Path) -> usize {
self.files
.get(path)
.map(|entry| entry.updates.receiver_count())
.unwrap_or_default()
}
#[cfg(test)]
pub(crate) fn watched_files(&self) -> usize {
self.files.len()
}
}
#[cfg(test)]
mod tests {
use super::*;
const BUDGET: usize = 8 * 1024 * 1024;
fn registry(
budget: &SubscriptionBudget,
) -> (WatchRegistry, LiveWatcher, mpsc::Sender<WatchEvent>) {
let (events_tx, events) = mpsc::channel(64);
let (registry, watcher) =
WatchRegistry::new(budget.clone(), Box::new(ManualChangeSource), events);
(registry, watcher, events_tx)
}
fn wake() -> (mpsc::Sender<()>, mpsc::Receiver<()>) {
mpsc::channel(1)
}
#[tokio::test]
async fn one_read_serves_every_subscriber_of_a_path() {
let directory = tempfile::tempdir().unwrap();
let path = directory.path().join("ticker.aml");
std::fs::write(&path, "first").unwrap();
let budget = SubscriptionBudget::new(BUDGET);
let (mut registry, _watcher, _events) = registry(&budget);
let mut keepalive = Vec::new();
let mut attached = Vec::new();
for index in 0..3 {
let (tx, rx) = wake();
keepalive.push(rx);
attached.push(registry.attach_now(path.clone(), tx, index).await.unwrap());
}
assert_eq!(registry.reads(), 1, "one file, one read, three subscribers");
for handle in &attached {
assert_eq!(handle.current.text, "first");
assert!(
Arc::ptr_eq(&handle.current, &attached[0].current),
"subscribers must share one allocation, not copies of it"
);
}
}
#[tokio::test]
async fn generation_bytes_are_charged_once_however_many_subscribers() {
let directory = tempfile::tempdir().unwrap();
let path = directory.path().join("ticker.aml");
std::fs::write(&path, "x".repeat(4096)).unwrap();
let budget = SubscriptionBudget::new(BUDGET);
let (mut registry, _watcher, _events) = registry(&budget);
let (tx, rx0) = wake();
let first = registry.attach_now(path.clone(), tx, 0).await.unwrap();
let after_one = budget.used();
let mut keepalive = vec![rx0];
let mut handles = vec![first];
for index in 1..16 {
let (tx, rx) = wake();
keepalive.push(rx);
handles.push(registry.attach_now(path.clone(), tx, index).await.unwrap());
}
let after_sixteen = budget.used();
assert_eq!(
after_one, after_sixteen,
"sixteen subscribers charged {after_sixteen} where one charged \
{after_one}; the generation is not actually shared"
);
assert_eq!(registry.reads(), 1);
}
#[tokio::test]
async fn a_generation_is_released_when_its_last_holder_drops() {
let directory = tempfile::tempdir().unwrap();
let path = directory.path().join("ticker.aml");
std::fs::write(&path, "x".repeat(4096)).unwrap();
let budget = SubscriptionBudget::new(BUDGET);
let (mut registry, _watcher, _events) = registry(&budget);
let (tx, rx) = wake();
let attached = registry.attach_now(path.clone(), tx, 0).await.unwrap();
assert!(budget.used() >= 4096);
drop(attached);
drop(rx);
registry.reap_now();
assert_eq!(registry.watched_files(), 0, "nobody is listening any more");
assert_eq!(budget.used(), 0, "every byte returned without bookkeeping");
}
#[tokio::test]
async fn an_event_storm_produces_a_single_read() {
let directory = tempfile::tempdir().unwrap();
let path = directory.path().join("ticker.aml");
std::fs::write(&path, "first").unwrap();
let budget = SubscriptionBudget::new(BUDGET);
let (mut registry, _watcher, _events) = registry(&budget);
let (tx, _rx) = wake();
let _attached = registry.attach_now(path.clone(), tx, 0).await.unwrap();
assert_eq!(registry.reads(), 1);
std::fs::write(&path, "second").unwrap();
for _ in 0..50 {
registry.mark(WatchEvent::Touched(path.clone()));
}
registry.refresh_dirty().await;
assert_eq!(
registry.reads(),
2,
"fifty events over one change must still be one read"
);
}
#[tokio::test]
async fn reconciling_an_unchanged_file_does_not_read_it() {
let directory = tempfile::tempdir().unwrap();
let path = directory.path().join("ticker.aml");
std::fs::write(&path, "first").unwrap();
let budget = SubscriptionBudget::new(BUDGET);
let (mut registry, _watcher, _events) = registry(&budget);
let (tx, _rx) = wake();
let _attached = registry.attach_now(path.clone(), tx, 0).await.unwrap();
let after_attach = registry.reads();
for _ in 0..5 {
registry.mark_all_dirty();
registry.refresh_dirty().await;
}
assert_eq!(
registry.reads(),
after_attach,
"an unchanged file must never be re-read"
);
}
#[tokio::test]
async fn an_oversize_file_leaves_the_previous_generation_intact() {
let directory = tempfile::tempdir().unwrap();
let path = directory.path().join("ticker.aml");
std::fs::write(&path, "first").unwrap();
let budget = SubscriptionBudget::new(BUDGET);
let (mut registry, _watcher, _events) = registry(&budget);
let (tx, _rx) = wake();
let attached = registry.attach_now(path.clone(), tx, 0).await.unwrap();
std::fs::write(&path, "x".repeat(max_shared_content_len() + 1)).unwrap();
registry.mark_all_dirty();
registry.refresh_dirty().await;
assert_eq!(
attached.updates.borrow().text,
"first",
"an unsendable file must not replace a good generation"
);
}
#[tokio::test]
async fn a_publish_reports_the_subscribers_it_served() {
let directory = tempfile::tempdir().unwrap();
let path = directory.path().join("ticker.aml");
std::fs::write(&path, "first").unwrap();
let budget = SubscriptionBudget::new(BUDGET);
let (mut registry, _watcher, _events) = registry(&budget);
let (tx, _rx) = wake();
let attached = registry.attach_now(path.clone(), tx, 0).await.unwrap();
assert_eq!(attached.current.text, "first");
assert_eq!(
registry.subscribers_of(&path),
1,
"the initial read must count the subscriber it is reading for"
);
}
#[tokio::test]
async fn attaching_does_not_leave_a_wake_for_the_generation_it_returned() {
let directory = tempfile::tempdir().unwrap();
let path = directory.path().join("ticker.aml");
std::fs::write(&path, "first").unwrap();
let budget = SubscriptionBudget::new(BUDGET);
let (mut registry, _watcher, _events) = registry(&budget);
let (tx, mut rx) = wake();
let _attached = registry.attach_now(path.clone(), tx, 0).await.unwrap();
assert!(
rx.try_recv().is_err(),
"attaching woke the subscriber for the value it was just given"
);
std::fs::write(&path, "second").unwrap();
registry.mark_all_dirty();
registry.refresh_dirty().await;
assert!(
rx.try_recv().is_ok(),
"a real change must wake the subscriber"
);
}
#[tokio::test]
async fn event_source_failure_falls_back_to_reconcile() {
let directory = tempfile::tempdir().unwrap();
let path = directory.path().join("ticker.aml");
std::fs::write(&path, "first").unwrap();
let budget = SubscriptionBudget::new(BUDGET);
let (events_tx, events) = mpsc::channel(8);
let (registry, watcher) =
WatchRegistry::new(budget.clone(), Box::new(ManualChangeSource), events);
let (_shutdown_tx, shutdown_rx) = watch::channel(false);
tokio::spawn(
registry
.with_intervals(Duration::from_millis(1), Duration::from_millis(5))
.run(shutdown_rx),
);
let (tx, _rx) = wake();
let attached = watcher.attach(path.clone(), tx, 0).await.unwrap();
assert_eq!(attached.current.text, "first");
events_tx.send(WatchEvent::SourceFailed).await.unwrap();
drop(events_tx);
std::fs::write(&path, "second").unwrap();
let deadline = std::time::Instant::now() + Duration::from_secs(5);
while attached.updates.borrow().text != "second" {
assert!(
std::time::Instant::now() < deadline,
"reconciliation never delivered the change without an event source"
);
tokio::time::sleep(Duration::from_millis(10)).await;
}
}
#[tokio::test]
async fn watch_entry_allocation_rejection_returns_every_byte() {
let directory = tempfile::tempdir().unwrap();
let path = directory.path().join("ticker.aml");
std::fs::write(&path, "first").unwrap();
for site in [
ServerAllocationSite::WatchEntry,
ServerAllocationSite::WakeSlots,
ServerAllocationSite::Content,
] {
let budget = SubscriptionBudget::new(BUDGET);
let (mut registry, _watcher, _events) = registry(&budget);
let owner = 7;
let rejection = crate::AllocationRejectionGuard::at(site, owner);
let (tx, _rx) = wake();
let result = registry.attach_now(path.clone(), tx, owner).await;
drop(rejection);
if site == ServerAllocationSite::Content {
assert!(result.is_ok());
} else {
assert!(result.is_err(), "{site:?} must refuse the attach");
}
drop(result);
registry.reap_now();
assert_eq!(budget.used(), 0, "{site:?} leaked budget");
}
}
}
#[cfg(test)]
mod notify_tests {
use super::*;
#[tokio::test]
async fn write_via_rename_is_observed() {
let directory = tempfile::tempdir().unwrap();
let path = directory.path().join("ticker.aml");
std::fs::write(&path, "first").unwrap();
let (events_tx, events) = mpsc::channel(64);
let Some(source) = NotifyChangeSource::new(events_tx) else {
return;
};
let budget = SubscriptionBudget::new(8 * 1024 * 1024);
let (mut registry, _watcher) = WatchRegistry::new(budget.clone(), Box::new(source), events);
let (tx, _rx) = mpsc::channel(1);
let attached = registry.attach_now(path.clone(), tx, 0).await.unwrap();
assert_eq!(attached.current.text, "first");
let staging = directory.path().join("ticker.aml.tmp");
std::fs::write(&staging, "second").unwrap();
std::fs::rename(&staging, &path).unwrap();
let deadline = std::time::Instant::now() + Duration::from_secs(5);
loop {
while let Ok(event) = registry.events.try_recv() {
registry.mark(event);
}
registry.mark_all_dirty();
registry.refresh_dirty().await;
if attached.updates.borrow().text == "second" {
break;
}
assert!(
std::time::Instant::now() < deadline,
"a write-via-rename was never observed"
);
tokio::time::sleep(Duration::from_millis(20)).await;
}
}
}