SftpFileReaderTest_DisposeShouldUnblockReadAndReadAhead.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Moq;
  3. using Renci.SshNet.Abstractions;
  4. using Renci.SshNet.Sftp;
  5. using System;
  6. using System.Diagnostics;
  7. using System.Threading;
  8. using BufferedRead = Renci.SshNet.Sftp.SftpFileReader.BufferedRead;
  9. namespace Renci.SshNet.Tests.Classes.Sftp
  10. {
  11. [TestClass]
  12. public class SftpFileReaderTest_DisposeShouldUnblockReadAndReadAhead : SftpFileReaderTestBase
  13. {
  14. private const int ChunkLength = 32 * 1024;
  15. private MockSequence _seq;
  16. private byte[] _handle;
  17. private int _fileSize;
  18. private WaitHandle[] _waitHandleArray;
  19. private int _operationTimeout;
  20. private SftpCloseAsyncResult _closeAsyncResult;
  21. private SftpFileReader _reader;
  22. private ObjectDisposedException _actualException;
  23. protected override void SetupData()
  24. {
  25. var random = new Random();
  26. _handle = CreateByteArray(random, 5);
  27. _fileSize = 5000;
  28. _waitHandleArray = new WaitHandle[2];
  29. _operationTimeout = random.Next(10000, 20000);
  30. _closeAsyncResult = new SftpCloseAsyncResult(null, null);
  31. }
  32. protected override void SetupMocks()
  33. {
  34. _seq = new MockSequence();
  35. SftpSessionMock.InSequence(_seq)
  36. .Setup(p => p.CreateWaitHandleArray(It.IsNotNull<WaitHandle>(), It.IsNotNull<WaitHandle>()))
  37. .Returns<WaitHandle, WaitHandle>((disposingWaitHandle, semaphoreAvailableWaitHandle) =>
  38. {
  39. _waitHandleArray[0] = disposingWaitHandle;
  40. _waitHandleArray[1] = semaphoreAvailableWaitHandle;
  41. return _waitHandleArray;
  42. });
  43. SftpSessionMock.InSequence(_seq).Setup(p => p.OperationTimeout).Returns(_operationTimeout);
  44. SftpSessionMock.InSequence(_seq)
  45. .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout))
  46. .Returns(() => WaitAny(_waitHandleArray, _operationTimeout));
  47. SftpSessionMock.InSequence(_seq)
  48. .Setup(p => p.BeginRead(_handle, 0, ChunkLength, It.IsNotNull<AsyncCallback>(), It.IsAny<BufferedRead>()))
  49. .Returns((SftpReadAsyncResult)null);
  50. SftpSessionMock.InSequence(_seq).Setup(p => p.OperationTimeout).Returns(_operationTimeout);
  51. SftpSessionMock.InSequence(_seq)
  52. .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout))
  53. .Returns(() => WaitAny(_waitHandleArray, _operationTimeout));
  54. SftpSessionMock.InSequence(_seq).Setup(p => p.BeginClose(_handle, null, null)).Returns(_closeAsyncResult);
  55. SftpSessionMock.InSequence(_seq).Setup(p => p.EndClose(_closeAsyncResult));
  56. }
  57. protected override void Arrange()
  58. {
  59. base.Arrange();
  60. _reader = new SftpFileReader(_handle, SftpSessionMock.Object, ChunkLength, 1, _fileSize);
  61. }
  62. protected override void Act()
  63. {
  64. ThreadAbstraction.ExecuteThread(() =>
  65. {
  66. Thread.Sleep(500);
  67. _reader.Dispose();
  68. });
  69. try
  70. {
  71. _reader.Read();
  72. Assert.Fail();
  73. }
  74. catch (ObjectDisposedException ex)
  75. {
  76. _actualException = ex;
  77. }
  78. }
  79. [TestMethod]
  80. public void ReadShouldHaveThrownObjectDisposedException()
  81. {
  82. Assert.IsNotNull(_actualException);
  83. Assert.AreEqual(typeof(SftpFileReader).FullName, _actualException.ObjectName);
  84. }
  85. [TestMethod]
  86. public void ReadAfterDisposeShouldThrowObjectDisposedException()
  87. {
  88. try
  89. {
  90. _reader.Read();
  91. Assert.Fail();
  92. }
  93. catch (ObjectDisposedException ex)
  94. {
  95. Assert.IsNull(ex.InnerException);
  96. Assert.AreEqual(typeof(SftpFileReader).FullName, ex.ObjectName);
  97. }
  98. }
  99. public void HandleShouldHaveBeenClosed()
  100. {
  101. SftpSessionMock.Verify(p => p.BeginClose(_handle, null, null), Times.Once);
  102. SftpSessionMock.Verify(p => p.EndClose(_closeAsyncResult), Times.Once);
  103. }
  104. [TestMethod]
  105. public void DisposeShouldCompleteImmediatelyAndNotAttemptToCloseHandleAgain()
  106. {
  107. var stopwatch = Stopwatch.StartNew();
  108. _reader.Dispose();
  109. stopwatch.Stop();
  110. Assert.IsTrue(stopwatch.ElapsedMilliseconds < 200, "Dispose took too long to complete: " + stopwatch.ElapsedMilliseconds);
  111. SftpSessionMock.Verify(p => p.BeginClose(_handle, null, null), Times.Once);
  112. SftpSessionMock.Verify(p => p.EndClose(_closeAsyncResult), Times.Once);
  113. }
  114. }
  115. }