Skip to content

Commit b86eba0

Browse files
committed
Harden PipeStream tests.
1 parent f3f0239 commit b86eba0

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

src/Renci.SshNet.Tests/Common/PipeStream_Close_BlockingRead.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public void Init()
2525

2626
Action readAction = () => _bytesRead = _pipeStream.Read(new byte[4], 0, 4);
2727
_asyncReadResult = readAction.BeginInvoke(null, null);
28+
// ensure we've started reading
2829
_asyncReadResult.AsyncWaitHandle.WaitOne(50);
2930

3031
Act();
@@ -36,9 +37,9 @@ protected void Act()
3637
}
3738

3839
[TestMethod]
39-
public void AsyncReadShouldHaveFinished()
40+
public void BlockingReadShouldHaveBeenInterrupted()
4041
{
41-
Assert.IsTrue(_asyncReadResult.IsCompleted);
42+
Assert.IsTrue(_asyncReadResult.AsyncWaitHandle.WaitOne(200));
4243
}
4344

4445
[TestMethod]

src/Renci.SshNet.Tests/Common/PipeStream_Close_BlockingWrite.cs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,25 @@ public void Init()
1717
_pipeStream = new PipeStream {MaxBufferLength = 3};
1818

1919
Action writeAction = () =>
20-
{
21-
_pipeStream.WriteByte(10);
22-
_pipeStream.WriteByte(13);
23-
_pipeStream.WriteByte(25);
24-
25-
try
26-
{
27-
_pipeStream.WriteByte(35);
28-
}
29-
catch (Exception ex)
3020
{
31-
_writeException = ex;
32-
throw;
33-
}
34-
};
21+
_pipeStream.WriteByte(10);
22+
_pipeStream.WriteByte(13);
23+
_pipeStream.WriteByte(25);
24+
25+
// attempting to write more bytes than the max. buffer length should block
26+
// until bytes are read or the stream is closed
27+
try
28+
{
29+
_pipeStream.WriteByte(35);
30+
}
31+
catch (Exception ex)
32+
{
33+
_writeException = ex;
34+
throw;
35+
}
36+
};
3537
_asyncWriteResult = writeAction.BeginInvoke(null, null);
38+
// ensure we've started writing
3639
_asyncWriteResult.AsyncWaitHandle.WaitOne(50);
3740

3841
Act();
@@ -44,14 +47,16 @@ protected void Act()
4447
}
4548

4649
[TestMethod]
47-
public void AsyncWriteShouldHaveFinished()
50+
public void BlockingWriteShouldHaveBeenInterrupted()
4851
{
49-
Assert.IsTrue(_asyncWriteResult.IsCompleted);
52+
Assert.IsTrue(_asyncWriteResult.AsyncWaitHandle.WaitOne(200));
5053
}
5154

5255
[TestMethod]
53-
public void WriteThatExceedsMaxBufferLengthShouldHaveThrownObjectDisposedException()
56+
public void WriteShouldHaveThrownObjectDisposedException()
5457
{
58+
_asyncWriteResult.AsyncWaitHandle.WaitOne(200);
59+
5560
Assert.IsNotNull(_writeException);
5661
Assert.AreEqual(typeof (ObjectDisposedException), _writeException.GetType());
5762
}

0 commit comments

Comments
 (0)