using Xunit.Abstractions;

namespace MultiThreadingStudy.xUnitTest
{
    public class ThreadTest:IDisposable
    {
        private readonly ITestOutputHelper _output;

        public ThreadTest(ITestOutputHelper testOutput)
        {
            _output = testOutput;
        }

        /// <summary>
        ///  测试方法的启动线程为后台线程
        ///  结论：在测试方法中新启动的线程(即使设置成前台线程)，默认情况下，不能阻止单元测试方法的执行结束。
        ///        测试方法很可能先于新启动的线程结束运行，造成新线程没有执行完就随着测试方法线程结束而结束。
        /// </summary>
        [Fact]
        public void TestRunThread_Test()
        {
            _output.WriteLine($"主线程Id={Thread.CurrentThread.ManagedThreadId}, 是否后台线程={Thread.CurrentThread.IsBackground}");

            //断言：测试的启动线程为后台线程
            Assert.True( Thread.CurrentThread.IsBackground);
        }

        /// <summary>
        /// 
        /// </summary>
        [Fact]
        public void Test1()
        {
            _output.WriteLine($"主线程Id={Thread.CurrentThread.ManagedThreadId}, 是否后台线程={Thread.CurrentThread.IsBackground}");


            Thread t = new Thread(() =>
            {
                _output.WriteLine($"新线程Id={Thread.CurrentThread.ManagedThreadId}, 新线程名称={Thread.CurrentThread.Name}");

                _output.WriteLine($"{Thread.CurrentThread.Name} 新线程，开始休眠");
                Thread.Sleep(100);
                _output.WriteLine($"{Thread.CurrentThread.Name} 新线程从休眠中唤醒，执行结束！");
            })
            { 
                Name = "FirstThread",
                Priority = ThreadPriority.Normal,
                IsBackground = false,
            };

            t.Start();
            t.Join();
        }

        public void Dispose()
        {
           
        }
    }
}