|
| 1 | +use std::{collections::VecDeque, future}; |
| 2 | + |
| 3 | +use async_trait::async_trait; |
| 4 | +use tokio::sync::Mutex; |
| 5 | + |
| 6 | +use mithril_common::{ |
| 7 | + entities::{SignedEntityType, SingleSignature}, |
| 8 | + StdResult, |
| 9 | +}; |
| 10 | + |
| 11 | +use super::SignatureConsumer; |
| 12 | + |
| 13 | +type SignatureBatchResult = StdResult<Vec<(SingleSignature, SignedEntityType)>>; |
| 14 | + |
| 15 | +/// A fake implementation of the [SignatureConsumer] trait (test only). |
| 16 | +pub struct FakeSignatureConsumer { |
| 17 | + signature_batches: Mutex<VecDeque<SignatureBatchResult>>, |
| 18 | +} |
| 19 | + |
| 20 | +impl FakeSignatureConsumer { |
| 21 | + /// Creates a new `FakeSignatureConsumer` instance. |
| 22 | + pub fn new(signature_batches: Vec<SignatureBatchResult>) -> Self { |
| 23 | + Self { |
| 24 | + signature_batches: Mutex::new(signature_batches.into()), |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +#[async_trait] |
| 30 | +impl SignatureConsumer for FakeSignatureConsumer { |
| 31 | + async fn get_signatures(&self) -> SignatureBatchResult { |
| 32 | + let mut signature_batches = self.signature_batches.lock().await; |
| 33 | + match signature_batches.pop_front() { |
| 34 | + None => future::pending().await, |
| 35 | + Some(signature_batch) => { |
| 36 | + return signature_batch; |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +#[cfg(test)] |
| 43 | +mod tests { |
| 44 | + use super::*; |
| 45 | + |
| 46 | + use mithril_common::{entities::Epoch, test_utils::fake_data}; |
| 47 | + |
| 48 | + #[tokio::test] |
| 49 | + async fn fake_signature_consumer_returns_signature_batches_in_expected_order() { |
| 50 | + let consumer = FakeSignatureConsumer::new(vec![ |
| 51 | + Ok(vec![( |
| 52 | + fake_data::single_signature(vec![1, 2, 3]), |
| 53 | + SignedEntityType::MithrilStakeDistribution(Epoch(1)), |
| 54 | + )]), |
| 55 | + Ok(vec![( |
| 56 | + fake_data::single_signature(vec![4, 5, 6]), |
| 57 | + SignedEntityType::MithrilStakeDistribution(Epoch(2)), |
| 58 | + )]), |
| 59 | + ]); |
| 60 | + |
| 61 | + let result = consumer.get_signatures().await.unwrap(); |
| 62 | + assert_eq!( |
| 63 | + vec![( |
| 64 | + fake_data::single_signature(vec![1, 2, 3]), |
| 65 | + SignedEntityType::MithrilStakeDistribution(Epoch(1)), |
| 66 | + )], |
| 67 | + result |
| 68 | + ); |
| 69 | + |
| 70 | + let result = consumer.get_signatures().await.unwrap(); |
| 71 | + assert_eq!( |
| 72 | + vec![( |
| 73 | + fake_data::single_signature(vec![4, 5, 6]), |
| 74 | + SignedEntityType::MithrilStakeDistribution(Epoch(2)), |
| 75 | + )], |
| 76 | + result |
| 77 | + ); |
| 78 | + } |
| 79 | +} |
0 commit comments