-
-
Notifications
You must be signed in to change notification settings - Fork 14
feat: implement mock PeriodicTimer in testing package
#965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
33 changes: 33 additions & 0 deletions
33
Source/Testably.Abstractions.Testing/TimeSystem/PeriodicTimerFactoryMock.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| #if FEATURE_PERIODIC_TIMER | ||
| using System; | ||
| using System.Threading; | ||
| using Testably.Abstractions.Testing.Helpers; | ||
| using Testably.Abstractions.TimeSystem; | ||
|
|
||
| namespace Testably.Abstractions.Testing.TimeSystem; | ||
|
|
||
| internal sealed class PeriodicTimerFactoryMock : IPeriodicTimerFactory | ||
| { | ||
| private readonly MockTimeSystem _mockTimeSystem; | ||
|
|
||
| internal PeriodicTimerFactoryMock(MockTimeSystem timeSystem) | ||
| { | ||
| _mockTimeSystem = timeSystem; | ||
| } | ||
|
|
||
| #region IPeriodicTimerFactory Members | ||
|
|
||
| /// <inheritdoc cref="ITimeSystemEntity.TimeSystem" /> | ||
| public ITimeSystem TimeSystem => _mockTimeSystem; | ||
|
|
||
| /// <inheritdoc cref="IPeriodicTimerFactory.New(TimeSpan)" /> | ||
| public IPeriodicTimer New(TimeSpan period) | ||
| => new PeriodicTimerMock(_mockTimeSystem, period); | ||
|
|
||
| /// <inheritdoc cref="IPeriodicTimerFactory.Wrap(PeriodicTimer)" /> | ||
| public IPeriodicTimer Wrap(PeriodicTimer timer) | ||
| => throw ExceptionFactory.NotSupportedPeriodicTimerWrapping(); | ||
|
|
||
| #endregion | ||
| } | ||
| #endif |
85 changes: 85 additions & 0 deletions
85
Source/Testably.Abstractions.Testing/TimeSystem/PeriodicTimerMock.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| #if FEATURE_PERIODIC_TIMER | ||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Testably.Abstractions.Testing.Helpers; | ||
| using Testably.Abstractions.TimeSystem; | ||
|
|
||
| namespace Testably.Abstractions.Testing.TimeSystem; | ||
|
|
||
| internal sealed class PeriodicTimerMock : IPeriodicTimer | ||
| { | ||
| private bool _isDisposed; | ||
| private DateTime _lastTime; | ||
| private readonly MockTimeSystem _timeSystem; | ||
|
|
||
| internal PeriodicTimerMock(MockTimeSystem timeSystem, | ||
| TimeSpan period) | ||
| { | ||
| ThrowIfPeriodIsInvalid(period, nameof(period)); | ||
|
|
||
| _timeSystem = timeSystem; | ||
| _lastTime = _timeSystem.DateTime.UtcNow; | ||
| Period = period; | ||
| } | ||
|
|
||
| #region IPeriodicTimer Members | ||
|
|
||
| /// <inheritdoc cref="IPeriodicTimer.Period" /> | ||
| public TimeSpan Period | ||
| { | ||
| get; | ||
| set | ||
| { | ||
| ThrowIfPeriodIsInvalid(value, nameof(value)); | ||
| field = value; | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc cref="ITimeSystemEntity.TimeSystem" /> | ||
| public ITimeSystem TimeSystem => _timeSystem; | ||
|
|
||
| /// <inheritdoc cref="IDisposable.Dispose()" /> | ||
| public void Dispose() | ||
| => _isDisposed = true; | ||
|
|
||
| /// <inheritdoc cref="IPeriodicTimer.WaitForNextTickAsync(CancellationToken)" /> | ||
| public async ValueTask<bool> WaitForNextTickAsync(CancellationToken cancellationToken = new()) | ||
| { | ||
| if (cancellationToken.IsCancellationRequested) | ||
| { | ||
| throw ExceptionFactory.TaskWasCanceled(); | ||
| } | ||
|
|
||
| if (_isDisposed) | ||
| { | ||
| return false; | ||
vbreuss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| DateTime now = _timeSystem.DateTime.UtcNow; | ||
| DateTime nextTime = _lastTime + Period; | ||
| if (nextTime > now) | ||
| { | ||
| _timeSystem.TimeProvider.AdvanceBy(nextTime - now); | ||
| _lastTime = nextTime; | ||
| } | ||
| else | ||
| { | ||
| _lastTime = now; | ||
| } | ||
|
|
||
| await Task.Yield(); | ||
| return true; | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
| private static void ThrowIfPeriodIsInvalid(TimeSpan period, string paramName) | ||
| { | ||
| if (period.TotalMilliseconds < 1 && period != Timeout.InfiniteTimeSpan) | ||
| { | ||
| throw new ArgumentOutOfRangeException(paramName); | ||
| } | ||
| } | ||
| } | ||
| #endif | ||
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
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
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
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
24 changes: 24 additions & 0 deletions
24
Tests/Testably.Abstractions.Testing.Tests/TimeSystem/PeriodicTimerFactoryMockTests.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| #if FEATURE_PERIODIC_TIMER | ||
| using System.Threading; | ||
|
|
||
| namespace Testably.Abstractions.Testing.Tests.TimeSystem; | ||
|
|
||
| public class PeriodicTimerFactoryMockTests | ||
| { | ||
| [Test] | ||
| public async Task Wrap_ShouldThrowNotSupportedException() | ||
| { | ||
| MockTimeSystem timeSystem = new(); | ||
| using PeriodicTimer periodicTimer = new(TimeSpan.FromSeconds(5)); | ||
|
|
||
| void Act() | ||
| { | ||
| // ReSharper disable once AccessToDisposedClosure | ||
| _ = timeSystem.PeriodicTimer.Wrap(periodicTimer); | ||
| } | ||
|
|
||
| await That(Act).Throws<NotSupportedException>().WithMessage( | ||
| "You cannot wrap an existing PeriodicTimer in the MockTimeSystem instance!"); | ||
| } | ||
| } | ||
| #endif |
47 changes: 47 additions & 0 deletions
47
Tests/Testably.Abstractions.Tests/TimeSystem/PeriodicTimerFactoryTests.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| #if FEATURE_PERIODIC_TIMER | ||
| using Testably.Abstractions.TimeSystem; | ||
|
|
||
| namespace Testably.Abstractions.Tests.TimeSystem; | ||
|
|
||
| [TimeSystemTests] | ||
| public class PeriodicTimerFactoryTests(TimeSystemTestData testData) : TimeSystemTestBase(testData) | ||
| { | ||
| [Test] | ||
| public async Task New_PeriodIsInfinite_ShouldNotThrow() | ||
| { | ||
| TimeSpan period = TimeSpan.FromMilliseconds(-1); | ||
|
|
||
| void Act() | ||
| { | ||
| _ = TimeSystem.PeriodicTimer.New(period); | ||
| } | ||
|
|
||
| await That(Act).DoesNotThrow(); | ||
| } | ||
|
|
||
| [Test] | ||
| [Arguments(0)] | ||
| [Arguments(-2)] | ||
| public async Task New_PeriodIsZeroOrNegative_ShouldThrowArgumentOutOfRangeException( | ||
| int milliseconds) | ||
| { | ||
| TimeSpan period = TimeSpan.FromMilliseconds(milliseconds); | ||
|
|
||
| void Act() | ||
| { | ||
| _ = TimeSystem.PeriodicTimer.New(period); | ||
| } | ||
|
|
||
| await That(Act).Throws<ArgumentOutOfRangeException>().WithParamName("period"); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task New_ShouldCreatePeriodicTimerWithGivenPeriod() | ||
| { | ||
| TimeSpan period = TimeSpan.FromSeconds(1); | ||
| using IPeriodicTimer timer = TimeSystem.PeriodicTimer.New(period); | ||
|
|
||
| await That(timer.Period).IsEqualTo(period); | ||
| } | ||
| } | ||
| #endif |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.