Skip to content

Commit

Permalink
Expose WaitHandle.
Browse files Browse the repository at this point in the history
  • Loading branch information
drieseng committed Sep 9, 2017
1 parent 05e82b1 commit 82634b4
Show file tree
Hide file tree
Showing 6 changed files with 379 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/Renci.SshNet.Tests.NET35/Renci.SshNet.Tests.NET35.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@
<Compile Include="..\Renci.SshNet.Tests\Classes\Common\CountdownEventTest.cs">
<Link>Classes\Common\CountdownEventTest.cs</Link>
</Compile>
<Compile Include="..\Renci.SshNet.Tests\Classes\Common\CountdownEventTest_Dispose_NotSet.cs">
<Link>Classes\Common\CountdownEventTest_Dispose_NotSet.cs</Link>
</Compile>
<Compile Include="..\Renci.SshNet.Tests\Classes\Common\CountdownEventTest_Dispose_Set.cs">
<Link>Classes\Common\CountdownEventTest_Dispose_Set.cs</Link>
</Compile>
<Compile Include="..\Renci.SshNet.Tests\Classes\Common\DerDataTest.cs">
<Link>Classes\Common\DerDataTest.cs</Link>
</Compile>
Expand Down Expand Up @@ -1596,7 +1602,7 @@
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ProjectExtensions>
<VisualStudio>
<UserProperties ProjectLinkReference="c45379b9-17b1-4e89-bc2e-6d41726413e8" ProjectLinkerExcludeFilter="\\?desktop(\\.*)?$;\\?silverlight(\\.*)?$;\.desktop;\.silverlight;\.xaml;^service references(\\.*)?$;\.clientconfig;^web references(\\.*)?$" />
<UserProperties ProjectLinkerExcludeFilter="\\?desktop(\\.*)?$;\\?silverlight(\\.*)?$;\.desktop;\.silverlight;\.xaml;^service references(\\.*)?$;\.clientconfig;^web references(\\.*)?$" ProjectLinkReference="c45379b9-17b1-4e89-bc2e-6d41726413e8" />
</VisualStudio>
</ProjectExtensions>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
141 changes: 136 additions & 5 deletions src/Renci.SshNet.Tests/Classes/Common/CountdownEventTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public void Ctor_InitialCountGreatherThanZero()
var countdownEvent = CreateCountdownEvent(initialCount);
Assert.AreEqual(initialCount, countdownEvent.CurrentCount);
Assert.IsFalse(countdownEvent.IsSet);
Assert.IsFalse(countdownEvent.WaitHandle.WaitOne(0));
countdownEvent.Dispose();
}

Expand All @@ -37,6 +38,7 @@ public void Ctor_InitialCountZero()
var countdownEvent = CreateCountdownEvent(0);
Assert.AreEqual(initialCount, countdownEvent.CurrentCount);
Assert.IsTrue(countdownEvent.IsSet);
Assert.IsTrue(countdownEvent.WaitHandle.WaitOne(0));
countdownEvent.Dispose();
}

Expand All @@ -49,6 +51,7 @@ public void Signal_CurrentCountGreatherThanOne()
Assert.IsFalse(countdownEvent.Signal());
Assert.AreEqual(--initialCount, countdownEvent.CurrentCount);
Assert.IsFalse(countdownEvent.IsSet);
Assert.IsFalse(countdownEvent.WaitHandle.WaitOne(0));
countdownEvent.Dispose();
}

Expand All @@ -59,6 +62,7 @@ public void Signal_CurrentCountOne()
Assert.IsTrue(countdownEvent.Signal());
Assert.AreEqual(0, countdownEvent.CurrentCount);
Assert.IsTrue(countdownEvent.IsSet);
Assert.IsTrue(countdownEvent.WaitHandle.WaitOne(0));
countdownEvent.Dispose();
}

Expand All @@ -82,10 +86,6 @@ public void Signal_CurrentCountZero()
}
}

public void CurrentCountShouldReturnZeroAfterAttemptToDecrementCountBelowZero()
{
}

