Skip to content

Commit

Permalink
refa (WaitTaskWithOptionalTimeoutAsync() -> WaitAndFossilizeTaskWithO…
Browse files Browse the repository at this point in the history
…ptionalTimeoutAsync()): rename these extensions for clarity

    also turn the TaskCompletionSourceExtensions to internal considering that it shouldn't be visible to the outside world
  • Loading branch information
ksidirop-laerdal committed Dec 4, 2024
1 parent 9ef4349 commit 6f4ac6f
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 28 deletions.
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
2 changes: 1 addition & 1 deletion Laerdal.McuMgr/Shared/DeviceResetter/DeviceResetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public async Task ResetAsync(int timeoutInMs = -1)
if (verdict != EDeviceResetterInitializationVerdict.Success)
throw new ArgumentException(verdict.ToString());

await taskCompletionSource.WaitTaskWithOptionalTimeoutAsync(timeoutInMs);
await taskCompletionSource.WaitAndFossilizeTaskWithOptionalTimeoutAsync(timeoutInMs);
}
catch (TimeoutException ex)
{
Expand Down
2 changes: 1 addition & 1 deletion Laerdal.McuMgr/Shared/FileDownloader/FileDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public async Task<byte[]> DownloadAsync(
if (verdict != EFileDownloaderVerdict.Success)
throw new ArgumentException(verdict.ToString());

result = await taskCompletionSource.WaitTaskWithOptionalTimeoutAsync(timeoutForDownloadInMs);
result = await taskCompletionSource.WaitAndFossilizeTaskWithOptionalTimeoutAsync(timeoutForDownloadInMs);
break;
}
catch (TimeoutException ex)
Expand Down
2 changes: 1 addition & 1 deletion Laerdal.McuMgr/Shared/FileUploader/FileUploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ public async Task UploadAsync<TData>(
if (verdict != EFileUploaderVerdict.Success)
throw new ArgumentException(verdict.ToString());

await taskCompletionSource.WaitTaskWithOptionalTimeoutAsync(timeoutForUploadInMs); //order
await taskCompletionSource.WaitAndFossilizeTaskWithOptionalTimeoutAsync(timeoutForUploadInMs); //order
break;
}
catch (TimeoutException ex)
Expand Down
2 changes: 1 addition & 1 deletion Laerdal.McuMgr/Shared/FirmwareEraser/FirmwareEraser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public async Task EraseAsync(int imageIndex = 1, int timeoutInMs = -1)
if (verdict != EFirmwareErasureInitializationVerdict.Success)
throw new ArgumentException(verdict.ToString());

await taskCompletionSource.WaitTaskWithOptionalTimeoutAsync(timeoutInMs);
await taskCompletionSource.WaitAndFossilizeTaskWithOptionalTimeoutAsync(timeoutInMs);
}
catch (TimeoutException ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public async Task InstallAsync(
if (verdict != EFirmwareInstallationVerdict.Success)
throw new ArgumentException(verdict.ToString());

await taskCompletionSource.WaitTaskWithOptionalTimeoutAsync(timeoutInMs);
await taskCompletionSource.WaitAndFossilizeTaskWithOptionalTimeoutAsync(timeoutInMs);
}
catch (TimeoutException ex)
{
Expand Down

0 comments on commit 6f4ac6f

Please sign in to comment.