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 20, 2023
1 parent 41f6585 commit 0a8e5e2
Showing 8 changed files with 680 additions and 223 deletions.
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
@@ -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
@@ -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]
@@ -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

0 comments on commit 0a8e5e2

Please sign in to comment.