2
0

PipeStream_Close_BlockingRead.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.Threading;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Renci.SshNet.Common;
  4. using Renci.SshNet.Tests.Common;
  5. namespace Renci.SshNet.Tests.Classes.Common
  6. {
  7. [TestClass]
  8. public class PipeStream_Close_BlockingRead : TripleATestBase
  9. {
  10. private PipeStream _pipeStream;
  11. private int _bytesRead;
  12. private Thread _readThread;
  13. protected override void Arrange()
  14. {
  15. _pipeStream = new PipeStream();
  16. _pipeStream.WriteByte(10);
  17. _pipeStream.WriteByte(13);
  18. _pipeStream.WriteByte(25);
  19. _bytesRead = 123;
  20. _readThread = new Thread(() => _bytesRead = _pipeStream.Read(new byte[4], 0, 4));
  21. _readThread.Start();
  22. // ensure we've started reading
  23. Assert.IsFalse(_readThread.Join(50));
  24. }
  25. protected override void Act()
  26. {
  27. _pipeStream.Close();
  28. // give async read time to complete
  29. _ = _readThread.Join(100);
  30. }
  31. [TestMethod]
  32. public void BlockingReadShouldHaveBeenInterrupted()
  33. {
  34. Assert.AreEqual(ThreadState.Stopped, _readThread.ThreadState);
  35. }
  36. [TestMethod]
  37. public void ReadShouldHaveReturnedZero()
  38. {
  39. Assert.AreEqual(0, _bytesRead);
  40. }
  41. }
  42. }