Skip to content

Commit 5650a7e

Browse files
committed
apply review changes
1 parent b2d133c commit 5650a7e

2 files changed

Lines changed: 12 additions & 14 deletions

File tree

rpc/tests/cli.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ fn test_cli_no_connection() {
101101
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
102102
async fn test_cli_wallet_balance() {
103103
let testenv = TestEnv::new().expect("testEnv could not start");
104-
let listener = testenv::get_bound_port().await.expect("listener");
105-
let port = listener.local_addr().unwrap().port();
104+
let (port, listener) = TestEnv::get_bound_port().await.expect("listener");
106105
spawn_wallet_grpc_service(
107106
listener,
108107
WalletServiceImpl::create_with_rpc_params(testenv.bitcoin_core_rpc_client().unwrap()),
@@ -118,8 +117,7 @@ async fn test_cli_wallet_balance() {
118117
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
119118
async fn test_cli_new_address() {
120119
let testenv = TestEnv::new().expect("testEnv could not start"); // TODO: this doesnt make sense as a CLI make only sense if the bitcoind is
121-
let listener = testenv::get_bound_port().await.expect("listener");
122-
let port = listener.local_addr().unwrap().port();
120+
let (port, listener) = TestEnv::get_bound_port().await.expect("listener");
123121
spawn_wallet_grpc_service(
124122
listener,
125123
WalletServiceImpl::create_with_rpc_params(testenv.bitcoin_core_rpc_client().unwrap()),
@@ -144,8 +142,7 @@ async fn test_cli_list_unspent() {
144142
.some_call(matching!()).returns(vec![mock_utxo()]);
145143
let mock_wallet_service = Unimock::new(clause).no_verify_in_drop();
146144

147-
let listener = testenv::get_bound_port().await.expect("listener");
148-
let port = listener.local_addr().unwrap().port();
145+
let (port, listener) = TestEnv::get_bound_port().await.expect("listener");
149146
spawn_wallet_grpc_service(listener, mock_wallet_service);
150147

151148
task::spawn_blocking(move || assert_cli_with_port(port, ["list-unspent"]))
@@ -163,8 +160,7 @@ async fn test_cli_notify_confidence() {
163160
.answers(&|_, _| mock_confidence_stream());
164161
let mock_wallet_service = Unimock::new(clause).no_verify_in_drop();
165162

166-
let listener = testenv::get_bound_port().await.expect("listener");
167-
let port = listener.local_addr().unwrap().port();
163+
let (port, listener) = TestEnv::get_bound_port().await.expect("listener");
168164
spawn_wallet_grpc_service(listener, mock_wallet_service);
169165

170166
task::spawn_blocking(move || assert_cli_with_port(port, ["notify-confidence",

testenv/src/lib.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ use bdk_wallet::bitcoin::secp256k1::All;
1515
use bdk_wallet::bitcoin::{Address, Amount, BlockHash, Network, Transaction, Txid};
1616
use bmp_tracing::tracing;
1717
use tokio::net::TcpListener;
18-
19-
/// Returns a TcpListener bound to an available port (port 0 lets OS assign).
20-
/// This avoids race conditions by keeping the port bound until used.
21-
pub async fn get_bound_port() -> Result<TcpListener> {
22-
TcpListener::bind("127.0.0.1:0").await.map_err(Into::into)
23-
}
2418
use electrsd::corepc_node::Node;
2519
use electrsd::electrum_client::{Client, ElectrumApi};
2620
use electrsd::{ElectrsD, corepc_node};
@@ -468,6 +462,14 @@ impl TestEnv {
468462
pub fn p2p_socket_addr(&self) -> Option<SocketAddrV4> {
469463
self.bitcoind.params.p2p_socket
470464
}
465+
466+
/// Returns a TcpListener bound to an available port (port 0 lets OS assign).
467+
/// This avoids race conditions by keeping the port bound until used.
468+
pub async fn get_bound_port() -> Result<(u16, TcpListener)> {
469+
let listener = TcpListener::bind("127.0.0.1:0").await?;
470+
let port = listener.local_addr().unwrap().port();
471+
Ok((port, listener))
472+
}
471473
}
472474

473475
impl Drop for TestEnv {

0 commit comments

Comments
 (0)