Skip to content
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

refactor(tests): Hide bitcoind stopper #261

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
make stopper non-optional in tests
In some tests the carrier didn't need a stopper since it was already in the test scope so it would drop at the end of the test and do usual cleanup.
This commit unifies stuff and still moves the stopper inside the carrier so that all the tests look alike (not having ones with Some(stopper) and ones with None).
mariocynicys committed Jun 29, 2024
commit ff8a3adec20dd2765737cf4d3c9a09f05d330081
95 changes: 80 additions & 15 deletions teos/src/carrier.rs
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ pub struct Carrier {
block_height: u32,
#[cfg(test)]
/// A stopper that stops the mock bitcoind server in tests when the [`Carrier`] is dropped.
_stopper: Option<crate::test_utils::BitcoindStopper>,
_stopper: crate::test_utils::BitcoindStopper,
}

impl Carrier {
@@ -35,7 +35,7 @@ impl Carrier {
bitcoin_cli: Arc<BitcoindClient>,
bitcoind_reachable: Arc<(Mutex<bool>, Condvar)>,
last_known_block_height: u32,
#[cfg(test)] stopper: Option<crate::test_utils::BitcoindStopper>,
#[cfg(test)] stopper: crate::test_utils::BitcoindStopper,
) -> Self {
Carrier {
bitcoin_cli,
@@ -218,7 +218,12 @@ mod tests {
let bitcoin_cli = Arc::new(BitcoindClient::new(bitcoind_mock.url(), Auth::None).unwrap());
let start_height = START_HEIGHT as u32;

let mut carrier = Carrier::new(bitcoin_cli, bitcoind_reachable, start_height, None);
let mut carrier = Carrier::new(
bitcoin_cli,
bitcoind_reachable,
start_height,
bitcoind_mock.stopper,
);

// Lets add some dummy data into the cache
for i in 0..10 {
@@ -242,7 +247,12 @@ mod tests {
let start_height = START_HEIGHT as u32;
start_server(bitcoind_mock.server);

let mut carrier = Carrier::new(bitcoin_cli, bitcoind_reachable, start_height, None);
let mut carrier = Carrier::new(
bitcoin_cli,
bitcoind_reachable,
start_height,
bitcoind_mock.stopper,
);
let tx = consensus::deserialize(&Vec::from_hex(TX_HEX).unwrap()).unwrap();
let r = carrier.send_transaction(&tx);

@@ -260,7 +270,12 @@ mod tests {
let start_height = START_HEIGHT as u32;
start_server(bitcoind_mock.server);

let mut carrier = Carrier::new(bitcoin_cli, bitcoind_reachable, start_height, None);
let mut carrier = Carrier::new(
bitcoin_cli,
bitcoind_reachable,
start_height,
bitcoind_mock.stopper,
);
let tx = consensus::deserialize(&Vec::from_hex(TX_HEX).unwrap()).unwrap();
let r = carrier.send_transaction(&tx);

@@ -280,7 +295,12 @@ mod tests {
let start_height = START_HEIGHT as u32;
start_server(bitcoind_mock.server);

let mut carrier = Carrier::new(bitcoin_cli, bitcoind_reachable, start_height, None);
let mut carrier = Carrier::new(
bitcoin_cli,
bitcoind_reachable,
start_height,
bitcoind_mock.stopper,
);
let tx = consensus::deserialize(&Vec::from_hex(TX_HEX).unwrap()).unwrap();
let r = carrier.send_transaction(&tx);

@@ -302,7 +322,12 @@ mod tests {
let start_height = START_HEIGHT as u32;
start_server(bitcoind_mock.server);

let mut carrier = Carrier::new(bitcoin_cli, bitcoind_reachable, start_height, None);
let mut carrier = Carrier::new(
bitcoin_cli,
bitcoind_reachable,
start_height,
bitcoind_mock.stopper,
);
let tx = consensus::deserialize(&Vec::from_hex(TX_HEX).unwrap()).unwrap();
let r = carrier.send_transaction(&tx);

@@ -325,7 +350,12 @@ mod tests {
let start_height = START_HEIGHT as u32;
start_server(bitcoind_mock.server);

let mut carrier = Carrier::new(bitcoin_cli, bitcoind_reachable, start_height, None);
let mut carrier = Carrier::new(
bitcoin_cli,
bitcoind_reachable,
start_height,
bitcoind_mock.stopper,
);
let tx = consensus::deserialize(&Vec::from_hex(TX_HEX).unwrap()).unwrap();
let r = carrier.send_transaction(&tx);

@@ -344,7 +374,12 @@ mod tests {
let start_height = START_HEIGHT as u32;
start_server(bitcoind_mock.server);

let mut carrier = Carrier::new(bitcoin_cli, bitcoind_reachable, start_height, None);
let mut carrier = Carrier::new(
bitcoin_cli,
bitcoind_reachable,
start_height,
bitcoind_mock.stopper,
);
let tx = consensus::deserialize(&Vec::from_hex(TX_HEX).unwrap()).unwrap();
let r = carrier.send_transaction(&tx);

@@ -364,7 +399,12 @@ mod tests {
let bitcoind_reachable = Arc::new((Mutex::new(false), Condvar::new()));
let bitcoin_cli = Arc::new(BitcoindClient::new(bitcoind_mock.url(), Auth::None).unwrap());
let start_height = START_HEIGHT as u32;
let mut carrier = Carrier::new(bitcoin_cli, bitcoind_reachable.clone(), start_height, None);
let mut carrier = Carrier::new(
bitcoin_cli,
bitcoind_reachable.clone(),
start_height,
bitcoind_mock.stopper,
);

let tx = consensus::deserialize(&Vec::from_hex(TX_HEX).unwrap()).unwrap();
let delay = std::time::Duration::new(3, 0);
@@ -394,7 +434,12 @@ mod tests {
let start_height = START_HEIGHT as u32;
start_server(bitcoind_mock.server);

let carrier = Carrier::new(bitcoin_cli, bitcoind_reachable, start_height, None);
let carrier = Carrier::new(
bitcoin_cli,
bitcoind_reachable,
start_height,
bitcoind_mock.stopper,
);
let txid = Txid::from_hex(TXID_HEX).unwrap();
assert!(carrier.in_mempool(&txid));
}
@@ -407,7 +452,12 @@ mod tests {
let start_height = START_HEIGHT as u32;
start_server(bitcoind_mock.server);

let carrier = Carrier::new(bitcoin_cli, bitcoind_reachable, start_height, None);
let carrier = Carrier::new(
bitcoin_cli,
bitcoind_reachable,
start_height,
bitcoind_mock.stopper,
);
let txid = Txid::from_hex(TXID_HEX).unwrap();
assert!(!carrier.in_mempool(&txid));
}
@@ -422,7 +472,12 @@ mod tests {
let start_height = START_HEIGHT as u32;
start_server(bitcoind_mock.server);

let carrier = Carrier::new(bitcoin_cli, bitcoind_reachable, start_height, None);
let carrier = Carrier::new(
bitcoin_cli,
bitcoind_reachable,
start_height,
bitcoind_mock.stopper,
);
let txid = Txid::from_hex(TXID_HEX).unwrap();
assert!(!carrier.in_mempool(&txid));
}
@@ -436,7 +491,12 @@ mod tests {
let start_height = START_HEIGHT as u32;
start_server(bitcoind_mock.server);

let carrier = Carrier::new(bitcoin_cli, bitcoind_reachable, start_height, None);
let carrier = Carrier::new(
bitcoin_cli,
bitcoind_reachable,
start_height,
bitcoind_mock.stopper,
);
let txid = Txid::from_hex(TXID_HEX).unwrap();
assert!(!carrier.in_mempool(&txid));
}
@@ -448,7 +508,12 @@ mod tests {
let bitcoind_reachable = Arc::new((Mutex::new(false), Condvar::new()));
let bitcoin_cli = Arc::new(BitcoindClient::new(bitcoind_mock.url(), Auth::None).unwrap());
let start_height = START_HEIGHT as u32;
let carrier = Carrier::new(bitcoin_cli, bitcoind_reachable.clone(), start_height, None);
let carrier = Carrier::new(
bitcoin_cli,
bitcoind_reachable.clone(),
start_height,
bitcoind_mock.stopper,
);

let txid = Txid::from_hex(TXID_HEX).unwrap();
let delay = std::time::Duration::new(3, 0);
4 changes: 2 additions & 2 deletions teos/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -388,7 +388,7 @@ pub(crate) fn create_carrier(query: MockedServerQuery, height: u32) -> Carrier {
bitcoin_cli,
bitcoind_reachable,
height,
Some(bitcoind_mock.stopper),
bitcoind_mock.stopper,
)
}

@@ -531,7 +531,7 @@ impl Debug for BitcoindStopper {
pub(crate) struct BitcoindMock {
pub url: String,
pub server: Server,
stopper: BitcoindStopper,
pub stopper: BitcoindStopper,
}

#[derive(Default)]