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

Streamline random generation #3100

Merged
merged 1 commit into from
Mar 30, 2024
Merged

Conversation

krmahadevan
Copy link
Member

@krmahadevan krmahadevan commented Mar 21, 2024

  • Add edit checks to ensure that when using Random it always gives a non zero value

Summary by CodeRabbit

  • Refactor
    • Enhanced security and consistency in random number generation across various test classes by replacing SecureRandom and Random with a new SafeRandoms utility.
  • New Features
    • Introduced SafeRandoms class for secure and uniform random number generation within specified bounds.
  • Bug Fixes
    • Fixed sleep time calculation in test methods by replacing SecureRandom with SafeRandoms.

Copy link

coderabbitai bot commented Mar 21, 2024

Walkthrough

The recent updates across the testng-core project focus on enhancing random number generation security and consistency. This was achieved by substituting the use of SecureRandom and java.util.Random with a new utility class, SafeRandoms. This class ensures safer and more reliable random number generation across various test classes, impacting methods primarily related to sleep durations and random number utilities.

Changes

File Path Change Summary
.../issue3081/TestClassSample.java
.../issue3081/TestClassWithPrioritiesSample.java
Replaced SecureRandom with SafeRandoms for generating sleep times.
.../factory/issue326/SampleTestClass.java
.../thread/TrueParallelSampleTest.java
.../thread/issue188/Issue188TestSample.java
Removed Random import, introduced SafeRandoms for random number generation.
.../support/SafeRandoms.java Added SafeRandoms class for generating random integers with a secure method, ensuring reliability.

🐇✨
In the realm of code, where tests doth run,
A change was made, under the virtual sun.
SafeRandoms came, with security tight,
Replacing the old, with new might.
Through tests and threads, it weaves its spell,
Ensuring randomness, now safer and swell.
🌟🐰

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

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

Oop should be used


private SafeRandoms() {}

