Skip to content

Time the dirlock lapse tests with a test clock - #4462

Open
yohamta0 wants to merge 2 commits into
mainfrom
fix-dirlock-slow-sweep-clock
Open

yohamta0 wants to merge 2 commits into
mainfrom
fix-dirlock-slow-sweep-clock

Conversation

@yohamta0

@yohamta0 yohamta0 commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Summary

internal/cmn/dirlock TestStaleTakeover_SlowSweep failed on a Windows runner (#4453, Windows 2/2) with "Expected error with "directory is locked by another process" … but got nil" at dirlock_test.go:1014. The test checks that a lock created just after a takeover looks fresh to the next contender. It judged that against real time with a 200 ms threshold, so any pause on a loaded runner between the takeover and the check made the lock look stale. The observer tests were exposed the same way, because they renewed and checked in real time against the same observerStale.

Changes

  • TestStaleTakeover_SlowSweep uses a stopped clock through the dirLock.now hook. Its slow sweep ages whatever lock directory exists while the sweep runs, instead of sleeping past the threshold. This replaces the 1 s threshold that Give each step only the secrets it lists #4511 gave the test, so the threshold goes back to 200 ms.
  • TestObserverSkew, ColdStart, ReadError, FencedHeartbeat, and LegacyHolder share a testClock that the test advances by observerStale/4 between looks, instead of sleeping.
  • TestHeartbeatAfterLapse uses the same testClock instead of its own atomic clock. ColdStart and LegacyHolder both require a conflict through exactly observerStale.
  • TestObserverFrozen and TestObserverRestrictedDir keep real time. Their only timing assertions are a lower bound and a 5 s Eventually, which a slow runner cannot break.
  • Test-only.

Core change

Lock staleness compares l.now() with a directory timestamp or with the time a heartbeat was last seen to change. With both locks on a stopped clock, the new lock's directory, created after the clock stopped, is never older than the threshold, however slow the runner is. To simulate a sweep that takes an hour, the test ages the lock directory that exists while the sweep runs. With the current order (sweep, then create) nothing exists yet, so nothing is aged. If the sweep ran after the new lock was created, it would age that lock and the test would fail. Running the test against that reordered code makes it fail.

internal/cmn/dirlock/dirlock_test.go:

	first.now = stopped
	first.beforeSweep = func() {
		lockPath := filepath.Join(dir, LockDirectoryName)
		if info, err := os.Stat(lockPath); err == nil {
			aged := info.ModTime().Add(-time.Hour)
			require.NoError(t, os.Chtimes(lockPath, aged, aged))
		}
	}
	require.NoError(t, first.TryLock())

	second := New(dir, &LockOptions{StaleThreshold: threshold}).(*dirLock)
	second.now = stopped
	require.ErrorIs(t, second.TryLock(), ErrLockConflict)

Testing

  • go test -race -run 'SlowSweep|Observer|HeartbeatAfterLapse' -count=20 passes. The full package passes.
  • Moving removeAbandonedTempDirs() after createLock makes TestStaleTakeover_SlowSweep fail.
  • GOOS=windows go vet and golangci-lint (plain and GOOS=windows) pass on the package.

Performance

No performance risk: the change is test-only.

Agent

Claude Code, claude-opus-5-5, high

Summary by CodeRabbit

  • Tests
    • Expanded automated coverage for lock conflicts, stale-lock takeover, heartbeat timing, and legacy lock holders using deterministic time controls. These checks verify consistent outcomes across timing-sensitive scenarios.
    • No user-facing behavior changes are included.

@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The dirlock tests now use injected and manually advanced clocks for stale-takeover, observer, and heartbeat-lapse scenarios. The tests check lock conflicts and acquisition at defined points without relying on several wall-clock waits.

Changes: Dirlock test timing

Layer / File(s) Summary
Clock-driven lock tests
internal/cmn/dirlock/dirlock_test.go
The observer test helper accepts an injected clock, and a test clock supports explicit time advancement. The stale-takeover, observer, and heartbeat-lapse tests use these clocks to check lock outcomes at defined intervals.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • dagucloud/dagu#4427: Adds the dirLock.now clock field and uses a shared test clock in TestHeartbeatAfterLapse.
  • dagucloud/dagu#3960: Changes late heartbeat behavior and covers the lapse cases that these tests time.
  • dagucloud/dagu#3961: Changes late lock renewal behavior and discusses the TestHeartbeatAfterLapse cases.
Merge Risk: Minimal · up to b001a

This change only makes the dirlock timing tests deterministic and does not touch production behavior. No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "Time the dirlock lapse tests with a test clock" matches the main change. The PR replaces real-time sleeps and polling in the dirlock lapse tests with a shared, manually advanced test clock.…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description follows the repository template closely. It has Summary, Changes, Core change, Testing, Performance, and Agent sections. Summary explains the Windows CI failure and why real-time check…
Full details: Title check

Explanation

The title "Time the dirlock lapse tests with a test clock" matches the main change. The PR replaces real-time sleeps and polling in the dirlock lapse tests with a shared, manually advanced test clock. The title is specific and concise, and it does not describe anything absent from the changeset. The wording "Time the ... tests" is slightly unusual, but a teammate scanning history can still tell what changed.

Full details: Description check

Explanation

The description follows the repository template closely. It has Summary, Changes, Core change, Testing, Performance, and Agent sections. Summary explains the Windows CI failure and why real-time checks were flaky. Changes lists each test touched. Core change explains the stopped clock mechanism and includes a code excerpt of about 15 lines with file reference. Testing lists the race-enabled package run, the reorder check that makes the test fail, and vet and lint results. Performance states no risk because the change is test-only. Related Issues is absent, but the description cites #4453 and #4511 as context, so the omission is minor. The Checklist section is also absent. Those omissions are non-critical, so the description is mostly complete.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
Fix failing CI checks

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 805bff35-070d-455a-9589-52d764431e0d
Commits

Reviewing files that changed from the base of the PR and between 50ab52a and b001a2d.

Files selected for processing: 1

  • internal/cmn/dirlock/dirlock_test.go

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 3 remain after this review.

@yohamta0 yohamta0 added the windows-ci Run the Windows tests on this pull request, without conformance and E2E label Dec 1, 2026

@yohamta0 yohamta0 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

LGTM

@yohamta0

yohamta0 commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

Note

Claude Opus 5.5 with high responding on behalf of Yota

Both notes are addressed in 245c976:

  • TestHeartbeatAfterLapse now uses testClock (started an hour back with advance(-time.Hour)) instead of its own atomic.Int64 clock.
  • TestObserverColdStart now requires a conflict through exactly observerStale, then takes the lock, like TestObserverLegacyHolder. I also dropped the extra advance that LegacyHolder no longer needed, since requireConflictFor already leaves the clock one step past the threshold.

@greptile-apps

greptile-apps Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5RetriggerView in Greptile

[Low impact] The PR appears safe to merge; the revised tests keep the intended timing and takeover checks.

Summary

Makes directory-lock timing tests independent of pauses on busy runners.

  • The slow-sweep test simulates elapsed time without waiting.
  • Observer and heartbeat-lapse tests advance a clock instead of sleeping.

Reviews (1) · Last reviewed commit: "Reuse the test clock and pin the cold st..." · Reviewed by Greptile

@yohamta0
yohamta0 force-pushed the fix-dirlock-slow-sweep-clock branch 2 times, most recently from dc3138c to bbc8997 Compare December 3, 2026 03:22
TestStaleTakeover_SlowSweep failed on a loaded Windows runner: it
expected a contender to find a just-created lock fresh, but judged
that against real time with a 200 ms threshold, so a pause between
the takeover and the check made the lock look stale. The observer
tests had the same exposure, renewing and checking in real time
against the same threshold.

These tests now read a clock they control through the dirLock clock
hook. The slow sweep is simulated by aging the lock directory that
exists while it runs, which still fails if the sweep ever runs after
the new lock is created. The observer tests move their clock a step
between looks instead of sleeping.
TestHeartbeatAfterLapse kept its own atomic clock, which the shared
testClock now covers. TestObserverColdStart now checks for a conflict
at exactly the stale threshold, as TestObserverLegacyHolder does, so
it fails if a contender takes the lock one step early.
@yohamta0
yohamta0 force-pushed the fix-dirlock-slow-sweep-clock branch from bbc8997 to 49daf42 Compare December 3, 2026 04:25

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

windows-ci Run the Windows tests on this pull request, without conformance and E2E

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant