From b9330f7a82771c5ba2e88b6ef67b16d2072a71b7 Mon Sep 17 00:00:00 2001 From: Julian Eager Date: Sun, 19 Nov 2023 16:34:22 +0800 Subject: [PATCH 1/6] try connecting instead --- core/tests/create_snapshot.rs | 43 +++++++++++++++++++++++++++++++---- core/tests/execute_block.rs | 2 +- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/core/tests/create_snapshot.rs b/core/tests/create_snapshot.rs index 03534b5f128..9331976c2ec 100644 --- a/core/tests/create_snapshot.rs +++ b/core/tests/create_snapshot.rs @@ -46,8 +46,6 @@ async fn create_snapshot_works() { } } }); - // Wait some time to ensure the node is warmed up. - std::thread::sleep(Duration::from_secs(90)); // Run the command with tokio let temp_dir = tempfile::Builder::new() @@ -56,7 +54,43 @@ async fn create_snapshot_works() { .expect("Failed to create a tempdir"); let snap_file_path = temp_dir.path().join("snapshot.snap"); - common::run_with_timeout(Duration::from_secs(60), async move { + common::run_with_timeout(Duration::from_secs(360), async move { + async fn block_hash(block_number: u64, url: &str, timeout: u64) -> Result { + use substrate_rpc_client::{ws_client, ChainApi}; + use sp_rpc::{list::ListOrValue, number::NumberOrHex}; + use node_primitives::{Header}; + + let start = std::time::Instant::now(); + let mut interval = 27; + + let rpc = loop { + match ws_client(url).await { + Ok(rpc) => break rpc, + Err(err) => { + let elapse = start.elapsed(); + if elapse > Duration::from_secs(timeout) { + panic!("Failed to connect within {:?}: {}", elapse, err); + } + } + } + tokio::time::sleep(Duration::from_secs(interval)).await; + interval = (interval / 3 * 2).max(8); + }; + + let result = ChainApi::<(), Hash, Header, ()>::block_hash( + &rpc, + Some(ListOrValue::Value(NumberOrHex::Number(block_number))), + ) + .await + .map_err(|_| "Couldn't get block hash".to_string())?; + + match result { + ListOrValue::Value(maybe_block_hash) if maybe_block_hash.is_some() => + Ok(maybe_block_hash.unwrap()), + _ => Err("Couldn't get block hash".to_string()), + } + } + fn create_snapshot(ws_url: &str, snap_file: &PathBuf, at: Hash) -> tokio::process::Child { Command::new(cargo_bin("try-runtime")) .stdout(std::process::Stdio::piped()) @@ -69,8 +103,9 @@ async fn create_snapshot_works() { .spawn() .unwrap() } + let block_number = 2; - let block_hash = common::block_hash(block_number, &ws_url).await.unwrap(); + let block_hash = block_hash(block_number, &ws_url, 300).await.unwrap(); // Try to create a snapshot. let child = create_snapshot(&ws_url, &snap_file_path, block_hash); diff --git a/core/tests/execute_block.rs b/core/tests/execute_block.rs index 93d9b85143d..3dba892fedd 100644 --- a/core/tests/execute_block.rs +++ b/core/tests/execute_block.rs @@ -75,7 +75,7 @@ async fn execute_block_works() { // Assert that the block-execution process has executed the expected block. assert!(matched.is_ok()); - // Assert that the block-execution exited succesfully + // Assert that the block-execution exited successfully assert!(block_execution .wait_with_output() .await From e9d393d9c974d9fa2cae371c4acb91ceddaab9c1 Mon Sep 17 00:00:00 2001 From: Julian Eager Date: Thu, 30 Nov 2023 08:37:29 +0800 Subject: [PATCH 2/6] retry block_hash() --- core/tests/create_snapshot.rs | 47 +++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/core/tests/create_snapshot.rs b/core/tests/create_snapshot.rs index 9331976c2ec..59984791599 100644 --- a/core/tests/create_snapshot.rs +++ b/core/tests/create_snapshot.rs @@ -55,39 +55,44 @@ async fn create_snapshot_works() { let snap_file_path = temp_dir.path().join("snapshot.snap"); common::run_with_timeout(Duration::from_secs(360), async move { - async fn block_hash(block_number: u64, url: &str, timeout: u64) -> Result { - use substrate_rpc_client::{ws_client, ChainApi}; + async fn block_hash(block_number: u64, uri: &str, timeout: u64) -> Result { + use std::time::Duration; + + use node_primitives::Header; use sp_rpc::{list::ListOrValue, number::NumberOrHex}; - use node_primitives::{Header}; + use substrate_rpc_client::{ws_client, ChainApi}; let start = std::time::Instant::now(); - let mut interval = 27; let rpc = loop { - match ws_client(url).await { + match ws_client(uri).await { Ok(rpc) => break rpc, Err(err) => { - let elapse = start.elapsed(); - if elapse > Duration::from_secs(timeout) { - panic!("Failed to connect within {:?}: {}", elapse, err); + if start.elapsed() > Duration::from_secs(timeout) { + return Err(format!("Connection timed out: {}", err)); } } } - tokio::time::sleep(Duration::from_secs(interval)).await; - interval = (interval / 3 * 2).max(8); + tokio::time::sleep(Duration::from_secs(8)).await; }; - let result = ChainApi::<(), Hash, Header, ()>::block_hash( - &rpc, - Some(ListOrValue::Value(NumberOrHex::Number(block_number))), - ) - .await - .map_err(|_| "Couldn't get block hash".to_string())?; - - match result { - ListOrValue::Value(maybe_block_hash) if maybe_block_hash.is_some() => - Ok(maybe_block_hash.unwrap()), - _ => Err("Couldn't get block hash".to_string()), + loop { + let result = ChainApi::<(), Hash, Header, ()>::block_hash( + &rpc, + Some(ListOrValue::Value(NumberOrHex::Number(block_number))), + ) + .await; + + match result { + Ok(ListOrValue::Value(Some(block_hash))) => break Ok(block_hash), + Err(err) => break Err(err.to_string()), + _ => { + if start.elapsed() > Duration::from_secs(timeout) { + break Err(format!("Mining block timed out")); + } + } + } + tokio::time::sleep(Duration::from_secs(8)).await; } } From 7e76ed8b926fd12749bba24e2f2705198b6afdb8 Mon Sep 17 00:00:00 2001 From: Julian Eager Date: Thu, 30 Nov 2023 10:52:29 +0800 Subject: [PATCH 3/6] single integration mod --- .../{ => integration}/create_snapshot.rs | 0 core/tests/{ => integration}/execute_block.rs | 0 core/tests/{ => integration}/follow_chain.rs | 0 core/tests/integration/lib.rs | 21 +++++++++++++++++++ .../{ => integration}/on_runtime_upgrade.rs | 0 5 files changed, 21 insertions(+) rename core/tests/{ => integration}/create_snapshot.rs (100%) rename core/tests/{ => integration}/execute_block.rs (100%) rename core/tests/{ => integration}/follow_chain.rs (100%) create mode 100644 core/tests/integration/lib.rs rename core/tests/{ => integration}/on_runtime_upgrade.rs (100%) diff --git a/core/tests/create_snapshot.rs b/core/tests/integration/create_snapshot.rs similarity index 100% rename from core/tests/create_snapshot.rs rename to core/tests/integration/create_snapshot.rs diff --git a/core/tests/execute_block.rs b/core/tests/integration/execute_block.rs similarity index 100% rename from core/tests/execute_block.rs rename to core/tests/integration/execute_block.rs diff --git a/core/tests/follow_chain.rs b/core/tests/integration/follow_chain.rs similarity index 100% rename from core/tests/follow_chain.rs rename to core/tests/integration/follow_chain.rs diff --git a/core/tests/integration/lib.rs b/core/tests/integration/lib.rs new file mode 100644 index 00000000000..d37b14a3128 --- /dev/null +++ b/core/tests/integration/lib.rs @@ -0,0 +1,21 @@ +// This file is part of try-runtime-cli. + +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +mod create_snapshot; +mod execute_block; +mod follow_chain; +mod on_runtime_upgrade; diff --git a/core/tests/on_runtime_upgrade.rs b/core/tests/integration/on_runtime_upgrade.rs similarity index 100% rename from core/tests/on_runtime_upgrade.rs rename to core/tests/integration/on_runtime_upgrade.rs From c8065987ae11e013dfd16ff26e66a68e9b7e1575 Mon Sep 17 00:00:00 2001 From: Julian Eager Date: Thu, 30 Nov 2023 12:10:01 +0800 Subject: [PATCH 4/6] lib.rs -> main.rs --- core/tests/integration/{lib.rs => main.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename core/tests/integration/{lib.rs => main.rs} (100%) diff --git a/core/tests/integration/lib.rs b/core/tests/integration/main.rs similarity index 100% rename from core/tests/integration/lib.rs rename to core/tests/integration/main.rs From 6fd894a8ea337af1b5d89de7a7530db658bb10fe Mon Sep 17 00:00:00 2001 From: Julian Eager Date: Thu, 30 Nov 2023 12:34:01 +0800 Subject: [PATCH 5/6] update .gitignore --- .gitignore | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/.gitignore b/.gitignore index 92e5210724f..085c5fd43e3 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,14 @@ # Substrate *.log *.out + +# Linux +*~ +.fuse_hidden* +.directory +.Trash-* +.nfs* + +# macOS +.DS_Store +._* From fc34c8a16acdad576de0538f96676440cbbf50dc Mon Sep 17 00:00:00 2001 From: Julian Eager Date: Sat, 2 Dec 2023 09:26:50 +0800 Subject: [PATCH 6/6] share a dev node --- core/tests/integration/create_snapshot.rs | 16 ++-------------- core/tests/integration/execute_block.rs | 15 ++------------- core/tests/integration/follow_chain.rs | 15 ++------------- core/tests/integration/main.rs | 20 ++++++++++++++++++++ 4 files changed, 26 insertions(+), 40 deletions(-) diff --git a/core/tests/integration/create_snapshot.rs b/core/tests/integration/create_snapshot.rs index 59984791599..65dea35dacf 100644 --- a/core/tests/integration/create_snapshot.rs +++ b/core/tests/integration/create_snapshot.rs @@ -33,19 +33,7 @@ async fn create_snapshot_works() { let port = 45789; let ws_url = format!("ws://localhost:{}", port); - // Spawn a dev node. - let _ = std::thread::spawn(move || { - match common::start_node_inline(vec![ - "--no-hardware-benchmarks", - "--dev", - format!("--rpc-port={}", port).as_str(), - ]) { - Ok(_) => {} - Err(e) => { - panic!("Node exited with error: {}", e); - } - } - }); + crate::start_dev_node(port); // Run the command with tokio let temp_dir = tempfile::Builder::new() @@ -88,7 +76,7 @@ async fn create_snapshot_works() { Err(err) => break Err(err.to_string()), _ => { if start.elapsed() > Duration::from_secs(timeout) { - break Err(format!("Mining block timed out")); + break Err(String::from("Mining block timed out")); } } } diff --git a/core/tests/integration/execute_block.rs b/core/tests/integration/execute_block.rs index 3dba892fedd..23b7ca48d2f 100644 --- a/core/tests/integration/execute_block.rs +++ b/core/tests/integration/execute_block.rs @@ -30,19 +30,8 @@ async fn execute_block_works() { let port = 45789; let ws_url = format!("ws://localhost:{}", port); - // Spawn a dev node. - let _ = std::thread::spawn(move || { - match common::start_node_inline(vec![ - "--no-hardware-benchmarks", - "--dev", - format!("--rpc-port={}", port).as_str(), - ]) { - Ok(_) => {} - Err(e) => { - panic!("Node exited with error: {}", e); - } - } - }); + crate::start_dev_node(port); + // Wait some time to ensure the node is warmed up. std::thread::sleep(Duration::from_secs(90)); diff --git a/core/tests/integration/follow_chain.rs b/core/tests/integration/follow_chain.rs index 677c5a1e75d..b33d57d4ec5 100644 --- a/core/tests/integration/follow_chain.rs +++ b/core/tests/integration/follow_chain.rs @@ -29,19 +29,8 @@ async fn follow_chain_works() { let port = 45789; let ws_url = format!("ws://localhost:{}", port); - // Spawn a dev node. - let _ = std::thread::spawn(move || { - match common::start_node_inline(vec![ - "--no-hardware-benchmarks", - "--dev", - format!("--rpc-port={}", port).as_str(), - ]) { - Ok(_) => {} - Err(e) => { - panic!("Node exited with error: {}", e); - } - } - }); + crate::start_dev_node(port); + // Wait some time to ensure the node is warmed up. std::thread::sleep(Duration::from_secs(90)); diff --git a/core/tests/integration/main.rs b/core/tests/integration/main.rs index d37b14a3128..0fc8698401b 100644 --- a/core/tests/integration/main.rs +++ b/core/tests/integration/main.rs @@ -19,3 +19,23 @@ mod create_snapshot; mod execute_block; mod follow_chain; mod on_runtime_upgrade; + +use std::sync::OnceLock; + +fn start_dev_node(port: u32) { + static NODE: OnceLock<()> = OnceLock::new(); + NODE.get_or_init(|| { + let _ = std::thread::spawn(move || { + match substrate_cli_test_utils::start_node_inline(vec![ + "--no-hardware-benchmarks", + "--dev", + format!("--rpc-port={}", port).as_str(), + ]) { + Ok(_) => {} + Err(e) => { + panic!("Node exited with error: {}", e); + } + } + }); + }); +}