69 lines · 3.0 KB · RustRaw
use super::*;fn w(id: &str, written_at: i64, attempts: u32, tried_at: Option<i64>) -> Waiting { Waiting { id: id.into(), thread: "t".into(), written_at, attempts, tried_at }}fn wt(thread: &str, id: &str, written_at: i64, attempts: u32, tried_at: Option<i64>) -> Waiting { Waiting { id: id.into(), thread: thread.into(), written_at, attempts, tried_at }}/// RULE: Within a conversation the oldest waiting message is due, so none overtakes another.#[test]fn the_oldest_goes_first_and_alone() { let waiting = [w("b", 20, 0, None), w("a", 10, 2, Some(1000))]; // a waits 4 s after its second failure; b, younger, is not due while a waits. assert_eq!(due(&waiting, 3000), None); assert_eq!(due(&waiting, 5000).map(|m| m.id.as_str()), Some("a")); assert_eq!(next_at(&waiting), Some(5000)); assert_eq!(due(&[], 0), None);}/// RULE: The wait doubles from two seconds to five minutes.#[test]fn the_wait_doubles_to_five_minutes() { assert_eq!([0, 1, 2, 3, 8, 9, 40].map(backoff_ms), [0, 2000, 4000, 8000, 256_000, 300_000, 300_000]);}/// RULE: Taken is sent; no answer retries; a refusal the object meant fails, as does the fiftieth try.#[test]fn what_an_answer_means() { assert_eq!(outcome(200, 0), Outcome::Taken); assert_eq!(outcome(0, 0), Outcome::Retry); assert_eq!(outcome(429, 3), Outcome::Retry); assert_eq!(outcome(503, 3), Outcome::Retry); assert_eq!(outcome(400, 0), Outcome::Failed); assert_eq!(outcome(0, ATTEMPTS_MAX - 1), Outcome::Failed);}/// RULE: A receipt only moves a message forward, and never a failed one.#[test]fn receipts_move_forward_only() { assert_eq!(advanced("sent", "delivered"), "delivered"); assert_eq!(advanced("read", "delivered"), "read"); assert_eq!(advanced("delivered", "read"), "read"); assert_eq!(advanced("failed", "read"), "failed");}/// RULE: A message waiting in one conversation never holds up another conversation.#[test]fn conversations_go_side_by_side() { let waiting = [wt("t1", "a", 10, 1, Some(1000)), wt("t2", "b", 20, 0, None)]; // a waits until 3000; b, in another chat, goes now. assert_eq!(due(&waiting, 1500).map(|m| m.id.as_str()), Some("b")); assert_eq!(due(&waiting, 3000).map(|m| m.id.as_str()), Some("a")); assert_eq!(next_at(&waiting), Some(0));}/// RULE: Once tried three times, a stuck message stops holding up the ones behind it in its conversation.#[test]fn a_stuck_message_lets_the_rest_go() { let young = [w("a", 10, STUCK_AFTER - 1, Some(1000)), w("b", 20, 0, None)]; assert_eq!(due(&young, 1500), None); assert_eq!(next_at(&young), Some(1000 + backoff_ms(STUCK_AFTER - 1))); let stuck = [w("a", 10, STUCK_AFTER, Some(1000)), w("b", 20, 0, None), w("c", 30, 0, None)]; assert_eq!(due(&stuck, 1500).map(|m| m.id.as_str()), Some("b")); // c still waits behind b, which has not been tried. assert_eq!(due(&[stuck[0].clone(), stuck[2].clone()], 1500).map(|m| m.id.as_str()), Some("c")); assert_eq!(next_at(&stuck), Some(0));}