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

[dotnet] Add SystemClock singleton #15285

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from

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

    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

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

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    1 participant