diff --git a/event_sidecar/src/event_stream_server/sse_server.rs b/event_sidecar/src/event_stream_server/sse_server.rs index 75030803..dade84b3 100644 --- a/event_sidecar/src/event_stream_server/sse_server.rs +++ b/event_sidecar/src/event_stream_server/sse_server.rs @@ -15,6 +15,10 @@ use futures::{future, Stream, StreamExt}; use http::StatusCode; use hyper::Body; #[cfg(test)] +use once_cell::sync::Lazy; +#[cfg(test)] +use regex::Regex; +#[cfg(test)] use serde::Serialize; #[cfg(test)] use serde_json::Value; @@ -90,6 +94,12 @@ const SIGNATURES_FILTER: [EventFilter; 2] = [EventFilter::ApiVersion, EventFilter::FinalitySignature]; /// The filter associated with `/events/sidecar` path. const SIDECAR_FILTER: [EventFilter; 1] = [EventFilter::SidecarVersion]; + +#[cfg(test)] +static ENDS_WITH_ID_REGEX: Lazy = Lazy::new(|| Regex::new(r"\nid:\d*$").unwrap()); +#[cfg(test)] +static STARTS_WITH_REGEX: Lazy = Lazy::new(|| Regex::new(r"^data:").unwrap()); + /// The "id" field of the events sent on the event stream to clients. pub type Id = u32; pub type IsLegacyFilter = bool; @@ -684,7 +694,6 @@ mod tests { use super::*; use casper_types::{testing::TestRng, TransactionHash}; use rand::Rng; - use regex::Regex; use std::iter; #[cfg(feature = "additional-metrics")] use tokio::sync::mpsc::channel; @@ -946,9 +955,6 @@ mod tests { )) .collect(); - let ends_with_id = Regex::new(r"\nid:\d*$").unwrap(); - let starts_with_data = Regex::new(r"^data:").unwrap(); - // Run three cases; where only a single event is duplicated, where five are duplicated, and // where the whole initial stream (except the `ApiVersion`) is duplicated. for duplicate_count in &[1, 5, NUM_INITIAL_EVENTS] { @@ -1019,12 +1025,12 @@ mod tests { if let Some(id) = deduplicated_event.id { assert!(received_event_str.ends_with(format!("\nid:{}", id).as_str())); } else { - assert!(!ends_with_id.is_match(received_event_str.as_str())); + assert!(!ENDS_WITH_ID_REGEX.is_match(received_event_str.as_str())); }; - received_event_str = ends_with_id + received_event_str = ENDS_WITH_ID_REGEX .replace_all(received_event_str.as_str(), "") .into_owned(); - received_event_str = starts_with_data + received_event_str = STARTS_WITH_REGEX .replace_all(received_event_str.as_str(), "") .into_owned(); if is_legacy_endpoint { diff --git a/rust-toolchain.toml b/rust-toolchain.toml index 6d4bd803..f02e0fc6 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -2,4 +2,4 @@ channel = "1.84.0" components = [ "rustfmt", "clippy" ] targets = [ "wasm32-unknown-unknown" ] -profile = "minimal" \ No newline at end of file +profile = "minimal"