Skip to content

Commit

Permalink
Introduced VersionFetcher trait to decouple EventListener from http i…
Browse files Browse the repository at this point in the history
…mplementation of version fetching and making unit testing of EventListener possible.
  • Loading branch information
Jakub Zajkowski committed Dec 19, 2023
1 parent 41f6585 commit bcd65f4
Show file tree
Hide file tree
Showing 7 changed files with 661 additions and 218 deletions.
108 changes: 67 additions & 41 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"}
57 changes: 56 additions & 1 deletion listener/src/connection_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,17 @@ pub mod tests {
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;

use super::ConnectionManager;

#[tokio::test]
async fn given_connection_fail_should_return_error() {
let connector = Box::new(MockSseConnection::build_failing_on_connection());
Expand Down Expand Up @@ -483,4 +490,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

0 comments on commit bcd65f4

Please sign in to comment.