mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 13:15:07 +00:00
Begin adding tests for ThreadingUtils and SafeCriticalSection
This commit is contained in:
65
ICD.Common.Utils.Tests/SafeCriticalSectionTest.cs
Normal file
65
ICD.Common.Utils.Tests/SafeCriticalSectionTest.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
41
ICD.Common.Utils.Tests/ThreadingUtilsTest.cs
Normal file
41
ICD.Common.Utils.Tests/ThreadingUtilsTest.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user