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 UdpClient to return errors instead of panicking #814

Merged
merged 1 commit into from
May 2, 2024

Conversation

hungfnt
Copy link
Contributor

@hungfnt hungfnt commented Apr 26, 2024

I replaced panics with error handling using anyhow::Result<T>, which is equivalent to std::result::Result<T, anyhow::Error>. Although I can run cargo run without errors, I'm not certain if there are any hidden bugs.

@hungfnt hungfnt marked this pull request as draft April 26, 2024 08:14
Copy link
Member

@josecelano josecelano left a comment

Choose a reason for hiding this comment

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

Hi @ngthhu thank you!! Look good. However, some workflows are failing. You can start fixing:

https://github.com/torrust/torrust-tracker/actions/runs/8845155075/job/24288517159?pr=814#step:6:800

You can run at least some commands locally before committing. For example:

Clippy

CARGO_INCREMENTAL=0 cargo clippy --no-deps --tests --benches --examples --workspace --all-targets --all-features -- -D clippy::correctness -D clippy::suspicious -D clippy::complexity -D clippy::perf -D clippy::style -D clippy::pedantic

Unit tests

cargo test

I would suggest installing Clippy in your IDE.

You can run all commands in the testing workflow locally.

What I do is:

  1. Use Visual Studio Code with Clippy (and other extensions)
  2. Run unit tests after small changes cargo test.
  3. Run E2E tests before committing (cargo run --bin e2e_tests_runner ./share/default/config/tracker.e2e.container.sqlite3.toml).
    Let me know if you have any trouble fixing those errors.

Regarding the new feature to test some trackers in bulk. I would do it after merging all console commands in one. See #660

Finally, I would split this PR in two anyway:

  1. One for changing the API returning an error instead of
    panicking.
  2. Another to add the new feature to test a list of trackers.

http_trackers.sh Outdated
Copy link
Member

Choose a reason for hiding this comment

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

Hi @ngthhu, I've just started the PR review, I'm commenting on this and other things in the next minutes.

First, we favor scripting in Rust over Bash unless Bash offers crucial features or simplifies script maintenance or implementation. Se, for example:

https://github.com/torrust/torrust-tracker/blob/develop/src/bin/e2e_tests_runner.rs

The reasons are:

  • It's enjoyable
  • It's easier to test
  • Developers can also contribute, not only sys admins. I mean, it lowers the entry barrier to maintain those scripts (in my opinion).

On the other hand, this looks like a good feature for the UDP client. It could accept both:

  • A filepath to a file containing a list of trackers (trackers.txt)
  • It could be passed via stdin.
cargo run --bin udp_tracker_client announce --trackers="./tracker.txt" 
9c38422213e30bff212b30c360d26f9a02136422

cat ./tracker.txt cargo run --bin udp_tracker_client announce 9c38422213e30bff212b30c360d26f9a02136422

I have yet to think about the best option to pass that argument. I would depend on what it's the standard or best way to do it using clap. I will think about this and give more feedback.

trackers.txt Outdated
Copy link
Member

Choose a reason for hiding this comment

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

@ngthhu I would move this file to a fixtures folder (maybe tests/fixtures/) like this:

https://github.com/torrust/torrust-index/tree/develop/tests/fixtures/torrents

And I would rename it to http_trackers.txt.

Maybe we don't need to add the list to the repo. Maybe we could get an updated list from: https://newtrackon.com/. For example, for HTTP trackers:

https://newtrackon.com/api/http

We could run the test like this:

curl https://newtrackon.com/api/http | cargo run --bin http_tracker_client announce 9c38422213e30bff212b30c360d26f9a02136422

or

curl -o http_trackers.txt https://newtrackon.com/api/http
cargo run --bin http_tracker_client announce --tracker="./http_trackers.txt" 9c38422213e30bff212b30c360d26f9a02136422

@hungfnt
Copy link
Contributor Author

hungfnt commented Apr 26, 2024

Thank you for your responses @josecelano.

Use Visual Studio Code with Clippy (and other extensions)

Currently I'm using Visual Studio Code too and I see Clippy running everytime I save a .rs file.

