Verified Commit ef14d93f authored by onyinyang's avatar onyinyang 🔒
Browse files

Add advance time endpoint to distributor test

parent ca30ff27
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -45,3 +45,6 @@ tempdir = "0.3"
[dependencies.chrono]
version = "0.4.38"
features = ["serde"]

[features]
test-branch = []
+33 −3
Original line number Diff line number Diff line
@@ -373,9 +373,9 @@ impl LoxServerContext {
        ba_obj.bridge_update(&bridgeline)
    }

    #[cfg(test)]
    /// For testing only: manually advance the day by the given number
    /// of days.
    #[cfg(feature = "test-branch")]
    // For testing only: manually advance the day by the given number
    // of days.
    pub fn advance_days_test(&self, num: u16) {
        let mut ba_obj = self.ba.lock().unwrap();
        ba_obj.advance_days(num); // FOR TESTING ONLY
@@ -827,6 +827,36 @@ impl LoxServerContext {
            }
        }
    }

    #[cfg(feature = "test-branch")]
    fn today(&self) -> u32 {
        self.ba.lock().unwrap().today()
    }

    #[cfg(feature = "test-branch")]
    fn send_today(self) -> Response<BoxBody<Bytes, Infallible>> {
        let today = self.today();
        prepare_header(serde_json::to_string(&today).unwrap())
    }

    #[cfg(feature = "test-branch")]
    /// For testing only: manually advance the day by the given number
    /// of days and send back the current day.
    pub fn advance_days_with_response_test(
        self,
        request: Bytes,
    ) -> Response<BoxBody<Bytes, Infallible>> {
        let req: u16 = match serde_json::from_slice(&request) {
            Ok(req) => req,
            Err(e) => {
                let response = json!({"error": e.to_string()});
                let val = serde_json::to_string(&response).unwrap();
                return prepare_header(val);
            }
        };
        self.advance_days_test(req);
        self.send_today()
    }
}

fn full<T: Into<Bytes>>(chunk: T) -> BoxBody<Bytes, Infallible> {
+10 −1
Original line number Diff line number Diff line
@@ -25,6 +25,11 @@ where
            .body(full("Allow POST"))
            .unwrap()),
        _ => match (req.method(), req.uri().path()) {
            #[cfg(feature = "test-branch")]
            (&Method::POST, "/advancedays") => Ok::<_, Infallible>({
                let bytes = req.into_body().collect().await.unwrap().to_bytes();
                cloned_context.advance_days_with_response_test(bytes)
            }),
            (&Method::POST, "/invite") => Ok::<_, Infallible>(cloned_context.generate_invite()),
            (&Method::POST, "/reachability") => {
                Ok::<_, Infallible>(cloned_context.send_reachability_cred())
@@ -294,7 +299,11 @@ mod tests {
        }

        fn advance_days(&mut self, days: u16) {
            self.context.advance_days_test(days)
            // For testing only: manually advance the day by the given number
            // of days.
            let mut ba_obj = self.context.ba.lock().unwrap();
            ba_obj.advance_days(days); // FOR TESTING ONLY
            println!("Today's date according to server: {}", ba_obj.today());
        }

        fn rotate_lox_keys(&mut self) {