Skip to content

Commit

Permalink
Add TaskRunTest
Browse files Browse the repository at this point in the history
  • Loading branch information
jerviscui committed Jun 12, 2024
1 parent e27d9be commit cdf6d14
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 46 deletions.
17 changes: 13 additions & 4 deletions src/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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: 检测到无法访问的代码
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
19 changes: 13 additions & 6 deletions src/Tests/ThreadingTest/CancellationTokenTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
70 changes: 34 additions & 36 deletions src/Tests/ThreadingTest/Program.cs
Original file line number Diff line number Diff line change
@@ -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}");
}
100 changes: 100 additions & 0 deletions src/Tests/ThreadingTest/TaskRunTest.cs
Original file line number Diff line number Diff line change
@@ -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

}

0 comments on commit cdf6d14

Please sign in to comment.