ln.threading/ln.threading.tests/UnitTest1.cs

87 lines
2.4 KiB
C#

using NUnit.Framework;
using System;
using System.Threading;
namespace ln.threading.tests
{
public class Tests
{
bool setup;
[SetUp]
public void Setup()
{
if (!setup)
{
DynamicThreadPool.DefaultPool.Enqueue(()=>{
while (true)
{
Thread.Sleep(1000);
TestContext.Error.WriteLine("SimpleThreadPool: Threads={0} Idle={1} AvgIdle={5} Tasks={2} Min={3} Max={4}", DynamicThreadPool.DefaultPool.CurrentThreads, DynamicThreadPool.DefaultPool.IdleThreads, DynamicThreadPool.DefaultPool.QueuedTasks, DynamicThreadPool.DefaultPool.MinThreads, DynamicThreadPool.DefaultPool.MaxThreads, DynamicThreadPool.DefaultPool.AverageIdleThreads);
TestContext.Error.Flush();
}
});
setup = true;
}
}
[Test]
public void Test0_SimpleThreadPool()
{
bool success = false;
DynamicThreadPool.DefaultPool.Enqueue(()=>{success=true;});
Thread.Sleep(TimeSpan.FromSeconds(1));
Assert.IsTrue(success);
for (int n=0;n<32;n++)
DynamicThreadPool.DefaultPool.Enqueue(CreateTask(n));
Thread.Sleep(3200);
Assert.AreEqual(0, DynamicThreadPool.DefaultPool.QueuedTasks);
}
public Action CreateTask(int n)
{
return ()=>{
TestContext.Error.WriteLine("STP_task_{0}", n);
TestContext.Error.Flush();
Thread.Sleep(100 * n);
};
}
[Test]
public void Test1()
{
int a = 0;
PoolTimer timer = new PoolTimer(100, ()=>{
lock (this)
{
a++;
TestContext.Error.WriteLine("timer elapsed: {0}", a);
TestContext.Error.Flush();
}
});
timer.Start();
Thread.Sleep(TimeSpan.FromSeconds(2.05));
timer.Stop();
Assert.AreEqual(20, a);
Assert.Pass();
}
[Test]
public void ZZZ_Waiter()
{
Thread.Sleep(30000);
}
[Test]
public void AAA_Waiter()
{
Thread.Sleep(5000);
}
}
}