Run unit tests after small changes cargo test.
Run E2E tests before committing (cargo run --bin e2e_tests_runner ./share/default/config/tracker.e2e.container.sqlite3.toml).
Let me know if you have any trouble fixing those errors.

I haven't done this before.

When I run cargo test, there are errors in tests/servers/udp/contract.rs, that file has some calls to functions in the src/shared/bit_torrent/tracker/udp/client.rs file that I have modified with errors, I guest I would resolve them.

I will run E2E test later then provide more info.

Regarding the new feature to test some trackers in bulk

I did some test on another issue and forgot to exclude them in this pull request. I manually edited the list so it contain only http url without the /announce suffix.

@josecelano
Copy link
Member

Thank you for your responses @josecelano.

Use Visual Studio Code with Clippy (and other extensions)

Currently I'm using Visual Studio Code too and I see Clippy running everytime I save a .rs file.

OK. Sometimes, you could have errors in workflows (but not locally) just because they started using a newer Rust version. I usually work with the updated nightly version.

Run unit tests after small changes cargo test.
Run E2E tests before committing (cargo run --bin e2e_tests_runner ./share/default/config/tracker.e2e.container.sqlite3.toml).
Let me know if you have any trouble fixing those errors.

I haven't done this before.

You will need to setup some system dependencies like docker and other things. Let me know if docs are not clear enougth.

When I run cargo test, there are errors in tests/servers/udp/contract.rs, that file has some calls to functions in the src/shared/bit_torrent/tracker/udp/client.rs file that I have modified with errors, I guest I would resolve them.

I will run E2E test later then provide more info.

Regarding the new feature to test some trackers in bulk

I did some test on another issue and forgot to exclude them in this pull request. I manually edited the list so it contain only http url without the /announce suffix.

Cool! It's a nice-to-have feature. I wanted to do it just to see if the client works fine with different trackers. But I'm focused on other things now.

@hungfnt
Copy link
Contributor Author

hungfnt commented Apr 26, 2024

I will run E2E test later then provide more info.

I run E2E test on my wsl2 with docker and there's a permission error

$ cargo run --bin e2e_tests_runner ./share/default/config/tracker.e2e.container.sqlite3.toml
    Finished dev [optimized + debuginfo] target(s) in 0.47s
     Running `target/debug/e2e_tests_runner ./share/default/config/tracker.e2e.container.sqlite3.toml`
2024-04-26T20:37:06.350717686+07:00 [torrust_tracker::console::ci::e2e::runner][INFO] Reading tracker configuration from file: ./share/default/config/tracker.e2e.container.sqlite3.toml ...
2024-04-26T20:37:06.350912390+07:00 [torrust_tracker::console::ci::e2e::tracker_container][INFO] Building tracker container image with tag: torrust-tracker:local ...
ERROR: permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/_ping": dial unix /var/run/docker.sock: connect: permission denied
thread 'main' panicked at src/console/ci/e2e/tracker_container.rs:44:55:
A tracker local docker image should be built: Custom { kind: Other, error: "Failed to build Docker image from dockerfile ./Containerfile" }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
2024-04-26T20:37:06.711867433+07:00 [torrust_tracker::console::ci::e2e::tracker_container][INFO] Dropping tracker container: tracker_tC9w2x2olkoJhbrSbJew

I followed this solution, then restart docker, wsl distro and that error is fixed.

@josecelano
Copy link
Member

I will run E2E test later then provide more info.

I run E2E test on my wsl2 with docker and there's a permission error

$ cargo run --bin e2e_tests_runner ./share/default/config/tracker.e2e.container.sqlite3.toml
    Finished dev [optimized + debuginfo] target(s) in 0.47s
     Running `target/debug/e2e_tests_runner ./share/default/config/tracker.e2e.container.sqlite3.toml`
