-
Notifications
You must be signed in to change notification settings - Fork 176
tests(solana-indexer) PR 5.2: Add unit tests for solana-indexer ingester component
#4550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: solana-indexer/PR5.1-bootstrap
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,208 @@ | ||
| use { | ||
| super::{Error, INGEST_TO_DECODER_CAPACITY, Ingester}, | ||
| crate::types::{ | ||
| Signature, | ||
| shared::StreamUpdate, | ||
| wire::{ | ||
| SubscribeUpdate, | ||
| SubscribeUpdateAccount, | ||
| SubscribeUpdateAccountInfo, | ||
| SubscribeUpdateBlock, | ||
| SubscribeUpdateBlockMeta, | ||
| SubscribeUpdateEntry, | ||
| SubscribeUpdatePing, | ||
| SubscribeUpdatePong, | ||
| SubscribeUpdateSlot, | ||
| SubscribeUpdateTransaction, | ||
| SubscribeUpdateTransactionInfo, | ||
| SubscribeUpdateTransactionStatus, | ||
| UpdateOneof, | ||
| }, | ||
| }, | ||
| futures::stream, | ||
| std::sync::{ | ||
| Arc, | ||
| atomic::{AtomicU64, Ordering}, | ||
| }, | ||
| tokio::sync::mpsc::channel, | ||
| yellowstone_grpc_proto::tonic::Status, | ||
| }; | ||
|
|
||
| fn signature(n: u8) -> Signature { | ||
| Signature::from([n; 64]) | ||
| } | ||
|
|
||
| fn signature_bytes(n: u8) -> Vec<u8> { | ||
| signature(n).as_ref().to_vec() | ||
| } | ||
|
|
||
| fn tx_update(slot: u64, sig: u8) -> Result<SubscribeUpdate, Status> { | ||
| Ok(SubscribeUpdate { | ||
| update_oneof: Some(UpdateOneof::Transaction(SubscribeUpdateTransaction { | ||
| slot, | ||
| transaction: Some(SubscribeUpdateTransactionInfo { | ||
| signature: signature_bytes(sig), | ||
| ..Default::default() | ||
| }), | ||
| })), | ||
| ..Default::default() | ||
| }) | ||
| } | ||
|
|
||
| fn account_update(slot: u64, sig: u8) -> Result<SubscribeUpdate, Status> { | ||
| Ok(SubscribeUpdate { | ||
| update_oneof: Some(UpdateOneof::Account(SubscribeUpdateAccount { | ||
| slot, | ||
| account: Some(SubscribeUpdateAccountInfo { | ||
| txn_signature: Some(signature_bytes(sig)), | ||
| ..Default::default() | ||
| }), | ||
| ..Default::default() | ||
| })), | ||
| ..Default::default() | ||
| }) | ||
| } | ||
|
|
||
| fn slot_update(slot: u64) -> Result<SubscribeUpdate, Status> { | ||
| Ok(SubscribeUpdate { | ||
| update_oneof: Some(UpdateOneof::Slot(SubscribeUpdateSlot { | ||
| slot, | ||
| ..Default::default() | ||
| })), | ||
| ..Default::default() | ||
| }) | ||
| } | ||
|
|
||
| fn update_of(update: UpdateOneof) -> Result<SubscribeUpdate, Status> { | ||
| Ok(SubscribeUpdate { | ||
| update_oneof: Some(update), | ||
| ..Default::default() | ||
| }) | ||
| } | ||
|
|
||
| fn ingester( | ||
| stream: impl stream::Stream<Item = Result<SubscribeUpdate, Status>> + Unpin + Send, | ||
| ) -> ( | ||
| Ingester<impl stream::Stream<Item = Result<SubscribeUpdate, Status>> + Unpin + Send>, | ||
| tokio::sync::mpsc::Receiver<StreamUpdate>, | ||
| Arc<AtomicU64>, | ||
| ) { | ||
| let (tx, rx) = channel(INGEST_TO_DECODER_CAPACITY); | ||
| let latest_chain_slot = Arc::new(AtomicU64::new(0)); | ||
| ( | ||
| Ingester::new(stream, tx, latest_chain_slot.clone()), | ||
| rx, | ||
| latest_chain_slot, | ||
| ) | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn transaction_update_with_valid_signature_is_forwarded() { | ||
| let signature = signature(1); | ||
| let (mut ingester, mut rx, _) = ingester(stream::iter(vec![tx_update(42, 1)])); | ||
|
|
||
| assert!(matches!(ingester.run().await, Err(Error::StreamEnded))); | ||
| let update = rx.recv().await.unwrap(); | ||
| assert!(matches!(update, StreamUpdate::Tx { slot: 42, signature: s, .. } if s == signature)); | ||
| assert!(rx.is_empty()); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn account_update_with_body_is_forwarded() { | ||
| let signature = signature(2); | ||
| let (mut ingester, mut rx, _) = ingester(stream::iter(vec![account_update(100, 2)])); | ||
|
|
||
| assert!(matches!(ingester.run().await, Err(Error::StreamEnded))); | ||
| let update = rx.recv().await.unwrap(); | ||
| assert!( | ||
| matches!(update, StreamUpdate::Account { slot: 100, txn_signature: Some(s), .. } if s == signature) | ||
| ); | ||
| assert!(rx.is_empty()); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn slot_update_advances_latest_chain_slot() { | ||
| let (mut ingester, _rx, slot) = ingester(stream::iter(vec![slot_update(9_001)])); | ||
|
|
||
| assert!(matches!(ingester.run().await, Err(Error::StreamEnded))); | ||
| assert_eq!(slot.load(Ordering::Relaxed), 9_001); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn unrelated_and_empty_updates_are_ignored() { | ||
| let (mut ingester, mut rx, slot) = ingester(stream::iter(vec![ | ||
| Ok(SubscribeUpdate::default()), | ||
| update_of(UpdateOneof::Ping(SubscribeUpdatePing::default())), | ||
| update_of(UpdateOneof::Pong(SubscribeUpdatePong::default())), | ||
| update_of(UpdateOneof::TransactionStatus( | ||
| SubscribeUpdateTransactionStatus::default(), | ||
| )), | ||
| update_of(UpdateOneof::Block(SubscribeUpdateBlock::default())), | ||
| update_of(UpdateOneof::BlockMeta(SubscribeUpdateBlockMeta::default())), | ||
| update_of(UpdateOneof::Entry(SubscribeUpdateEntry::default())), | ||
| tx_update(7, 3), | ||
| ])); | ||
|
|
||
| assert!(matches!(ingester.run().await, Err(Error::StreamEnded))); | ||
| let update = rx.recv().await.unwrap(); | ||
| assert!(matches!(update, StreamUpdate::Tx { slot: 7, signature: s, .. } if s == signature(3))); | ||
| assert!(rx.is_empty()); | ||
| assert_eq!(slot.load(Ordering::Relaxed), 0); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn transaction_without_body_or_malformed_signature_is_skipped() { | ||
| let signature = signature(4); | ||
| let (mut ingester, mut rx, _) = ingester(stream::iter(vec![ | ||
| Ok(SubscribeUpdate { | ||
| update_oneof: Some(UpdateOneof::Transaction(SubscribeUpdateTransaction { | ||
| slot: 1, | ||
| transaction: None, | ||
| })), | ||
| ..Default::default() | ||
| }), | ||
| Ok(SubscribeUpdate { | ||
| update_oneof: Some(UpdateOneof::Transaction(SubscribeUpdateTransaction { | ||
| slot: 2, | ||
| transaction: Some(SubscribeUpdateTransactionInfo { | ||
| signature: vec![1, 2, 3], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is (most likely) not a valid signature which is a good indicator that this should use a proper new type to encode things like the size requirements of a signature. |
||
| ..Default::default() | ||
| }), | ||
| })), | ||
| ..Default::default() | ||
| }), | ||
| tx_update(3, 4), | ||
| ])); | ||
|
|
||
| assert!(matches!(ingester.run().await, Err(Error::StreamEnded))); | ||
| let update = rx.recv().await.unwrap(); | ||
| assert!(matches!(update, StreamUpdate::Tx { slot: 3, signature: s, .. } if s == signature)); | ||
| assert!(rx.is_empty()); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn terminal_grpc_status_returns_stream_error() { | ||
| let status = Status::invalid_argument("boom"); | ||
| let (mut ingester, _rx, _) = ingester(stream::iter(vec![Err(status.clone())])); | ||
|
|
||
| let result = ingester.run().await; | ||
| assert!( | ||
| matches!(result, Err(Error::Stream(s)) if s.code() == status.code() && s.message() == status.message()) | ||
| ); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn clean_stream_end_returns_stream_ended() { | ||
| let (mut ingester, _rx, _) = | ||
| ingester(stream::iter(Vec::<Result<SubscribeUpdate, Status>>::new())); | ||
|
|
||
| assert!(matches!(ingester.run().await, Err(Error::StreamEnded))); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn closed_decoder_receiver_stops_cleanly() { | ||
| let (mut ingester, rx, _) = ingester(stream::iter(vec![tx_update(1, 5)])); | ||
| drop(rx); | ||
|
|
||
| assert!(ingester.run().await.is_ok()); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No needed to first create a vector.