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

Expose test helpers #484

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft

Conversation

spacebear21
Copy link
Collaborator

This is an effort to address #422.

It adds a test_utils module, gated behind the _test_utils feature. So far I only extracted common functions and made no attempt to re-architect anything (e.g. by exposing a TestServices struct or similar).

As written currently, those common functions depend on many dev_dependencies. To satisfy the compiler I brought these into the main dependencies and marked them as optional, so the _test_utils feature can enable them. I'm not sure if this is a best practice but it seems to be what rust-lightning does (albeit with way less dependencies).

This adds a test_utils module, gated behind the `_test_utils` feature.

It contains internal test utilities shared between payjoin integration
tests and payjoin-cli e2e tests.

As written currently, those common functions depend on many
`dev_dependencies` which must now be brought into the main
`dependencies` and marked as optional, and are enabled by the
`_test_utils` feature. I'm unsure if this is a best practice.
@coveralls
Copy link
Collaborator

coveralls commented Jan 15, 2025

Pull Request Test Coverage Report for Build 12838560831

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 99 of 104 (95.19%) changed or added relevant lines in 1 file are covered.
  • 283 unchanged lines in 8 files lost coverage.
  • Overall coverage increased (+0.4%) to 61.163%

Changes Missing Coverage Covered Lines Changed/Added Lines %
payjoin-test-utils/src/lib.rs 99 104 95.19%
Files with Coverage Reduction New Missed Lines %
payjoin/src/receive/mod.rs 1 95.45%
payjoin-directory/src/db.rs 18 75.32%
payjoin/src/receive/v2/error.rs 18 20.0%
payjoin-directory/src/lib.rs 29 77.85%
payjoin/src/receive/v2/mod.rs 31 86.14%
payjoin-cli/src/app/v1.rs 45 0.0%
payjoin-cli/src/app/v2.rs 64 0.0%
payjoin/src/receive/error.rs 77 21.28%
Totals Coverage Status
Change from base Build 12771456917: 0.4%
Covered Lines: 3041
Relevant Lines: 4972

💛 - Coveralls

@DanGould
Copy link
Contributor

Hmm our extensive list of dev-dependencies is why I started doing putting the test utility functions into #405, thinking a _test-utils feature would just change the visibility of some internal types. We do already have _danger-local-https, which makes --all-features insecure, but I'm a bit more wary about adding all of these dev-dependencies as optional features.

I recognize that this is musing / complaining, so I want to make sure we're looking at all of the angles we could be approaching this from. @nothingmuch do you have any experience with test utilities? Any ideas of someone outside the org we might ask who has more experience? Maybe matt

@spacebear21
Copy link
Collaborator Author

I started doing putting the test utility functions into #405

Assuming you meant #425 - somehow I overlooked that PR when I started this one yesterday. I do share your concern about exposing too many optional dev-dependencies, so making a separate crate seems like a better approach. I'm open to other suggestions also.

@DanGould
Copy link
Contributor

Yes I did mean #425 glad you found it.

@nothingmuch
Copy link
Collaborator

@nothingmuch do you have any experience with test utilities?

Yes, but not in rust... In other languages, my experience is that reusable code for testing should just be treated as reusable code, so naively I would guess the way to tackle this is the approach of #425

spacebear21 and others added 3 commits January 17, 2025 15:26
Feature-gating behind _test_utils poses several concerns, notably making
bloating --all-features and requiring adding a bunch of optional
dependencies due to our extensive list of dev-dependencies.

Instead, expose payjoin-test-utils as a standalone crate.
This new crate also provides an opportunity to create downstream
testfixtures in payjoin-ffi and language bindings downstream of it.

Co-authored-by: DanGould <[email protected]>
TestServices is a helper struct that initializes a Payjoin directory and
OHTTP relay to facilitate v2 integration tests. It holds onto the
running process handles until ownership is taken away explicitly with
`take_*_handle`.
@spacebear21
Copy link
Collaborator Author

I took from #425 to move the test utils to a new crate.

Additionally, the last commit introduces TestServices based on @nothingmuch's earlier suggestion. I'd like to hear your thoughts (and @DanGould's) on the "take handle" approach I took for transferring ownership of the handles when needed in the tokio::select!.

I'm also considering whether it would be beneficial to add some other convenience methods to TestServices:

  • Addhttp_agent: Arc<Client> and a corresponding getter since we initialize an agent in every V2 test
  • Add a wait_for_all_services_ready helper since we also wait for both services to be ready in all tests.
  • Then, the inner do_v*_to_v* test functions can just take a single services: &TestServices parameter instead of two individual URLs plus the cert.
  • Maybe also a helper for fetching OHTTP keys?

Copy link
Collaborator

@nothingmuch nothingmuch left a comment

Choose a reason for hiding this comment

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

this is looking good

apart from my one comment, the only other suggestion i have is in the non draft version of this PR squashing the first and 3rd to last commits to eliminate the feature & dev-dependencies churn

Comment on lines 234 to +237
tokio::select!(
err = ohttp_relay_handle => panic!("Ohttp relay exited early: {:?}", err),
err = directory_handle => panic!("Directory server exited early: {:?}", err),
res = do_expiration_tests(ohttp_relay, directory, cert) => assert!(res.is_ok(), "v2 send receive failed: {:#?}", res)
err = services.take_ohttp_relay_handle().unwrap() => panic!("Ohttp relay exited early: {:?}", err),
err = services.take_directory_handle().unwrap() => panic!("Directory server exited early: {:?}", err),
res = do_expiration_tests(services.ohttp_relay_url(), services.directory_url(), services.cert()) => assert!(res.is_ok(), "v2 send receive failed: {:#?}", res)
Copy link
Collaborator

Choose a reason for hiding this comment

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

perhaps this pattern can be put in a test serivces method that takes an Fn for the do_ methods, and seelects on its own.

somehing like services.run_test_body(do_expiration_tests), and modify those helpers too take services as the single argument. i didn't check to see if that would work for all tests but even if it's only for a large chunk of them this could help simplify things further

@nothingmuch
Copy link
Collaborator

Hmm, another thought, I was putting together a flake app to start regtest, the directory and the relay, and wrote a yucky shell script with an exit trap and background tasks, it's disgusting... and then realized I'm just replicating this PR but in bash. Anyway, what do you think about adding a main.rs to the test crate that effectively exposes TestServices, for manual testing purposes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants