TestValidator now implements Drop, no need to close() it

This commit is contained in:
Michael Vines
2020-12-10 17:28:52 -08:00
committed by mergify[bot]
parent 0a9ff1dc9d
commit bbad3fe501
15 changed files with 38 additions and 78 deletions

View File

@ -5,6 +5,7 @@ use {
rpc::JsonRpcConfig,
validator::{Validator, ValidatorConfig},
},
solana_client::rpc_client::RpcClient,
solana_ledger::{blockstore::create_new_ledger, create_new_tmp_ledger},
solana_runtime::{
bank_forks::{CompressionType, SnapshotConfig, SnapshotVersion},
@ -12,6 +13,8 @@ use {
hardened_unpack::MAX_GENESIS_ARCHIVE_UNPACKED_SIZE,
},
solana_sdk::{
clock::DEFAULT_MS_PER_SLOT,
commitment_config::CommitmentConfig,
fee_calculator::FeeRateGovernor,
native_token::sol_to_lamports,
pubkey::Pubkey,
@ -23,6 +26,8 @@ use {
net::SocketAddr,
path::{Path, PathBuf},
sync::Arc,
thread::sleep,
time::Duration,
},
};
@ -34,17 +39,19 @@ pub struct TestValidatorGenesisConfig {
#[derive(Default)]
pub struct TestValidatorStartConfig {
pub preserve_ledger: bool,
pub rpc_config: JsonRpcConfig,
pub rpc_ports: Option<(u16, u16)>, // (JsonRpc, JsonRpcPubSub), None == random ports
}
pub struct TestValidator {
ledger_path: PathBuf,
preserve_ledger: bool,
rpc_pubsub_url: String,
rpc_url: String,
tpu: SocketAddr,
gossip: SocketAddr,
validator: Validator,
validator: Option<Validator>,
vote_account_address: Pubkey,
}
@ -223,7 +230,7 @@ impl TestValidator {
..ValidatorConfig::default()
};
let validator = Validator::new(
let validator = Some(Validator::new(
node,
&Arc::new(validator_identity_keypair),
&ledger_path,
@ -231,7 +238,7 @@ impl TestValidator {
vec![Arc::new(validator_vote_account)],
None,
&validator_config,
);
));
// Needed to avoid panics in `solana-responder-gossip` in tests that create a number of
// test validators concurrently...
@ -239,6 +246,7 @@ impl TestValidator {
Ok(TestValidator {
ledger_path: ledger_path.to_path_buf(),
preserve_ledger: false,
rpc_pubsub_url,
rpc_url,
gossip,
@ -248,12 +256,6 @@ impl TestValidator {
})
}
/// Stop the test validator and delete its ledger directory
pub fn close(self) {
self.validator.close().unwrap();
remove_dir_all(&self.ledger_path).unwrap();
}
/// Return the test validator's TPU address
pub fn tpu(&self) -> &SocketAddr {
&self.tpu
@ -279,3 +281,20 @@ impl TestValidator {
self.vote_account_address
}
}
impl Drop for TestValidator {
fn drop(&mut self) {
if let Some(validator) = self.validator.take() {
validator.close();
}
if !self.preserve_ledger {
remove_dir_all(&self.ledger_path).unwrap_or_else(|err| {
panic!(
"Failed to remove ledger directory {}: {}",
self.ledger_path.display(),
err
)
});
}
}
}

View File

@ -65,7 +65,7 @@ use std::{
sync::atomic::{AtomicBool, Ordering},
sync::mpsc::Receiver,
sync::{mpsc::channel, Arc, Mutex, RwLock},
thread::{sleep, Result},
thread::sleep,
time::Duration,
};
@ -635,9 +635,9 @@ impl Validator {
}
}
pub fn close(mut self) -> Result<()> {
pub fn close(mut self) {
self.exit();
self.join()
self.join();
}
fn print_node_info(node: &Node) {
@ -665,7 +665,7 @@ impl Validator {
);
}
pub fn join(self) -> Result<()> {
pub fn join(self) {
self.poh_service.join().expect("poh_service");
drop(self.poh_recorder);
if let Some(RpcServices {
@ -718,8 +718,6 @@ impl Validator {
.join()
.expect("completed_data_sets_service");
self.ip_echo_server.shutdown_now();
Ok(())
}
}
@ -1267,7 +1265,7 @@ mod tests {
Some(&leader_node.info),
&config,
);
validator.close().unwrap();
validator.close();
remove_dir_all(validator_ledger_path).unwrap();
}
@ -1345,7 +1343,7 @@ mod tests {
// While join is called sequentially, the above exit call notified all the
// validators to exit from all their threads
validators.into_iter().for_each(|validator| {
validator.join().unwrap();
validator.join();
});
for path in ledger_paths {