[TestMethod]
public void Wait_TimeoutInfinite_ShouldBlockUntilCountdownEventIsSet()
{
Expand Down Expand Up @@ -118,6 +118,7 @@ public void Wait_TimeoutInfinite_ShouldBlockUntilCountdownEventIsSet()
Assert.IsTrue(actual);
Assert.AreEqual(expectedSignalCount, signalCount);
Assert.IsTrue(countdownEvent.IsSet);
Assert.IsTrue(countdownEvent.WaitHandle.WaitOne(0));
Assert.IsTrue(elapsedTime >= sleep);
Assert.IsTrue(elapsedTime <= sleep.Add(TimeSpan.FromMilliseconds(100)));

Expand Down Expand Up @@ -156,14 +157,15 @@ public void Wait_ShouldReturnTrueWhenCountdownEventIsSetBeforeTimeoutExpires()
Assert.IsTrue(actual);
Assert.AreEqual(expectedSignalCount, signalCount);
Assert.IsTrue(countdownEvent.IsSet);
Assert.IsTrue(countdownEvent.WaitHandle.WaitOne(0));
Assert.IsTrue(elapsedTime >= sleep);
Assert.IsTrue(elapsedTime <= timeout);

countdownEvent.Dispose();
}

[TestMethod]
public void Wait_ShouldReturnFalseTimeoutExpiresBeforeCountdownEventIsSet()
public void Wait_ShouldReturnFalseWhenTimeoutExpiresBeforeCountdownEventIsSet()
{
var sleep = TimeSpan.FromMilliseconds(100);
var timeout = TimeSpan.FromMilliseconds(30);
Expand Down Expand Up @@ -193,6 +195,135 @@ public void Wait_ShouldReturnFalseTimeoutExpiresBeforeCountdownEventIsSet()

Assert.IsFalse(actual);
Assert.IsFalse(countdownEvent.IsSet);
Assert.IsFalse(countdownEvent.WaitHandle.WaitOne(0));
Assert.IsTrue(elapsedTime >= timeout);

countdownEvent.Wait(Session.InfiniteTimeSpan);
countdownEvent.Dispose();
}

[TestMethod]
public void WaitHandle_ShouldAlwaysReturnSameInstance()
{
var countdownEvent = CreateCountdownEvent(1);

var waitHandleA = countdownEvent.WaitHandle;
Assert.IsNotNull(waitHandleA);

var waitHandleB = countdownEvent.WaitHandle;
Assert.AreSame(waitHandleA, waitHandleB);
}

[TestMethod]
public void WaitHandle_WaitOne_TimeoutInfinite_ShouldBlockUntilCountdownEventIsSet()
{
var sleep = TimeSpan.FromMilliseconds(100);
var timeout = Session.InfiniteTimeSpan;

var countdownEvent = CreateCountdownEvent(1);
var signalCount = 0;
var expectedSignalCount = _random.Next(5, 20);

for (var i = 0; i < (expectedSignalCount - 1); i++)
countdownEvent.AddCount();

var threads = new Thread[expectedSignalCount];
for (var i = 0; i < expectedSignalCount; i++)
{
threads[i] = new Thread(() =>
{
Thread.Sleep(sleep);
Interlocked.Increment(ref signalCount);
countdownEvent.Signal();
});
threads[i].Start();
}

var start = DateTime.Now;
var actual = countdownEvent.WaitHandle.WaitOne(timeout);
var elapsedTime = DateTime.Now - start;

Assert.IsTrue(actual);
Assert.AreEqual(expectedSignalCount, signalCount);
Assert.IsTrue(countdownEvent.IsSet);
Assert.IsTrue(countdownEvent.WaitHandle.WaitOne(0));
Assert.IsTrue(elapsedTime >= sleep);
Assert.IsTrue(elapsedTime <= sleep.Add(TimeSpan.FromMilliseconds(100)));

countdownEvent.Dispose();
}

[TestMethod]
public void WaitHandle_WaitOne_ShouldReturnTrueWhenCountdownEventIsSetBeforeTimeoutExpires()
{
var sleep = TimeSpan.FromMilliseconds(100);
var timeout = sleep.Add(TimeSpan.FromSeconds(2));

var countdownEvent = CreateCountdownEvent(1);
var signalCount = 0;
var expectedSignalCount = _random.Next(5, 20);

for (var i = 0; i < (expectedSignalCount - 1); i++)
countdownEvent.AddCount();

var threads = new Thread[expectedSignalCount];
for (var i = 0; i < expectedSignalCount; i++)
{
threads[i] = new Thread(() =>
{
Thread.Sleep(sleep);
Interlocked.Increment(ref signalCount);
countdownEvent.Signal();
});
threads[i].Start();
}

var start = DateTime.Now;
var actual = countdownEvent.Wait(timeout);
var elapsedTime = DateTime.Now - start;

Assert.IsTrue(actual);
Assert.AreEqual(expectedSignalCount, signalCount);
Assert.IsTrue(countdownEvent.IsSet);
Assert.IsTrue(countdownEvent.WaitHandle.WaitOne(0));
Assert.IsTrue(elapsedTime >= sleep);
Assert.IsTrue(elapsedTime <= timeout);

countdownEvent.Dispose();
}

