Skip to content

Commit

Permalink
refactor: code clean up and mistakes in code docs
Browse files Browse the repository at this point in the history
  • Loading branch information
egil committed Oct 2, 2023
1 parent 216ee47 commit b9fcbcd
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 280 deletions.
22 changes: 11 additions & 11 deletions docs/TimeProviderExtensions.ManualTimeProvider.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ManualTimeProvider : System.TimeProvider
Inheritance [System.Object](https://docs.microsoft.com/en-us/dotnet/api/System.Object 'System.Object') 🡒 [System.TimeProvider](https://docs.microsoft.com/en-us/dotnet/api/System.TimeProvider 'System.TimeProvider') 🡒 ManualTimeProvider
### Remarks
Learn more at [https://github.com/egil/TimeProviderExtensions](https://github.com/egil/TimeProviderExtensions 'https://github.com/egil/TimeProviderExtensions').
Learn more at <a href="https://github.com/egil/TimeProviderExtensions">TimeProviderExtensions on GitHub</a>.
### Constructors

<a name='TimeProviderExtensions.ManualTimeProvider.ManualTimeProvider()'></a>
Expand Down Expand Up @@ -193,7 +193,7 @@ var timer = manualTimeProvider.CreateTimer(
dueTime: Span.FromSecond(1),
period: TimeSpan.FromSecond(1));

manualtTimeProvider.Advance(TimeSpan.FromSecond(3));
manualTimeProvider.Advance(TimeSpan.FromSecond(3));
```
The call to `Advance(TimeSpan.FromSecond(3))` causes the `timer`s callback to be invoked three times,
and the result of the `manualTimeProvider.GetElapsedTime(start)` in the callback call will be <em>1 second</em>, <em>2 seconds</em>,
Expand All @@ -204,7 +204,7 @@ If the desired result is to jump time by [delta](TimeProviderExtensions.ManualTi
the expected number of times, i.e. such that the result of `manualTimeProvider.GetElapsedTime(start)` in the callback is
<em>3 seconds</em>, <em>3 seconds</em>, and <em>3 seconds</em>, use [Jump(DateTimeOffset)](TimeProviderExtensions.ManualTimeProvider.md#TimeProviderExtensions.ManualTimeProvider.Jump(System.DateTimeOffset) 'TimeProviderExtensions.ManualTimeProvider.Jump(System.DateTimeOffset)') or [Jump(TimeSpan)](TimeProviderExtensions.ManualTimeProvider.md#TimeProviderExtensions.ManualTimeProvider.Jump(System.TimeSpan) 'TimeProviderExtensions.ManualTimeProvider.Jump(System.TimeSpan)') instead.

Learn more about this behavior at [https://github.com/egil/TimeProviderExtensions/#difference-between-manualtimeprovider-and-faketimeprovider](https://github.com/egil/TimeProviderExtensions/#difference-between-manualtimeprovider-and-faketimeprovider 'https://github.com/egil/TimeProviderExtensions/#difference-between-manualtimeprovider-and-faketimeprovider').
Learn more about this behavior at <a href="https://github.com/egil/TimeProviderExtensions/#difference-between-manualtimeprovider-and-faketimeprovider">in the documentation</a>.

<a name='TimeProviderExtensions.ManualTimeProvider.CreateManualTimer(System.Threading.TimerCallback,object,TimeProviderExtensions.ManualTimeProvider)'></a>

Expand Down Expand Up @@ -397,17 +397,17 @@ var timer = manualTimeProvider.CreateTimer(
dueTime: Span.FromSecond(1),
period: TimeSpan.FromSecond(1));

manualtTimeProvider.Jump(manualtTimeProvider.Start + TimeSpan.FromSecond(3));
manualTimeProvider.Jump(manualTimeProvider.Start + TimeSpan.FromSecond(3));
```
The call to `Jump(manualtTimeProvider.Start + TimeSpan.FromSecond(3))` causes the `timer`s callback to be invoked three times,
The call to `Jump(manualTimeProvider.Start + TimeSpan.FromSecond(3))` causes the `timer`s callback to be invoked three times,
and the result of the `manualTimeProvider.GetElapsedTime(start)` in the callback call will be <em>3 seconds</em>
during all three invocations.

If the desired result is that timer callbacks happens exactly at their scheduled callback time, i.e. such that the result
of `manualTimeProvider.GetElapsedTime(start)` in the callback will be <em>1 second</em>, <em>2 seconds</em>, and <em>3 seconds</em>,
use [Advance(TimeSpan)](TimeProviderExtensions.ManualTimeProvider.md#TimeProviderExtensions.ManualTimeProvider.Advance(System.TimeSpan) 'TimeProviderExtensions.ManualTimeProvider.Advance(System.TimeSpan)') or [SetUtcNow(DateTimeOffset)](TimeProviderExtensions.ManualTimeProvider.md#TimeProviderExtensions.ManualTimeProvider.SetUtcNow(System.DateTimeOffset) 'TimeProviderExtensions.ManualTimeProvider.SetUtcNow(System.DateTimeOffset)') instead.

Learn more about this behavior at [https://github.com/egil/TimeProviderExtensions/#difference-between-manualtimeprovider-and-faketimeprovider](https://github.com/egil/TimeProviderExtensions/#difference-between-manualtimeprovider-and-faketimeprovider 'https://github.com/egil/TimeProviderExtensions/#difference-between-manualtimeprovider-and-faketimeprovider').
Learn more about this behavior at <a href="https://github.com/egil/TimeProviderExtensions/#difference-between-manualtimeprovider-and-faketimeprovider">in the documentation</a>. ///
<a name='TimeProviderExtensions.ManualTimeProvider.Jump(System.TimeSpan)'></a>

Expand Down Expand Up @@ -452,7 +452,7 @@ var timer = manualTimeProvider.CreateTimer(
dueTime: Span.FromSecond(1),
period: TimeSpan.FromSecond(1));

manualtTimeProvider.Jump(TimeSpan.FromSecond(3));
manualTimeProvider.Jump(TimeSpan.FromSecond(3));
```
The call to `Jump(TimeSpan.FromSecond(3))` causes the `timer`s callback to be invoked three times,
and the result of the `manualTimeProvider.GetElapsedTime(start)` in the callback call will be <em>3 seconds</em>
Expand All @@ -462,7 +462,7 @@ If the desired result is that timer callbacks happens exactly at their scheduled
of `manualTimeProvider.GetElapsedTime(start)` in the callback will be <em>1 second</em>, <em>2 seconds</em>, and <em>3 seconds</em>,
use [Advance(TimeSpan)](TimeProviderExtensions.ManualTimeProvider.md#TimeProviderExtensions.ManualTimeProvider.Advance(System.TimeSpan) 'TimeProviderExtensions.ManualTimeProvider.Advance(System.TimeSpan)') or [SetUtcNow(DateTimeOffset)](TimeProviderExtensions.ManualTimeProvider.md#TimeProviderExtensions.ManualTimeProvider.SetUtcNow(System.DateTimeOffset) 'TimeProviderExtensions.ManualTimeProvider.SetUtcNow(System.DateTimeOffset)') instead.

Learn more about this behavior at [https://github.com/egil/TimeProviderExtensions/#difference-between-manualtimeprovider-and-faketimeprovider](https://github.com/egil/TimeProviderExtensions/#difference-between-manualtimeprovider-and-faketimeprovider 'https://github.com/egil/TimeProviderExtensions/#difference-between-manualtimeprovider-and-faketimeprovider').
Learn more about this behavior at <a href="https://github.com/egil/TimeProviderExtensions/#difference-between-manualtimeprovider-and-faketimeprovider">in the documentation</a>. ///
<a name='TimeProviderExtensions.ManualTimeProvider.SetLocalTimeZone(System.TimeZoneInfo)'></a>

Expand Down Expand Up @@ -525,9 +525,9 @@ var timer = manualTimeProvider.CreateTimer(
dueTime: Span.FromSecond(1),
period: TimeSpan.FromSecond(1));

manualtTimeProvider.SetUtcNow(manualtTimeProvider.Start + TimeSpan.FromSecond(3));
manualTimeProvider.SetUtcNow(manualTimeProvider.Start + TimeSpan.FromSecond(3));
```
The call to `SetUtcNow(manualtTimeProvider.Start + TimeSpan.FromSecond(3))` causes the `timer`s callback to be invoked three times,
The call to `SetUtcNow(manualTimeProvider.Start + TimeSpan.FromSecond(3))` causes the `timer`s callback to be invoked three times,
and the result of the `manualTimeProvider.GetElapsedTime(start)` in the callback call will be <em>1 second</em>, <em>2 seconds</em>,
and <em>3 seconds</em>. In other words, the time of the provider is set before the time callback is invoked
to the time that the callback is scheduled to be invoked at.
Expand All @@ -536,7 +536,7 @@ If the desired result is to jump to the time specified in [value](TimeProviderEx
the expected number of times, i.e. such that the result of `manualTimeProvider.GetElapsedTime(start)` in the callback is
<em>3 seconds</em>, <em>3 seconds</em>, and <em>3 seconds</em>, use [Jump(DateTimeOffset)](TimeProviderExtensions.ManualTimeProvider.md#TimeProviderExtensions.ManualTimeProvider.Jump(System.DateTimeOffset) 'TimeProviderExtensions.ManualTimeProvider.Jump(System.DateTimeOffset)') or [Jump(TimeSpan)](TimeProviderExtensions.ManualTimeProvider.md#TimeProviderExtensions.ManualTimeProvider.Jump(System.TimeSpan) 'TimeProviderExtensions.ManualTimeProvider.Jump(System.TimeSpan)') instead.

Learn more about this behavior at [https://github.com/egil/TimeProviderExtensions/#difference-between-manualtimeprovider-and-faketimeprovider](https://github.com/egil/TimeProviderExtensions/#difference-between-manualtimeprovider-and-faketimeprovider 'https://github.com/egil/TimeProviderExtensions/#difference-between-manualtimeprovider-and-faketimeprovider').
Learn more about this behavior at <a href="https://github.com/egil/TimeProviderExtensions/#difference-between-manualtimeprovider-and-faketimeprovider">in the documentation</a>. ///
<a name='TimeProviderExtensions.ManualTimeProvider.ToString()'></a>

Expand Down
39 changes: 18 additions & 21 deletions src/TimeProviderExtensions/ManualTimeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace TimeProviderExtensions;
/// Represents a synthetic time provider that can be used to enable deterministic behavior in tests.
/// </summary>
/// <remarks>
/// Learn more at <see href="https://github.com/egil/TimeProviderExtensions"/>.
/// Learn more at <a href="https://github.com/egil/TimeProviderExtensions">TimeProviderExtensions on GitHub</a>.
/// </remarks>
[DebuggerDisplay("UtcNow: {ToString(),nq}. Active timers: {ActiveTimers}. {AutoAdvanceBehavior,nq}.")]
public class ManualTimeProvider : TimeProvider
Expand Down Expand Up @@ -232,14 +232,14 @@ public void SetLocalTimeZone(TimeZoneInfo localTimeZone)
/// For example:
/// <code>
/// var start = sut.GetTimestamp();
///
///
/// var timer = manualTimeProvider.CreateTimer(
/// callback: _ => manualTimeProvider.GetElapsedTime(start),
/// state: null,
/// dueTime: Span.FromSecond(1),
/// period: TimeSpan.FromSecond(1));
///
/// manualtTimeProvider.Advance(TimeSpan.FromSecond(3));
///
/// manualTimeProvider.Advance(TimeSpan.FromSecond(3));
/// </code>
/// The call to <c>Advance(TimeSpan.FromSecond(3))</c> causes the <c>timer</c>s callback to be invoked three times,
/// and the result of the <c>manualTimeProvider.GetElapsedTime(start)</c> in the callback call will be <em>1 second</em>, <em>2 seconds</em>,
Expand All @@ -252,7 +252,7 @@ public void SetLocalTimeZone(TimeZoneInfo localTimeZone)
/// <em>3 seconds</em>, <em>3 seconds</em>, and <em>3 seconds</em>, use <see cref="Jump(DateTimeOffset)"/> or <see cref="Jump(TimeSpan)"/> instead.
/// </para>
/// <para>
/// Learn more about this behavior at <see href="https://github.com/egil/TimeProviderExtensions/#difference-between-manualtimeprovider-and-faketimeprovider"/>.
/// Learn more about this behavior at <a href="https://github.com/egil/TimeProviderExtensions/#difference-between-manualtimeprovider-and-faketimeprovider">in the documentation</a>.
/// </para>
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="delta"/> is negative. Going back in time is not supported.</exception>
Expand Down Expand Up @@ -290,16 +290,16 @@ public void Advance(TimeSpan delta)
/// For example:
/// <code>
/// var start = sut.GetTimestamp();
///
///
/// var timer = manualTimeProvider.CreateTimer(
/// callback: _ => manualTimeProvider.GetElapsedTime(start),
/// state: null,
/// dueTime: Span.FromSecond(1),
/// period: TimeSpan.FromSecond(1));
///
/// manualtTimeProvider.SetUtcNow(manualtTimeProvider.Start + TimeSpan.FromSecond(3));
///
/// manualTimeProvider.SetUtcNow(manualTimeProvider.Start + TimeSpan.FromSecond(3));
/// </code>
/// The call to <c>SetUtcNow(manualtTimeProvider.Start + TimeSpan.FromSecond(3))</c> causes the <c>timer</c>s callback to be invoked three times,
/// The call to <c>SetUtcNow(manualTimeProvider.Start + TimeSpan.FromSecond(3))</c> causes the <c>timer</c>s callback to be invoked three times,
/// and the result of the <c>manualTimeProvider.GetElapsedTime(start)</c> in the callback call will be <em>1 second</em>, <em>2 seconds</em>,
/// and <em>3 seconds</em>. In other words, the time of the provider is set before the time callback is invoked
/// to the time that the callback is scheduled to be invoked at.
Expand All @@ -310,8 +310,7 @@ public void Advance(TimeSpan delta)
/// <em>3 seconds</em>, <em>3 seconds</em>, and <em>3 seconds</em>, use <see cref="Jump(DateTimeOffset)"/> or <see cref="Jump(TimeSpan)"/> instead.
/// </para>
/// <para>
/// Learn more about this behavior at <see href="https://github.com/egil/TimeProviderExtensions/#difference-between-manualtimeprovider-and-faketimeprovider"/>.
/// </para>
/// Learn more about this behavior at <a href="https://github.com/egil/TimeProviderExtensions/#difference-between-manualtimeprovider-and-faketimeprovider">in the documentation</a>. /// </para>
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is less than the value returned by <see cref="GetUtcNow()"/>. Going back in time is not supported.</exception>
public void SetUtcNow(DateTimeOffset value)
Expand Down Expand Up @@ -376,14 +375,14 @@ public void SetUtcNow(DateTimeOffset value)
/// For example:
/// <code>
/// var start = sut.GetTimestamp();
///
///
/// var timer = manualTimeProvider.CreateTimer(
/// callback: _ => manualTimeProvider.GetElapsedTime(start),
/// state: null,
/// dueTime: Span.FromSecond(1),
/// period: TimeSpan.FromSecond(1));
///
/// manualtTimeProvider.Jump(TimeSpan.FromSecond(3));
///
/// manualTimeProvider.Jump(TimeSpan.FromSecond(3));
/// </code>
/// The call to <c>Jump(TimeSpan.FromSecond(3))</c> causes the <c>timer</c>s callback to be invoked three times,
/// and the result of the <c>manualTimeProvider.GetElapsedTime(start)</c> in the callback call will be <em>3 seconds</em>
Expand All @@ -395,8 +394,7 @@ public void SetUtcNow(DateTimeOffset value)
/// use <see cref="Advance(TimeSpan)"/> or <see cref="SetUtcNow(DateTimeOffset)"/> instead.
/// </para>
/// <para>
/// Learn more about this behavior at <see href="https://github.com/egil/TimeProviderExtensions/#difference-between-manualtimeprovider-and-faketimeprovider"/>.
/// </para>
/// Learn more about this behavior at <a href="https://github.com/egil/TimeProviderExtensions/#difference-between-manualtimeprovider-and-faketimeprovider">in the documentation</a>. /// </para>
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="delta"/> is negative. Going back in time is not supported.</exception>
public void Jump(TimeSpan delta)
Expand Down Expand Up @@ -433,16 +431,16 @@ public void Jump(TimeSpan delta)
/// For example:
/// <code>
/// var start = sut.GetTimestamp();
///
///
/// var timer = manualTimeProvider.CreateTimer(
/// callback: _ => manualTimeProvider.GetElapsedTime(start),
/// state: null,
/// dueTime: Span.FromSecond(1),
/// period: TimeSpan.FromSecond(1));
///
/// manualtTimeProvider.Jump(manualtTimeProvider.Start + TimeSpan.FromSecond(3));
/// manualTimeProvider.Jump(manualTimeProvider.Start + TimeSpan.FromSecond(3));
/// </code>
/// The call to <c>Jump(manualtTimeProvider.Start + TimeSpan.FromSecond(3))</c> causes the <c>timer</c>s callback to be invoked three times,
/// The call to <c>Jump(manualTimeProvider.Start + TimeSpan.FromSecond(3))</c> causes the <c>timer</c>s callback to be invoked three times,
/// and the result of the <c>manualTimeProvider.GetElapsedTime(start)</c> in the callback call will be <em>3 seconds</em>
/// during all three invocations.
/// </para>
Expand All @@ -452,8 +450,7 @@ public void Jump(TimeSpan delta)
/// use <see cref="Advance(TimeSpan)"/> or <see cref="SetUtcNow(DateTimeOffset)"/> instead.
/// </para>
/// <para>
/// Learn more about this behavior at <see href="https://github.com/egil/TimeProviderExtensions/#difference-between-manualtimeprovider-and-faketimeprovider"/>.
/// </para>
/// Learn more about this behavior at <a href="https://github.com/egil/TimeProviderExtensions/#difference-between-manualtimeprovider-and-faketimeprovider">in the documentation</a>. /// </para>
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is less than the value returned by <see cref="GetUtcNow()"/>. Going back in time is not supported.</exception>
public void Jump(DateTimeOffset value)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#if !NET6_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;

namespace System.Runtime.CompilerServices;

[ExcludeFromCodeCoverage]
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
internal sealed class CallerArgumentExpressionAttribute : Attribute
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#if NETSTANDARD2_0
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel;

using System.Diagnostics.CodeAnalysis;

namespace System.Runtime.CompilerServices
{
/// <summary>
/// Reserved to be used by the compiler for tracking metadata.
/// This class should not be used by developers in source code.
/// This dummy class is required to compile records when targeting .NET Standard
/// </summary>
[ExcludeFromCodeCoverage]
[EditorBrowsable(EditorBrowsableState.Never)]
internal static class IsExternalInit
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace TimeProviderExtensions;
/// may be in flight at any given moment. <see cref="Dispose"/> may be used concurrently with an active <see cref="WaitForNextTickAsync"/>
/// to interrupt it and cause it to return false.
/// </remarks>
[ExcludeFromCodeCoverage]
internal sealed class PeriodicTimerPort : IDisposable
{
internal const uint MaxSupportedTimeout = 0xfffffffe;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if NET6_0_OR_GREATER && !NET8_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
using TimeProviderExtensions;

namespace System.Threading;
Expand All @@ -14,6 +15,7 @@ namespace System.Threading;
/// to interrupt it and cause it to return false.
/// </para>
/// </remarks>
[ExcludeFromCodeCoverage]
public abstract class PeriodicTimerWrapper : IDisposable
{
/// <summary>Wait for the next tick of the timer, or for the timer to be stopped.</summary>
Expand Down
10 changes: 5 additions & 5 deletions test/TimeProviderExtensions.Tests/ManualTimeProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ public async Task Callbacks_happens_in_schedule_order()
periodicTimer.Dispose();
await callbacksTask;

async Task AsyncCallbacks(PeriodicTimer periodicTimer)
async Task AsyncCallbacks(PeriodicTimer timer)
{
while (await periodicTimer.WaitForNextTickAsync().ConfigureAwait(false))
while (await timer.WaitForNextTickAsync().ConfigureAwait(false))
{
callbacks.Add(sut.GetUtcNow());
await sut.Delay(TimeSpan.FromSeconds(3));
Expand Down Expand Up @@ -292,7 +292,7 @@ public void ActiveTimers_with_active_timers()
{
var sut = new ManualTimeProvider();

var timer = sut.CreateTimer(_ => { }, null, 1.Seconds(), Timeout.InfiniteTimeSpan);
using var timer = sut.CreateTimer(_ => { }, null, 1.Seconds(), Timeout.InfiniteTimeSpan);

sut.ActiveTimers.Should().Be(1);
}
Expand All @@ -302,7 +302,7 @@ public void ActiveTimers_with_inactive_timers()
{
var sut = new ManualTimeProvider();

var timer = sut.CreateTimer(_ => { }, null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);
using var timer = sut.CreateTimer(_ => { }, null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);

sut.ActiveTimers.Should().Be(0);
}
Expand All @@ -312,7 +312,7 @@ public void ActiveTimers_with_after_timer_state_change()
{
var sut = new ManualTimeProvider();

var timer = sut.CreateTimer(_ => { }, null, 1.Seconds(), Timeout.InfiniteTimeSpan);
using var timer = sut.CreateTimer(_ => { }, null, 1.Seconds(), Timeout.InfiniteTimeSpan);
sut.Advance(1.Seconds());

sut.ActiveTimers.Should().Be(0);
Expand Down
Loading

0 comments on commit b9fcbcd

Please sign in to comment.