Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private async Task RandomArrayTimerAsync()
Console.WriteLine(item);
}

Console.WriteLine($"Created {numberOfArrays++} arrays in {stopWatch.Elapsed.Seconds}s");
Console.WriteLine($"Created {numberOfArrays++} arrays in {stopWatch.Elapsed.TotalSeconds:F1}s");
Console.WriteLine(DateTime.Now.ToString("O"));
}
}
Expand All @@ -65,6 +65,7 @@ public async Task StopTaskAsync()
_cancellationToken.Cancel();
await _task;
_cancellationToken.Dispose();
_periodicTimer.Dispose();
Console.WriteLine("The array task was canceled");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
var timerTask = new PeriodicTimerMethods(TimeSpan.FromSeconds(1), numOfElements);
timerTask.Start();

Console.WriteLine("Press any button to terminate the task");
Console.WriteLine("Press any key to stop the task");
Console.ReadKey();

await timerTask.StopTaskAsync();
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Microsoft.Extensions.Time.Testing;

namespace PeriodicTimerCSharpTests;

[TestClass]
public class PeriodicTimerBehaviorTests
{
[TestMethod]
public async Task GivenMissedTicks_WhenWeWaitAgain_ThenTheyCollapseIntoOne()
{
var time = new FakeTimeProvider();
using var timer = new PeriodicTimer(TimeSpan.FromSeconds(1), time);

time.Advance(TimeSpan.FromSeconds(3.5));

Assert.IsTrue(await timer.WaitForNextTickAsync());

var nextTick = timer.WaitForNextTickAsync();
Assert.IsFalse(nextTick.IsCompleted);

time.Advance(TimeSpan.FromSeconds(0.5));
Assert.IsTrue(await nextTick);
}

[TestMethod]
public void GivenAWaitInFlight_WhenWeWaitTwice_ThenTheSecondCallThrows()
{
using var timer = new PeriodicTimer(TimeSpan.FromSeconds(1));

_ = timer.WaitForNextTickAsync();

Assert.ThrowsExactly<InvalidOperationException>(() => timer.WaitForNextTickAsync());
}

[TestMethod]
public async Task GivenAWaitInFlight_WhenWeDisposeTheTimer_ThenTheWaitReturnsFalse()
{
var timer = new PeriodicTimer(TimeSpan.FromSeconds(1));
var tick = timer.WaitForNextTickAsync();

timer.Dispose();

Assert.IsFalse(await tick);
}

[TestMethod]
public void GivenANewPeriod_WhenTimePasses_ThenTheTimerTicksOnTheNewSchedule()
{
var time = new FakeTimeProvider();
using var timer = new PeriodicTimer(TimeSpan.FromSeconds(1), time);

timer.Period = TimeSpan.FromSeconds(5);
var tick = timer.WaitForNextTickAsync();

time.Advance(TimeSpan.FromSeconds(4));
Assert.IsFalse(tick.IsCompleted);

time.Advance(TimeSpan.FromSeconds(1));
Assert.IsTrue(tick.IsCompleted);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,10 +10,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="4.5.2" />
<PackageReference Include="MSTest.TestFramework" Version="4.5.2" />
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="10.11.0" />
<PackageReference Include="coverlet.collector" Version="10.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Loading