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

Fix unstable honeypot reroll test #8639

Merged
merged 2 commits into from
Nov 5, 2024

Conversation

zhiltsov-max
Copy link
Contributor

@zhiltsov-max zhiltsov-max commented Nov 4, 2024

Motivation and context

The test can fail sometimes because of the random endpoint behavior (this is by design). Allow some flexibility in expected cases

How has this been tested?

Checklist

  • I submit my changes into the develop branch
  • I have created a changelog fragment
  • I have updated the documentation accordingly
  • I have added tests to cover my changes
  • I have linked related issues (see GitHub docs)
  • I have increased versions of npm packages if it is necessary
    (cvat-canvas,
    cvat-core,
    cvat-data and
    cvat-ui)

License

  • I submit my code changes under the same MIT License that covers the project.
    Feel free to contact the maintainers if that's a concern.

Summary by CodeRabbit

  • Bug Fixes

    • Improved reliability of REST API tests related to task management by adding a retry mechanism for random outcomes.
  • Tests

    • Introduced a new constant to manage random test attempts, enhancing the robustness of specific test cases.

Copy link
Contributor

coderabbitai bot commented Nov 4, 2024

Walkthrough

The changes in this pull request involve two main files: cvat/apps/engine/cache.py and tests/python/rest_api/test_tasks.py. In the first file, the order of import statements has been modified without affecting the functionality of the code. In the second file, enhancements have been made to the test suite for REST API tasks, including the introduction of a new constant to manage randomness in test outcomes and improvements to the retry logic for specific test cases, aimed at increasing test reliability.

Changes

File Change Summary
cvat/apps/engine/cache.py Reordered import statements for load_image and md5_hash from cvat.apps.engine.utils.
tests/python/rest_api/test_tasks.py Added constant _MAX_RANDOM_ATTEMPTS and improved retry logic for test_can_change_honeypot_frames_in_annotation_jobs.

Sequence Diagram(s)

sequenceDiagram
    participant Test as Test Suite
    participant Task as Task Management System

    Test->>Task: Attempt to change honeypot frames
    alt Random outcome fails
        Test->>Test: Retry up to _MAX_RANDOM_ATTEMPTS
        Test->>Task: Retry change
    end
    Test->>Task: Confirm change success or fail with message
Loading

🐰 "In the code where imports align,
A constant now helps tests refine.
With retries in place, they won't despair,
For randomness can't catch us unaware!
So hop along, let tests take flight,
In our code garden, all feels right!" 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

sonarqubecloud bot commented Nov 4, 2024

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (2)
tests/python/rest_api/test_tasks.py (2)

4534-4535: Add docstring for the constant.

The constant _MAX_RANDOM_ATTEMPTS would benefit from a docstring explaining its purpose and why the value 20 was chosen.

-    _MAX_RANDOM_ATTEMPTS = 20  # This test can have random outcomes, it's expected
+    # Maximum number of attempts to retry random honeypot frame selection before failing the test.
+    # The value is chosen to balance between test reliability and execution time.
+    _MAX_RANDOM_ATTEMPTS = 20

4557-4584: Consider extracting retry logic into a helper function.

The retry logic for handling random honeypot frame selection could be extracted into a reusable helper function to improve code maintainability and reusability.

+    def _retry_random_frame_selection(self, api_client, job_id, params, old_validation_layout):
+        """
+        Helper function to retry random honeypot frame selection.
+        Returns the new validation layout after successful selection or fails the test.
+        """
+        attempt = 0
+        while attempt < self._MAX_RANDOM_ATTEMPTS:
+            new_validation_layout = json.loads(
+                api_client.jobs_api.partial_update_validation_layout(
+                    job_id,
+                    patched_job_validation_layout_write_request=(
+                        models.PatchedJobValidationLayoutWriteRequest(**params)
+                    ),
+                )[1].data
+            )
+
+            if (
+                params["frame_selection_method"] != "random_uniform"
+                or new_validation_layout["honeypot_real_frames"]
+                != old_validation_layout["honeypot_real_frames"]
+            ):
+                return new_validation_layout
+
+            attempt += 1
+
+        pytest.fail(
+            f"Failed to get different honeypot frames after {attempt} attempts. "
+            "This may indicate an issue with the random selection logic."
+        )

-        attempt = 0
-        while attempt < _MAX_RANDOM_ATTEMPTS:
-            new_validation_layout = json.loads(
-                api_client.jobs_api.partial_update_validation_layout(
-                    annotation_job["id"],
-                    patched_job_validation_layout_write_request=(
-                        models.PatchedJobValidationLayoutWriteRequest(**params)
-                    ),
-                )[1].data
-            )
-
-            new_honeypot_real_frames = new_validation_layout["honeypot_real_frames"]
-
-            if (
-                frame_selection_method == "random_uniform"
-                and new_honeypot_real_frames
-                == old_validation_layout["honeypot_real_frames"]
-            ):
-                attempt += 1
-            else:
-                break
-
-        if attempt >= _MAX_RANDOM_ATTEMPTS and frame_selection_method == "random_uniform":
-            pytest.fail(f"too many attempts ({attempt}) with random honeypot updating")
+        new_validation_layout = self._retry_random_frame_selection(
+            api_client, annotation_job["id"], params, old_validation_layout
+        )
+        new_honeypot_real_frames = new_validation_layout["honeypot_real_frames"]
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 7ce5452 and 3796022.

📒 Files selected for processing (2)
  • cvat/apps/engine/cache.py (1 hunks)
  • tests/python/rest_api/test_tasks.py (2 hunks)
✅ Files skipped from review due to trivial changes (1)
  • cvat/apps/engine/cache.py

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 74.30%. Comparing base (ceba1b7) to head (3796022).
Report is 3 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #8639      +/-   ##
===========================================
- Coverage    74.30%   74.30%   -0.01%     
===========================================
  Files          401      401              
  Lines        43419    43421       +2     
  Branches      3951     3951              
===========================================
+ Hits         32263    32264       +1     
- Misses       11156    11157       +1     
Components Coverage Δ
cvat-ui 78.64% <100.00%> (-0.01%) ⬇️
cvat-server 70.58% <100.00%> (ø)

@zhiltsov-max zhiltsov-max merged commit 6a60642 into develop Nov 5, 2024
34 checks passed
@zhiltsov-max zhiltsov-max deleted the zm/fix-unstable-honeypot-reroll-test branch November 5, 2024 12:26
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.

2 participants