Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

metrics: tests: Fix flaky runtime_can_publish_metrics #7279

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions node/metrics/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ async fn runtime_can_publish_metrics() {
// Start validator Bob.
let _bob = run_validator_node(bob_config, None);

// Wait for Alice to author two blocks.
alice.wait_for_blocks(2).await;
// Wait for Alice to see two finalized blocks.
alice.wait_for_finalized_blocks(2).await;

let metrics_uri = format!("http://localhost:{}/metrics", DEFAULT_PROMETHEUS_PORT);
let metrics = scrape_prometheus_metrics(&metrics_uri).await;
Expand Down
24 changes: 21 additions & 3 deletions node/test/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
pub mod chain_spec;

pub use chain_spec::*;
use futures::future::Future;
use futures::{future::Future, stream::StreamExt};
use polkadot_node_primitives::{CollationGenerationConfig, CollatorFn};
use polkadot_node_subsystem::messages::{CollationGenerationMessage, CollatorProtocolMessage};
use polkadot_overseer::Handle;
Expand All @@ -35,8 +35,9 @@ use polkadot_test_runtime::{
ParasCall, ParasSudoWrapperCall, Runtime, SignedExtra, SignedPayload, SudoCall,
UncheckedExtrinsic, VERSION,
};

use sc_chain_spec::ChainSpec;
use sc_client_api::execution_extensions::ExecutionStrategies;
use sc_client_api::{execution_extensions::ExecutionStrategies, BlockchainEvents};
use sc_network::{
config::{NetworkConfiguration, TransportConfig},
multiaddr, NetworkStateInfo,
Expand All @@ -54,14 +55,14 @@ use sp_keyring::Sr25519Keyring;
use sp_runtime::{codec::Encode, generic, traits::IdentifyAccount, MultiSigner};
use sp_state_machine::BasicExternalities;
use std::{
collections::HashSet,
net::{Ipv4Addr, SocketAddr},
path::PathBuf,
sync::Arc,
};
use substrate_test_client::{
BlockchainEventsExt, RpcHandlersExt, RpcTransactionError, RpcTransactionOutput,
};

/// Declare an instance of the native executor named `PolkadotTestExecutorDispatch`. Include the wasm binary as the
/// equivalent wasm code.
pub struct PolkadotTestExecutorDispatch;
Expand Down Expand Up @@ -335,6 +336,23 @@ impl PolkadotTestNode {
self.client.wait_for_blocks(count)
}

/// Wait for `count` blocks to be finalized and then exit. Similarly with `wait_for_blocks` this function will
/// not return if no block are ever finalized.
pub async fn wait_for_finalized_blocks(&self, count: usize) {
let mut import_notification_stream = self.client.finality_notification_stream();
let mut blocks = HashSet::new();

Box::pin(async move {
while let Some(notification) = import_notification_stream.next().await {
blocks.insert(notification.hash);
if blocks.len() == count {
break
}
}
})
.await;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Box::pin(async move {
while let Some(notification) = import_notification_stream.next().await {
blocks.insert(notification.hash);
if blocks.len() == count {
break
}
}
})
.await;
while let Some(notification) = import_notification_stream.next().await {
blocks.insert(notification.hash);
if blocks.len() == count {
break
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

}

/// Register the collator functionality in the overseer of this node.
pub async fn register_collator(
&mut self,
Expand Down