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 +._* diff --git a/core/tests/create_snapshot.rs b/core/tests/integration/create_snapshot.rs similarity index 62% rename from core/tests/create_snapshot.rs rename to core/tests/integration/create_snapshot.rs index 03534b5f128..65dea35dacf 100644 --- a/core/tests/create_snapshot.rs +++ b/core/tests/integration/create_snapshot.rs @@ -33,21 +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); - } - } - }); - // Wait some time to ensure the node is warmed up. - std::thread::sleep(Duration::from_secs(90)); + crate::start_dev_node(port); // Run the command with tokio let temp_dir = tempfile::Builder::new() @@ -56,7 +42,48 @@ 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, uri: &str, timeout: u64) -> Result { + use std::time::Duration; + + use node_primitives::Header; + use sp_rpc::{list::ListOrValue, number::NumberOrHex}; + use substrate_rpc_client::{ws_client, ChainApi}; + + let start = std::time::Instant::now(); + + let rpc = loop { + match ws_client(uri).await { + Ok(rpc) => break rpc, + Err(err) => { + if start.elapsed() > Duration::from_secs(timeout) { + return Err(format!("Connection timed out: {}", err)); + } + } + } + tokio::time::sleep(Duration::from_secs(8)).await; + }; + + 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(String::from("Mining block timed out")); + } + } + } + tokio::time::sleep(Duration::from_secs(8)).await; + } + } + 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 +96,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/integration/execute_block.rs similarity index 85% rename from core/tests/execute_block.rs rename to core/tests/integration/execute_block.rs index 93d9b85143d..23b7ca48d2f 100644 --- a/core/tests/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)); @@ -75,7 +64,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 diff --git a/core/tests/follow_chain.rs b/core/tests/integration/follow_chain.rs similarity index 84% rename from core/tests/follow_chain.rs rename to core/tests/integration/follow_chain.rs index 677c5a1e75d..b33d57d4ec5 100644 --- a/core/tests/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 new file mode 100644 index 00000000000..0fc8698401b --- /dev/null +++ b/core/tests/integration/main.rs @@ -0,0 +1,41 @@ +// 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; + +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); + } + } + }); + }); +} 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