Skip to content

Commit 546a7d9

Browse files
committed
Add BlockStreamerTestExtensions trait for tests
1 parent 4583578 commit 546a7d9

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

mithril-common/src/cardano_block_scanner/block_scanner.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ impl BlockScanner for CardanoBlockScanner {
6060

6161
#[cfg(test)]
6262
mod tests {
63+
use crate::cardano_block_scanner::BlockStreamerTestExtensions;
6364
use crate::test_utils::{TempDir, TestLogger};
6465

6566
use super::*;

mithril-common/src/cardano_block_scanner/dumb_block_scanner.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ impl BlockStreamer for DumbBlockStreamer {
6868

6969
#[cfg(test)]
7070
mod tests {
71+
use crate::cardano_block_scanner::BlockStreamerTestExtensions;
72+
7173
use super::*;
7274

7375
#[tokio::test]

mithril-common/src/cardano_block_scanner/immutable_block_streamer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ impl ImmutableBlockStreamer {
124124

125125
#[cfg(test)]
126126
mod tests {
127+
use crate::cardano_block_scanner::BlockStreamerTestExtensions;
127128
use crate::test_utils::{TempDir, TestLogger};
128129

129130
use super::*;

mithril-common/src/cardano_block_scanner/interface.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,32 @@ pub enum ChainScannedBlocks {
7070
pub trait BlockStreamer: Sync + Send {
7171
/// Stream the next available blocks
7272
async fn poll_next(&mut self) -> StdResult<Option<ChainScannedBlocks>>;
73+
}
74+
75+
cfg_test_tools! {
76+
/// Tests extensions methods for the [BlockStreamer] trait.
77+
#[async_trait]
78+
pub trait BlockStreamerTestExtensions{
79+
/// Stream all the available blocks, may be very memory intensive
80+
async fn poll_all(&mut self) -> StdResult<Vec<ScannedBlock>>;
81+
}
7382

74-
/// Stream all the available blocks, may be very memory intensive
75-
async fn poll_all(&mut self) -> StdResult<Vec<ScannedBlock>> {
76-
let mut all_blocks = Vec::new();
77-
while let Some(next_blocks) = self.poll_next().await? {
78-
match next_blocks {
79-
ChainScannedBlocks::RollForwards(mut forward_blocks) => {
80-
all_blocks.append(&mut forward_blocks);
81-
}
82-
ChainScannedBlocks::RollBackward(_) => {
83-
return Err(anyhow!("poll_all: RollBackward not supported"));
84-
}
85-
};
83+
#[async_trait]
84+
impl <S: BlockStreamer + ?Sized> BlockStreamerTestExtensions for S {
85+
async fn poll_all(&mut self) -> StdResult<Vec<ScannedBlock>> {
86+
let mut all_blocks = Vec::new();
87+
while let Some(next_blocks) = self.poll_next().await? {
88+
match next_blocks {
89+
ChainScannedBlocks::RollForwards(mut forward_blocks) => {
90+
all_blocks.append(&mut forward_blocks);
91+
}
92+
ChainScannedBlocks::RollBackward(_) => {
93+
return Err(anyhow!("poll_all: RollBackward not supported"));
94+
}
95+
};
96+
}
97+
Ok(all_blocks)
8698
}
87-
Ok(all_blocks)
8899
}
100+
89101
}

0 commit comments

Comments
 (0)