diff --git a/test/Renci.SshNet.Tests/Classes/Common/CountdownEventTest.cs b/test/Renci.SshNet.Tests/Classes/Common/CountdownEventTest.cs deleted file mode 100644 index 12d927a90..000000000 --- a/test/Renci.SshNet.Tests/Classes/Common/CountdownEventTest.cs +++ /dev/null @@ -1,353 +0,0 @@ -using System; -using System.Diagnostics; -using System.Threading; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace Renci.SshNet.Tests.Classes.Common -{ - [TestClass] - public class CountdownEventTest - { - private Random _random; - - [TestInitialize] - public void Init() - { - _random = new Random(); - } - - [TestMethod] - public void Ctor_InitialCountGreatherThanZero() - { - var initialCount = _random.Next(1, 500); - - var countdownEvent = CreateCountdownEvent(initialCount); - Assert.AreEqual(initialCount, countdownEvent.CurrentCount); - Assert.IsFalse(countdownEvent.IsSet); - Assert.IsFalse(countdownEvent.WaitHandle.WaitOne(0)); - countdownEvent.Dispose(); - } - - [TestMethod] - public void Ctor_InitialCountZero() - { - const int initialCount = 0; - - var countdownEvent = CreateCountdownEvent(0); - Assert.AreEqual(initialCount, countdownEvent.CurrentCount); - Assert.IsTrue(countdownEvent.IsSet); - Assert.IsTrue(countdownEvent.WaitHandle.WaitOne(0)); - countdownEvent.Dispose(); - } - - [TestMethod] - public void Signal_CurrentCountGreatherThanOne() - { - var initialCount = _random.Next(2, 1000); - - var countdownEvent = CreateCountdownEvent(initialCount); - Assert.IsFalse(countdownEvent.Signal()); - Assert.AreEqual(--initialCount, countdownEvent.CurrentCount); - Assert.IsFalse(countdownEvent.IsSet); - Assert.IsFalse(countdownEvent.WaitHandle.WaitOne(0)); - countdownEvent.Dispose(); - } - - [TestMethod] - public void Signal_CurrentCountOne() - { - var countdownEvent = CreateCountdownEvent(1); - Assert.IsTrue(countdownEvent.Signal()); - Assert.AreEqual(0, countdownEvent.CurrentCount); - Assert.IsTrue(countdownEvent.IsSet); - Assert.IsTrue(countdownEvent.WaitHandle.WaitOne(0)); - countdownEvent.Dispose(); - } - - [TestMethod] - public void Signal_CurrentCountZero() - { - var countdownEvent = CreateCountdownEvent(0); - - try - { - _ = countdownEvent.Signal(); - Assert.Fail(); - } - catch (InvalidOperationException) - { - // Invalid attempt made to decrement the event's count below zero - } - finally - { - countdownEvent.Dispose(); - } - } - - [TestMethod] - public void Wait_TimeoutInfinite_ShouldBlockUntilCountdownEventIsSet() - { - var sleep = TimeSpan.FromMilliseconds(100); - var timeout = Timeout.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 watch = new Stopwatch(); - watch.Start(); - var actual = countdownEvent.Wait(timeout); - watch.Stop(); - - Assert.IsTrue(actual); - Assert.AreEqual(expectedSignalCount, signalCount); - Assert.IsTrue(countdownEvent.IsSet); - Assert.IsTrue(countdownEvent.WaitHandle.WaitOne(0)); - Assert.IsTrue(watch.Elapsed >= sleep); - Assert.IsTrue(watch.Elapsed <= sleep.Add(TimeSpan.FromMilliseconds(100))); - - countdownEvent.Dispose(); - } - - [TestMethod] - public void Wait_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 watch = new Stopwatch(); - watch.Start(); - var actual = countdownEvent.Wait(timeout); - watch.Stop(); - - Assert.IsTrue(actual); - Assert.AreEqual(expectedSignalCount, signalCount); - Assert.IsTrue(countdownEvent.IsSet); - Assert.IsTrue(countdownEvent.WaitHandle.WaitOne(0)); - Assert.IsTrue(watch.Elapsed >= sleep); - Assert.IsTrue(watch.Elapsed <= timeout); - - countdownEvent.Dispose(); - } - - [TestMethod] - public void Wait_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 watch = new Stopwatch(); - watch.Start(); - var actual = countdownEvent.Wait(timeout); - watch.Stop(); - - Assert.IsFalse(actual); - Assert.IsFalse(countdownEvent.IsSet); - Assert.IsFalse(countdownEvent.WaitHandle.WaitOne(0)); - - _ = countdownEvent.Wait(Timeout.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 = Timeout.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 watch = new Stopwatch(); - watch.Start(); - var actual = countdownEvent.WaitHandle.WaitOne(timeout); - watch.Stop(); - - Assert.IsTrue(actual); - Assert.AreEqual(expectedSignalCount, signalCount); - Assert.IsTrue(countdownEvent.IsSet); - Assert.IsTrue(countdownEvent.WaitHandle.WaitOne(0)); - Assert.IsTrue(watch.Elapsed >= sleep); - Assert.IsTrue(watch.Elapsed <= 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 watch = new Stopwatch(); - watch.Start(); - var actual = countdownEvent.Wait(timeout); - watch.Stop(); - - Assert.IsTrue(actual); - Assert.AreEqual(expectedSignalCount, signalCount); - Assert.IsTrue(countdownEvent.IsSet); - Assert.IsTrue(countdownEvent.WaitHandle.WaitOne(0)); - Assert.IsTrue(watch.Elapsed >= sleep); - Assert.IsTrue(watch.Elapsed <= 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 watch = new Stopwatch(); - watch.Start(); - var actual = countdownEvent.WaitHandle.WaitOne(timeout); - watch.Stop(); - - Assert.IsFalse(actual); - Assert.IsFalse(countdownEvent.IsSet); - Assert.IsFalse(countdownEvent.WaitHandle.WaitOne(0)); - - _ = countdownEvent.Wait(Timeout.InfiniteTimeSpan); - countdownEvent.Dispose(); - } - - private static CountdownEvent CreateCountdownEvent(int initialCount) - { - return new CountdownEvent(initialCount); - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Common/CountdownEventTest_Dispose_NotSet.cs b/test/Renci.SshNet.Tests/Classes/Common/CountdownEventTest_Dispose_NotSet.cs deleted file mode 100644 index c85115f65..000000000 --- a/test/Renci.SshNet.Tests/Classes/Common/CountdownEventTest_Dispose_NotSet.cs +++ /dev/null @@ -1,106 +0,0 @@ -using System; -using System.Threading; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -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) - { - } - } - } -} diff --git a/test/Renci.SshNet.Tests/Classes/Common/CountdownEventTest_Dispose_Set.cs b/test/Renci.SshNet.Tests/Classes/Common/CountdownEventTest_Dispose_Set.cs deleted file mode 100644 index 511ace48d..000000000 --- a/test/Renci.SshNet.Tests/Classes/Common/CountdownEventTest_Dispose_Set.cs +++ /dev/null @@ -1,104 +0,0 @@ -using System; -using System.Threading; - -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace Renci.SshNet.Tests.Classes.Common -{ - [TestClass] - public class CountdownEventTest_Dispose_Set - { - private CountdownEvent _countdownEvent; - - [TestInitialize] - public void Initialize() - { - Arrange(); - Act(); - } - - private void Arrange() - { - _countdownEvent = new CountdownEvent(0); - } - - private void Act() - { - _countdownEvent.Dispose(); - } - - [TestMethod] - public void AddCount_ShouldThrowObjectDisposedException() - { - try - { - _countdownEvent.AddCount(); - Assert.Fail(); - } - catch (ObjectDisposedException) - { - } - } - - [TestMethod] - public void CurrentCount_ShouldReturnZero() - { - var actual = _countdownEvent.CurrentCount; - - Assert.AreEqual(0, actual); - } - - [TestMethod] - public void Dispose_ShouldNotThrow() - { - _countdownEvent.Dispose(); - } - - [TestMethod] - public void IsSet_ShouldReturnTrue() - { - var actual = _countdownEvent.IsSet; - - Assert.IsTrue(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) - { - } - } - } -}