mln-rs-engine 0.0.22

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
// The network of the online file source's and the loader's tests: an online
// file source over the simulated network (`crate::storage::simulated_network`), whose
// transfers a `Server` answers on the test's thread with real status codes
// and headers, and whose clock moves only when the test moves it.

use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;

use mln_style::time::Instant;
use parking_lot::Mutex;

use crate::exec::queue::Queue;
use crate::storage::file_source::{FileSource, FileSourceExt};
use crate::storage::network_status::NetworkStatus;
use crate::storage::online_file_source::OnlineFileSource;
use crate::storage::request::Request;
use crate::storage::resource::Resource;
use crate::storage::response::Response;
use crate::storage::simulated_network::{Clock, OnlineDriver, Transfer, Transport, online_file_source};

/// How a [`Server`] answers a transfer.
pub(crate) enum Reply {
    /// An HTTP response: its status, its headers in order and its body.
    Http(u16, Vec<(String, String)>, Vec<u8>),
    /// No response: the connection failed.
    Refused,
    /// Not yet: the transfer waits for the test in [`Server::held`].
    Hold,
}

impl Reply {
    /// A 200 with `body` and no headers.
    pub(crate) fn ok(body: &str) -> Reply {
        Reply::Http(200, Vec::new(), body.as_bytes().to_vec())
    }

    /// A response of `status` with no headers and no body.
    pub(crate) fn status(status: u16) -> Reply {
        Reply::Http(status, Vec::new(), Vec::new())
    }

    /// The response with the header `name: value` after its others.
    pub(crate) fn header(mut self, name: &str, value: impl Into<String>) -> Reply {
        if let Reply::Http(_, headers, _) = &mut self {
            headers.push((name.to_owned(), value.into()));
        }
        self
    }

    fn answer(self, transfer: Transfer) -> Option<Transfer> {
        match self {
            Reply::Http(status, headers, body) => transfer.respond(status, headers, body),
            Reply::Refused => transfer.fail("Connection refused"),
            Reply::Hold => return Some(transfer),
        }
        None
    }
}

type Route = Box<dyn Fn(&Resource) -> Reply + Send + Sync>;

/// Answers each transfer as its route says, at once, and keeps the
/// resource of each it was asked for.
pub(crate) struct Server {
    route: Route,
    held: Mutex<Vec<Transfer>>,
    requested: Mutex<Vec<Resource>>,
}

impl Server {
    /// Takes the transfers that wait for an answer, the first started first.
    pub(crate) fn held(&self) -> Vec<Transfer> {
        std::mem::take(&mut self.held.lock())
    }

    /// Takes the URLs of the transfers started since the last call, in the
    /// order they started.
    pub(crate) fn requested(&self) -> Vec<String> {
        self.requested.lock().drain(..).map(|resource| resource.url).collect()
    }

    /// Takes the resources of the transfers started since the last call.
    pub(crate) fn requested_resources(&self) -> Vec<Resource> {
        std::mem::take(&mut self.requested.lock())
    }
}

impl Transport for Server {
    fn start(&self, transfer: Transfer) {
        self.requested.lock().push(transfer.resource().clone());
        if let Some(held) = (self.route)(transfer.resource()).answer(transfer) {
            self.held.lock().push(held);
        }
    }
}

/// The time on a [`Network`]'s clock, which starts at the system's time
/// when the network is made and moves only with [`advance`](Self::advance).
#[derive(Clone)]
pub(crate) struct TestClock {
    base: Instant,
    elapsed: Arc<AtomicU64>,
}

impl TestClock {
    /// Moves the clock on by `by`. The network runs the deadlines that
    /// passed in its next [`run`](Network::run).
    pub(crate) fn advance(&self, by: Duration) {
        self.elapsed.fetch_add(u64::try_from(by.as_nanos()).unwrap(), Ordering::SeqCst);
    }

    fn now(&self) -> Instant {
        self.base + Duration::from_nanos(self.elapsed.load(Ordering::SeqCst))
    }
}

/// The answers to a request, in the order they came.
pub(crate) type Answers = Arc<Mutex<Vec<Response>>>;

/// An online file source over a [`Server`], following `status`, whose core
/// runs only in [`run`](Self::run) and whose deadlines go by `clock`.
pub(crate) struct Network {
    pub(crate) source: Arc<OnlineFileSource>,
    pub(crate) status: Arc<NetworkStatus>,
    pub(crate) server: Arc<Server>,
    pub(crate) clock: TestClock,
    driver: OnlineDriver,
    /// Woken by the source and by answers, for [`run_until`](Self::run_until)
    /// to park on.
    turns: Queue<()>,
}

impl Network {
    /// A network whose server answers by `route`.
    pub(crate) fn serving(route: impl Fn(&Resource) -> Reply + Send + Sync + 'static) -> Network {
        let server = Arc::new(Server { route: Box::new(route), held: Mutex::default(), requested: Mutex::default() });
        let clock = TestClock { base: crate::tests::start_time(), elapsed: Arc::default() };
        let time = clock.clone();
        let time: Clock = Arc::new(move || time.now());
        let status = Arc::new(NetworkStatus::new());
        let (source, driver) = online_file_source(&status, Arc::<Server>::clone(&server), time);
        Network { source: Arc::new(source), status, server, clock, driver, turns: Queue::new() }
    }

    /// A network whose server holds every transfer for the test to answer.
    pub(crate) fn holding() -> Network {
        Network::serving(|_| Reply::Hold)
    }

    /// Runs what the source has to do now, until nothing is left: its
    /// requests, the server's answers, and the deadlines that passed.
    pub(crate) fn run(&mut self) {
        self.driver.run();
    }

    /// Moves the clock on by `by`, then [`run`](Self::run)s.
    pub(crate) fn advance(&mut self, by: Duration) {
        self.clock.advance(by);
        self.run();
    }

    /// Requests `resource` from the network's source, whose answers the
    /// returned list collects until the returned request drops.
    pub(crate) fn request(&self, resource: &Resource) -> (Request, Answers) {
        self.request_from(&*self.source, resource)
    }

    /// Requests `resource` from `source`, which loads over this network, and
    /// whose answers the returned list collects, from any thread, until the
    /// returned request drops.
    pub(crate) fn request_from(&self, source: &(impl FileSource + ?Sized), resource: &Resource) -> (Request, Answers) {
        let answers = Answers::default();
        let (collected, turns) = (Arc::clone(&answers), self.turns.clone());
        let request = source.request(resource, move |response| {
            collected.lock().push(response);
            turns.wake();
        });
        (request, answers)
    }

    /// Runs the network on this thread until `answers` holds `count`.
    pub(crate) fn run_until(&mut self, answers: &Answers, count: usize) {
        let waker = self.turns.clone();
        self.driver.set_waker(Some(Arc::new(move || waker.wake())));
        loop {
            self.driver.run();
            if answers.lock().len() >= count {
                break;
            }
            self.turns.park();
        }
        self.driver.set_waker(None);
    }

    /// The first `count` answers to `resource` from `source`, which loads
    /// over this network.
    pub(crate) fn answers(
        &mut self,
        source: &(impl FileSource + ?Sized),
        resource: &Resource,
        count: usize,
    ) -> Vec<Response> {
        let (request, answers) = self.request_from(source, resource);
        self.run_until(&answers, count);
        drop(request);
        answers.lock().drain(..count).collect()
    }
}