public static int nextInt(int bound) {
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 use util method instead of an object?

Copy link
Member Author

Choose a reason for hiding this comment

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

I didnt quite understand the concern here. Can you please elaborate?

Copy link
Member

Choose a reason for hiding this comment

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

SafeRandom random = new SafeRandom();
int value = random.nextInt();

Copy link
Member Author

Choose a reason for hiding this comment

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

Can you let me know what is the issue with the current implementation? You are suggesting inheritance and I am making use of composition to do the same thing. I am not sure if understand the benefits of having to use inheritance here.

I need a random number which is NOT zero. Why wouldnt a utility method suffice here?

Copy link
Member

Choose a reason for hiding this comment

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

Utils are imperative programming, not composition.

Composition here will be to create your own random that create another random.
Inheritance here will be to create your own random that extends another random.
Both approach are oop and valid.

Copy link
Member Author

Choose a reason for hiding this comment

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

Inheritance is not going to allow me to use another flavor of random (if there's a need for it). The current implementation works with its own random implementation if that's what is needed but it also helps with allowing a user to inject the random as a dependency. I was hinting at composition in that aspect as in I have a randomness object with me which I believe is what composition stands for (has a vs is a relationship).

My point here is that, we don't need an OOP approach here but just a plain vanilla utility that helps in generating random numbers that aren't zero.

Copy link
Member

Choose a reason for hiding this comment

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

That's the point. Everything is possible with utils methods. But why is jdk providing an object for random instead of few static methods?

Copy link
Member Author

Choose a reason for hiding this comment

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

The JDK provides classes for random because there can be multiple types of randoms being used. I am using a static method because it can take any different type of random.

I am still not understanding as to what is wrong with using a utility method for a test and why does it need to be a class that uses inheritance. Its only used in tests. Cant we not use a utility method here? If there are any functional or performance discrepancies I can understand. But this is just a different programming style. I am using a utility method to get it done while you prefer using an object. If there are no pitfalls in terms of using a utility method then I would suggest that we merge this PR.

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: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between f8d722e and ec2c17b.
Files selected for processing (6)
  • testng-core/src/test/java/test/dataprovider/issue3081/TestClassSample.java (2 hunks)
  • testng-core/src/test/java/test/dataprovider/issue3081/TestClassWithPrioritiesSample.java (2 hunks)
  • testng-core/src/test/java/test/factory/issue326/SampleTestClass.java (3 hunks)
  • testng-core/src/test/java/test/support/SafeRandoms.java (1 hunks)
  • testng-core/src/test/java/test/thread/TrueParallelSampleTest.java (2 hunks)
  • testng-core/src/test/java/test/thread/issue188/Issue188TestSample.java (2 hunks)
Additional comments: 6
testng-core/src/test/java/test/support/SafeRandoms.java (1)
  • 8-8: Using SecureRandom as the default random number generator in SafeRandoms is a good choice for security-sensitive applications. However, it's worth noting that SecureRandom can be significantly slower than Random. If the primary use case of SafeRandoms does not involve security-sensitive random numbers, consider allowing the use of Random for scenarios where performance is more critical.
testng-core/src/test/java/test/thread/TrueParallelSampleTest.java (1)
  • 14-14: The replacement of random.nextInt(10) with SafeRandoms.nextInt(random, 10) in Thread.sleep is correctly implemented and aligns with the PR's objectives. This change ensures that the sleep duration is never zero, which could be important for the test's logic.
testng-core/src/test/java/test/dataprovider/issue3081/TestClassSample.java (1)
  • 31-31: The use of SafeRandoms.nextInt(500) to generate sleep times in milliseconds is correctly implemented. However, it's important to note that the original code likely intended to use SecureRandom, which is not directly passed to SafeRandoms.nextInt here. Ensure that the security level of random number generation is consistent with the original intent.
testng-core/src/test/java/test/dataprovider/issue3081/TestClassWithPrioritiesSample.java (1)
  • 31-31: The replacement of direct random number generation with SafeRandoms.nextInt(500) for sleep times is correctly implemented. This ensures non-zero sleep durations, aligning with the PR's objectives. As with TestClassSample, consider the security implications of the random number generation method used.
testng-core/src/test/java/test/thread/issue188/Issue188TestSample.java (1)
  • 39-39: The use of SafeRandoms.nextInt(10) to generate a multiplier for the sleep duration is correctly implemented. This change ensures that the sleep duration is never zero, which could be important for the test's logic. However, consider the impact of multiplying by a random number, especially since the range is now 1-9 instead of 0-9, which slightly alters the behavior.
testng-core/src/test/java/test/factory/issue326/SampleTestClass.java (1)
  • 51-51: The replacement of direct random number generation with SafeRandoms.nextInt(100) for determining sleep duration is correctly implemented. This ensures non-zero sleep durations, aligning with the PR's objectives. As with other files, consider the impact of the change on the test's behavior, especially in terms of the distribution of sleep durations.

testng-core/src/test/java/test/support/SafeRandoms.java Outdated Show resolved Hide resolved
@krmahadevan
Copy link
Member Author

Oop should be used

I didnt understand what did you mean by this.

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 7180ae4 and b3e1f8e.
Files selected for processing (6)
  • testng-core/src/test/java/test/dataprovider/issue3081/TestClassSample.java (2 hunks)
  • testng-core/src/test/java/test/dataprovider/issue3081/TestClassWithPrioritiesSample.java (2 hunks)
  • testng-core/src/test/java/test/factory/issue326/SampleTestClass.java (3 hunks)
  • testng-core/src/test/java/test/support/SafeRandoms.java (1 hunks)
  • testng-core/src/test/java/test/thread/TrueParallelSampleTest.java (2 hunks)
  • testng-core/src/test/java/test/thread/issue188/Issue188TestSample.java (2 hunks)
Files skipped from review as they are similar to previous changes (6)
  • testng-core/src/test/java/test/dataprovider/issue3081/TestClassSample.java
  • testng-core/src/test/java/test/dataprovider/issue3081/TestClassWithPrioritiesSample.java
  • testng-core/src/test/java/test/factory/issue326/SampleTestClass.java
  • testng-core/src/test/java/test/support/SafeRandoms.java
  • testng-core/src/test/java/test/thread/TrueParallelSampleTest.java
  • testng-core/src/test/java/test/thread/issue188/Issue188TestSample.java

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 7180ae4 and 9fa3167.
Files selected for processing (6)
  • testng-core/src/test/java/test/dataprovider/issue3081/TestClassSample.java (2 hunks)
  • testng-core/src/test/java/test/dataprovider/issue3081/TestClassWithPrioritiesSample.java (2 hunks)
  • testng-core/src/test/java/test/factory/issue326/SampleTestClass.java (3 hunks)
  • testng-core/src/test/java/test/support/SafeRandoms.java (1 hunks)
  • testng-core/src/test/java/test/thread/TrueParallelSampleTest.java (2 hunks)
  • testng-core/src/test/java/test/thread/issue188/Issue188TestSample.java (2 hunks)
Files skipped from review as they are similar to previous changes (6)
  • testng-core/src/test/java/test/dataprovider/issue3081/TestClassSample.java
  • testng-core/src/test/java/test/dataprovider/issue3081/TestClassWithPrioritiesSample.java
  • testng-core/src/test/java/test/factory/issue326/SampleTestClass.java
  • testng-core/src/test/java/test/support/SafeRandoms.java
  • testng-core/src/test/java/test/thread/TrueParallelSampleTest.java
  • testng-core/src/test/java/test/thread/issue188/Issue188TestSample.java

* Add edit checks to ensure that when using Random
it always gives a non zero value
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.

Actionable comments posted: 0

Review Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between bb2d8d2 and 5769631.
Files selected for processing (6)
  • testng-core/src/test/java/test/dataprovider/issue3081/TestClassSample.java (2 hunks)
  • testng-core/src/test/java/test/dataprovider/issue3081/TestClassWithPrioritiesSample.java (2 hunks)
  • testng-core/src/test/java/test/factory/issue326/SampleTestClass.java (3 hunks)
  • testng-core/src/test/java/test/support/SafeRandoms.java (1 hunks)
  • testng-core/src/test/java/test/thread/TrueParallelSampleTest.java (2 hunks)
  • testng-core/src/test/java/test/thread/issue188/Issue188TestSample.java (2 hunks)
Additional Context Used
Additional comments not posted (11)
testng-core/src/test/java/test/thread/TrueParallelSampleTest.java (2)

5-5: The addition of import test.support.SafeRandoms; is appropriate for utilizing the SafeRandoms utility class within this test class. This change aligns with the PR's objective to standardize random number generation across tests.


14-14: Replacing random.nextInt(10) with SafeRandoms.nextInt(3, 7, random) in the Thread.sleep call is a good practice to ensure non-zero random values are generated within a specified range. This change enhances the predictability of test outcomes by avoiding potential zero delays that could affect the test's behavior.

testng-core/src/test/java/test/dataprovider/issue3081/TestClassSample.java (2)

10-10: The addition of import test.support.SafeRandoms; is appropriate for utilizing the SafeRandoms utility class within this test class. This change aligns with the PR's objective to standardize random number generation across tests.


31-31: Replacing the previous random number generation mechanism with SafeRandoms.nextInt(200, 300) in the TimeUnit.MILLISECONDS.sleep call is a good practice to ensure non-zero random values are generated within a specified range. This change enhances the predictability of test outcomes by ensuring consistent sleep durations within the specified range.

testng-core/src/test/java/test/dataprovider/issue3081/TestClassWithPrioritiesSample.java (2)

10-10: The addition of import test.support.SafeRandoms; is appropriate for utilizing the SafeRandoms utility class within this test class. This change aligns with the PR's objective to standardize random number generation across tests.


31-31: Replacing the previous random number generation mechanism with SafeRandoms.nextInt(200, 300) in the TimeUnit.MILLISECONDS.sleep call is a good practice to ensure non-zero random values are generated within a specified range. This change enhances the predictability of test outcomes by ensuring consistent sleep durations within the specified range.

testng-core/src/test/java/test/thread/issue188/Issue188TestSample.java (2)

11-11: The addition of import test.support.SafeRandoms; is appropriate for utilizing the SafeRandoms utility class within this test class. This change aligns with the PR's objective to standardize random number generation across tests.


39-39: Replacing the previous random number generation mechanism with 500L * SafeRandoms.nextInt(1, 10) in the TimeUnit.MILLISECONDS.sleep call is a good practice to ensure non-zero random values are generated within a specified range. This change enhances the predictability of test outcomes by ensuring consistent sleep durations within the specified range. However, it's important to ensure that the multiplication by 500L aligns with the intended sleep duration range for the test.

testng-core/src/test/java/test/factory/issue326/SampleTestClass.java (2)

12-12: The addition of import test.support.SafeRandoms; is appropriate for utilizing the SafeRandoms utility class within this test class. This change aligns with the PR's objective to standardize random number generation across tests.


51-51: Replacing the previous random number generation mechanism with 10L * SafeRandoms.nextInt(30, 70) in the TimeUnit.MILLISECONDS.sleep call is a good practice to ensure non-zero random values are generated within a specified range. This change enhances the predictability of test outcomes by ensuring consistent sleep durations within the specified range. However, it's important to ensure that the multiplication by 10L aligns with the intended sleep duration range for the test.

testng-core/src/test/java/test/support/SafeRandoms.java (1)

8-39: The SafeRandoms utility class is well-designed to provide a centralized way to generate random numbers with a guarantee of non-zero values. The use of static methods allows for easy access without needing to instantiate the class. The argument checks ensure that the methods are used correctly, preventing common errors such as negative bounds. However, consider adding a more detailed JavaDoc to explain the behavior when delta is zero, as it might not be immediately clear to users how the method behaves in this case.

@krmahadevan krmahadevan merged commit e5218ab into testng-team:master Mar 30, 2024
4 of 9 checks passed
@krmahadevan krmahadevan deleted the fix_randoms branch March 30, 2024 06:29
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.

3 participants