Skip to content

Commit e878f75

Browse files
committed
Make test_esplora_syncs more robust
The test generally works and the parts in question *should* never fail. However, they did, so we give the test server instances some leeway.
1 parent 9f10203 commit e878f75

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

lightning-transaction-sync/tests/integration_tests.rs

+24-9
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ fn get_bitcoind() -> &'static BitcoinD {
3232
);
3333
let mut conf = bitcoind::Conf::default();
3434
conf.network = "regtest";
35-
BitcoinD::with_conf(bitcoind_exe, &conf).unwrap()
35+
let bitcoind = BitcoinD::with_conf(bitcoind_exe, &conf).unwrap();
36+
std::thread::sleep(Duration::from_secs(1));
37+
bitcoind
3638
})
3739
}
3840

@@ -46,29 +48,42 @@ fn get_electrsd() -> &'static ElectrsD {
4648
let mut conf = electrsd::Conf::default();
4749
conf.http_enabled = true;
4850
conf.network = "regtest";
49-
ElectrsD::with_conf(electrs_exe, &bitcoind, &conf).unwrap()
51+
let electrsd = ElectrsD::with_conf(electrs_exe, &bitcoind, &conf).unwrap();
52+
std::thread::sleep(Duration::from_secs(1));
53+
electrsd
5054
})
5155
}
5256

5357
fn generate_blocks_and_wait(num: usize) {
5458
let miner_lock = MINER_LOCK.get_or_init(|| Mutex::new(()));
5559
let _miner = miner_lock.lock().unwrap();
56-
let cur_height = get_bitcoind().client.get_block_count().unwrap();
57-
let address = get_bitcoind().client.get_new_address(Some("test"), Some(AddressType::Legacy)).unwrap();
58-
let _block_hashes = get_bitcoind().client.generate_to_address(num as u64, &address).unwrap();
60+
let cur_height = get_bitcoind().client.get_block_count().expect("failed to get current block height");
61+
let address = get_bitcoind().client.get_new_address(Some("test"), Some(AddressType::Legacy)).expect("failed to get new address");
62+
// TODO: expect this Result once the WouldBlock issue is resolved upstream.
63+
let _block_hashes_res = get_bitcoind().client.generate_to_address(num as u64, &address);
5964
wait_for_block(cur_height as usize + num);
6065
}
6166

6267
fn wait_for_block(min_height: usize) {
63-
let mut header = get_electrsd().client.block_headers_subscribe().unwrap();
68+
let mut header = match get_electrsd().client.block_headers_subscribe() {
69+
Ok(header) => header,
70+
Err(_) => {
71+
// While subscribing should succeed the first time around, we ran into some cases where
72+
// it didn't. Since we can't proceed without subscribing, we try again after a delay
73+
// and panic if it still fails.
74+
std::thread::sleep(Duration::from_secs(1));
75+
get_electrsd().client.block_headers_subscribe().expect("failed to subscribe to block headers")
76+
}
77+
};
78+
6479
loop {
6580
if header.height >= min_height {
6681
break;
6782
}
6883
header = exponential_backoff_poll(|| {
69-
get_electrsd().trigger().unwrap();
70-
get_electrsd().client.ping().unwrap();
71-
get_electrsd().client.block_headers_pop().unwrap()
84+
get_electrsd().trigger().expect("failed to trigger electrsd");
85+
get_electrsd().client.ping().expect("failed to ping electrsd");
86+
get_electrsd().client.block_headers_pop().expect("failed to pop block header")
7287
});
7388
}
7489
}

0 commit comments

Comments
 (0)