PipeStream_Close_BlockingRead.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.Threading;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using Renci.SshNet.Common;
  5. namespace Renci.SshNet.Tests.Common
  6. {
  7. [TestClass]
  8. public class PipeStream_Close_BlockingRead
  9. {
  10. private PipeStream _pipeStream;
  11. private int _bytesRead;
  12. private IAsyncResult _asyncReadResult;
  13. [TestInitialize]
  14. public void Init()
  15. {
  16. _pipeStream = new PipeStream();
  17. _pipeStream.WriteByte(10);
  18. _pipeStream.WriteByte(13);
  19. _pipeStream.WriteByte(25);
  20. _bytesRead = 123;
  21. Action readAction = () => _bytesRead = _pipeStream.Read(new byte[4], 0, 4);
  22. _asyncReadResult = readAction.BeginInvoke(null, null);
  23. // ensure we've started reading
  24. _asyncReadResult.AsyncWaitHandle.WaitOne(50);
  25. Act();
  26. }
  27. protected void Act()
  28. {
  29. _pipeStream.Close();
  30. }
  31. [TestMethod]
  32. public void BlockingReadShouldHaveBeenInterrupted()
  33. {
  34. Assert.IsTrue(_asyncReadResult.AsyncWaitHandle.WaitOne(200));
  35. }
  36. [TestMethod]
  37. public void ReadShouldHaveReturnedZero()
  38. {
  39. Assert.AreEqual(0, _bytesRead);
  40. }
  41. }
  42. }