[TestMethod]
public void WaitHandle_WaitOne_ShouldReturnFalseWhenTimeoutExpiresBeforeCountdownEventIsSet()
{
var sleep = TimeSpan.FromMilliseconds(100);
var timeout = TimeSpan.FromMilliseconds(30);

var countdownEvent = CreateCountdownEvent(1);
var signalCount = 0;
var expectedSignalCount = _random.Next(5, 20);

for (var i = 0; i < (expectedSignalCount - 1); i++)
countdownEvent.AddCount();

var threads = new Thread[expectedSignalCount];
for (var i = 0; i < expectedSignalCount; i++)
{
threads[i] = new Thread(() =>
{
Thread.Sleep(sleep);
countdownEvent.Signal();
Interlocked.Increment(ref signalCount);
});
threads[i].Start();
}

var start = DateTime.Now;
var actual = countdownEvent.WaitHandle.WaitOne(timeout);
var elapsedTime = DateTime.Now - start;

Assert.IsFalse(actual);
Assert.IsFalse(countdownEvent.IsSet);
Assert.IsFalse(countdownEvent.WaitHandle.WaitOne(0));
Assert.IsTrue(elapsedTime >= timeout);

countdownEvent.Wait(Session.InfiniteTimeSpan);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
#if !FEATURE_THREAD_COUNTDOWNEVENT
using CountdownEvent = Renci.SshNet.Common.CountdownEvent;
#else
using System.Threading;
#endif

namespace Renci.SshNet.Tests.Classes.Common
{
[TestClass]
public class CountdownEventTest_Dispose_NotSet
{
private int _signalsRequired;
private CountdownEvent _countdownEvent;

[TestInitialize]
public void Initialize()
{
Arrange();
Act();
}

private void Arrange()
{
_signalsRequired = new Random().Next(1, 20);
_countdownEvent = new CountdownEvent(_signalsRequired);
}

private void Act()
{
_countdownEvent.Dispose();
}

[TestMethod]
public void AddCount_ShouldThrowObjectDisposedException()
{
try
{
_countdownEvent.AddCount();
Assert.Fail();
}
catch (ObjectDisposedException)
{
}
}

[TestMethod]
public void CurrentCount_ShouldReturnRemainingSignalsRequiredToSetEvent()
{
var actual = _countdownEvent.CurrentCount;

Assert.AreEqual(_signalsRequired, actual);
}

[TestMethod]
public void Dispose_ShouldNotThrow()
{
_countdownEvent.Dispose();
}

[TestMethod]
public void IsSet_ShouldReturnFalse()
{
var actual = _countdownEvent.IsSet;

Assert.IsFalse(actual);
}

[TestMethod]
public void Signal_ShouldThrowObjectDisposedException()
{
try
{
var set = _countdownEvent.Signal();
Assert.Fail("Should have thrown ObjectDisposedException, but returned: " + set);
}
catch (ObjectDisposedException)
{
}
}

[TestMethod]
public void Wait_TimeSpan_ShouldThrowObjectDisposedException()
{
try
{
var set = _countdownEvent.Wait(TimeSpan.FromSeconds(5));
Assert.Fail("Should have thrown ObjectDisposedException, but returned: " + set);
}
catch (ObjectDisposedException)
{
}
}

[TestMethod]
public void WaitHandle_ShouldThrowObjectDisposedException()
{
try
{
var waitHandle = _countdownEvent.WaitHandle;
Assert.Fail("Should have thrown ObjectDisposedException, but returned: " + waitHandle);
}
catch (ObjectDisposedException)
{
}
}
}
}
Loading

0 comments on commit 82634b4

Please sign in to comment.