Skip to content

Give the failing-renewals lease test room for late timers - #1064

Merged
KrzysztofPajak merged 1 commit into
developfrom
fix/flaky-lease-renewal-test
Nov 18, 2026
Merged

KrzysztofPajak merged 1 commit into
developfrom
fix/flaky-lease-renewal-test

Conversation

@KrzysztofPajak

Copy link
Copy Markdown
Member

Type: bugfix

Issue

BackgroundServiceTaskTests.ExecuteAsync_RenewalsKeepFailing_CancelsTheRunBeforeTheLeaseRunsOut fails now and then on CI. It failed on #1063, while the second CI workflow on the same commit passed:

cancelled at 2026-11-17T21:00:59.3005164Z, lease until 2026-11-17T21:00:59.3001925Z
Assert.IsTrue(cancelledUtc < leasedUntilUtc)

The test used a 200 ms renewal interval and a 1 s lease. When renewals keep failing, RenewLease stops the run once the next renewal would come too late, which is one interval before the lease runs out. That leaves 200 ms of slack, shared by four Task.Delay(200 ms) calls. On a busy runner those calls ran over 200 ms late in total, and the cancellation landed 0.3 ms after the lease ended.

Production uses a 1 min interval and a 5 min lease, so this is a test timing problem, not a scheduler bug.

Solution

The test now uses a 500 ms interval and a 2 s lease. The run is still expected to stop before the lease ends, but the check now has 500 ms of slack over three delays. The test takes about 1.5 s instead of about 0.8 s. WaitFor allows about 3 s, which is enough. The scheduler code is unchanged.

Breaking changes

None.

Testing

  1. dotnet test src/Tests/Grand.Web.Common.Tests --filter "FullyQualifiedName~BackgroundServiceTaskTests": 11/11, run five times in a row locally.
  2. The test still fails if the run is not stopped before the lease ends, because cancelledUtc < leasedUntilUtc is unchanged.

The run stops one renewal interval before its lease runs out. With a 200 ms
interval, four late Task.Delay calls on a busy CI runner were enough to push
the cancellation past the lease, and the test failed. A 500 ms interval with
a 2 s lease leaves the same check 500 ms of slack.
Copilot AI balanced review requested due to automatic review settings November 18, 2026 06:53

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@KrzysztofPajak
KrzysztofPajak merged commit 1ba01e8 into develop Nov 18, 2026
9 checks passed
@KrzysztofPajak
KrzysztofPajak deleted the fix/flaky-lease-renewal-test branch November 18, 2026 16:25
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.

2 participants