Skip to content

Commit

Permalink
chore(test): Added tests for `WindowsFormsSynchronizationContextProvi…
Browse files Browse the repository at this point in the history
…der`
  • Loading branch information
samtrion committed Mar 25, 2024
1 parent 95a7c44 commit e202d5e
Show file tree
Hide file tree
Showing 2 changed files with 381 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

/// <inheritdoc />
[SuppressMessage(
"Usage",
"VSTHRD001:Avoid legacy thread switching APIs",
Justification = "As designed."
)]
internal sealed class WindowsFormsSynchronizationContextProvider
: IWindowsFormsSynchronizationContextProvider
{
internal WindowsFormsSynchronizationContext Context { get; set; } = default!;
internal SynchronizationContext Context { get; set; } = default!;

/// <inheritdoc/>
public void Invoke([NotNull] Action action)
{
ArgumentNullException.ThrowIfNull(action);
ArgumentNullException.ThrowIfNull(Context);

Context.Send(
delegate
Expand All @@ -31,6 +36,7 @@ public void Invoke([NotNull] Action action)
public TResult Invoke<TResult>([NotNull] Func<TResult> action)
{
ArgumentNullException.ThrowIfNull(action);
ArgumentNullException.ThrowIfNull(Context);

TResult result = default!;
Context.Send(
Expand All @@ -40,6 +46,7 @@ public TResult Invoke<TResult>([NotNull] Func<TResult> action)
},
null
);

return result;
}

Expand All @@ -48,6 +55,7 @@ public TResult Invoke<TResult>([NotNull] Func<TResult> action)
public TResult Invoke<TResult, TInput>([NotNull] Func<TInput, TResult> action, TInput input)
{
ArgumentNullException.ThrowIfNull(action);
ArgumentNullException.ThrowIfNull(Context);

TResult result = default!;
Context.Send(
Expand All @@ -57,6 +65,7 @@ public TResult Invoke<TResult, TInput>([NotNull] Func<TInput, TResult> action, T
},
null
);

return result;
}

Expand All @@ -67,6 +76,7 @@ public async ValueTask InvokeAsync(
)
{
ArgumentNullException.ThrowIfNull(action);
ArgumentNullException.ThrowIfNull(Context);

var tcs = new TaskCompletionSource();
Context.Post(
Expand Down Expand Up @@ -95,6 +105,7 @@ public async ValueTask<TResult> InvokeAsync<TResult>(
)
{
ArgumentNullException.ThrowIfNull(action);
ArgumentNullException.ThrowIfNull(Context);

var tcs = new TaskCompletionSource<TResult>();
Context.Post(
Expand All @@ -112,6 +123,7 @@ public async ValueTask<TResult> InvokeAsync<TResult>(
},
tcs
);

return await tcs.Task.WaitAsync(cancellationToken).ConfigureAwait(true);
}

Expand All @@ -123,6 +135,7 @@ public async ValueTask<TResult> InvokeAsync<TResult, TInput>(
)
{
ArgumentNullException.ThrowIfNull(action);
ArgumentNullException.ThrowIfNull(Context);

var tcs = new TaskCompletionSource<TResult>();
Context.Post(
Expand All @@ -140,6 +153,7 @@ public async ValueTask<TResult> InvokeAsync<TResult, TInput>(
},
tcs
);

return await tcs.Task.WaitAsync(cancellationToken).ConfigureAwait(true);
}
}
Loading

0 comments on commit e202d5e

Please sign in to comment.