Skip to content

Commit 82634b4

Browse files
committed
Expose WaitHandle.
1 parent 05e82b1 commit 82634b4

File tree

6 files changed

+379
-6
lines changed

6 files changed

+379
-6
lines changed

src/Renci.SshNet.Tests.NET35/Renci.SshNet.Tests.NET35.csproj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,12 @@
273273
<Compile Include="..\Renci.SshNet.Tests\Classes\Common\CountdownEventTest.cs">
274274
<Link>Classes\Common\CountdownEventTest.cs</Link>
275275
</Compile>
276+
<Compile Include="..\Renci.SshNet.Tests\Classes\Common\CountdownEventTest_Dispose_NotSet.cs">
277+
<Link>Classes\Common\CountdownEventTest_Dispose_NotSet.cs</Link>
278+
</Compile>
279+
<Compile Include="..\Renci.SshNet.Tests\Classes\Common\CountdownEventTest_Dispose_Set.cs">
280+
<Link>Classes\Common\CountdownEventTest_Dispose_Set.cs</Link>
281+
</Compile>
276282
<Compile Include="..\Renci.SshNet.Tests\Classes\Common\DerDataTest.cs">
277283
<Link>Classes\Common\DerDataTest.cs</Link>
278284
</Compile>
@@ -1596,7 +1602,7 @@
15961602
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
15971603
<ProjectExtensions>
15981604
<VisualStudio>
1599-
<UserProperties ProjectLinkReference="c45379b9-17b1-4e89-bc2e-6d41726413e8" ProjectLinkerExcludeFilter="\\?desktop(\\.*)?$;\\?silverlight(\\.*)?$;\.desktop;\.silverlight;\.xaml;^service references(\\.*)?$;\.clientconfig;^web references(\\.*)?$" />
1605+
<UserProperties ProjectLinkerExcludeFilter="\\?desktop(\\.*)?$;\\?silverlight(\\.*)?$;\.desktop;\.silverlight;\.xaml;^service references(\\.*)?$;\.clientconfig;^web references(\\.*)?$" ProjectLinkReference="c45379b9-17b1-4e89-bc2e-6d41726413e8" />
16001606
</VisualStudio>
16011607
</ProjectExtensions>
16021608
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

src/Renci.SshNet.Tests/Classes/Common/CountdownEventTest.cs

Lines changed: 136 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public void Ctor_InitialCountGreatherThanZero()
2626
var countdownEvent = CreateCountdownEvent(initialCount);
2727
Assert.AreEqual(initialCount, countdownEvent.CurrentCount);
2828
Assert.IsFalse(countdownEvent.IsSet);
29+
Assert.IsFalse(countdownEvent.WaitHandle.WaitOne(0));
2930
countdownEvent.Dispose();
3031
}
3132

@@ -37,6 +38,7 @@ public void Ctor_InitialCountZero()
3738
var countdownEvent = CreateCountdownEvent(0);
3839
Assert.AreEqual(initialCount, countdownEvent.CurrentCount);
3940
Assert.IsTrue(countdownEvent.IsSet);
41+
Assert.IsTrue(countdownEvent.WaitHandle.WaitOne(0));
4042
countdownEvent.Dispose();
4143
}
4244

@@ -49,6 +51,7 @@ public void Signal_CurrentCountGreatherThanOne()
4951
Assert.IsFalse(countdownEvent.Signal());
5052
Assert.AreEqual(--initialCount, countdownEvent.CurrentCount);
5153
Assert.IsFalse(countdownEvent.IsSet);
54+
Assert.IsFalse(countdownEvent.WaitHandle.WaitOne(0));
5255
countdownEvent.Dispose();
5356
}
5457

@@ -59,6 +62,7 @@ public void Signal_CurrentCountOne()
5962
Assert.IsTrue(countdownEvent.Signal());
6063
Assert.AreEqual(0, countdownEvent.CurrentCount);
6164
Assert.IsTrue(countdownEvent.IsSet);
65+
Assert.IsTrue(countdownEvent.WaitHandle.WaitOne(0));
6266
countdownEvent.Dispose();
6367
}
6468

@@ -82,10 +86,6 @@ public void Signal_CurrentCountZero()
8286
}
8387
}
8488

85-
public void CurrentCountShouldReturnZeroAfterAttemptToDecrementCountBelowZero()
86-
{
87-
}
88-
8989
[TestMethod]
9090
public void Wait_TimeoutInfinite_ShouldBlockUntilCountdownEventIsSet()
9191
{
@@ -118,6 +118,7 @@ public void Wait_TimeoutInfinite_ShouldBlockUntilCountdownEventIsSet()
118118
Assert.IsTrue(actual);
119119
Assert.AreEqual(expectedSignalCount, signalCount);
120120
Assert.IsTrue(countdownEvent.IsSet);
121+
Assert.IsTrue(countdownEvent.WaitHandle.WaitOne(0));
121122
Assert.IsTrue(elapsedTime >= sleep);
122123
Assert.IsTrue(elapsedTime <= sleep.Add(TimeSpan.FromMilliseconds(100)));
123124

@@ -156,14 +157,15 @@ public void Wait_ShouldReturnTrueWhenCountdownEventIsSetBeforeTimeoutExpires()
156157
Assert.IsTrue(actual);
157158
Assert.AreEqual(expectedSignalCount, signalCount);
158159
Assert.IsTrue(countdownEvent.IsSet);
160+
Assert.IsTrue(countdownEvent.WaitHandle.WaitOne(0));
159161
Assert.IsTrue(elapsedTime >= sleep);
160162
Assert.IsTrue(elapsedTime <= timeout);
161163

162164
countdownEvent.Dispose();
163165
}
164166

