Skip to content

Commit

Permalink
Close chains after tests finish (#2782)
Browse files Browse the repository at this point in the history
* Remove `--from` requirement for `close-chain` cmd.

Change the CLI to always expect the chain ID without the flag name.

* Cache the command binary path in `ClientWrapper`

Avoid resolving the binary path every time the command needs to be
executed, and prepare to have the path available for a synchronous call
inside a `Drop` implementation.

* Add a `linera wallet show --short` option

Print a simple formatted list of chains in the wallet.

* Refactor to add a `command_arguments` helper

Prepare to be able to get all the common arguments in the synchronous
`Drop` implementation.

* Create an `OnClientDrop` helper enumeration

An option to configure if chains should be closed when the
`ClientWrapper` is dropped.

* Add a `ClientWrapper::close_chain_on_drop` config.

Allow configuring if all chains should be closed when the client
finishes.

* Close chains when `ClientWrapper` is dropped

List all chains in the wallet and close them one by one.

* Drop the clients in separate threads

It's not possible to know which client will have the round to be able to
close the shared chain, so both clients must run in parallel (not in
separate asynchronous tasks, because the `Drop` is blocking) so that the
one that has the round can actually close it and unblock the other
client.

* Refactor to create a helper function in the test

Avoid duplicating the code and improve readability.
  • Loading branch information
jvff authored and ma2bd committed Nov 12, 2024
1 parent 1fb6b00 commit 139fb69
Show file tree
Hide file tree
Showing 11 changed files with 272 additions and 50 deletions.
14 changes: 9 additions & 5 deletions CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,11 @@ Close an existing chain.

A closed chain cannot execute operations or accept messages anymore. It can still reject incoming messages, so they bounce back to the sender.

**Usage:** `linera close-chain --from <CHAIN_ID>`
**Usage:** `linera close-chain <CHAIN_ID>`

###### **Options:**
###### **Arguments:**

* `--from <CHAIN_ID>` — Chain ID (must be one of our chains)
* `<CHAIN_ID>` — Chain ID (must be one of our chains)



Expand Down Expand Up @@ -704,11 +704,15 @@ Show the contents of the wallet

Show the contents of the wallet

**Usage:** `linera wallet show [CHAIN_ID]`
**Usage:** `linera wallet show [OPTIONS] [CHAIN_ID]`

###### **Arguments:**

* `<CHAIN_ID>`
* `<CHAIN_ID>` — The chain to show the metadata

###### **Options:**

* `--short` — Only print a non-formatted list of the wallet's chain IDs



Expand Down
9 changes: 7 additions & 2 deletions linera-client/src/client_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,6 @@ pub enum ClientCommand {
/// It can still reject incoming messages, so they bounce back to the sender.
CloseChain {
/// Chain ID (must be one of our chains)
#[arg(long = "from")]
chain_id: ChainId,
},

Expand Down Expand Up @@ -959,7 +958,13 @@ impl fmt::Display for ResourceControlPolicyConfig {
#[derive(Clone, clap::Subcommand)]
pub enum WalletCommand {
/// Show the contents of the wallet.
Show { chain_id: Option<ChainId> },
Show {
/// The chain to show the metadata.
chain_id: Option<ChainId>,
/// Only print a non-formatted list of the wallet's chain IDs.
#[arg(long)]
short: bool,
},

/// Change the wallet default chain.
SetDefault { chain_id: ChainId },
Expand Down
18 changes: 15 additions & 3 deletions linera-service/src/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use linera_base::{
use linera_sdk::abis::fungible::{self, FungibleTokenAbi, InitialState, Parameters};
use linera_service::cli_wrappers::{
local_net::{PathProvider, ProcessInbox},
ApplicationWrapper, ClientWrapper, Faucet, FaucetOption, Network,
ApplicationWrapper, ClientWrapper, Faucet, FaucetOption, Network, OnClientDrop,
};
use port_selector::random_free_tcp_port;
use rand::{Rng as _, SeedableRng};
Expand Down Expand Up @@ -79,14 +79,26 @@ async fn benchmark_with_fungible(
) -> Result<()> {
info!("Creating the clients and initializing the wallets");
let path_provider = PathProvider::create_temporary_directory().unwrap();
let publisher = ClientWrapper::new(path_provider, Network::Grpc, None, num_wallets);
let publisher = ClientWrapper::new(
path_provider,
Network::Grpc,
None,
num_wallets,
OnClientDrop::CloseChains,
);
publisher
.wallet_init(&[], FaucetOption::NewChain(&faucet))
.await?;
let clients = (0..num_wallets)
.map(|n| {
let path_provider = PathProvider::create_temporary_directory().unwrap();
Ok(ClientWrapper::new(path_provider, Network::Grpc, None, n))
Ok(ClientWrapper::new(
path_provider,
Network::Grpc,
None,
n,
OnClientDrop::CloseChains,
))
})
.collect::<Result<Vec<_>, anyhow::Error>>()?;
try_join_all(
Expand Down
3 changes: 2 additions & 1 deletion linera-service/src/cli_wrappers/local_kubernetes_net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::cli_wrappers::{
kubectl::KubectlInstance,
local_net::PathProvider,
util::get_github_root,
ClientWrapper, LineraNet, LineraNetConfig, Network,
ClientWrapper, LineraNet, LineraNetConfig, Network, OnClientDrop,
};

#[cfg(with_testing)]
Expand Down Expand Up @@ -257,6 +257,7 @@ impl LineraNet for LocalKubernetesNet {
self.network,
self.testing_prng_seed,
self.next_client_id,
OnClientDrop::LeakChains,
);
if let Some(seed) = self.testing_prng_seed {
self.testing_prng_seed = Some(seed + 1);
Expand Down
5 changes: 4 additions & 1 deletion linera-service/src/cli_wrappers/local_net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ use tonic_health::pb::{
use tracing::{info, warn};

use crate::{
cli_wrappers::{ClientWrapper, LineraNet, LineraNetConfig, Network, NetworkConfig},
cli_wrappers::{
ClientWrapper, LineraNet, LineraNetConfig, Network, NetworkConfig, OnClientDrop,
},
util::ChildExt,
};

Expand Down Expand Up @@ -317,6 +319,7 @@ impl LineraNet for LocalNet {
self.network.external,
self.testing_prng_seed,
self.next_client_id,
OnClientDrop::LeakChains,
);
if let Some(seed) = self.testing_prng_seed {
self.testing_prng_seed = Some(seed + 1);
Expand Down
4 changes: 3 additions & 1 deletion linera-service/src/cli_wrappers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ mod wallet;
use anyhow::Result;
use async_trait::async_trait;
use linera_execution::ResourceControlPolicy;
pub use wallet::{ApplicationWrapper, ClientWrapper, Faucet, FaucetOption, NodeService};
pub use wallet::{
ApplicationWrapper, ClientWrapper, Faucet, FaucetOption, NodeService, OnClientDrop,
};

/// The information needed to start a Linera net of a particular kind.
#[async_trait]
Expand Down
3 changes: 2 additions & 1 deletion linera-service/src/cli_wrappers/remote_net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use tempfile::{tempdir, TempDir};

use super::{
local_net::PathProvider, ClientWrapper, Faucet, FaucetOption, LineraNet, LineraNetConfig,
Network,
Network, OnClientDrop,
};

pub struct RemoteNetTestingConfig {
Expand Down Expand Up @@ -97,6 +97,7 @@ impl LineraNet for RemoteNet {
self.network,
self.testing_prng_seed,
self.next_client_id,
OnClientDrop::CloseChains,
);
if let Some(seed) = self.testing_prng_seed {
self.testing_prng_seed = Some(seed + 1);
Expand Down
Loading

0 comments on commit 139fb69

Please sign in to comment.