Skip to content

Commit

Permalink
Use Stopwatch
Browse files Browse the repository at this point in the history
  • Loading branch information
WojciechNagorski committed Apr 14, 2023
1 parent 419cc2a commit cecb0dd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 29 deletions.
51 changes: 29 additions & 22 deletions src/Renci.SshNet.Tests/Classes/Common/CountdownEventTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
#if !FEATURE_THREAD_COUNTDOWNEVENT
Expand Down Expand Up @@ -111,16 +112,17 @@ public void Wait_TimeoutInfinite_ShouldBlockUntilCountdownEventIsSet()
threads[i].Start();
}

var start = DateTime.Now;
var watch = new Stopwatch();
watch.Start();
var actual = countdownEvent.Wait(timeout);
var elapsedTime = DateTime.Now - start;
watch.Stop();

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)));
Assert.IsTrue(watch.Elapsed >= sleep);
Assert.IsTrue(watch.Elapsed <= sleep.Add(TimeSpan.FromMilliseconds(100)));

countdownEvent.Dispose();
}
Expand Down Expand Up @@ -150,16 +152,17 @@ public void Wait_ShouldReturnTrueWhenCountdownEventIsSetBeforeTimeoutExpires()
threads[i].Start();
}

var start = DateTime.Now;
var watch = new Stopwatch();
watch.Start();
var actual = countdownEvent.Wait(timeout);
var elapsedTime = DateTime.Now - start;
watch.Stop();

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);
Assert.IsTrue(watch.Elapsed >= sleep);
Assert.IsTrue(watch.Elapsed <= timeout);

countdownEvent.Dispose();
}
Expand Down Expand Up @@ -189,14 +192,15 @@ public void Wait_ShouldReturnFalseWhenTimeoutExpiresBeforeCountdownEventIsSet()
threads[i].Start();
}

var start = DateTime.Now;
var watch = new Stopwatch();
watch.Start();
var actual = countdownEvent.Wait(timeout);
var elapsedTime = DateTime.Now - start;
watch.Stop();

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

countdownEvent.Wait(Session.InfiniteTimeSpan);
countdownEvent.Dispose();
Expand Down Expand Up @@ -239,16 +243,17 @@ public void WaitHandle_WaitOne_TimeoutInfinite_ShouldBlockUntilCountdownEventIsS
threads[i].Start();
}

var start = DateTime.Now;
var watch = new Stopwatch();
watch.Start();
var actual = countdownEvent.WaitHandle.WaitOne(timeout);
var elapsedTime = DateTime.Now - start;
watch.Stop();

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)));
Assert.IsTrue(watch.Elapsed >= sleep);
Assert.IsTrue(watch.Elapsed <= sleep.Add(TimeSpan.FromMilliseconds(100)));

countdownEvent.Dispose();
}
Expand Down Expand Up @@ -278,16 +283,17 @@ public void WaitHandle_WaitOne_ShouldReturnTrueWhenCountdownEventIsSetBeforeTime
threads[i].Start();
}

var start = DateTime.Now;
var watch = new Stopwatch();
watch.Start();
var actual = countdownEvent.Wait(timeout);
var elapsedTime = DateTime.Now - start;
watch.Stop();

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);
Assert.IsTrue(watch.Elapsed >= sleep);
Assert.IsTrue(watch.Elapsed <= timeout);

countdownEvent.Dispose();
}
Expand Down Expand Up @@ -317,14 +323,15 @@ public void WaitHandle_WaitOne_ShouldReturnFalseWhenTimeoutExpiresBeforeCountdow
threads[i].Start();
}

var start = DateTime.Now;
var watch = new Stopwatch();
watch.Start();
var actual = countdownEvent.WaitHandle.WaitOne(timeout);
var elapsedTime = DateTime.Now - start;
watch.Stop();

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

countdownEvent.Wait(Session.InfiniteTimeSpan);
countdownEvent.Dispose();
Expand Down
15 changes: 8 additions & 7 deletions src/Renci.SshNet.Tests/Classes/Common/SemaphoreLightTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Renci.SshNet.Common;
Expand Down Expand Up @@ -58,12 +59,13 @@ public void WaitTest()
const int initialCount = 2;
var target = new SemaphoreLight(initialCount);

var start = DateTime.Now;
var watch = new Stopwatch();
watch.Start();

target.Wait();
target.Wait();

Assert.IsTrue((DateTime.Now - start).TotalMilliseconds < 50);
Assert.IsTrue(watch.ElapsedMilliseconds < 50);

var releaseThread = new Thread(
() =>
Expand All @@ -75,11 +77,10 @@ public void WaitTest()

target.Wait();

var end = DateTime.Now;
var elapsed = end - start;
watch.Stop();

Assert.IsTrue(elapsed.TotalMilliseconds > 200);
Assert.IsTrue(elapsed.TotalMilliseconds < 250);
Assert.IsTrue(watch.ElapsedMilliseconds > 200);
Assert.IsTrue(watch.ElapsedMilliseconds < 250);
}

[TestMethod]
Expand Down

0 comments on commit cecb0dd

Please sign in to comment.