#![cfg(all(unix, feature = "jobserver"))]
use std::collections::BTreeMap;
use std::ffi::CString;
use std::fs::{File, OpenOptions};
use std::io;
use std::ops::Bound;
use std::os::unix::ffi::OsStrExt;
use std::os::unix::fs::OpenOptionsExt;
use std::path::{Path, PathBuf};
use std::time::Duration;
use async_channel::{Receiver, Sender};
use async_io::Async;
use futures_lite::future;
use polling::{Event, Events, PollMode, Poller};
use rustix::io as rustix_io;
#[cfg(any(target_os = "linux", target_os = "android"))]
use rustix::fs::inotify;
#[cfg(any(target_os = "linux", target_os = "android"))]
use std::collections::HashMap;
#[cfg(any(target_os = "linux", target_os = "android"))]
use std::os::unix::io::OwnedFd;
const TOKEN: u8 = b'+';
const POOL_KEY: usize = 0;
#[cfg(any(target_os = "linux", target_os = "android"))]
const INOTIFY_KEY: usize = usize::MAX - 1;
#[derive(Debug, thiserror::Error)]
pub enum JobserverError {
#[error("jobserver I/O error: {0}")]
Io(#[from] io::Error),
#[error("jobserver broker stopped")]
BrokerStopped,
#[error("jobserver fifo path is not valid: {0}")]
InvalidPath(PathBuf),
}
impl From<rustix_io::Errno> for JobserverError {
fn from(error: rustix_io::Errno) -> Self {
Self::Io(io::Error::from(error))
}
}
fn open_ends(path: &Path) -> io::Result<(File, File)> {
let read = OpenOptions::new()
.read(true)
.custom_flags(libc::O_NONBLOCK)
.open(path)?;
let write = OpenOptions::new()
.write(true)
.custom_flags(libc::O_NONBLOCK)
.open(path)?;
Ok((read, write))
}
fn mkfifo(path: &Path) -> Result<(), JobserverError> {
let c_path = CString::new(path.as_os_str().as_bytes())
.map_err(|_| JobserverError::InvalidPath(path.to_path_buf()))?;
if unsafe { libc::mkfifo(c_path.as_ptr(), 0o600) } == -1 {
return Err(io::Error::last_os_error().into());
}
Ok(())
}
fn pending_tokens(file: &File) -> io::Result<usize> {
let count = rustix_io::ioctl_fionread(file)?;
Ok(usize::try_from(count).unwrap_or(usize::MAX))
}
#[derive(Debug)]
enum TokenRead {
Token,
Empty,
Eof,
}
fn read_token(file: &File) -> io::Result<TokenRead> {
let mut byte = [0u8; 1];
match rustix_io::read(file, &mut byte) {
Ok(0) => Ok(TokenRead::Eof),
Ok(1) => Ok(TokenRead::Token),
Ok(_) => unreachable!("read of a 1-byte buffer returned more"),
Err(error) if error == rustix_io::Errno::AGAIN => Ok(TokenRead::Empty),
Err(error) => Err(error.into()),
}
}
fn write_token(file: &File) -> io::Result<()> {
match rustix_io::write(file, &[TOKEN]) {
Ok(1) => Ok(()),
Ok(_) => Err(io::Error::new(
io::ErrorKind::WriteZero,
"jobserver token write returned zero",
)),
Err(error) => Err(error.into()),
}
}
pub struct JobPool {
read: File,
write: File,
}
impl std::fmt::Debug for JobPool {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("JobPool").finish_non_exhaustive()
}
}
impl JobPool {
pub fn fifo(path: impl AsRef<Path>) -> Result<Self, JobserverError> {
let path = path.as_ref();
let (read, write) = open_ends(path)?;
Ok(Self { read, write })
}
pub fn create_fifo(path: impl AsRef<Path>, tokens: usize) -> Result<Self, JobserverError> {
let path = path.as_ref();
mkfifo(path)?;
let pool = Self::fifo(path)?;
for _ in 0..tokens {
write_token(&pool.write)?;
}
Ok(pool)
}
}
fn jobserver_makeflags(auth_path: &str) -> String {
format!("-j --jobserver-auth=fifo:{auth_path}")
}
enum Wake {
Command(Result<BrokerCommand, async_channel::RecvError>),
Ready(io::Result<()>),
}
enum BrokerCommand {
Register {
reply: Sender<Result<CommandFifo, JobserverError>>,
},
Finish {
id: usize,
},
Finished {
id: usize,
},
}
pub struct CommandFifo {
id: usize,
tx: Sender<BrokerCommand>,
path: PathBuf,
}
impl std::fmt::Debug for CommandFifo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CommandFifo")
.field("id", &self.id)
.field("path", &self.path)
.finish_non_exhaustive()
}
}
impl CommandFifo {
#[must_use]
pub fn path(&self) -> &Path {
&self.path
}
#[must_use]
pub fn environment(&self) -> Vec<(String, String)> {
let auth_path = self
.path
.to_str()
.expect("jobserver fifo dir is validated UTF-8 at broker setup");
let flags = jobserver_makeflags(auth_path);
["MAKEFLAGS", "CARGO_MAKEFLAGS", "MFLAGS"]
.iter()
.map(|name| ((*name).to_string(), flags.clone()))
.collect()
}
}
impl Drop for CommandFifo {
fn drop(&mut self) {
let _ = self.tx.try_send(BrokerCommand::Finish { id: self.id });
}
}
#[derive(Clone, Debug)]
pub struct JobBroker {
tx: Sender<BrokerCommand>,
}
impl JobBroker {
pub async fn open_fifo(&self) -> Result<CommandFifo, JobserverError> {
let (reply, rx) = async_channel::bounded(1);
self.tx
.send(BrokerCommand::Register { reply })
.await
.map_err(|_| JobserverError::BrokerStopped)?;
rx.recv().await.map_err(|_| JobserverError::BrokerStopped)?
}
}
pub fn job_broker_channel(
pool: JobPool,
fifo_dir: impl AsRef<Path>,
) -> Result<(JobBroker, JobBrokerService), JobserverError> {
let dir = fifo_dir.as_ref();
if dir.to_str().is_none() {
return Err(JobserverError::InvalidPath(dir.to_path_buf()));
}
std::fs::create_dir_all(dir)?;
let poller = Poller::new()?;
unsafe {
poller.add_with_mode(&pool.read, Event::readable(POOL_KEY), PollMode::Edge)?;
}
#[cfg(any(target_os = "linux", target_os = "android"))]
let inotify_fd = {
let fd = inotify::init(inotify::CreateFlags::NONBLOCK | inotify::CreateFlags::CLOEXEC)?;
unsafe { poller.add_with_mode(&fd, Event::readable(INOTIFY_KEY), PollMode::Edge)? };
fd
};
let poller = Async::new_nonblocking(poller)?;
let (tx, rx) = async_channel::unbounded();
Ok((
JobBroker { tx: tx.clone() },
JobBrokerService {
rx,
lease_tx: tx.downgrade(),
pool,
poller,
dir: dir.to_path_buf(),
fifos: BTreeMap::new(),
#[cfg(any(target_os = "linux", target_os = "android"))]
inotify: inotify_fd,
#[cfg(any(target_os = "linux", target_os = "android"))]
watches: HashMap::new(),
next_id: 1,
cursor: None,
},
))
}
pub struct JobBrokerService {
rx: Receiver<BrokerCommand>,
lease_tx: async_channel::WeakSender<BrokerCommand>,
pool: JobPool,
poller: Async<Poller>,
dir: PathBuf,
fifos: BTreeMap<usize, FifoState>,
#[cfg(any(target_os = "linux", target_os = "android"))]
inotify: OwnedFd,
#[cfg(any(target_os = "linux", target_os = "android"))]
watches: HashMap<i32, usize>,
next_id: usize,
cursor: Option<usize>,
}
impl std::fmt::Debug for JobBrokerService {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("JobBrokerService")
.field("fifos", &self.fifos.len())
.finish_non_exhaustive()
}
}
struct FifoState {
read: File,
write: Option<File>,
path: PathBuf,
handed: usize,
finishing: bool,
}
impl JobBrokerService {
pub async fn serve(mut self) {
let mut events = Events::new();
loop {
let wake = future::or(async { Wake::Command(self.rx.recv().await) }, async {
Wake::Ready(self.poller.readable().await)
})
.await;
match wake {
Wake::Command(Ok(BrokerCommand::Register { reply })) => {
let _ = reply.send(self.register().await).await;
}
Wake::Command(Ok(BrokerCommand::Finish { id })) => self.begin_finish(id),
Wake::Command(Ok(BrokerCommand::Finished { id })) => self.finished(id),
Wake::Command(Err(_)) => break,
Wake::Ready(Err(error)) => {
panic!("jobserver poller readiness wait failed: {error}");
}
Wake::Ready(Ok(())) => self.reconcile(&mut events),
}
}
}
async fn register(&mut self) -> Result<CommandFifo, JobserverError> {
let dir = self.dir.clone();
let (path, read, write) = blocking::unblock(move || {
let path = dir.join(format!("{}.fifo", uuid::Uuid::new_v4()));
mkfifo(&path)?;
let (read, write) = open_ends(&path)?;
Ok::<_, JobserverError>((path, read, write))
})
.await?;
let id = self.next_id;
self.next_id = self
.next_id
.checked_add(1)
.filter(|&id| id < usize::MAX - 1)
.expect("jobserver fifo ids exhausted");
if let Err(error) = self.watch_fifo(&path, &read, &write, id) {
let _ = async_fs::remove_file(&path).await;
return Err(error);
}
self.fifos.insert(
id,
FifoState {
read,
write: Some(write),
path: path.clone(),
handed: 0,
finishing: false,
},
);
Ok(CommandFifo {
id,
tx: self
.lease_tx
.upgrade()
.expect("broker handle outlives its register request"),
path,
})
}
fn watch_fifo(
&mut self,
path: &Path,
read: &File,
write: &File,
id: usize,
) -> Result<(), JobserverError> {
#[cfg(any(target_os = "linux", target_os = "android"))]
{
unsafe {
self.poller
.get_ref()
.add_with_mode(read, Event::readable(id), PollMode::Edge)?;
}
let wd = inotify::add_watch(
&self.inotify,
path,
inotify::WatchFlags::ACCESS | inotify::WatchFlags::MODIFY,
)
.inspect_err(|_| {
let _ = self.poller.get_ref().delete(read);
})?;
self.watches.insert(wd, id);
let _ = write;
}
#[cfg(not(any(target_os = "linux", target_os = "android")))]
{
unsafe {
self.poller
.get_ref()
.add_with_mode(read, Event::readable(id), PollMode::Edge)?;
}
let result = unsafe {
self.poller
.get_ref()
.add_with_mode(write, Event::writable(id), PollMode::Edge)
};
if let Err(error) = result {
let _ = self.poller.get_ref().delete(read);
return Err(error.into());
}
let _ = path;
}
Ok(())
}
fn reconcile(&mut self, events: &mut Events) {
loop {
let count = self
.poller
.get_ref()
.wait(events, Some(Duration::ZERO))
.expect("jobserver poller wait failed");
if count == 0 {
break;
}
let keys: Vec<usize> = events.iter().map(|event| event.key).collect();
events.clear();
for key in keys {
if key == POOL_KEY {
continue;
}
#[cfg(any(target_os = "linux", target_os = "android"))]
if key == INOTIFY_KEY {
for id in self.inotify_fired() {
self.settle_or_drain(id);
}
continue;
}
self.settle_or_drain(key);
}
}
self.feed();
}
#[cfg(any(target_os = "linux", target_os = "android"))]
fn inotify_fired(&mut self) -> Vec<usize> {
let mut buf = [std::mem::MaybeUninit::<u8>::uninit(); 4096];
let mut reader = inotify::Reader::new(&self.inotify, &mut buf);
let mut ids = Vec::new();
loop {
match reader.next() {
Ok(event) => {
if event.events().contains(inotify::ReadFlags::QUEUE_OVERFLOW) {
tracing::warn!("jobserver inotify queue overflow; reconciling every fifo");
ids.extend(self.fifos.keys().copied());
continue;
}
if event.events().contains(inotify::ReadFlags::IGNORED) {
match self.watches.remove(&event.wd()) {
Some(id) if self.fifos[&id].finishing => {}
None => {}
Some(id) => panic!(
"jobserver fifo watch on {} was removed while the command was live; consumption can no longer wake the broker",
self.fifos[&id].path.display()
),
}
continue;
}
if let Some(&id) = self.watches.get(&event.wd()) {
ids.push(id);
}
}
Err(error) if error == rustix_io::Errno::AGAIN => break,
Err(error) => panic!("jobserver inotify read failed: {error}"),
}
}
ids
}
fn settle_or_drain(&mut self, id: usize) {
let Some(state) = self.fifos.get(&id) else {
return;
};
if !state.finishing {
self.settle(id);
}
}
fn settle(&mut self, id: usize) {
let Some(state) = self.fifos.get_mut(&id) else {
return;
};
let pending = pending_tokens(&state.read).expect("jobserver fifo FIONREAD failed");
let legit = pending.min(state.handed);
if pending > legit {
let fabricated = pending - legit;
tracing::warn!(
id,
fabricated,
"jobserver command wrote tokens it was never handed; discarding them"
);
for _ in 0..fabricated {
if !matches!(read_token(&state.read), Ok(TokenRead::Token)) {
break;
}
}
}
for _ in 1..legit {
match read_token(&state.read) {
Ok(TokenRead::Token) => {
write_token(&self.pool.write).expect("jobserver pool write failed");
state.handed -= 1;
}
_ => break,
}
}
}
fn begin_finish(&mut self, id: usize) {
let Some(state) = self.fifos.get_mut(&id) else {
return;
};
if state.finishing {
return;
}
state.finishing = true;
if let Some(write) = state.write.take() {
#[cfg(not(any(target_os = "linux", target_os = "android")))]
self.poller
.get_ref()
.delete(&write)
.expect("jobserver fifo write end deregistration failed");
drop(write);
}
let tx = self
.lease_tx
.upgrade()
.expect("a Finish came from a live lease");
assert!(
spawn_drain(&self.pool.write, state, id, Some(tx)),
"jobserver fifo drain thread could not be equipped"
);
}
fn finished(&mut self, id: usize) {
let Some(state) = self.fifos.remove(&id) else {
return;
};
self.poller
.get_ref()
.delete(&state.read)
.expect("jobserver fifo read end deregistration failed");
#[cfg(any(target_os = "linux", target_os = "android"))]
self.watches.retain(|_, fifo_id| *fifo_id != id);
}
fn feed(&mut self) {
let ids: Vec<usize> = match self.cursor {
Some(cursor) => self
.fifos
.range((Bound::Excluded(cursor), Bound::Unbounded))
.chain(self.fifos.range(..=cursor))
.map(|(id, _)| *id)
.collect(),
None => self.fifos.keys().copied().collect(),
};
for id in ids {
let Some(state) = self.fifos.get_mut(&id) else {
continue;
};
if state.finishing
|| pending_tokens(&state.read).expect("jobserver fifo FIONREAD failed") != 0
{
continue;
}
match read_token(&self.pool.read) {
Ok(TokenRead::Token) => {
let write = state
.write
.as_ref()
.expect("an active fifo keeps a broker write end");
if let Err(error) = write_token(write) {
let _ = write_token(&self.pool.write);
panic!("jobserver fifo write failed: {error}");
}
state.handed += 1;
self.cursor = Some(id);
}
Ok(TokenRead::Empty | TokenRead::Eof) => break,
Err(error) => panic!("jobserver pool read failed: {error}"),
}
}
}
}
fn spawn_drain(
pool_write_src: &File,
state: &FifoState,
id: usize,
tx: Option<Sender<BrokerCommand>>,
) -> bool {
let equipped = || -> Option<(File, File)> {
let drain = state.read.try_clone().ok()?;
rustix::fs::fcntl_setfl(&drain, rustix::fs::OFlags::empty()).ok()?;
let pool_write = pool_write_src.try_clone().ok()?;
Some((drain, pool_write))
};
let Some((drain, pool_write)) = equipped() else {
return false;
};
let limit = state.handed;
let path = state.path.clone();
std::thread::spawn(move || {
drain_fifo(id, limit, &drain, &pool_write);
let _ = std::fs::remove_file(&path);
if let Some(tx) = tx {
let _ = tx.send_blocking(BrokerCommand::Finished { id });
}
});
true
}
fn drain_fifo(id: usize, limit: usize, read: &File, pool: &File) {
let mut forwarded = 0usize;
let mut discarded = 0usize;
let mut byte = [0u8; 1];
loop {
match rustix_io::read(read, &mut byte) {
Ok(1) if forwarded < limit => {
rustix_io::write(pool, &byte).expect("jobserver pool token return failed");
forwarded += 1;
}
Ok(1) => discarded += 1,
Ok(0) => break,
Ok(_) => unreachable!("a one-byte read buffer cannot return more"),
Err(error) if error == rustix::io::Errno::INTR => {}
Err(error) => panic!("jobserver fifo drain read failed: {error}"),
}
}
if discarded > 0 {
tracing::warn!(
id,
discarded,
"jobserver command returned tokens it was never handed"
);
}
for _ in forwarded..limit {
rustix_io::write(pool, &[TOKEN]).expect("jobserver pool token mint failed");
}
}
impl Drop for JobBrokerService {
fn drop(&mut self) {
let _ = self.poller.get_ref().delete(&self.pool.read);
#[cfg(any(target_os = "linux", target_os = "android"))]
let _ = self.poller.get_ref().delete(&self.inotify);
for (id, mut state) in std::mem::take(&mut self.fifos) {
if !state.finishing
&& let Some(write) = state.write.take()
{
#[cfg(not(any(target_os = "linux", target_os = "android")))]
let _ = self.poller.get_ref().delete(&write);
drop(write);
}
let _ = self.poller.get_ref().delete(&state.read);
if !state.finishing && !spawn_drain(&self.pool.write, &state, id, None) {
tracing::warn!(
handed = state.handed,
path = %state.path.display(),
"jobserver tokens leak: drain thread could not be equipped"
);
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use executor_core::tokio::TokioGlobal;
use executor_core::{Executor, Task};
use std::time::{Duration, Instant};
use tempfile::tempdir;
struct Harness {
_dir: tempfile::TempDir,
broker: JobBroker,
observer: JobPool,
}
impl Harness {
fn new(size: usize) -> Self {
let dir = tempdir().unwrap();
let path = dir.path().join("pool.fifo");
let pool = JobPool::create_fifo(&path, size).unwrap();
let observer = JobPool::fifo(&path).unwrap();
let (broker, service) = job_broker_channel(pool, dir.path().join("commands")).unwrap();
TokioGlobal
.spawn(async move { service.serve().await })
.detach();
Self {
_dir: dir,
broker,
observer,
}
}
async fn command(&self) -> (CommandFifo, File) {
let lease = self.broker.open_fifo().await.unwrap();
let child = OpenOptions::new()
.read(true)
.write(true)
.custom_flags(libc::O_NONBLOCK)
.open(lease.path())
.unwrap();
(lease, child)
}
fn in_pool(&self) -> usize {
pending_tokens(&self.observer.read).unwrap()
}
}
async fn settle(what: &str, mut condition: impl FnMut() -> bool) {
let deadline = Instant::now() + Duration::from_secs(5);
while !condition() {
assert!(Instant::now() < deadline, "timed out waiting for {what}");
async_io::Timer::after(Duration::from_millis(5)).await;
}
}
fn take_all(child: &File, held: &mut usize) {
while matches!(read_token(child), Ok(TokenRead::Token)) {
*held += 1;
}
}
#[tokio::test]
async fn pool_create_and_take() {
let harness = Harness::new(4);
assert_eq!(harness.in_pool(), 4);
assert!(matches!(
read_token(&harness.observer.read),
Ok(TokenRead::Token)
));
assert_eq!(harness.in_pool(), 3);
}
#[tokio::test]
async fn environment_names_the_command_fifo_not_the_pool() {
let harness = Harness::new(2);
let (lease, _child) = harness.command().await;
let fifo = lease.path().to_string_lossy().into_owned();
assert!(fifo.contains("/commands/"));
let environment = lease.environment();
assert_eq!(environment.len(), 3);
for (name, value) in environment {
assert!(["MAKEFLAGS", "CARGO_MAKEFLAGS", "MFLAGS"].contains(&name.as_str()));
assert_eq!(value, format!("-j --jobserver-auth=fifo:{fifo}"));
assert!(!value.contains("pool.fifo"));
}
}
#[tokio::test]
async fn two_greedy_commands_never_hold_more_than_the_pool() {
const POOL: usize = 3;
let harness = Harness::new(POOL);
let (lease_a, child_a) = harness.command().await;
let (lease_b, child_b) = harness.command().await;
let (mut held_a, mut held_b) = (0, 0);
settle("both commands to drain the pool", || {
take_all(&child_a, &mut held_a);
take_all(&child_b, &mut held_b);
held_a + held_b == POOL
})
.await;
assert!(
held_a >= 1 && held_b >= 1,
"pool starved a command: {held_a}/{held_b}"
);
for _ in 0..20 {
async_io::Timer::after(Duration::from_millis(5)).await;
take_all(&child_a, &mut held_a);
take_all(&child_b, &mut held_b);
assert!(
held_a + held_b <= POOL,
"over-subscribed: {held_a}+{held_b}"
);
}
assert_eq!(harness.in_pool(), 0);
drop((lease_a, lease_b));
async_io::Timer::after(Duration::from_millis(200)).await;
assert_eq!(harness.in_pool(), 0, "tokens returned before EOF");
drop((child_a, child_b));
settle("pool to refill", || harness.in_pool() == POOL).await;
}
#[tokio::test]
async fn killed_command_returns_every_token() {
const POOL: usize = 4;
let harness = Harness::new(POOL);
let (lease, child) = harness.command().await;
let mut held = 0;
settle("command to hold all tokens", || {
take_all(&child, &mut held);
held == POOL
})
.await;
assert_eq!(harness.in_pool(), 0);
drop(child);
drop(lease);
settle("pool to refill after kill", || harness.in_pool() == POOL).await;
async_io::Timer::after(Duration::from_millis(50)).await;
assert_eq!(harness.in_pool(), POOL);
}
#[tokio::test]
async fn exiting_command_leaves_the_pool_full() {
const POOL: usize = 3;
let harness = Harness::new(POOL);
let (lease, child) = harness.command().await;
let mut held = 0;
settle("command to take a token", || {
take_all(&child, &mut held);
held >= 1
})
.await;
for _ in 0..held {
write_token(&child).unwrap();
}
drop(child);
drop(lease);
settle("pool to be full", || harness.in_pool() == POOL).await;
async_io::Timer::after(Duration::from_millis(50)).await;
assert_eq!(harness.in_pool(), POOL);
}
#[tokio::test]
async fn surplus_returned_on_exit_never_inflates_the_pool() {
const POOL: usize = 3;
let harness = Harness::new(POOL);
let (lease, child) = harness.command().await;
let mut held = 0;
settle("command to take tokens", || {
take_all(&child, &mut held);
held == 2
})
.await;
for _ in 0..=held {
write_token(&child).unwrap();
}
drop(child);
drop(lease);
settle("pool to refill after exit", || harness.in_pool() == POOL).await;
async_io::Timer::after(Duration::from_millis(200)).await;
assert_eq!(harness.in_pool(), POOL, "surplus token minted into pool");
}
#[tokio::test]
async fn finish_while_a_process_lives_keeps_tokens_out_of_the_pool() {
const POOL: usize = 4;
let harness = Harness::new(POOL);
let (lease, child) = harness.command().await;
let mut held = 0;
settle("command to hold all tokens", || {
take_all(&child, &mut held);
held == POOL
})
.await;
drop(lease);
async_io::Timer::after(Duration::from_millis(200)).await;
assert_eq!(harness.in_pool(), 0, "tokens returned before EOF");
for _ in 0..2 {
write_token(&child).unwrap();
}
settle("returned tokens to reach the pool", || {
harness.in_pool() == 2
})
.await;
drop(child);
settle("pool to refill after EOF", || harness.in_pool() == POOL).await;
async_io::Timer::after(Duration::from_millis(50)).await;
assert_eq!(harness.in_pool(), POOL);
}
#[tokio::test]
async fn fabricated_tokens_never_inflate_the_pool() {
const POOL: usize = 3;
let harness = Harness::new(POOL);
let (lease, child) = harness.command().await;
let mut held = 0;
settle("command to take two tokens", || {
take_all(&child, &mut held);
held == 2
})
.await;
assert_eq!(harness.in_pool(), 1);
for _ in 0..5 {
write_token(&child).unwrap();
}
settle("surplus to be discarded", || harness.in_pool() == 2).await;
async_io::Timer::after(Duration::from_millis(50)).await;
assert_eq!(harness.in_pool(), 2, "fabricated tokens reached the pool");
drop(child);
drop(lease);
settle("pool to refill", || harness.in_pool() == POOL).await;
async_io::Timer::after(Duration::from_millis(50)).await;
assert_eq!(harness.in_pool(), POOL);
}
#[tokio::test]
async fn spare_tokens_move_back_to_the_pool() {
const POOL: usize = 3;
let harness = Harness::new(POOL);
let (_lease, child) = harness.command().await;
let mut held = 0;
settle("command to hold all tokens", || {
take_all(&child, &mut held);
held == POOL
})
.await;
for _ in 0..held {
write_token(&child).unwrap();
}
settle("surplus to return to the pool", || {
harness.in_pool() == POOL - 1
})
.await;
let mut spare = 0;
take_all(&child, &mut spare);
assert_eq!(spare, 1);
}
#[tokio::test]
async fn open_fifo_fails_when_the_broker_is_gone() {
let dir = tempdir().unwrap();
let pool = JobPool::create_fifo(dir.path().join("pool.fifo"), 1).unwrap();
let (broker, service) = job_broker_channel(pool, dir.path().join("commands")).unwrap();
drop(service);
let error = broker.open_fifo().await.unwrap_err();
assert!(matches!(error, JobserverError::BrokerStopped));
}
}