crates/run/sched/tests/sched.rs 7.7 KiB 279 lines Raw
1 //! The queue in front of one peer.2 //!3 //! Every test here is about an order, so every test names the order that it4 //! expects and the reason it has to be that order.5 6 use std::sync::Arc;7 use std::thread;8 use std::time::{Duration, Instant};9 10 use inf3r_sched::{Priority, Sched, SchedError};11 12 fn now() -> Instant {13     Instant::now()14 }15 16 #[test]17 fn a_free_slot_is_taken_at_once() {18     let sched = Arc::new(Sched::new());19     sched.register("m", 2, 4);20     let lease = sched.acquire(21         "m",22         Priority::Interactive,23         now(),24         now() + Duration::from_secs(1),25     );26     assert!(lease.is_ok());27     assert_eq!(sched.in_flight("m"), 1);28     drop(lease);29     assert_eq!(sched.in_flight("m"), 0);30 }31 32 #[test]33 fn an_unknown_name_is_unknown() {34     let sched = Arc::new(Sched::new());35     let at = now();36     assert_eq!(37         sched38             .acquire(39                 "absent",40                 Priority::Interactive,41                 at,42                 at + Duration::from_secs(1)43             )44             .err(),45         Some(SchedError::Unknown)46     );47 }48 49 #[test]50 fn a_deadline_that_has_passed_is_expired_not_queued() {51     let sched = Arc::new(Sched::new());52     sched.register("m", 1, 4);53     let at = now();54     assert_eq!(55         sched.acquire("m", Priority::Interactive, at, at).err(),56         Some(SchedError::Expired)57     );58     assert_eq!(sched.queued("m"), 0);59 }60 61 #[test]62 fn a_full_queue_is_refused() {63     let sched = Arc::new(Sched::new());64     sched.register("m", 1, 1);65     let held = sched66         .acquire(67             "m",68             Priority::Interactive,69             now(),70             now() + Duration::from_secs(1),71         )72         .expect("first");73 74     // One place in the queue is taken by a waiter that is still waiting.75     let waiter = {76         let sched = Arc::clone(&sched);77         thread::spawn(move || {78             let at = Instant::now();79             sched80                 .acquire(81                     "m",82                     Priority::Background,83                     at,84                     at + Duration::from_millis(60),85                 )86                 .err()87         })88     };89     thread::sleep(Duration::from_millis(20));90 91     // The next arrival has nowhere to go, and says so at once.92     let at = now();93     assert_eq!(94         sched95             .acquire(96                 "m",97                 Priority::Background,98                 at,99                 at + Duration::from_millis(60)100             )101             .err(),102         Some(SchedError::QueueFull)103     );104     assert_eq!(waiter.join().expect("join"), Some(SchedError::Timeout));105     drop(held);106 }107 108 #[test]109 fn an_interactive_waiter_goes_before_a_background_waiter() {110     let sched = Arc::new(Sched::new());111     sched.register("m", 1, 8);112     let held = sched113         .acquire(114             "m",115             Priority::Interactive,116             now(),117             now() + Duration::from_secs(5),118         )119         .expect("the holder");120 121     // The background waiter arrives first, and must still lose.122     let background = {123         let sched = Arc::clone(&sched);124         thread::spawn(move || {125             let at = Instant::now();126             sched127                 .acquire("m", Priority::Background, at, at + Duration::from_secs(5))128                 .map(|lease| lease.plugin().to_string())129         })130     };131     thread::sleep(Duration::from_millis(50));132     let interactive = {133         let sched = Arc::clone(&sched);134         thread::spawn(move || {135             let at = Instant::now();136             sched137                 .acquire("m", Priority::Interactive, at, at + Duration::from_secs(5))138                 .map(|lease| lease.plugin().to_string())139         })140     };141     thread::sleep(Duration::from_millis(50));142 143     // The interactive waiter takes the slot that is freed next.144     drop(held);145     let won = interactive.join().expect("join").expect("interactive");146     assert_eq!(won, "m");147     drop(background.join().expect("join"));148 }149 150 #[test]151 fn a_wait_that_lasts_too_long_is_a_timeout() {152     let sched = Arc::new(Sched::new());153     sched.register("m", 1, 4);154     let held = sched155         .acquire(156             "m",157             Priority::Interactive,158             now(),159             now() + Duration::from_secs(5),160         )161         .expect("the holder");162 163     let waiter = {164         let sched = Arc::clone(&sched);165         thread::spawn(move || {166             let at = Instant::now();167             sched168                 .acquire(169                     "m",170                     Priority::Interactive,171                     at,172                     at + Duration::from_millis(20),173                 )174                 .err()175         })176     };177     assert_eq!(waiter.join().expect("join"), Some(SchedError::Timeout));178     assert_eq!(sched.queued("m"), 0);179     drop(held);180 }181 182 #[test]183 fn a_release_hands_the_slot_to_the_waiter() {184     let sched = Arc::new(Sched::new());185     sched.register("m", 1, 4);186     let held = sched187         .acquire(188             "m",189             Priority::Interactive,190             now(),191             now() + Duration::from_secs(5),192         )193         .expect("the holder");194 195     let waiter = {196         let sched = Arc::clone(&sched);197         thread::spawn(move || {198             let at = Instant::now();199             sched200                 .acquire("m", Priority::Interactive, at, at + Duration::from_secs(5))201                 .is_ok()202         })203     };204     thread::sleep(Duration::from_millis(20));205     assert!(!waiter.is_finished(), "the waiter must still wait");206     drop(held);207     assert!(waiter.join().expect("join"));208     // The waiter dropped its lease as it returned, so the peer is idle again.209     assert_eq!(sched.in_flight("m"), 0);210 }211 212 #[test]213 fn a_snapshot_names_every_peer() {214     let sched = Arc::new(Sched::new());215     sched.register("a", 2, 4);216     sched.register("b", 1, 2);217     let _lease = sched218         .acquire(219             "a",220             Priority::Interactive,221             now(),222             now() + Duration::from_secs(1),223         )224         .expect("a slot");225     let rows = sched.snapshot();226     assert_eq!(rows.len(), 2);227     assert_eq!(rows[0], ("a".to_string(), 1, 2, 0));228     assert_eq!(rows[1], ("b".to_string(), 0, 1, 0));229 }230 231 #[test]232 fn an_aged_background_waiter_goes_before_a_new_interactive() {233     let sched = Arc::new(Sched::new());234     sched.register("m", 1, 8);235     let held = sched236         .acquire(237             "m",238             Priority::Interactive,239             now(),240             now() + Duration::from_secs(5),241         )242         .expect("the holder");243 244     let background = {245         let sched = Arc::clone(&sched);246         thread::spawn(move || {247             let at = Instant::now();248             sched249                 .acquire("m", Priority::Background, at, at + Duration::from_secs(5))250                 .map(|lease| lease.plugin().to_string())251         })252     };253     let queued = Instant::now() + Duration::from_secs(1);254     while sched.queued("m") == 0 {255         assert!(Instant::now() < queued, "the background job never queued");256         thread::sleep(Duration::from_millis(5));257     }258     thread::sleep(inf3r_sched::BACKGROUND_AGING + Duration::from_millis(50));259     let interactive = {260         let sched = Arc::clone(&sched);261         thread::spawn(move || {262             let at = Instant::now();263             sched264                 .acquire("m", Priority::Interactive, at, at + Duration::from_secs(5))265                 .map(|lease| lease.plugin().to_string())266         })267     };268     thread::sleep(Duration::from_millis(20));269 270     // The background waiter is older than the aging window, so it outranks271     // the newcomer even though the newcomer is interactive.272     drop(held);273     assert_eq!(274         background.join().expect("join").as_deref(),275         Ok("m"),276         "the aged background waiter takes the slot"277     );278     assert!(interactive.join().expect("join").is_ok());279 }