165167
[TestMethod]
166-
public void Wait_ShouldReturnFalseTimeoutExpiresBeforeCountdownEventIsSet()
168+
public void Wait_ShouldReturnFalseWhenTimeoutExpiresBeforeCountdownEventIsSet()
167169
{
168170
var sleep = TimeSpan.FromMilliseconds(100);
169171
var timeout = TimeSpan.FromMilliseconds(30);
@@ -193,6 +195,135 @@ public void Wait_ShouldReturnFalseTimeoutExpiresBeforeCountdownEventIsSet()
193195

194196
Assert.IsFalse(actual);
195197
Assert.IsFalse(countdownEvent.IsSet);
198+
Assert.IsFalse(countdownEvent.WaitHandle.WaitOne(0));
199+
Assert.IsTrue(elapsedTime >= timeout);
200+
201+
countdownEvent.Wait(Session.InfiniteTimeSpan);
202+
countdownEvent.Dispose();
203+
}
204+
205+
[TestMethod]
206+
public void WaitHandle_ShouldAlwaysReturnSameInstance()
207+
{
208+
var countdownEvent = CreateCountdownEvent(1);
209+
210+
var waitHandleA = countdownEvent.WaitHandle;
211+
Assert.IsNotNull(waitHandleA);
212+
213+
var waitHandleB = countdownEvent.WaitHandle;
214+
Assert.AreSame(waitHandleA, waitHandleB);
215+
}
216+
217+
[TestMethod]
218+
public void WaitHandle_WaitOne_TimeoutInfinite_ShouldBlockUntilCountdownEventIsSet()
219+
{
220+
var sleep = TimeSpan.FromMilliseconds(100);
221+
var timeout = Session.InfiniteTimeSpan;
222+
223+
var countdownEvent = CreateCountdownEvent(1);
224+
var signalCount = 0;
225+
var expectedSignalCount = _random.Next(5, 20);
226+
227+
for (var i = 0; i < (expectedSignalCount - 1); i++)
228+
countdownEvent.AddCount();
229+
230+
var threads = new Thread[expectedSignalCount];
231+
for (var i = 0; i < expectedSignalCount; i++)
232+
{
233+
threads[i] = new Thread(() =>
234+
{
235+
Thread.Sleep(sleep);
236+
Interlocked.Increment(ref signalCount);
237+
countdownEvent.Signal();
238+
});
239+
threads[i].Start();
240+
}
241+
242+
var start = DateTime.Now;
243+
var actual = countdownEvent.WaitHandle.WaitOne(timeout);
244+
var elapsedTime = DateTime.Now - start;
245+
246+
Assert.IsTrue(actual);
247+
Assert.AreEqual(expectedSignalCount, signalCount);
248+
Assert.IsTrue(countdownEvent.IsSet);
249+
Assert.IsTrue(countdownEvent.WaitHandle.WaitOne(0));
250+
Assert.IsTrue(elapsedTime >= sleep);
251+
Assert.IsTrue(elapsedTime <= sleep.Add(TimeSpan.FromMilliseconds(100)));
252+
253+
countdownEvent.Dispose();
254+
}
255+
256+
[TestMethod]
257+
public void WaitHandle_WaitOne_ShouldReturnTrueWhenCountdownEventIsSetBeforeTimeoutExpires()
258+
{
259+
var sleep = TimeSpan.FromMilliseconds(100);
260+
var timeout = sleep.Add(TimeSpan.FromSeconds(2));
261+
262+
var countdownEvent = CreateCountdownEvent(1);
263+
var signalCount = 0;
264+
var expectedSignalCount = _random.Next(5, 20);
265+
266+
for (var i = 0; i < (expectedSignalCount - 1); i++)
267+
countdownEvent.AddCount();
268+
269+
var threads = new Thread[expectedSignalCount];
270+
for (var i = 0; i < expectedSignalCount; i++)
271+
{
272+
threads[i] = new Thread(() =>
273+
{
274+
Thread.Sleep(sleep);
275+
Interlocked.Increment(ref signalCount);
276+
countdownEvent.Signal();
277+
});
278+
threads[i].Start();
279+
}
280+
281+
var start = DateTime.Now;
282+
var actual = countdownEvent.Wait(timeout);
283+
var elapsedTime = DateTime.Now - start;
284+
285+
Assert.IsTrue(actual);
286+
Assert.AreEqual(expectedSignalCount, signalCount);
287+
Assert.IsTrue(countdownEvent.IsSet);
288+
Assert.IsTrue(countdownEvent.WaitHandle.WaitOne(0));
289+
Assert.IsTrue(elapsedTime >= sleep);
290+
Assert.IsTrue(elapsedTime <= timeout);
291+
292+
countdownEvent.Dispose();
293+
}
294+
295+
[TestMethod]
296+
public void WaitHandle_WaitOne_ShouldReturnFalseWhenTimeoutExpiresBeforeCountdownEventIsSet()
297+
{
298+
var sleep = TimeSpan.FromMilliseconds(100);
299+
var timeout = TimeSpan.FromMilliseconds(30);
300+
301+
var countdownEvent = CreateCountdownEvent(1);
302+
var signalCount = 0;
303+
var expectedSignalCount = _random.Next(5, 20);
304+
305+
for (var i = 0; i < (expectedSignalCount - 1); i++)
306+
countdownEvent.AddCount();
307+
308+
var threads = new Thread[expectedSignalCount];
309+
for (var i = 0; i < expectedSignalCount; i++)
310+
{
311+
threads[i] = new Thread(() =>
312+
{
313+
Thread.Sleep(sleep);
314+
countdownEvent.Signal();
315+
Interlocked.Increment(ref signalCount);
316+
});
317+
threads[i].Start();
318+
}
319+
320+
var start = DateTime.Now;
321+
var actual = countdownEvent.WaitHandle.WaitOne(timeout);
322+
var elapsedTime = DateTime.Now - start;
323+
324+
Assert.IsFalse(actual);
325+
Assert.IsFalse(countdownEvent.IsSet);
326+
Assert.IsFalse(countdownEvent.WaitHandle.WaitOne(0));
196327
Assert.IsTrue(elapsedTime >= timeout);
197328

198329
countdownEvent.Wait(Session.InfiniteTimeSpan);
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
#if !FEATURE_THREAD_COUNTDOWNEVENT
4+
using CountdownEvent = Renci.SshNet.Common.CountdownEvent;
5+
#else
6+
using System.Threading;
7+
#endif
8+
9+
namespace Renci.SshNet.Tests.Classes.Common
10+
{
11+
[TestClass]
12+
public class CountdownEventTest_Dispose_NotSet
13+
{
14+
private int _signalsRequired;
15+
private CountdownEvent _countdownEvent;
16+
17+
[TestInitialize]
18+
public void Initialize()
19+
{
20+
Arrange();
21+
Act();
22+
}
23+
24+
private void Arrange()
25+
{
26+
_signalsRequired = new Random().Next(1, 20);
27+
_countdownEvent = new CountdownEvent(_signalsRequired);
28+
}
29+
30+
private void Act()
31+
{
32+
_countdownEvent.Dispose();
33+
}
34+
35+
[TestMethod]
36+
public void AddCount_ShouldThrowObjectDisposedException()
37+
{
38+
try
39+
{
40+
_countdownEvent.AddCount();
41+
Assert.Fail();
42+
}
43+
catch (ObjectDisposedException)
44+
{
45+
}
46+
}
47+
48+
[TestMethod]
49+
public void CurrentCount_ShouldReturnRemainingSignalsRequiredToSetEvent()
50+
{
51+
var actual = _countdownEvent.CurrentCount;
52+
53+
Assert.AreEqual(_signalsRequired, actual);
54+
}
55+
56+
[TestMethod]
57+
public void Dispose_ShouldNotThrow()
58+
{
59+
_countdownEvent.Dispose();
60+
}
61+
62+
[TestMethod]
63+
public void IsSet_ShouldReturnFalse()
64+
{
65+
var actual = _countdownEvent.IsSet;
66+
67+
Assert.IsFalse(actual);
68+
}
69+
70+
[TestMethod]
71+
public void Signal_ShouldThrowObjectDisposedException()
72+
{
73+
try
74+
{
75+
var set = _countdownEvent.Signal();
76+
Assert.Fail("Should have thrown ObjectDisposedException, but returned: " + set);
77+
}
78+
catch (ObjectDisposedException)
79+
{
80+
}
81+
}
82+
83+
[TestMethod]
84+
public void Wait_TimeSpan_ShouldThrowObjectDisposedException()
85+
{
86+
try
87+
{
88+
var set = _countdownEvent.Wait(TimeSpan.FromSeconds(5));
89+
Assert.Fail("Should have thrown ObjectDisposedException, but returned: " + set);
90+
}
91+
catch (ObjectDisposedException)
92+
{
93+
}
94+
}
95+
96+
[TestMethod]
97+
public void WaitHandle_ShouldThrowObjectDisposedException()
98+
{
99+
try
100+
{
101+
var waitHandle = _countdownEvent.WaitHandle;
102+
Assert.Fail("Should have thrown ObjectDisposedException, but returned: " + waitHandle);
103+
}
104+
catch (ObjectDisposedException)
105+
{
106+
}
107+
}
108+
}
109+
}

0 commit comments

Comments
 (0)