Use Time Provider to fix time related tests#3077
Merged
Merged
Conversation
…indow closure required.
preardon
approved these changes
May 12, 2024
DevJonny
pushed a commit
to DevJonny/Brighter
that referenced
this pull request
Feb 28, 2026
* Move to using TimeProvider with InMemoryBox to allow easier faking of times in tests. * Add in memory tests back; use Task.Delay to delay, but advance when window closure required.
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.
.NET 8 adds TimeProvider which can be used to provide system time; it also supports FakeTimeProvider which can be used to fake an advancing clock, provide a fake timer etc. There is a backport of the interface and library to netstandard2.0
We have tests that rely on time, specifically where we:
Many of these come together in Paramore.Brighter.InMemory.Tests. We stopped running these because they were flaky in contested CI environments as they relied on delaying action/assertion until time had passed.
With this change we use TimeProvider with our InMemoryBox (used by default if you don't provide an out of process alternative). This allows us to advance time for use with the tests around this functionality. The Advance method to set the clock forward works well, and helps with our tests where we want something to expire.
Note the Delay method doesn’t work as expected where we need to yield to allow another thread to complete. So we continue to use Task.Delay where we need a background thread to run. (It also doesn't seem to play nice with async xunit tests, which seem to spin).