torrust / torrust-tracker

A modern and feature-rich (private) BitTorrent tracker.
https://torrust.com
GNU Affero General Public License v3.0
364 stars 43 forks source link

De-duplicate Integration Testing and Tracker Client Code #767

Closed da2ce7 closed 5 months ago

da2ce7 commented 7 months ago

part of:

De-duplicating this code would mean that our client code would be tested with our integration tests.

josecelano commented 7 months ago

Relates to: https://github.com/torrust/torrust-tracker/pull/622#issue-2085940333

Hi @da2ce7 I wasn't sure if that is a good practice. I have the feeling we should move the client to another package and also primitives shared by the client and the server. However, I have to think deeply about it. The thing I don't like is tests could fail for two reasons: the client or the API endpoint.

ChatGPT

Moving your API client from test to production while maintaining reliability and ensuring comprehensive testing can indeed introduce some complexities. Your concerns about the potential for tests to fail due to issues with either the API itself or the client code are valid. Let’s explore the considerations and alternatives for managing this transition effectively.

Considerations for Using the Same API Client in Testing and Production

  1. Testing Isolation: One principle of testing is to isolate what you are testing. If you are testing the API, you ideally don’t want failures in the client code to affect the outcome of these tests. Similarly, when testing the client code, you want to isolate it from the API implementation.

  2. Code Reusability and Consistency: On the other hand, using the same code in testing and production can ensure that any bugs found in production are also catchable during testing. This consistency reduces the risk of discrepancies between how your production code behaves and how your test setup behaves.

  3. Reduced Duplication: By using the same client code in both testing and production environments, you reduce code duplication, which aligns with the DRY (Don't Repeat Yourself) principle. This can make maintenance easier but also requires careful management to ensure testing integrity.

Best Practices and Alternatives

  1. Mocking/Stubs for Client Testing: When testing the API client itself, consider using mocking or stubbing techniques to simulate API responses. This isolates the client code from the actual API, allowing you to test it under controlled conditions without relying on the availability or behavior of the API.

  2. Integration Tests with Real Requests: For integration tests that involve both the API and the client, using real requests can be beneficial to verify that the system works as expected in a real-world scenario. This can complement unit tests where components are tested in isolation.

  3. Separate Configuration for Testing and Production: Ensure that your API client can be configured differently for testing and production environments. This can include different base URLs, timeouts, and error handling strategies to better suit each context.

  4. Version Control and Feature Toggling: If introducing changes to the API client that might affect production, consider using version control branches or feature toggles. This allows you to test new changes in a controlled environment before rolling them out to production.

  5. Continuous Integration and Deployment (CI/CD) Pipelines: Utilize CI/CD pipelines to automate testing and ensure that changes to your API client are thoroughly tested at every stage of development before deployment.

  6. Canary Releases and Monitoring: When deploying changes to the API client in production, consider using canary releases to gradually roll out the change to a small subset of users first. Monitor the performance and error rates closely to catch any issues before a full rollout.

Conclusion

In essence, it's a common practice to use the same API client code in both testing and production to ensure consistency and reduce duplication. However, it's crucial to employ testing strategies that effectively isolate components under test, use mocking where appropriate, and carefully manage the transition of changes from development to production to maintain system reliability.

cgbosse commented 5 months ago

@da2ce7 Jose is waiting for a reply on this issue.

josecelano commented 5 months ago

Hi, we can close it. @da2ce7 feel free to re-open it. I think this is related to https://github.com/torrust/torrust-tracker/issues/689. Maybe the solution is to extract an independent package.