Standardise the use of TimeProvider across the error instance - #5843
Open
warwickschroeder wants to merge 9 commits into
Open
Standardise the use of TimeProvider across the error instance#5843warwickschroeder wants to merge 9 commits into
warwickschroeder wants to merge 9 commits into
Conversation
…rather than relying on other registrations such as YARP or AddHttpLogging.
…it to be properly tested.
…managers it creates. Keeps time management across archive functionality consistent.
…of certain time sensitive features
warwickschroeder
commented
Sep 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Every time-sensitive decision now reads an injected clock
Timestamps across the error instance came from
DateTime.UtcNow, so nothing time-dependent could be tested. They now come from a constructor-injectedTimeProvider. Production getsTimeProvider.Systemand behaves identically; tests get a fake clock they can advance.What is wrong today
BasePersistenceusedAddSingleton, and the container returns the last registration. A host-supplied clock was silently replaced, no error.AdvanceClockwas an empty method,UtcNowreturned the real one.InMemoryArchive.CompleteandInMemoryUnarchive.Complete(CompletionTimeandLast),MessageRedirectsController.NewRedirects(the redirect'sLastModifiedand the retry request'sPeriodTo), andLicensingDataStore, where three reads built one date range so a call straddling midnight got afromandtofrom different days.AddServiceControl, which is why it is the right place to prove the persister supplies its own clock. It is also where NServiceBus'sCriticalErrorgoes missing, since that is registered only insideAddServiceControlwhile the EF persister'sExternalIntegrationRequestsDataStoreasks for it in its constructor. So--maintenanceon SQL Server or PostgreSQL exits with an unresolved-service error instead of starting. RavenDB returns early from its own registration in maintenance mode, so it never reaches the equivalent class. Not a regression: it fails the same way onmaster.What it looks like afterwards
AddServiceControlregistersTimeProvider.SystemwithTryAddSingleton, so a host that supplies its own keeps it.--maintenancehas readRun RavenDB only - use for DB maintenanceall along.IPersistenceConfigurationgainedSupportsMaintenanceMode, true on RavenDB and false on the EF persisters, andPersistenceFactoryturns the request down with a sentence naming the configured persister rather than letting the container throw.ExpirationManagercomputes@expiresfrom it;ArchiveDocumentManager,UnarchiveDocumentManager,ArchivingManagerandUnarchivingManagertake it, withMessageArchiverpassing its own down;LicensingDataStorethroughput windows use it.FailedMessageLifecycleDataStorestampsStatusChangedAtandLastModifiedfrom it across all five transitions;EFCoreArchivingManagerandEFCoreUnarchivingManagerpass it to the operation summaries.MessageRedirectsControllerstampsLastModifiedon create and update;FailureGroupsRetryControllertakes the operation start time from it, so the message sent and the operation opened agree;RetriesGatewaybulk request types receive their start time instead of callingDateTime.UtcNowin a base constructor argument where nothing could reach it;RetryingManager,InMemoryRetryandRetryAllInGroupHandlertake it.CheckRavenDBIndexLagsubtractsLastIndexingTime, which the Raven server reports against its own clock, so an injected one would make the lag meaningless. Comment added so nobody "finishes the job".Test coverage
AddServiceControl, which is what catches theAddSingletonoverride. On RavenDB,StartupModeTests.CanRunMaintenanceModenow also asserts that the maintenance host resolves a clock of its own. On SQL Server and PostgreSQL, a newMaintenanceModeTestsasserts the refusal instead, and is excluded from the RavenDB project the same wayWhen_hosting_error_ingestion_onlyalready is.TryAddSingletonrespects it. Caveat in the code: it moves only what the persister stamps, since the Raven server expires on its own clock.PersistenceTestBaseno longer registers a clock, now that each backend context supplies one.