Skip to content

Commit

Permalink
make stopper non-optional in tests
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
mariocynicys committed Jun 29, 2024
1 parent c42b29c commit ff8a3ad
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 17 deletions.
95 changes: 80 additions & 15 deletions teos/src/carrier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);
Expand Down Expand Up @@ -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));
}
Expand All @@ -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));
}
Expand All @@ -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));
}
Expand All @@ -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));
}
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions teos/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}

Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit ff8a3ad

Please sign in to comment.