rmv-core 1.0.0

Configuration, retries, stop resolution and connection status for the RMV API
Documentation
//! What a repetition logs. A test binary of its own, so that its subscriber
//! is the only one.

use std::sync::{Arc, Mutex};
use std::time::Duration;

use hafas_sim::{ACCESS_ID, DEPARTURE_JOURNEY, Failure, Injection, Sim};
use rmv_core::{Config, Rmv, Settings};

#[derive(Clone, Default)]
struct Buffer(Arc<Mutex<Vec<u8>>>);

impl std::io::Write for Buffer {
    fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
        self.0.lock().unwrap().extend_from_slice(bytes);
        Ok(bytes.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

#[tokio::test(flavor = "current_thread")]
async fn logs_each_repetition_without_the_key() {
    let buffer = Buffer::default();
    let writer = buffer.clone();
    let subscriber = tracing_subscriber::fmt()
        .with_max_level(tracing::Level::TRACE)
        .with_ansi(false)
        .with_writer(move || writer.clone())
        .finish();
    tracing::subscriber::set_global_default(subscriber).unwrap();

    let sim = Sim::start().await;
    sim.inject(Injection::Fail(Failure::status(503).times(2)));
    let settings = Settings {
        access_id: Some(ACCESS_ID.to_owned()),
        base_url: Some(sim.base_url()),
        ..Settings::default()
    };
    let rmv = Rmv::new(&Config::new(settings, &|_| None), "rmv-test/0")
        .unwrap()
        .with_retry_delay(Duration::from_millis(1));
    rmv.journey(DEPARTURE_JOURNEY).await.unwrap();

    let log = String::from_utf8(buffer.0.lock().unwrap().clone()).unwrap();
    assert_eq!(log.matches("repeating the request").count(), 2, "{log}");
    assert!(!log.contains(ACCESS_ID), "{log}");
}