Browse Source

Harden PipeStream tests.

drieseng 9 năm trước cách đây
mục cha
commit
b86eba0d7c

+ 3 - 2
src/Renci.SshNet.Tests/Common/PipeStream_Close_BlockingRead.cs

@@ -25,6 +25,7 @@ namespace Renci.SshNet.Tests.Common
 
             Action readAction = () => _bytesRead = _pipeStream.Read(new byte[4], 0, 4);
             _asyncReadResult = readAction.BeginInvoke(null, null);
+            // ensure we've started reading
             _asyncReadResult.AsyncWaitHandle.WaitOne(50);
 
             Act();
@@ -36,9 +37,9 @@ namespace Renci.SshNet.Tests.Common
         }
 
         [TestMethod]
-        public void AsyncReadShouldHaveFinished()
+        public void BlockingReadShouldHaveBeenInterrupted()
         {
-            Assert.IsTrue(_asyncReadResult.IsCompleted);
+            Assert.IsTrue(_asyncReadResult.AsyncWaitHandle.WaitOne(200));
         }
 
         [TestMethod]

+ 22 - 17
src/Renci.SshNet.Tests/Common/PipeStream_Close_BlockingWrite.cs

@@ -17,22 +17,25 @@ namespace Renci.SshNet.Tests.Common
             _pipeStream = new PipeStream {MaxBufferLength = 3};
 
             Action writeAction = () =>
-            {
-                _pipeStream.WriteByte(10);
-                _pipeStream.WriteByte(13);
-                _pipeStream.WriteByte(25);
-
-                try
-                {
-                    _pipeStream.WriteByte(35);
-                }
-                catch (Exception ex)
                 {
-                    _writeException = ex;
-                    throw;
-                }
-            };
+                    _pipeStream.WriteByte(10);
+                    _pipeStream.WriteByte(13);
+                    _pipeStream.WriteByte(25);
+
+                    // attempting to write more bytes than the max. buffer length should block
+                    // until bytes are read or the stream is closed
+                    try
+                    {
+                        _pipeStream.WriteByte(35);
+                    }
+                    catch (Exception ex)
+                    {
+                        _writeException = ex;
+                        throw;
+                    }
+                };
             _asyncWriteResult = writeAction.BeginInvoke(null, null);
+            // ensure we've started writing
             _asyncWriteResult.AsyncWaitHandle.WaitOne(50);
 
             Act();
@@ -44,14 +47,16 @@ namespace Renci.SshNet.Tests.Common
         }
 
         [TestMethod]
-        public void AsyncWriteShouldHaveFinished()
+        public void BlockingWriteShouldHaveBeenInterrupted()
         {
-            Assert.IsTrue(_asyncWriteResult.IsCompleted);
+            Assert.IsTrue(_asyncWriteResult.AsyncWaitHandle.WaitOne(200));
         }
 
         [TestMethod]
-        public void WriteThatExceedsMaxBufferLengthShouldHaveThrownObjectDisposedException()
+        public void WriteShouldHaveThrownObjectDisposedException()
         {
+            _asyncWriteResult.AsyncWaitHandle.WaitOne(200);
+
             Assert.IsNotNull(_writeException);
             Assert.AreEqual(typeof (ObjectDisposedException), _writeException.GetType());
         }