Skip to content

Roll a custom TestEnv and drop lazy_static#176

Open
luisschwab wants to merge 3 commits intobitcoindevkit:masterfrom
luisschwab:feat/custom-testenv
Open

Roll a custom TestEnv and drop lazy_static#176
luisschwab wants to merge 3 commits intobitcoindevkit:masterfrom
luisschwab:feat/custom-testenv

Conversation

@luisschwab
Copy link
Member

@luisschwab luisschwab commented Mar 8, 2026

Closes #149

Implement a custom TestEnv, which is instantiated per-test and fixes the un-dropped bitcoind issue. This also allows running tests with more than 1 thread, since they no longer share bitcoind and electrsd instances.

Also adds helpers to fetch externally sourced addresses, fixing the issue where bitcoind's wallet would send change outputs to it's own wallet address, which would mess with test_get_scripthash_stats.

The test recipe and CI job are updated to now use 16 threads.

@luisschwab
Copy link
Member Author

Need to pin some stuff...

@luisschwab luisschwab force-pushed the feat/custom-testenv branch 3 times, most recently from 28bba06 to 4f5ee42 Compare March 8, 2026 16:36
@luisschwab luisschwab added the bug Something isn't working label Mar 8, 2026
Copy link
Member

@notmandatory notmandatory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran this through my AI friend 🤖 and it found two issue and one nit that I think are worth mentioning. Otherwise very good improvement to the testing!

@ValuedMammal
Copy link
Contributor

Concept ACK

Implement a custom `TestEnv`, which is instantiated per-test and
fixes the un-dropped `bitcoind` issue. This also allows running
tests with more than 1 thread, since they no longer share `bitcoind`
and `electrsd` instances.

Also adds helpers to fetch externally sourced addresses, fixing
the issue where `bitcoind`'s wallet would send change outputs to
it's own wallet address, which would mess with `test_get_scripthash_stats`.

The `test` recipe and CI job are updated to now use 16 threads.
And add a test to verify that both async
and blocking clients were built with headers.
@luisschwab luisschwab force-pushed the feat/custom-testenv branch from 4f5ee42 to 8653954 Compare March 11, 2026 22:58
@luisschwab
Copy link
Member Author

Addressed Steve's review and added a headers field to the AsyncClient that's pub(crate), for testing purposes (it will be removed once we move to bitreq).

@luisschwab luisschwab requested a review from notmandatory March 11, 2026 23:00
@ValuedMammal
Copy link
Contributor

Looks like your code is correctly setting the headers on the Builder for both the async and blocking clients, so no need for the unused AsyncClient::headers.

@luisschwab
Copy link
Member Author

Looks like your code is correctly setting the headers on the Builder for both the async and blocking clients, so no need for the unused AsyncClient::headers.

The extra field is for the test only, will be handy to catch any regressioms when we switch to bitreq.

Copy link
Collaborator

@oleonardolima oleonardolima left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good! However, we shouldn't have the new field on AsyncClient, left an explanation how to properly test it in a comment below.


# Pin dependencies for MSRV (1.75.0)
cargo update -p minreq --precise "2.13.2"
cargo update -p native-tls --precise "0.2.11"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you have to pin this down to 0.2.11, if the 0.2.13 already has 1.53.0 as MSRV ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some transitive dependency was breaking our MSRV.

Comment on lines +392 to +402
fn wait_until_electrum_sees_block(&self) {
let _header = self.electrsd.client.block_headers_subscribe().unwrap();
loop {
if self.electrsd.client.block_headers_pop().unwrap().is_some() {
break;
}
self.electrsd.trigger().unwrap();
self.electrsd.client.ping().unwrap();
std::thread::sleep(Duration::from_millis(500));
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should keep the exponential backoff approach, instead of having a fixed delay.

Comment on lines +411 to +416
/// Get a `Legacy` regtest address.
fn get_legacy_address(&self) -> Address {
Address::from_str("mvUsRD2pNeQQ8nZq8CDEx6fjVQsyzqyhVC")
.unwrap()
.assume_checked()
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the rationale for having fixed addresses, instead o fetching one from the client. If it's required only for test_get_scripthash_stats they should probably constants under that test only.

Comment on lines +48 to +50
#[allow(unused)]
/// HTTP headers to append on every request made to Esplora server.
pub(crate) headers: HashMap<String, String>, // TODO: remove this when `bitreq` is implemented
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NACK on adding this field.

AsyncClient already handles the headers properly, as they're passed to reqwest::Client when building it.

I agree that having test_build_client_with_headers is a good idea, thought the right way to do it is asserting that final requests built from both clients contains the expected headers, and not that the client struct has the header field.

As an example, you can do it by updating test_build_client_with_headers to build a GET request with blocking_client.get_request() and async_client.client().get()... and assert that both requests have the expected headers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working chore

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[test] Consider removing lazy_static

4 participants