Skip to content
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

[MAN-454] (b) Refine task-completion-source extensions and their usages #165

Merged
merged 8 commits into from
Dec 4, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public async Task SingleFileDownloadAsync_ShouldThrowAllDownloadAttemptsFailedEx

var mockedNativeFileDownloaderProxy = new MockedErroneousNativeFileDownloaderProxySpy13(
mockedFileData: mockedFileData,
downloaderCallbacksProxy: new GenericNativeFileDownloaderCallbacksProxy_(),
rogueNativeErrorMessage: nativeRogueErrorMessage
rogueNativeErrorMessage: nativeRogueErrorMessage,
downloaderCallbacksProxy: new GenericNativeFileDownloaderCallbacksProxy_()
);
var fileDownloader = new McuMgr.FileDownloader.FileDownloader(mockedNativeFileDownloaderProxy);

Expand All @@ -44,8 +44,7 @@ public async Task SingleFileDownloadAsync_ShouldThrowAllDownloadAttemptsFailedEx
));

// Assert
await work.Should()
.ThrowWithinAsync<AllDownloadAttemptsFailedException>(3.Seconds());
await work.Should().ThrowWithinAsync<AllDownloadAttemptsFailedException>(3.Seconds());

mockedNativeFileDownloaderProxy.CancelCalled.Should().BeFalse();
mockedNativeFileDownloaderProxy.DisconnectCalled.Should().BeFalse(); //00
Expand Down
8 changes: 4 additions & 4 deletions Laerdal.McuMgr.Tests/Laerdal.McuMgr.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="7.0.0-alpha.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="FluentAssertions" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />

