You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.4 KiB
C#

using MultiThreadingStudy.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MultiThreadingStudy.xUnitTest
{
/// <summary>
/// 线程的本地存储
/// </summary>
public class ThreadLocalStorageTest
{
private readonly ITestOutputHelper output;
public ThreadLocalStorageTest(ITestOutputHelper outputHelper)
{
output = outputHelper;
}
[Fact]
public void Computer_Test()
{
Computer computer = new Computer();
List<Thread> threads = new List<Thread>()
{
new Thread(new ThreadStart(computer.SumCount)) { Name = "thread_a" },
new Thread(new ThreadStart(computer.SumCount)) { Name = "thread_b" },
};
foreach (Thread thread in threads)
{
thread.Start();
}
foreach (Thread thread in threads)
{
thread.Join();
}
output.WriteLine($"调用线程 TotalCount = " + Computer.TotalCount);
//不严谨:应该是<=
Assert.True(Computer.TotalCount < computer.LoopNumber * threads.Count);
}
[Fact]
public async Task Demo_Test()
{
await Task.CompletedTask;
}
}
}