Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/monetize

crates/monetize-server/src/estate_order/tests/nudge.rs

//! **A22 #5 (F-B-08): the tick an answer nudges runs off the async gRPC task.** `Start`,
//! `Renew`, `ChangePlan`, `OrderSite` and `Settle` ran `estate_order::tick` on the handler's
//! own async task after their answer was made: the tick asks vendors (open pages), clouds
//! (accounts, the sweep) and the boxes' doors (updates), and for as long as they took it held
//! a runtime worker and the answer. Now it runs on the blocking pool; the answer waits for it
//! at most [`super::super::NUDGE_WAIT`], so a quick tick is still seen by the page's first
//! poll and a slow one finishes behind the answer.

use super::*;
use std::sync::atomic::AtomicBool;
use std::time::Duration;

/// ★ **A slow tick holds neither the answer nor the runtime**: on a one-thread runtime (the
/// worst case: the answer's own thread), the answer comes back at the bound, and the tick
/// finishes behind it.
#[tokio::test(flavor = "current_thread")]
async fn a_slow_tick_behind_an_answer_holds_neither_the_answer_nor_the_runtime() {
    let done = Arc::new(AtomicBool::new(false));
    let d = done.clone();
    let started = std::time::Instant::now();
    let seen = super::super::behind_the_answer(Duration::from_millis(200), move || {
        std::thread::sleep(Duration::from_secs(2));
        d.store(true, Ordering::SeqCst);
    })
    .await;
    assert!(!seen, "the answer did not wait for the slow tick");
    assert!(
        started.elapsed() < Duration::from_secs(1),
        "answered at the bound: {:?}",
        started.elapsed()
    );
    assert!(!done.load(Ordering::SeqCst));
    until("the tick finishes behind the answer", || {
        done.load(Ordering::SeqCst)
    });
}

/// ★ **A quick tick is seen by the answer**, as before: the page's first poll already reads
/// the vendor's answer.
#[tokio::test(flavor = "current_thread")]
async fn a_quick_tick_is_done_before_the_answer() {
    let done = Arc::new(AtomicBool::new(false));
    let d = done.clone();
    assert!(
        super::super::behind_the_answer(super::super::NUDGE_WAIT, move || d
            .store(true, Ordering::SeqCst))
        .await
    );
    assert!(done.load(Ordering::SeqCst));
    assert_eq!(super::super::NUDGE_WAIT, Duration::from_secs(2));
}