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

Tweaks around accessing SuiteResult #3099

Merged
merged 1 commit into from
Mar 22, 2024

Conversation

krmahadevan
Copy link
Member

@krmahadevan krmahadevan commented Mar 21, 2024

Closes #3078

Following changes were made:

  • Removed the lock based synchronization around SuiteResult because it’s already backed by a
    SynchronizedMap from Collections.
  • Altered the getter such that it returns back a regular linkedHashMap (without the synchronisation Because users are expected ONLY to iterate on it and NOT change its state)
  • Also just to ensure that users don’t garble the Suite result, wrapping it with an UnModifiableMap

Fixes #3078 .

Summary by CodeRabbit

  • Refactor
    • Improved thread safety in test suite execution by modifying synchronization mechanisms.
    • Enhanced the integrity of test results by ensuring the results map is unmodifiable.
    • Concealed internal implementation details for enhanced security.

Copy link

coderabbitai bot commented Mar 21, 2024

Walkthrough

The modifications in SuiteRunner.java address the handling of suiteResults by removing the use of AutoCloseableLock in the runTest method and ensuring getResults returns an unmodifiable view of suiteResults. This change aims to enhance the thread-safety and immutability aspects of handling test suite results in TestNG.

Changes

File Change Summary
.../testng/SuiteRunner.java Removed AutoCloseableLock usage in runTest; Modified getResults to return an unmodifiable map of suiteResults.

Assessment against linked issues

Objective Addressed Explanation
Use lock everywhere suiteResult is used (#3078) The changes remove the lock in one place and do not add it everywhere suiteResult is accessed, potentially not fully addressing the thread-safety concern.

Poem

In the realm of code, where tests doth run,
A rabbit hopped, under the virtual sun.
🐇💻 With every leap, a change is made,
Locks and maps, in Java's glade.

"To improve, to fix, to make it right,"
Whispered the rabbit, in the moon's soft light.
🌕✨ For every test, that passes through,
"Let's ensure," it said, "our results hold true."

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>.
    • Generate unit-tests 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 tests 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link

@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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 69dc232 and d394472.
Files selected for processing (1)
  • testng-core/src/main/java/org/testng/SuiteRunner.java (2 hunks)
Additional comments: 2
testng-core/src/main/java/org/testng/SuiteRunner.java (2)
  • 423-423: The removal of lock-based synchronization around suiteResults and the direct insertion into suiteResults here assumes that runTest is always called in a thread-safe manner. Given that SuiteRunner may execute tests in parallel, it's crucial to ensure that all modifications to suiteResults are thread-safe. The use of Collections.synchronizedMap for suiteResults does provide thread safety for the map itself, but iterating over the map or performing compound actions still requires external synchronization to avoid concurrent modification exceptions or other thread safety issues.

Ensure that all access patterns to suiteResults are safe and consider if additional synchronization is needed when iterating over or performing compound actions on suiteResults.

  • 513-518: The change to return an UnmodifiableMap wrapped around a new LinkedHashMap instance of suiteResults in the getResults method is a good practice to prevent unintended modifications by the consumers of this method. However, it's important to note that while this prevents modifications to the map itself, it does not prevent modifications to the objects contained within the map. If ISuiteResult objects are mutable, consumers could still alter their state, potentially leading to inconsistencies.

Consider documenting the expected immutability of the ISuiteResult objects or ensuring that they are deeply immutable if necessary to maintain the integrity of the suite results.

// to enter and exit the synchronized block.
// Also just to ensure that we guard the internals of the suite results we now wrap it
// around with an unmodifiable map.
return Collections.unmodifiableMap(new LinkedHashMap<>(suiteResults));
Copy link
Member

Choose a reason for hiding this comment

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

Why do you create a new intermediate map?

Copy link
Member Author

Choose a reason for hiding this comment

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

We are returning back a new map, because suiteResults is a synchronizedMap which means there's an added cost to entering/exiting the mutex barrier created by the synchronized block within the get operation of the SynchronizedMap.

testng-core/src/main/java/org/testng/SuiteRunner.java Outdated Show resolved Hide resolved
Closes testng-team#3078

Following changes were made:

* Removed the lock based synchronization around 
SuiteResult because it’s already backed by a 
SynchronizedMap from Collections.
* Altered the getter such that it returns back a
regular linkedHashMap (without the synchronisation
Because users are expected ONLY to iterate on it and
NOT change its state)
* Also just to ensure that users don’t garble the 
Suite result, wrapping it with an UnModifiableMap
Copy link

@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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between f8d722e and ac2bc22.
Files selected for processing (1)
  • testng-core/src/main/java/org/testng/SuiteRunner.java (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • testng-core/src/main/java/org/testng/SuiteRunner.java

Copy link
Member

@juherr juherr left a comment

Choose a reason for hiding this comment

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

Did you check that all map usages are synchronised?

@krmahadevan
Copy link
Member Author

Did you check that all map usages are synchronised?

This map is being written into in only one place. And that place is already guarded by an org.testng.internal.AutoCloseableLock. There are no other places wherein this map is even being read within SuiteRunner. All other readers of this map are accessing it via the getter, which is guarded by an unmodifiable constraint so writes are out of the question.

I think for concurrent reads we don't need to have any synchronization. Only when a read and write happens together on a collection is when it would throw a ConcurrentModificationException.

@krmahadevan krmahadevan merged commit 7180ae4 into testng-team:master Mar 22, 2024
6 of 9 checks passed
@krmahadevan krmahadevan deleted the fix_issue_3078 branch March 22, 2024 07:28
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.

Should not we use the lock everywhere suiteResult is used?
2 participants