From 9ecb93baf43e2d61caea0b4356da76901ca993d4 Mon Sep 17 00:00:00 2001 From: Marcel Moura Date: Tue, 26 Sep 2023 15:08:38 -0300 Subject: [PATCH] fixup! feat(offchain): implement the broker-listener for the authority-claimer --- offchain/authority-claimer/src/lib.rs | 2 +- offchain/authority-claimer/src/listener.rs | 18 +++--- offchain/authority-claimer/src/mock.rs | 75 ---------------------- 3 files changed, 10 insertions(+), 85 deletions(-) delete mode 100644 offchain/authority-claimer/src/mock.rs diff --git a/offchain/authority-claimer/src/lib.rs b/offchain/authority-claimer/src/lib.rs index 4e06d4f85..fc62bec53 100644 --- a/offchain/authority-claimer/src/lib.rs +++ b/offchain/authority-claimer/src/lib.rs @@ -9,7 +9,7 @@ pub mod metrics; pub mod sender; #[cfg(test)] -mod mock; +mod broker_mock; use config::Config; use rollups_events::DAppMetadata; diff --git a/offchain/authority-claimer/src/listener.rs b/offchain/authority-claimer/src/listener.rs index d3576471a..883e22624 100644 --- a/offchain/authority-claimer/src/listener.rs +++ b/offchain/authority-claimer/src/listener.rs @@ -76,12 +76,12 @@ mod tests { use test_fixtures::BrokerFixture; use crate::{ + broker_mock, listener::{BrokerListener, DefaultBrokerListener}, - mock, }; async fn setup(docker: &Cli) -> (BrokerFixture, DefaultBrokerListener) { - mock::setup_broker(docker, false).await.unwrap() + broker_mock::setup_broker(docker, false).await.unwrap() } #[tokio::test] @@ -93,7 +93,7 @@ mod tests { #[tokio::test] async fn instantiate_new_broker_listener_error() { let docker = Cli::default(); - let result = mock::setup_broker(&docker, true).await; + let result = broker_mock::setup_broker(&docker, true).await; assert!(result.is_err(), "setup_broker didn't fail as it should"); let error = result.err().unwrap().to_string(); assert_eq!(error, "error connecting to Redis"); @@ -104,8 +104,8 @@ mod tests { let docker = Cli::default(); let (fixture, mut broker_listener) = setup(&docker).await; let n = 5; - mock::produce_rollups_claims(&fixture, n, 0).await; - mock::produce_last_claim(&fixture, n).await; + broker_mock::produce_rollups_claims(&fixture, n, 0).await; + broker_mock::produce_last_claim(&fixture, n).await; let result = broker_listener.listen().await; assert!(result.is_ok()); } @@ -114,7 +114,7 @@ mod tests { async fn start_broker_listener_with_claims_enqueued() { let docker = Cli::default(); let (fixture, mut broker_listener) = setup(&docker).await; - mock::produce_last_claim(&fixture, 0).await; + broker_mock::produce_last_claim(&fixture, 0).await; let claim = broker_listener.listen().await; assert!(claim.is_ok()); } @@ -136,17 +136,17 @@ mod tests { let x = 2; println!("Creating {} claims.", x); - mock::produce_rollups_claims(&fixture, x, 0).await; + broker_mock::produce_rollups_claims(&fixture, x, 0).await; println!("Going to sleep for 2 seconds."); tokio::time::sleep(Duration::from_secs(2)).await; let y = 5; println!("Creating {} claims.", y); - mock::produce_rollups_claims(&fixture, y, x).await; + broker_mock::produce_rollups_claims(&fixture, y, x).await; assert_eq!(x + y, n); - mock::produce_last_claim(&fixture, n).await; + broker_mock::produce_last_claim(&fixture, n).await; broker_listener_thread.await.unwrap(); } diff --git a/offchain/authority-claimer/src/mock.rs b/offchain/authority-claimer/src/mock.rs deleted file mode 100644 index 42da4450b..000000000 --- a/offchain/authority-claimer/src/mock.rs +++ /dev/null @@ -1,75 +0,0 @@ -// (c) Cartesi and individual authors (see AUTHORS) -// SPDX-License-Identifier: Apache-2.0 (see LICENSE) - -use std::time::Duration; - -use backoff::ExponentialBackoffBuilder; -use rollups_events::{ - BrokerConfig, BrokerEndpoint, BrokerError, DAppMetadata, RedactedUrl, - RollupsClaim, Url, -}; -use snafu::Snafu; -use test_fixtures::BrokerFixture; -use testcontainers::clients::Cli; - -use crate::listener::DefaultBrokerListener; - -#[derive(Clone, Debug, Snafu)] -pub enum MockError { - EndError, - InternalError, - MockError, -} - -pub async fn setup_broker( - docker: &Cli, - should_fail: bool, -) -> Result<(BrokerFixture, DefaultBrokerListener), BrokerError> { - let fixture = BrokerFixture::setup(docker).await; - - let redis_endpoint = if should_fail { - BrokerEndpoint::Single(RedactedUrl::new( - Url::parse("https://invalid.com").unwrap(), - )) - } else { - fixture.redis_endpoint().clone() - }; - - let config = BrokerConfig { - redis_endpoint, - consume_timeout: 300000, - backoff: ExponentialBackoffBuilder::new() - .with_initial_interval(Duration::from_millis(1000)) - .with_max_elapsed_time(Some(Duration::from_millis(3000))) - .build(), - }; - let metadata = DAppMetadata { - chain_id: fixture.chain_id(), - dapp_address: fixture.dapp_address().clone(), - }; - let broker = DefaultBrokerListener::new(config, metadata).await?; - Ok((fixture, broker)) -} - -pub async fn produce_rollups_claims( - fixture: &BrokerFixture<'_>, - n: usize, - epoch_index_start: usize, -) -> Vec { - let mut rollups_claims = Vec::new(); - for i in 0..n { - let mut rollups_claim = RollupsClaim::default(); - rollups_claim.epoch_index = (i + epoch_index_start) as u64; - fixture.produce_rollups_claim(rollups_claim.clone()).await; - rollups_claims.push(rollups_claim); - } - rollups_claims -} - -/// The last claim should trigger an `EndError` error. -pub async fn produce_last_claim( - fixture: &BrokerFixture<'_>, - epoch_index: usize, -) -> Vec { - produce_rollups_claims(fixture, 1, epoch_index).await -}