Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduced VersionFetcher trait to decouple EventListener from http i… #227

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 69 additions & 43 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions listener/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ futures-util = { workspace = true }
[dev-dependencies]
casper-event-types = { path = "../types", version = "1.0.0", features = ["sse-data-testing"]}
eventsource-stream = "0.2.3"
mockito = "1.2.0"
portpicker = "0.1.1"
warp = { version = "0.3.6"}
56 changes: 55 additions & 1 deletion listener/src/connection_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,19 @@ fn count_error(reason: &str) {

#[cfg(test)]
pub mod tests {
use super::ConnectionManager;
use crate::{
connection_manager::{ConnectionManagerError, DefaultConnectionManager, FIRST_EVENT_EMPTY},
sse_connector::{tests::MockSseConnection, StreamConnector},
SseEvent,
};
use anyhow::Error;
use casper_event_types::{sse_data::test_support::*, Filter};
use tokio::sync::mpsc::{channel, Receiver};
use std::time::Duration;
use tokio::{
sync::mpsc::{channel, Receiver, Sender},
time::sleep,
};
use url::Url;

#[tokio::test]
Expand Down Expand Up @@ -483,4 +489,52 @@ pub mod tests {
};
(manager, data_rx, event_id_rx)
}

pub struct MockConnectionManager {
sender: Sender<String>,
finish_after: Duration,
to_return: Option<Result<(), ConnectionManagerError>>,
msg: Option<String>,
}

impl MockConnectionManager {
pub fn new(
finish_after: Duration,
to_return: Result<(), ConnectionManagerError>,
sender: Sender<String>,
msg: Option<String>,
) -> Self {
Self {
sender,
finish_after,
to_return: Some(to_return),
msg,
}
}
pub fn fail_fast(sender: Sender<String>) -> Self {
let error = Error::msg("xyz");
let a = Err(ConnectionManagerError::NonRecoverableError { error });
Self::new(Duration::from_millis(1), a, sender, None)
}

pub fn ok_long(sender: Sender<String>, msg: Option<&str>) -> Self {
Self::new(
Duration::from_secs(10),
Ok(()),
sender,
msg.map(|s| s.to_string()),
)
}
}

#[async_trait::async_trait]
impl ConnectionManager for MockConnectionManager {
async fn start_handling(&mut self) -> Result<(), ConnectionManagerError> {
if let Some(msg) = &self.msg {
self.sender.send(msg.clone()).await.unwrap();
}
sleep(self.finish_after).await;
self.to_return.take().unwrap() //Unwraping on purpose - this method should only be called once.
}
}
}
Loading
Loading