2024-04-26T20:37:06.350717686+07:00 [torrust_tracker::console::ci::e2e::runner][INFO] Reading tracker configuration from file: ./share/default/config/tracker.e2e.container.sqlite3.toml ...
2024-04-26T20:37:06.350912390+07:00 [torrust_tracker::console::ci::e2e::tracker_container][INFO] Building tracker container image with tag: torrust-tracker:local ...
ERROR: permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/_ping": dial unix /var/run/docker.sock: connect: permission denied
thread 'main' panicked at src/console/ci/e2e/tracker_container.rs:44:55:
A tracker local docker image should be built: Custom { kind: Other, error: "Failed to build Docker image from dockerfile ./Containerfile" }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
2024-04-26T20:37:06.711867433+07:00 [torrust_tracker::console::ci::e2e::tracker_container][INFO] Dropping tracker container: tracker_tC9w2x2olkoJhbrSbJew

I followed this solution, then restart docker, wsl distro and that error is fixed.

Hi, permissions are a very common problem with docker, unfortunately :-/. Sometimes, what I do (if I don't want to run E2E tests locally because they are very slow) is open a draft PR with just one commit while I'm working on the next commit (task increment). I check the workflow after a while to see if workflows have passed, if there is an error I fix it locally. This way, I can take advantage of both my PC and the runners :-). But sometimes, it is also slow because we are using shared runners.

Besides, if you find a problem in the future, I would suggest immediately opening an issue while you are working on fixing it. There are two advantages:

  1. People with the same problem in the future are more likely to see your solution if it is an issue rather than a comment on a PR.
  2. There could be someone else who can help you to fix it faster.

@hungfnt
Copy link
Contributor Author

hungfnt commented Apr 26, 2024

Sometimes, what I do (if I don't want to run E2E tests locally because they are very slow) is open a draft PR with just one commit while I'm working on the next commit (task increment).

They are very really slow, thank you for your method.

@josecelano
Copy link
Member

Sometimes, what I do (if I don't want to run E2E tests locally because they are very slow) is open a draft PR with just one commit while I'm working on the next commit (task increment).

They are very really slow, thank you for your method.

We run an isolated tracker for each test to avoid coupling between tests and because some tests need a custom tracker configuration.

Running tests only takes 14 seconds. The slowest part is the docker build with Rust compilation. In this project, the integration tests are more important. E2E tests are just to check containerization. In the Index they are more important because we test a lot of behaviour that could not be tested without a running tracker (at that time).

In both the Tracker and the Index I added a lot of E2E and Integration tests because there were no tests and I had to do a lot of reactorings like web server migration. It was the only way to cover the main functionality. Now, I would like to progressively create more unit tests and fewer integration/E2E tests. It should be easier.

Copy link

codecov bot commented May 2, 2024

Codecov Report

Attention: Patch coverage is 79.10448% with 14 lines in your changes are missing coverage. Please review.

Project coverage is 78.92%. Comparing base (92349d3) to head (effca56).

Files Patch % Lines
src/shared/bit_torrent/tracker/udp/client.rs 79.10% 14 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #814      +/-   ##
===========================================
+ Coverage    78.91%   78.92%   +0.01%     
===========================================
  Files          163      163              
  Lines         9186     9212      +26     
===========================================
+ Hits          7249     7271      +22     
- Misses        1937     1941       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@hungfnt hungfnt marked this pull request as ready for review May 2, 2024 08:39
@hungfnt hungfnt requested a review from josecelano May 2, 2024 08:39
Copy link
Member

@josecelano josecelano left a comment

Choose a reason for hiding this comment

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

Hi @ngthhu, Thank you!

In the future, you can return a Result in a test function:

https://doc.rust-lang.org/rust-by-example/testing/unit_testing.html#tests-and-

I think that would make the tests more concise by removing the "matches". I don't know if it's possible in this particular case.

@josecelano
Copy link
Member

ACK effca56

@josecelano josecelano merged commit 90c7780 into torrust:develop May 2, 2024
17 checks passed
@hungfnt
Copy link
Contributor Author

hungfnt commented May 3, 2024

Hi @josecelano, I think this pull request would resolve issue #671 as well.

@josecelano
Copy link
Member

Hi @josecelano, I think this pull request would resolve issue #671 as well.

Hi, @ngthhu, I've replied here.

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.

Refactor bit_torrent::tracker::udp::client::UdpClient to return errors instead of panicking
2 participants