use std::{path::PathBuf, sync::Arc, time::Duration};
use pretty_assertions::assert_eq;
use tempfile::TempDir;
use tokio::{sync::watch, time::timeout};
use super::JobFeed;
use crate::app::{
daemon_host::{
jobs::{JobId, JobRecord, JobRegistry, JobStore},
sessions::Sessions,
},
pull_requests::ScriptedForge,
};
const CHANGE_DEADLINE: Duration = Duration::from_secs(10);
fn ids(receiver: &watch::Receiver<Arc<rho_protocol::JobListResponse>>) -> Vec<String> {
let mut ids: Vec<_> = receiver
.borrow()
.jobs
.iter()
.map(|job| job.id.to_string())
.collect();
ids.sort();
ids
}
async fn add_session_job(registry: &JobRegistry) -> String {
let id = JobId::generate();
let record = JobRecord::new_session(
id.clone(),
PathBuf::from("/work"),
rho_sdk::SessionId::new(),
);
registry.register(record).unwrap();
id.to_string()
}
#[tokio::test]
async fn a_followed_list_moves_with_the_registry_across_followers() {
let home = TempDir::new().unwrap();
let registry = Arc::new(JobRegistry::open(JobStore::open(home.path()).unwrap()));
let sessions = Sessions::new(Arc::clone(®istry), home.path(), ScriptedForge::new());
let feed = JobFeed::new(Arc::clone(®istry), sessions);
let mut first = feed.subscribe();
assert_eq!(ids(&first), Vec::<String>::new());
let added = add_session_job(®istry).await;
timeout(CHANGE_DEADLINE, first.changed())
.await
.unwrap()
.unwrap();
assert_eq!(ids(&first), vec![added.clone()]);
drop(first);
let mut second = feed.subscribe();
second.borrow_and_update();
let later = add_session_job(®istry).await;
timeout(CHANGE_DEADLINE, second.changed())
.await
.unwrap()
.unwrap();
let mut expected = vec![added, later];
expected.sort();
assert_eq!(ids(&second), expected);
}