Begin adding tests for ThreadingUtils and SafeCriticalSection

This commit is contained in:
Chris Cameron
2017-10-01 19:14:25 -04:00
parent ab283cde4e
commit 206511b409
2 changed files with 106 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
using NUnit.Framework;
namespace ICD.Common.Utils.Tests
{
[TestFixture]
public sealed class SafeCriticalSectionTest
{
[Test]
public void ExecuteTest()
{
bool result = false;
SafeCriticalSection section = new SafeCriticalSection();
section.Execute(() => result = true);
Assert.IsTrue(result);
}
[Test]
public void ExecuteReturnTest()
{
SafeCriticalSection section = new SafeCriticalSection();
Assert.IsTrue(section.Execute(() => true));
}
[Test]
public void EnterTest()
{
Assert.Inconclusive();
}
[Test]
public void LeaveTest()
{
Assert.Inconclusive();
}
[Test]
public void TryEnterTest()
{
int result = 0;
SafeCriticalSection section = new SafeCriticalSection();
section.Enter();
// ReSharper disable once NotAccessedVariable
object handle = ThreadingUtils.SafeInvoke(() => { result = section.TryEnter() ? 0 : 1; });
ThreadingUtils.Sleep(1000);
Assert.AreEqual(1, result);
section.Leave();
// ReSharper disable once RedundantAssignment
handle = ThreadingUtils.SafeInvoke(() =>
{
result = section.TryEnter() ? 2 : 0;
section.Leave();
});
ThreadingUtils.Sleep(1000);
Assert.AreEqual(2, result);
}
}
}

View File

@@ -0,0 +1,41 @@
using System;
using NUnit.Framework;
namespace ICD.Common.Utils.Tests
{
[TestFixture]
public sealed class ThreadingUtilsTest
{
[Test]
public void Sleep()
{
DateTime now = IcdEnvironment.GetLocalTime();
ThreadingUtils.Sleep(1000);
DateTime now2 = IcdEnvironment.GetLocalTime();
Assert.AreEqual(1000, (now2 - now).TotalMilliseconds, 100);
}
[Test]
public void SafeInvokeTest()
{
bool result = false;
ThreadingUtils.SafeInvoke(() => result = true);
Assert.IsFalse(result);
ThreadingUtils.Sleep(1000);
Assert.IsTrue(result);
}
[Test]
public void SafeInvokeParamTest()
{
bool result = false;
ThreadingUtils.SafeInvoke(p => result = p, true);
Assert.IsFalse(result);
ThreadingUtils.Sleep(1000);
Assert.IsTrue(result);
}
}
}