-
Notifications
You must be signed in to change notification settings - Fork 259
test: ensure temporary directories are cleaned up after tests #5163
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| use crate::Node; | ||
| use crate::utils::{find_available_port, message_name, temp_path, wait_until}; | ||
| use crate::utils::{TempPathBuf, find_available_port, message_name, wait_until}; | ||
| use ckb_app_config::NetworkConfig; | ||
| use ckb_async_runtime::{Runtime, new_global_runtime}; | ||
| use ckb_chain_spec::consensus::Consensus; | ||
|
|
@@ -12,20 +12,22 @@ use ckb_network::{ | |
| }; | ||
| use ckb_util::Mutex; | ||
| use std::collections::HashMap; | ||
| use std::path::PathBuf; | ||
| use std::sync::Arc; | ||
| use std::time::Duration; | ||
|
|
||
| pub type NetMessage = (PeerIndex, ProtocolId, Bytes); | ||
|
|
||
| pub struct Net { | ||
| p2p_port: u16, | ||
| working_dir: PathBuf, | ||
| protocols: Vec<SupportProtocols>, | ||
| controller: NetworkController, | ||
| register_rx: Receiver<(String, PeerIndex, Receiver<NetMessage>)>, | ||
| receivers: HashMap<String, (PeerIndex, Receiver<NetMessage>)>, | ||
| // _async_runtime MUST be declared before _working_dir | ||
| // to ensure the runtime stops and releases file handles | ||
| // before the TempDir attempts to clean up. | ||
|
Comment on lines
+26
to
+28
Collaborator
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. Maybe we can combine |
||
| _async_runtime: Runtime, | ||
| _working_dir: TempPathBuf, | ||
| } | ||
|
|
||
| impl Net { | ||
|
|
@@ -35,13 +37,13 @@ impl Net { | |
| "Net cannot initialize with empty protocols" | ||
| ); | ||
| let p2p_port = find_available_port(); | ||
| let working_dir = temp_path(spec_name, "net"); | ||
| let working_dir = TempPathBuf::new(spec_name, "net"); | ||
|
|
||
| let p2p_listen = format!("/ip4/127.0.0.1/tcp/{p2p_port}").parse().unwrap(); | ||
| let network_state = Arc::new( | ||
| NetworkState::from_config(NetworkConfig { | ||
| listen_addresses: vec![p2p_listen], | ||
| path: (&working_dir).into(), | ||
| path: working_dir.to_path_buf(), | ||
| max_peers: 128, | ||
| max_outbound_peers: 128, | ||
| discovery_local_address: true, | ||
|
|
@@ -79,19 +81,15 @@ impl Net { | |
| .unwrap(); | ||
| Self { | ||
| p2p_port, | ||
| working_dir, | ||
| protocols, | ||
| controller, | ||
| register_rx, | ||
| receivers: Default::default(), | ||
| _async_runtime: async_runtime, | ||
| _working_dir: working_dir, | ||
| } | ||
| } | ||
|
|
||
| pub fn working_dir(&self) -> &PathBuf { | ||
| &self.working_dir | ||
| } | ||
|
|
||
| pub fn p2p_listen(&self) -> String { | ||
| format!("/ip4/127.0.0.1/tcp/{}", self.p2p_port) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| use crate::utils::TempPathBuf; | ||
| use crate::{Spec, utils::nodes_panicked}; | ||
| use ckb_channel::{Receiver, Sender, unbounded}; | ||
| use ckb_logger::{error, info}; | ||
|
|
@@ -24,18 +25,20 @@ pub enum Notify { | |
| Done { | ||
| spec_name: String, | ||
| seconds: u64, | ||
| node_paths: Vec<PathBuf>, | ||
| node_paths: Vec<TempPathBuf>, | ||
| }, | ||
| Error { | ||
| spec_error: Box<dyn Any + Send>, | ||
| spec_name: String, | ||
| seconds: u64, | ||
| node_log_paths: Vec<PathBuf>, | ||
| node_paths: Vec<TempPathBuf>, | ||
| }, | ||
| Panick { | ||
| spec_name: String, | ||
| seconds: u64, | ||
| node_log_paths: Vec<PathBuf>, | ||
| node_paths: Vec<TempPathBuf>, | ||
| }, | ||
| Stop, | ||
| } | ||
|
|
@@ -163,9 +166,10 @@ impl Worker { | |
| .unwrap(); | ||
|
|
||
| let mut nodes = spec.before_run(); | ||
| // Used to extend the lifecycle of TempPathBuf | ||
| let node_paths = nodes | ||
| .iter() | ||
| .map(|node| node.working_dir()) | ||
| .map(|node| node.owned_working_dir()) | ||
|
Comment on lines
+169
to
+172
Contributor
Author
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. Here is the reason for the changes to the |
||
| .collect::<Vec<_>>(); | ||
| let node_log_paths = nodes.iter().map(|node| node.log_path()).collect::<Vec<_>>(); | ||
| let result = panic::catch_unwind(panic::AssertUnwindSafe(|| { | ||
|
|
@@ -184,6 +188,7 @@ impl Worker { | |
| spec_name: spec.name().to_string(), | ||
| seconds: now.elapsed().as_secs(), | ||
| node_log_paths, | ||
| node_paths, | ||
| }) | ||
| .unwrap(); | ||
| } else if let Some(spec_error) = spec_error { | ||
|
|
@@ -193,6 +198,7 @@ impl Worker { | |
| spec_name: spec.name().to_string(), | ||
| seconds: now.elapsed().as_secs(), | ||
| node_log_paths, | ||
| node_paths, | ||
| }) | ||
| .unwrap(); | ||
| } else { | ||
|
|
||
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.
Calling
ckb_stop_handler::broadcast_exit_signals()inRpcTestSuite'sDropwill cancel the global stop token for the entire test process and cannot be reset. Since many RPC tests create and drop their ownRpcTestSuite, this risks prematurely stopping services in other tests (including tests that run after the first one), leading to flakiness or failures. Prefer an explicit, suite-local shutdown mechanism (e.g., stop/join the services started insetup_rpc_test_suite) or ensure the broadcast is performed once at the very end of the whole test binary rather than on every suite drop.