Skip to content

refactor(ChangeDetectionCleanTask): refine CleanTask logic#8166

Merged
ArgoZhang merged 2 commits into
mainfrom
dev-clean
Jun 26, 2026
Merged

refactor(ChangeDetectionCleanTask): refine CleanTask logic#8166
ArgoZhang merged 2 commits into
mainfrom
dev-clean

Conversation

@ArgoZhang

@ArgoZhang ArgoZhang commented Jun 26, 2026

Copy link
Copy Markdown
Member

Link issues

fixes #8165

Summary By Copilot

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Refine the change detection cleanup task to use a timer-driven loop and strengthen its test coverage.

Enhancements:

  • Simplify the change detection cleanup task by replacing cancellation-token-based control with a PeriodicTimer managed under a lock and configurable interval.

Tests:

  • Extend unit tests to verify timer creation and disposal in the cleanup task lifecycle and to validate the cleanup loop behavior against dynamically generated types.

@bb-auto bb-auto Bot added the enhancement New feature or request label Jun 26, 2026
@bb-auto bb-auto Bot added this to the v10.7.0 milestone Jun 26, 2026
@sourcery-ai

sourcery-ai Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Refactors ChangeDetectionCleanTask to replace the cancellation-token-based loop with a PeriodicTimer-managed loop, introduces deterministic timer lifecycle handling under a lock, and adds more focused unit tests that validate timer creation, disposal, and the clean loop behavior using a dynamic-assembly type.

Sequence diagram for ChangeDetectionCleanTask refactor to PeriodicTimer

sequenceDiagram
    participant Caller
    participant ChangeDetectionCleanTask
    participant CacheManager
    participant PeriodicTimer

    Caller->>ChangeDetectionCleanTask: Run()
    activate ChangeDetectionCleanTask
    ChangeDetectionCleanTask->>CacheManager: Options
    CacheManager-->>ChangeDetectionCleanTask: ChangeDetectionTaskInterval
    ChangeDetectionCleanTask->>PeriodicTimer: new PeriodicTimer(interval)
    ChangeDetectionCleanTask->>ChangeDetectionCleanTask: _timer = timer
    ChangeDetectionCleanTask->>ChangeDetectionCleanTask: Task.Run(() => Clean(timer))
    deactivate ChangeDetectionCleanTask

    loop until WaitForNextTickAsync returns false
        ChangeDetectionCleanTask->>PeriodicTimer: WaitForNextTickAsync()
        PeriodicTimer-->>ChangeDetectionCleanTask: true
        ChangeDetectionCleanTask->>ChangeDetectionCleanTask: DoTask()
    end

    Caller->>ChangeDetectionCleanTask: Stop()
    activate ChangeDetectionCleanTask
    ChangeDetectionCleanTask->>PeriodicTimer: Dispose()
    ChangeDetectionCleanTask->>ChangeDetectionCleanTask: _timer = null
    deactivate ChangeDetectionCleanTask

    PeriodicTimer-->>ChangeDetectionCleanTask: WaitForNextTickAsync() returns false
    ChangeDetectionCleanTask-->>Caller: Clean loop exits
Loading

File-Level Changes

Change Details Files
Refactor ChangeDetectionCleanTask to use a shared PeriodicTimer for its clean loop instead of a CancellationTokenSource-driven timer created inside Clean.
  • Replace the static CancellationTokenSource field with a static PeriodicTimer field to track the shared timer instance.
  • Update Run() to compute the interval from CacheManager.Options, create a PeriodicTimer under the existing lock, assign it to the static field, and start the background clean task by passing the timer into Clean.
  • Change Stop() to dispose and null out the shared PeriodicTimer under the lock so that timer creation and destruction are mutually exclusive with Run.
  • Rewrite Clean() to accept a PeriodicTimer parameter and loop while WaitForNextTickAsync() returns true, calling DoTask() on each tick and exiting when the timer is disposed.
src/BootstrapBlazor/Dynamic/ChangeDetectionTask.cs
Strengthen and extend unit tests to verify the new timer-based lifecycle and the clean loop behavior with dynamically generated types.
  • Adjust Clean_Ok test to synchronously call Stop, clear _cleanTask, then invoke Run and assert that the static _timer is created and later disposed and nulled by Stop for isolation between tests.
  • Add CleanLoop_Ok test that injects a temporary ConcurrentDictionary cache containing a type from a dynamically-built assembly, runs the Clean loop with a high-frequency PeriodicTimer, and asserts that DoTask removes the dynamic type and the loop exits cleanly when the timer is disposed.
  • Introduce a CreateDynamicType helper using Reflection.Emit to build a type in the BootstrapBlazor_DynamicAssembly assembly, matching the production dynamic-assembly naming used by DataTableDynamicContext.
test/UnitTest/Dynamic/ChangeDetectionCleanTaskTest.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#8165 Refactor and refine the logic of ChangeDetectionCleanTask.Clean (and its scheduling/stop mechanism) to improve how the clean task is started, runs, and is stopped.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@bb-auto bb-auto Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Auto approved by bb-auto

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • In Clean, disposing _timer from Stop while the loop is awaiting WaitForNextTickAsync can result in an unhandled ObjectDisposedException; consider catching this (or using a cancellation token) so the loop exits cleanly rather than faulting _cleanTask.
  • The new CleanLoop_Ok test relies on the private Clean(PeriodicTimer) signature and the use of PeriodicTimer itself, tightly coupling the test to implementation details; consider driving the clean loop via a higher-level trigger (e.g., a cancellation token or a public hook) to make future refactors easier.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `Clean`, disposing `_timer` from `Stop` while the loop is awaiting `WaitForNextTickAsync` can result in an unhandled `ObjectDisposedException`; consider catching this (or using a cancellation token) so the loop exits cleanly rather than faulting `_cleanTask`.
- The new `CleanLoop_Ok` test relies on the private `Clean(PeriodicTimer)` signature and the use of `PeriodicTimer` itself, tightly coupling the test to implementation details; consider driving the clean loop via a higher-level trigger (e.g., a cancellation token or a public hook) to make future refactors easier.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@ArgoZhang ArgoZhang enabled auto-merge (squash) June 26, 2026 00:18
@codecov

codecov Bot commented Jun 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (b31ea32) to head (40ef61d).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #8166   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          766       766           
  Lines        34234     34225    -9     
  Branches      4701      4699    -2     
=========================================
- Hits         34234     34225    -9     
Flag Coverage Δ
BB 100.00% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ArgoZhang ArgoZhang merged commit 2bc2722 into main Jun 26, 2026
7 checks passed
@ArgoZhang ArgoZhang deleted the dev-clean branch June 26, 2026 00:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor(ChangeDetectionCleanTask): refine CleanTask logic

1 participant