From b86eba0d7c27ec2491b4dfbe7e0bb8839b65c25e Mon Sep 17 00:00:00 2001 From: drieseng Date: Sat, 25 Jun 2016 12:58:08 +0200 Subject: [PATCH] Harden PipeStream tests. --- .../Common/PipeStream_Close_BlockingRead.cs | 5 ++- .../Common/PipeStream_Close_BlockingWrite.cs | 39 +++++++++++-------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/Renci.SshNet.Tests/Common/PipeStream_Close_BlockingRead.cs b/src/Renci.SshNet.Tests/Common/PipeStream_Close_BlockingRead.cs index d0430285..b687a144 100644 --- a/src/Renci.SshNet.Tests/Common/PipeStream_Close_BlockingRead.cs +++ b/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] diff --git a/src/Renci.SshNet.Tests/Common/PipeStream_Close_BlockingWrite.cs b/src/Renci.SshNet.Tests/Common/PipeStream_Close_BlockingWrite.cs index 48c702b4..913f5d3d 100644 --- a/src/Renci.SshNet.Tests/Common/PipeStream_Close_BlockingWrite.cs +++ b/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); + { + _pipeStream.WriteByte(10); + _pipeStream.WriteByte(13); + _pipeStream.WriteByte(25); - try - { - _pipeStream.WriteByte(35); - } - catch (Exception ex) - { - _writeException = ex; - throw; - } - }; + // 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()); }