From ba46adbe089329c78cd69ccdb08e27ed67bd77cf Mon Sep 17 00:00:00 2001 From: alexgparity Date: Mon, 21 Nov 2022 17:12:40 +0100 Subject: [PATCH] Deduplicate test code --- Cargo.lock | 1 + client/consensus/aura/src/lib.rs | 26 +---------------- client/consensus/slots/src/lib.rs | 24 +--------------- primitives/inherents/Cargo.toml | 1 + primitives/inherents/src/lib.rs | 23 +--------------- test-utils/runtime/src/lib.rs | 46 +++++++++++++++++-------------- 6 files changed, 30 insertions(+), 91 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1ae0331fb85c0..bf09a5e37404d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9646,6 +9646,7 @@ dependencies = [ "sp-core", "sp-runtime", "sp-std", + "substrate-test-runtime-client", "thiserror", ] diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index 50cfad7c2004b..b8b8f485b01b3 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -656,34 +656,10 @@ mod tests { time::{Duration, Instant}, }; use substrate_test_runtime_client::{ - runtime::{Header, H256}, + runtime::{Header, TestInherentDataProvider, H256}, TestClient, }; - #[derive(Clone)] - struct TestInherentDataProvider; - - const ERROR_TO_STRING: &str = "Found error!"; - const TEST_INHERENT_0: sp_inherents::InherentIdentifier = *b"testinh0"; - - #[async_trait::async_trait] - impl sp_inherents::InherentDataProvider for TestInherentDataProvider { - async fn provide_inherent_data( - &self, - data: &mut sp_inherents::InherentData, - ) -> Result<(), sp_inherents::Error> { - data.put_data(TEST_INHERENT_0, &42) - } - - async fn try_handle_error( - &self, - _: &sp_inherents::InherentIdentifier, - _: &[u8], - ) -> Option> { - Some(Err(sp_inherents::Error::Application(Box::from(ERROR_TO_STRING)))) - } - } - const SLOT_DURATION_MS: u64 = 1000; type Error = sp_blockchain::Error; diff --git a/client/consensus/slots/src/lib.rs b/client/consensus/slots/src/lib.rs index 8e9189cb4e682..506b982188d47 100644 --- a/client/consensus/slots/src/lib.rs +++ b/client/consensus/slots/src/lib.rs @@ -810,34 +810,12 @@ impl BackoffAuthoringBlocksStrategy for () { #[cfg(test)] mod test { use super::*; - use sp_inherents::{Error, InherentData, InherentDataProvider, InherentIdentifier}; use sp_runtime::traits::NumberFor; use std::time::{Duration, Instant}; - use substrate_test_runtime_client::runtime::{Block, Header}; + use substrate_test_runtime_client::runtime::{Block, Header, TestInherentDataProvider}; const SLOT_DURATION: Duration = Duration::from_millis(6000); - #[derive(Clone)] - struct TestInherentDataProvider; - - const ERROR_TO_STRING: &str = "Found error!"; - const TEST_INHERENT_0: InherentIdentifier = *b"testinh0"; - - #[async_trait::async_trait] - impl InherentDataProvider for TestInherentDataProvider { - async fn provide_inherent_data(&self, data: &mut InherentData) -> Result<(), Error> { - data.put_data(TEST_INHERENT_0, &42) - } - - async fn try_handle_error( - &self, - _: &InherentIdentifier, - _: &[u8], - ) -> Option> { - Some(Err(Error::Application(Box::from(ERROR_TO_STRING)))) - } - } - fn slot(slot: u64) -> super::slots::SlotInfo { super::slots::SlotInfo { slot: slot.into(), diff --git a/primitives/inherents/Cargo.toml b/primitives/inherents/Cargo.toml index 8f6d8aef155ac..59a5e2061dd37 100644 --- a/primitives/inherents/Cargo.toml +++ b/primitives/inherents/Cargo.toml @@ -24,6 +24,7 @@ sp-std = { version = "5.0.0", default-features = false, path = "../std" } [dev-dependencies] futures = "0.3.21" +substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" } [features] default = [ "std" ] diff --git a/primitives/inherents/src/lib.rs b/primitives/inherents/src/lib.rs index 59db5ef3e8802..ee113b3ef6357 100644 --- a/primitives/inherents/src/lib.rs +++ b/primitives/inherents/src/lib.rs @@ -436,30 +436,9 @@ mod tests { assert!(data.put_data(TEST_INHERENT_0, &10).is_err()); } - #[derive(Clone)] - struct TestInherentDataProvider; - - const ERROR_TO_STRING: &str = "Found error!"; - - #[async_trait::async_trait] - impl InherentDataProvider for TestInherentDataProvider { - async fn provide_inherent_data(&self, data: &mut InherentData) -> Result<(), Error> { - data.put_data(TEST_INHERENT_0, &42) - } - - async fn try_handle_error( - &self, - _: &InherentIdentifier, - _: &[u8], - ) -> Option> { - Some(Err(Error::Application(Box::from(ERROR_TO_STRING)))) - } - } - #[test] fn create_inherent_data() { - let provider = TestInherentDataProvider; - + let provider = substrate_test_runtime_client::runtime::TestInherentDataProvider; let inherent_data = futures::executor::block_on(provider.create_inherent_data()).unwrap(); assert_eq!(inherent_data.get_data::(&TEST_INHERENT_0).unwrap().unwrap(), 42u32); diff --git a/test-utils/runtime/src/lib.rs b/test-utils/runtime/src/lib.rs index 21820e100ba18..69de1ded6bac5 100644 --- a/test-utils/runtime/src/lib.rs +++ b/test-utils/runtime/src/lib.rs @@ -45,7 +45,7 @@ use frame_support::{ use frame_system::limits::{BlockLength, BlockWeights}; use sp_api::{decl_runtime_apis, impl_runtime_apis}; pub use sp_core::hash::H256; -use sp_inherents::{CheckInherentsResult, InherentData}; +use sp_inherents::{CheckInherentsResult, InherentData, InherentIdentifier}; #[cfg(feature = "std")] use sp_runtime::traits::NumberFor; use sp_runtime::{ @@ -1402,23 +1402,27 @@ mod tests { } } -//#[derive(Clone)] -//struct TestInherentDataProvider; -// -//const ERROR_TO_STRING: &str = "Found error!"; -//const TEST_INHERENT_0: InherentIdentifier = *b"testinh0"; -// -//#[async_trait::async_trait] -//impl sp_inherents::InherentDataProvider for TestInherentDataProvider { -// async fn provide_inherent_data(&self, data: &mut InherentData) -> Result<(), Error> { -// data.put_data(TEST_INHERENT_0, &42) -// } -// -// async fn try_handle_error( -// &self, -// _: &InherentIdentifier, -// _: &[u8], -// ) -> Option> { -// Some(Err(Error::Application(Box::from(ERROR_TO_STRING)))) -// } -//} +#[derive(Clone)] +pub struct TestInherentDataProvider; + +const ERROR_TO_STRING: &str = "Found error!"; +const TEST_INHERENT_0: InherentIdentifier = *b"testinh0"; + +#[cfg(feature = "std")] +#[async_trait::async_trait] +impl sp_inherents::InherentDataProvider for TestInherentDataProvider { + async fn provide_inherent_data( + &self, + data: &mut InherentData, + ) -> Result<(), sp_inherents::Error> { + data.put_data(TEST_INHERENT_0, &42) + } + + async fn try_handle_error( + &self, + _: &InherentIdentifier, + _: &[u8], + ) -> Option> { + Some(Err(sp_inherents::Error::Application(Box::from(ERROR_TO_STRING)))) + } +}