<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
4 changes: 4 additions & 0 deletions Laerdal.McuMgr.slnx.DotSettings.user
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=5074b48a_002Ddd85_002D447d_002Db5d1_002D16b7a75eed14/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from Solution" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Solution /&gt;&#xD;
&lt;/SessionState&gt;</s:String>
<s:Boolean x:Key="/Default/UnloadedProject/UnloadedProjects/=131e5d65_002D002c_002D354a_002D58d9_002D743f2733d18b_0023Laerdal_002EMcuMgr_002EBindings_002EAndroid/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UnloadedProject/UnloadedProjects/=1eebca17_002Dfd57_002Db6cc_002Dece8_002D1a5895cd083f_0023Laerdal_002EMcuMgr_002EBindings_002EMacCatalyst/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UnloadedProject/UnloadedProjects/=962e09e8_002D0e53_002D1451_002D63b3_002D13d08deac464_0023Laerdal_002EMcuMgr_002EBindings_002ENetStandard/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UnloadedProject/UnloadedProjects/=b3bb4f82_002D8427_002D49d7_002D5e69_002De0f8ffbb8be7_0023Laerdal_002EMcuMgr_002EBindings_002EiOS/@EntryIndexedValue">True</s:Boolean>



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Laerdal.McuMgr.Common.Helpers
/// Task is not actually in a "scheduler" (since TCS tasks are <see href="https://blog.stephencleary.com/2015/04/a-tour-of-task-part-10-promise-tasks.html">Promise Tasks</see>)
/// never completing tasks, of any type, is <see href="https://devblogs.microsoft.com/pfxteam/dont-forget-to-complete-your-tasks/">generally considered a bug</see>.
/// </summary>
public static class TaskCompletionSourceExtensions
static internal class TaskCompletionSourceExtensions
{
/// <summary>
/// Sets up a timeout-monitor on the given task. This is essentially a wrapper around <see cref="System.Threading.Tasks.Task.WaitAsync(TimeSpan)"/>
Expand All @@ -35,7 +35,7 @@ public static class TaskCompletionSourceExtensions
/// try
/// {
/// PropertyChanged += MyEventHandler_;
/// await tcs.WaitTaskWithOptionalTimeoutAsync(timeout);
/// await tcs.WaitAndFossilizeTaskWithOptionalTimeoutAsync(timeout);
/// }
/// finally
/// {
Expand All @@ -60,11 +60,11 @@ public static class TaskCompletionSourceExtensions
/// }
/// }
/// </code>
public static Task WaitTaskWithOptionalTimeoutAsync(this TaskCompletionSource tcs, int timeoutInMilliseconds, CancellationToken cancellationToken = default)
public static Task WaitAndFossilizeTaskWithOptionalTimeoutAsync(this TaskCompletionSource tcs, int timeoutInMilliseconds, CancellationToken cancellationToken = default)
{
return timeoutInMilliseconds <= 0
? tcs.Task
: tcs.WaitTaskWithTimeoutAsync(TimeSpan.FromMilliseconds(timeoutInMilliseconds), cancellationToken);
: tcs.WaitAndFossilizeTaskWithTimeoutAsync(TimeSpan.FromMilliseconds(timeoutInMilliseconds), cancellationToken);
}

/// <summary>
Expand All @@ -88,7 +88,7 @@ public static Task WaitTaskWithOptionalTimeoutAsync(this TaskCompletionSource tc
/// try
/// {
/// PropertyChanged += MyEventHandler_;
/// await tcs.WaitTaskWithOptionalTimeoutAsync(timeout);
/// await tcs.WaitAndFossilizeTaskWithOptionalTimeoutAsync(timeout);
/// }
/// finally
/// {
Expand All @@ -113,11 +113,11 @@ public static Task WaitTaskWithOptionalTimeoutAsync(this TaskCompletionSource tc
/// }
/// }
/// </code>
public static Task WaitTaskWithOptionalTimeoutAsync(this TaskCompletionSource tcs, TimeSpan timespan, CancellationToken cancellationToken = default)
public static Task WaitAndFossilizeTaskWithOptionalTimeoutAsync(this TaskCompletionSource tcs, TimeSpan timespan, CancellationToken cancellationToken = default)
{
return timespan <= TimeSpan.Zero
? tcs.Task
: tcs.WaitTaskWithTimeoutAsync(timespan, cancellationToken);
: tcs.WaitAndFossilizeTaskWithTimeoutAsync(timespan, cancellationToken);
}

/// <summary>
Expand All @@ -142,7 +142,7 @@ public static Task WaitTaskWithOptionalTimeoutAsync(this TaskCompletionSource tc
/// try
/// {
/// PropertyChanged += MyEventHandler_;
/// await tcs.WaitTaskWithOptionalTimeoutAsync(timeout);
/// await tcs.WaitAndFossilizeTaskWithOptionalTimeoutAsync(timeout);
/// }
/// finally
/// {
Expand All @@ -167,9 +167,9 @@ public static Task WaitTaskWithOptionalTimeoutAsync(this TaskCompletionSource tc
/// }
/// }
/// </code>
public static Task WaitTaskWithTimeoutAsync(this TaskCompletionSource tcs, int timeoutInMilliseconds, CancellationToken cancellationToken = default)
public static Task WaitAndFossilizeTaskWithTimeoutAsync(this TaskCompletionSource tcs, int timeoutInMilliseconds, CancellationToken cancellationToken = default)
{
return tcs.WaitTaskWithTimeoutAsync(TimeSpan.FromMilliseconds(timeoutInMilliseconds), cancellationToken);
return tcs.WaitAndFossilizeTaskWithTimeoutAsync(TimeSpan.FromMilliseconds(timeoutInMilliseconds), cancellationToken);
}

/// <summary>
Expand All @@ -194,7 +194,7 @@ public static Task WaitTaskWithTimeoutAsync(this TaskCompletionSource tcs, int t
/// try
/// {
/// PropertyChanged += MyEventHandler_;
/// await tcs.WaitTaskWithTimeoutAsync(timeout);
/// await tcs.WaitAndFossilizeTaskWithTimeoutAsync(timeout);
/// }
/// finally
/// {
Expand All @@ -219,7 +219,7 @@ public static Task WaitTaskWithTimeoutAsync(this TaskCompletionSource tcs, int t
/// }
/// }
/// </code>
public async static Task WaitTaskWithTimeoutAsync(this TaskCompletionSource tcs, TimeSpan timespan, CancellationToken cancellationToken = default)
public async static Task WaitAndFossilizeTaskWithTimeoutAsync(this TaskCompletionSource tcs, TimeSpan timespan, CancellationToken cancellationToken = default)
{
if (timespan < TimeSpan.Zero) //note that this deviates from the behaviour of .WaitAsync() which does accept -1 milliseconds which means "wait forever"
{
Expand Down Expand Up @@ -279,7 +279,7 @@ public async static Task WaitTaskWithTimeoutAsync(this TaskCompletionSource tcs,
/// try
/// {
/// PropertyChanged += MyEventHandler_;
/// await tcs.WaitTaskWithTimeoutAsync&lt;int&gt;(timeout);
/// await tcs.WaitAndFossilizeTaskWithTimeoutAsync&lt;int&gt;(timeout);
/// }
/// finally
/// {
Expand All @@ -304,11 +304,11 @@ public async static Task WaitTaskWithTimeoutAsync(this TaskCompletionSource tcs,
/// }
/// }
/// </code>
public static Task<T> WaitTaskWithOptionalTimeoutAsync<T>(this TaskCompletionSource<T> tcs, int timeoutInMilliseconds, CancellationToken cancellationToken = default)
public static Task<T> WaitAndFossilizeTaskWithOptionalTimeoutAsync<T>(this TaskCompletionSource<T> tcs, int timeoutInMilliseconds, CancellationToken cancellationToken = default)
{
return timeoutInMilliseconds <= 0
? tcs.Task
: tcs.WaitTaskWithTimeoutAsync(TimeSpan.FromMilliseconds(timeoutInMilliseconds), cancellationToken);
: tcs.WaitAndFossilizeTaskWithTimeoutAsync(TimeSpan.FromMilliseconds(timeoutInMilliseconds), cancellationToken);
}

/// <summary>
Expand All @@ -332,7 +332,7 @@ public static Task<T> WaitTaskWithOptionalTimeoutAsync<T>(this TaskCompletionSou
/// try
/// {
/// PropertyChanged += MyEventHandler_;
/// await tcs.WaitTaskWithOptionalTimeoutAsync&lt;int&gt;(timeout);
/// await tcs.WaitAndFossilizeTaskWithOptionalTimeoutAsync&lt;int&gt;(timeout);
/// }
/// finally
/// {
Expand All @@ -357,11 +357,11 @@ public static Task<T> WaitTaskWithOptionalTimeoutAsync<T>(this TaskCompletionSou
/// }
/// }
/// </code>
public static Task<T> WaitTaskWithOptionalTimeoutAsync<T>(this TaskCompletionSource<T> tcs, TimeSpan timespan, CancellationToken cancellationToken = default)
public static Task<T> WaitAndFossilizeTaskWithOptionalTimeoutAsync<T>(this TaskCompletionSource<T> tcs, TimeSpan timespan, CancellationToken cancellationToken = default)
{
return timespan <= TimeSpan.Zero
? tcs.Task
: tcs.WaitTaskWithTimeoutAsync(timespan, cancellationToken);
: tcs.WaitAndFossilizeTaskWithTimeoutAsync(timespan, cancellationToken);
}

/// <summary>
Expand All @@ -385,7 +385,7 @@ public static Task<T> WaitTaskWithOptionalTimeoutAsync<T>(this TaskCompletionSou
/// try
/// {
/// PropertyChanged += MyEventHandler_;
/// await tcs.WaitTaskWithTimeoutAsync&lt;int&gt;(timeout);
/// await tcs.WaitAndFossilizeTaskWithTimeoutAsync&lt;int&gt;(timeout);
/// }
/// finally
/// {
Expand All @@ -410,9 +410,9 @@ public static Task<T> WaitTaskWithOptionalTimeoutAsync<T>(this TaskCompletionSou
/// }
/// }
/// </code>
public static Task<T> WaitTaskWithTimeoutAsync<T>(this TaskCompletionSource<T> tcs, int timeoutInMilliseconds, CancellationToken cancellationToken = default)
public static Task<T> WaitAndFossilizeTaskWithTimeoutAsync<T>(this TaskCompletionSource<T> tcs, int timeoutInMilliseconds, CancellationToken cancellationToken = default)
{
return tcs.WaitTaskWithTimeoutAsync(TimeSpan.FromMilliseconds(timeoutInMilliseconds), cancellationToken);
return tcs.WaitAndFossilizeTaskWithTimeoutAsync(TimeSpan.FromMilliseconds(timeoutInMilliseconds), cancellationToken);
}

/// <summary>
Expand All @@ -436,7 +436,7 @@ public static Task<T> WaitTaskWithTimeoutAsync<T>(this TaskCompletionSource<T> t
/// try
/// {
/// PropertyChanged += MyEventHandler_;
/// await tcs.WaitTaskWithTimeoutAsync&lt;int&gt;(timeout);
/// await tcs.WaitAndFossilizeTaskWithTimeoutAsync&lt;int&gt;(timeout);
/// }
/// finally
/// {
Expand All @@ -461,7 +461,7 @@ public static Task<T> WaitTaskWithTimeoutAsync<T>(this TaskCompletionSource<T> t
/// }
/// }
/// </code>
public async static Task<T> WaitTaskWithTimeoutAsync<T>(this TaskCompletionSource<T> tcs, TimeSpan timespan, CancellationToken cancellationToken = default)
public async static Task<T> WaitAndFossilizeTaskWithTimeoutAsync<T>(this TaskCompletionSource<T> tcs, TimeSpan timespan, CancellationToken cancellationToken = default)
{
if (timespan < TimeSpan.Zero) //note that this deviates from the behaviour of .WaitAsync() which does accept -1 milliseconds which means "wait forever"
{
Expand Down
Loading
Loading