From cdf6d14c7404f43af56254f3308bc377c7db5143 Mon Sep 17 00:00:00 2001 From: jerviscui Date: Wed, 12 Jun 2024 16:27:21 +0800 Subject: [PATCH] Add TaskRunTest --- src/.editorconfig | 17 ++- .../ThreadingTest/CancellationTokenTest.cs | 19 ++-- src/Tests/ThreadingTest/Program.cs | 70 ++++++------ src/Tests/ThreadingTest/TaskRunTest.cs | 100 ++++++++++++++++++ 4 files changed, 160 insertions(+), 46 deletions(-) create mode 100644 src/Tests/ThreadingTest/TaskRunTest.cs diff --git a/src/.editorconfig b/src/.editorconfig index 8ae06b4..e11f614 100644 --- a/src/.editorconfig +++ b/src/.editorconfig @@ -685,6 +685,12 @@ dotnet_diagnostic.CS8825.severity = error # CS8847 - switch 表达式不处理某些空输入(它并不详尽)。但是,带有“when”子句的模式可能会成功匹配该值。 dotnet_diagnostic.CS8847.severity = error +#### performance warnings #### +# https://learn.microsoft.com/zh-cn/dotnet/fundamentals/code-analysis/quality-rules/performance-warnings + +# CA1848: Use the LoggerMessage delegates +dotnet_diagnostic.CA1848.severity = warning + #### compile warning #### # CS0162: 检测到无法访问的代码 @@ -761,7 +767,7 @@ dotnet_diagnostic.S1172.severity = none dotnet_diagnostic.S125.severity = none # S1481: Unused local variables should be removed -dotnet_diagnostic.S1481.severity = none +# dotnet_diagnostic.S1481.severity = none # S1854: Unused assignments should be removed dotnet_diagnostic.S1854.severity = none @@ -834,6 +840,9 @@ dotnet_diagnostic.S2583.severity = error # S2589: Boolean expressions should not be gratuitous dotnet_diagnostic.S2589.severity = error +# S2696: Instance members should not write to "static" fields +dotnet_diagnostic.S2696.severity = error + # S2930: "IDisposables" should be disposed dotnet_diagnostic.S2930.severity = error @@ -847,11 +856,11 @@ dotnet_diagnostic.S3400.severity = error # S3459: Unassigned members should be removed dotnet_diagnostic.S3459.severity = error +# S3903: Types should be defined in named namespaces +dotnet_diagnostic.S3903.severity = none + # S3923: All branches in a conditional structure should not have exactly the same implementation dotnet_diagnostic.S3923.severity = error # S4158: Empty collections should not be accessed or iterated dotnet_diagnostic.S4158.severity = error - -# S2696: Instance members should not write to "static" fields -dotnet_diagnostic.S2696.severity = error diff --git a/src/Tests/ThreadingTest/CancellationTokenTest.cs b/src/Tests/ThreadingTest/CancellationTokenTest.cs index aea384d..ced4ae1 100644 --- a/src/Tests/ThreadingTest/CancellationTokenTest.cs +++ b/src/Tests/ThreadingTest/CancellationTokenTest.cs @@ -5,19 +5,26 @@ namespace ThreadingTest; internal sealed class CancellationTokenTest { + + #region Constants & Statics + public static void Cancel_Test() { - var cts = new CancellationTokenSource(); + using var cts = new CancellationTokenSource(); Console.WriteLine($"start: {Environment.CurrentManagedThreadId}"); - cts.Token.Register(() => - { - Console.WriteLine($"callback: {Environment.CurrentManagedThreadId}"); - Thread.Sleep(5000); - }); + _ = cts.Token + .Register(() => + { + Console.WriteLine($"callback: {Environment.CurrentManagedThreadId}"); + Thread.Sleep(5000); + }); Console.WriteLine($"Canceling: {Environment.CurrentManagedThreadId}"); cts.Cancel(); Console.WriteLine($"Canceled: {Environment.CurrentManagedThreadId}"); } + + #endregion + } diff --git a/src/Tests/ThreadingTest/Program.cs b/src/Tests/ThreadingTest/Program.cs index ae0331d..1deb299 100644 --- a/src/Tests/ThreadingTest/Program.cs +++ b/src/Tests/ThreadingTest/Program.cs @@ -1,52 +1,50 @@ using System; using System.Threading; using System.Threading.Tasks; +using ThreadingTest; -namespace ThreadingTest -{ - internal sealed class Program - { - private static async Task Main(string[] args) - { - //TaskDelayTest.Test(); +// TaskDelayTest.Test(); + +// TaskDelayTest.Exception_Test(); +// TaskDelayTest.Exception_Catch_Test(); - //TaskDelayTest.Exception_Test(); - //TaskDelayTest.Exception_Catch_Test(); +// ReaderWriterLockTest.ReaderWriterLock_Test(); +// ReaderWriterLockTest.UpgradeToWriterLock_Test(); +// ReaderWriterLockTest.ReleaseLock_Test(); - //ReaderWriterLockTest.ReaderWriterLock_Test(); - //ReaderWriterLockTest.UpgradeToWriterLock_Test(); - //ReaderWriterLockTest.ReleaseLock_Test(); +// ReaderWriterLockSlimTest.TryEnterReadLock_Test(); +// ReaderWriterLockSlimTest.EnterUpgradeableReadLock_WhenHasRead_Test(); +// ReaderWriterLockSlimTest.EnterUpgradeableReadLock_WhenHasWrite_Test(); +// ReaderWriterLockSlimTest.ExitUpgradeableReadLock_Test(); +// ReaderWriterLockSlimTest.EnterUpgradeableReadLock_OnlyOne_Test(); +// ReaderWriterLockSlimTest.Upgradeable_ToRead_Test(); - //ReaderWriterLockSlimTest.TryEnterReadLock_Test(); - //ReaderWriterLockSlimTest.EnterUpgradeableReadLock_WhenHasRead_Test(); - //ReaderWriterLockSlimTest.EnterUpgradeableReadLock_WhenHasWrite_Test(); - //ReaderWriterLockSlimTest.ExitUpgradeableReadLock_Test(); - //ReaderWriterLockSlimTest.EnterUpgradeableReadLock_OnlyOne_Test(); - //ReaderWriterLockSlimTest.Upgradeable_ToRead_Test(); +// ChannelTest.Reader_Test(); - //ChannelTest.Reader_Test(); +// CancellationTokenTest.Cancel_Test(); - //CancellationTokenTest.Cancel_Test(); +// InterruptTest.Sleeping_Interrupt_Test(); - //InterruptTest.Sleeping_Interrupt_Test(); +// ThreadPoolTest.Enqueue_Test(); +// ThreadPoolTest.Starvation_WaitThread_Test(); +// ThreadPoolTest.Starvation_SetMinThreads_Test(); - //ThreadPoolTest.Enqueue_Test(); - //ThreadPoolTest.Starvation_WaitThread_Test(); - //ThreadPoolTest.Starvation_SetMinThreads_Test(); +// ThreadPoolTest.Starvation_UseGlobalQueue_Test1(); +// ThreadPoolTest.Starvation_UseThreadLocalQueue_Test2(); +// ThreadPoolTest.Starvation_UseGlobalQueue_Test3(); - //ThreadPoolTest.Starvation_UseGlobalQueue_Test1(); - //ThreadPoolTest.Starvation_UseThreadLocalQueue_Test2(); - //ThreadPoolTest.Starvation_UseGlobalQueue_Test3(); +// ThreadPoolTest.WithoutDelay_UseGlobalQueue_Test1(); - ThreadPoolTest.WithoutDelay_UseGlobalQueue_Test1(); +// ThreadPoolTest.SetMinThreads_UseGlobalQueue_Test1(); - //ThreadPoolTest.SetMinThreads_UseGlobalQueue_Test1(); +// TaskRunTest.RunWithThrow_NoWait(CancellationToken.None); +// TaskRunTest.RunWithThrow_NoWait_Continue(CancellationToken.None); +// await TaskRunTest.RunWithThrow_Async(CancellationToken.None); +// await TaskRunTest.RunWithThrow_Async_ContinueAsync(CancellationToken.None); +TaskRunTest.CallAsync_NoWait(CancellationToken.None); - while (true) - { - await Task.Delay(1000); - Console.WriteLine($"ThreadCount: {ThreadPool.ThreadCount}"); - } - } - } +while (true) +{ + await Task.Delay(1000); + Console.WriteLine($"ThreadCount: {ThreadPool.ThreadCount}"); } diff --git a/src/Tests/ThreadingTest/TaskRunTest.cs b/src/Tests/ThreadingTest/TaskRunTest.cs new file mode 100644 index 0000000..7ebfcf2 --- /dev/null +++ b/src/Tests/ThreadingTest/TaskRunTest.cs @@ -0,0 +1,100 @@ +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace ThreadingTest; + +internal static class TaskRunTest +{ + + #region Constants & Statics + + public static void CallAsync_NoWait(CancellationToken cancellationToken = default) + { + // don't crash! + +#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed + _ = RunWithThrow_Async(cancellationToken); +#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed + } + + public static async Task RunWithThrow_Async(CancellationToken cancellationToken = default) + { + // program crash! + // Unhandled exception. + + var t = Task.Run( + () => + { + Console.WriteLine("RunWithThrow_Async"); + throw new NotSupportedException("throw internal Task.Run"); + }, + cancellationToken); + + await t; + } + + public static async Task RunWithThrow_Async_ContinueAsync(CancellationToken cancellationToken = default) + { + // don't crash! + + var t = Task + .Run( + () => + { + Console.WriteLine("RunWithThrow_Async"); + throw new NotSupportedException("throw internal Task.Run"); + }, + cancellationToken) + .ContinueWith( + (t) => + { + if (t.IsFaulted) + { + Console.WriteLine(t.Exception.Message); + } + }, + cancellationToken); + + await t; + } + + public static void RunWithThrow_NoWait(CancellationToken cancellationToken = default) + { + // don't crash! + + _ = Task.Run( + () => + { + Console.WriteLine("RunWithThrow_NoWait"); + throw new NotSupportedException("throw internal Task.Run"); + }, + cancellationToken); + } + + public static void RunWithThrow_NoWait_Continue(CancellationToken cancellationToken = default) + { + // don't crash! + + _ = Task + .Run( + () => + { + Console.WriteLine("RunWithThrow_NoWait"); + throw new NotSupportedException("throw internal Task.Run"); + }, + cancellationToken) + .ContinueWith( + (t) => + { + if (t.IsFaulted) + { + Console.WriteLine(t.Exception.Message); + } + }, + cancellationToken); + } + + #endregion + +}