2
0

PipeStream_Close_BlockingWrite.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Threading;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using Renci.SshNet.Common;
  5. using Renci.SshNet.Tests.Common;
  6. namespace Renci.SshNet.Tests.Classes.Common
  7. {
  8. [TestClass]
  9. public class PipeStream_Close_BlockingWrite : TripleATestBase
  10. {
  11. private PipeStream _pipeStream;
  12. private Exception _writeException;
  13. private Thread _writehread;
  14. protected override void Arrange()
  15. {
  16. _pipeStream = new PipeStream { MaxBufferLength = 3 };
  17. _writehread = new Thread(() =>
  18. {
  19. _pipeStream.WriteByte(10);
  20. _pipeStream.WriteByte(13);
  21. _pipeStream.WriteByte(25);
  22. // attempting to write more bytes than the max. buffer length should block
  23. // until bytes are read or the stream is closed
  24. try
  25. {
  26. _pipeStream.WriteByte(35);
  27. }
  28. catch (Exception ex)
  29. {
  30. _writeException = ex;
  31. }
  32. });
  33. _writehread.Start();
  34. // ensure we've started writing
  35. Assert.IsFalse(_writehread.Join(50));
  36. }
  37. protected override void Act()
  38. {
  39. _pipeStream.Close();
  40. // give write time to complete
  41. _ = _writehread.Join(100);
  42. }
  43. [TestMethod]
  44. public void BlockingWriteShouldHaveBeenInterrupted()
  45. {
  46. Assert.AreEqual(ThreadState.Stopped, _writehread.ThreadState);
  47. }
  48. [TestMethod]
  49. public void WriteShouldHaveThrownObjectDisposedException()
  50. {
  51. Assert.IsNotNull(_writeException);
  52. Assert.AreEqual(typeof(ObjectDisposedException), _writeException.GetType());
  53. }
  54. }
  55. }