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

Commit f654ffe

Browse files
authored
metrics: tests: Fix flaky runtime_can_publish_metrics (#7279)
* metrics: tests: Fix flaky runtime_can_publish_metrics When an re-org happens wait_for_blocks(2) would actually exit after the second import of blocks 1, so the conditions for the metric to exist won't be met hence the occasional test failure. More details in: #7267 Signed-off-by: Alexandru Gheorghe <alexandru.gheorghe@parity.io> * metrics: tests: Cleanup un-needed box pin Signed-off-by: Alexandru Gheorghe <alexandru.gheorghe@parity.io> --------- Signed-off-by: Alexandru Gheorghe <alexandru.gheorghe@parity.io>
1 parent 8024a80 commit f654ffe

2 files changed

Lines changed: 20 additions & 5 deletions

File tree

node/metrics/src/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ async fn runtime_can_publish_metrics() {
6161
// Start validator Bob.
6262
let _bob = run_validator_node(bob_config, None);
6363

64-
// Wait for Alice to author two blocks.
65-
alice.wait_for_blocks(2).await;
64+
// Wait for Alice to see two finalized blocks.
65+
alice.wait_for_finalized_blocks(2).await;
6666

6767
let metrics_uri = format!("http://localhost:{}/metrics", DEFAULT_PROMETHEUS_PORT);
6868
let metrics = scrape_prometheus_metrics(&metrics_uri).await;

node/test/service/src/lib.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
pub mod chain_spec;
2222

2323
pub use chain_spec::*;
24-
use futures::future::Future;
24+
use futures::{future::Future, stream::StreamExt};
2525
use polkadot_node_primitives::{CollationGenerationConfig, CollatorFn};
2626
use polkadot_node_subsystem::messages::{CollationGenerationMessage, CollatorProtocolMessage};
2727
use polkadot_overseer::Handle;
@@ -35,8 +35,9 @@ use polkadot_test_runtime::{
3535
ParasCall, ParasSudoWrapperCall, Runtime, SignedExtra, SignedPayload, SudoCall,
3636
UncheckedExtrinsic, VERSION,
3737
};
38+
3839
use sc_chain_spec::ChainSpec;
39-
use sc_client_api::execution_extensions::ExecutionStrategies;
40+
use sc_client_api::{execution_extensions::ExecutionStrategies, BlockchainEvents};
4041
use sc_network::{
4142
config::{NetworkConfiguration, TransportConfig},
4243
multiaddr, NetworkStateInfo,
@@ -54,14 +55,14 @@ use sp_keyring::Sr25519Keyring;
5455
use sp_runtime::{codec::Encode, generic, traits::IdentifyAccount, MultiSigner};
5556
use sp_state_machine::BasicExternalities;
5657
use std::{
58+
collections::HashSet,
5759
net::{Ipv4Addr, SocketAddr},
5860
path::PathBuf,
5961
sync::Arc,
6062
};
6163
use substrate_test_client::{
6264
BlockchainEventsExt, RpcHandlersExt, RpcTransactionError, RpcTransactionOutput,
6365
};
64-
6566
/// Declare an instance of the native executor named `PolkadotTestExecutorDispatch`. Include the wasm binary as the
6667
/// equivalent wasm code.
6768
pub struct PolkadotTestExecutorDispatch;
@@ -335,6 +336,20 @@ impl PolkadotTestNode {
335336
self.client.wait_for_blocks(count)
336337
}
337338

339+
/// Wait for `count` blocks to be finalized and then exit. Similarly with `wait_for_blocks` this function will
340+
/// not return if no block are ever finalized.
341+
pub async fn wait_for_finalized_blocks(&self, count: usize) {
342+
let mut import_notification_stream = self.client.finality_notification_stream();
343+
let mut blocks = HashSet::new();
344+
345+
while let Some(notification) = import_notification_stream.next().await {
346+
blocks.insert(notification.hash);
347+
if blocks.len() == count {
348+
break
349+
}
350+
}
351+
}
352+
338353
/// Register the collator functionality in the overseer of this node.
339354
pub async fn register_collator(
340355
&mut self,

0 commit comments

Comments
 (0)