Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[dotnet] Add SystemClock singleton #15285

Merged
merged 5 commits into from
Feb 25, 2025

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Feb 13, 2025

User description

Thanks for contributing to Selenium!
A PR well described will help maintainers to quickly review and merge it

Before submitting your PR, please check our contributing guidelines.
Avoid large PRs, help reviewers by making them as simple and short as possible.

Add SystemClock singleton

Motivation and Context

This minimizes an allocation when using the WebDriverWait type, and adjacent types like components and waits

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

PR Type

Enhancement, Tests


Description

  • Introduced a singleton SystemClock.Instance to minimize allocations.

  • Updated various classes to use SystemClock.Instance instead of creating new instances.

  • Refactored FakeClock to HandCrankClock with improved naming and functionality.

  • Adjusted related tests to align with the new SystemClock.Instance and HandCrankClock.


Changes walkthrough 📝

Relevant files
Enhancement
PopupWindowFinder.cs
Use `SystemClock.Instance` in `PopupWindowFinder`               

dotnet/src/support/UI/PopupWindowFinder.cs

  • Replaced new SystemClock() with SystemClock.Instance.
  • Simplified WebDriverWait initialization.
  • +1/-1     
    SlowLoadableComponent{T}.cs
    Use `SystemClock.Instance` in `SlowLoadableComponent`       

    dotnet/src/support/UI/SlowLoadableComponent{T}.cs

  • Replaced new SystemClock() with SystemClock.Instance.
  • Updated constructor for consistency.
  • +1/-1     
    DefaultWait{T}.cs
    Use `SystemClock.Instance` in `DefaultWait`                           

    dotnet/src/webdriver/Support/DefaultWait{T}.cs

  • Replaced new SystemClock() with SystemClock.Instance.
  • Simplified constructor logic.
  • +1/-1     
    SystemClock.cs
    Introduce singleton instance for `SystemClock`                     

    dotnet/src/webdriver/Support/SystemClock.cs

  • Added SystemClock.Instance as a singleton instance.
  • Ensured thread-safe initialization.
  • +5/-0     
    WebDriverWait.cs
    Use `SystemClock.Instance` in `WebDriverWait`                       

    dotnet/src/webdriver/Support/WebDriverWait.cs

  • Replaced new SystemClock() with SystemClock.Instance.
  • Updated constructor for consistency.
  • +1/-1     
    HandCrankClock.cs
    Refactor `FakeClock` to `HandCrankClock`                                 

    dotnet/test/support/UI/HandCrankClock.cs

  • Renamed FakeClock to HandCrankClock.
  • Improved method naming for clarity.
  • Simplified property and method implementations.
  • +3/-12   
    Tests
    SlowLoadableComponentTest.cs
    Update tests for `SystemClock` and `HandCrankClock`           

    dotnet/test/support/UI/SlowLoadableComponentTest.cs

  • Updated tests to use SystemClock.Instance.
  • Replaced FakeClock with HandCrankClock.
  • Adjusted test logic to align with new clock implementation.
  • +10/-14 

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Thread Safety

    The SystemClock singleton instance is initialized using an auto-property. Consider using explicit thread-safe initialization pattern like lazy initialization to ensure thread safety.

    public static SystemClock Instance { get; } = new();
    Naming Clarity

    The variable name 'fakeNow' could be renamed to something more descriptive like 'currentTime' or 'simulatedTime' to better reflect its purpose in the test clock implementation.

    private DateTime fakeNow = new DateTime(50000);

    Copy link
    Contributor

    qodo-merge-pro bot commented Feb 13, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    General
    Enforce singleton pattern implementation

    Add a private constructor to prevent direct instantiation of SystemClock since
    it's designed as a singleton. This enforces the singleton pattern and prevents
    potential multiple instances.

    dotnet/src/webdriver/Support/SystemClock.cs [29-34]

     public class SystemClock : IClock
     {
         /// <summary>
         /// An instance of the <see cref="SystemClock"/> type.
         /// </summary>
         public static SystemClock Instance { get; } = new();
     
    +    private SystemClock() { }
    +
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    __

    Why: Adding a private constructor is a good practice to properly implement the singleton pattern, preventing accidental instantiation and ensuring better encapsulation of the SystemClock class.

    Medium

    @RenderMichael
    Copy link
    Contributor Author

    RenderMichael commented Feb 13, 2025

    SystemClock is a stateless type, and it gets initialized each time a WebDriverWait is created. This can remove an intermediate allocation from a commonly-used type.

    Copy link
    Member

    @nvborisenko nvborisenko left a comment

    Choose a reason for hiding this comment

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

    Thanks, micro-optimizations are always good, even if it is nano :) I concern this approach will not work if in future we will add parameter in ctor. But I cannot image which one might be added.

    @RenderMichael
    Copy link
    Contributor Author

    If we add a parameter in the constructor, then users who use new SystemClock() will be broken too!

    The only reason users may be forced to care about the SystemClock (or the IClock interface in general) is if they want a custom sleep interval. We do not have a constructor in WebDriverWait which lets you specify a sleep interval without an IClock too.

    For that reason, I opened #15314 👀

    @RenderMichael RenderMichael merged commit 0bc8c54 into SeleniumHQ:trunk Feb 25, 2025
    10 checks passed
    @RenderMichael RenderMichael deleted the system-clock branch February 25, 2025 19:44
    # for free to join this conversation on GitHub. Already have an account? # to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants