use std::{
sync::{
Arc,
atomic::{AtomicBool, Ordering},
},
time::Duration,
};
use servlet::{App, FailureKind, Role, ecosystem::tokio, sync::oneshot, task, time::timeout};
use super::support::{config, failures, get};
#[tokio::test]
async fn an_api_service_starts_after_setup_and_gates_readiness() {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let admin = listener.local_addr().unwrap();
drop(listener);
let mut settings = config();
settings.admin_bind = Some(admin);
let (entered_setup, setup_entered) = oneshot::channel();
let (release_setup, setup_release) = oneshot::channel();
let (started, mut service_started) = oneshot::channel();
let (ready, service_ready) = oneshot::channel();
let setup_finished = Arc::new(AtomicBool::new(false));
let observed_setup = setup_finished.clone();
let startup = tokio::spawn(
App::new("api-service-startup")
.config(settings)
.with_role(Role::Api)
.setup(move |_| async move {
entered_setup.send(()).unwrap();
setup_release.await.unwrap();
setup_finished.store(true, Ordering::SeqCst);
Ok(())
})
.service("slow-service", |ctx| async move {
assert!(observed_setup.load(Ordering::SeqCst));
started.send(()).unwrap();
service_ready.await.unwrap();
ctx.ready();
ctx.shutdown().cancelled().await;
Ok(())
})
.start(),
);
timeout(Duration::from_secs(1), setup_entered)
.await
.unwrap()
.unwrap();
assert!(get(admin, "/live").await.starts_with("HTTP/1.1 200"));
assert!(get(admin, "/ready").await.starts_with("HTTP/1.1 503"));
assert!(matches!(
service_started.try_recv(),
Err(oneshot::error::TryRecvError::Empty)
));
assert!(!startup.is_finished());
release_setup.send(()).unwrap();
timeout(Duration::from_secs(1), service_started)
.await
.unwrap()
.unwrap();
assert!(get(admin, "/ready").await.starts_with("HTTP/1.1 503"));
assert!(!startup.is_finished());
ready.send(()).unwrap();
let running = startup.await.unwrap().unwrap();
assert!(get(admin, "/ready").await.starts_with("HTTP/1.1 200"));
running.handle().shutdown();
running.wait().await.unwrap();
}
#[tokio::test]
async fn service_errors_panics_and_early_exits_keep_the_registered_name() {
let fail: fn() -> task::Result = || Err(std::io::Error::other("service unavailable").into());
for (exit, expected) in [
(fail, FailureKind::Error("service unavailable".into())),
(|| panic!("service panic"), FailureKind::Panicked),
(|| Ok(()), FailureKind::UnexpectedExit),
] {
let (trigger, triggered) = oneshot::channel();
let running = App::new("api-service-supervision")
.config(config())
.with_role(Role::Api)
.service("required-service", move |ctx| async move {
ctx.ready();
triggered.await.unwrap();
exit()
})
.start()
.await
.unwrap();
trigger.send(()).unwrap();
let error = timeout(Duration::from_secs(2), running.wait())
.await
.unwrap()
.unwrap_err();
let observed = failures(error);
assert_eq!(observed.len(), 1);
assert_eq!(observed[0].component, "required-service");
assert_eq!(observed[0].kind, expected);
}
}
#[tokio::test(start_paused = true)]
async fn shutdown_drains_the_service_before_releasing_managed_resources() {
let (notified, shutdown) = oneshot::channel();
let (finish, finishing) = oneshot::channel();
let drained = Arc::new(AtomicBool::new(false));
let observed_drain = drained.clone();
let running = App::new("api-service-shutdown")
.config(config())
.with_role(Role::Api)
.setup(|mut ctx| async move {
ctx.cleanup("service-resource", || async move {
assert!(observed_drain.load(Ordering::SeqCst));
Ok(())
})?;
Ok(())
})
.service("draining-service", |ctx| async move {
ctx.ready();
ctx.shutdown().cancelled().await;
notified.send(()).unwrap();
finishing.await.unwrap();
drained.store(true, Ordering::SeqCst);
Ok(())
})
.start()
.await
.unwrap();
let handle = running.handle();
handle.shutdown();
timeout(Duration::from_secs(1), shutdown)
.await
.unwrap()
.unwrap();
assert!(!handle.is_ready());
finish.send(()).unwrap();
running.wait().await.unwrap();
}
#[tokio::test]
async fn services_and_active_workers_share_registration_names() {
let app = |role| {
App::new("shared-task-name")
.config(config())
.with_role(role)
.service("shared", |ctx| async move {
ctx.ready();
ctx.shutdown().cancelled().await;
Ok(())
})
.worker("shared", |_| async {
panic!("duplicate or inactive worker must not run")
})
};
for role in [Role::All, Role::Worker] {
let error = app(role).start().await.err().unwrap();
let observed = failures(error);
assert_eq!(observed.len(), 1);
assert_eq!(observed[0].component, "startup");
assert!(matches!(observed[0].kind, FailureKind::Registration(_)));
}
let running = app(Role::Api).start().await.unwrap();
running.handle().shutdown();
running.wait().await.unwrap();
}