SftpFileReaderTest_DisposeShouldUnblockReadAndReadAhead.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using Moq;
  6. using Renci.SshNet.Abstractions;
  7. using Renci.SshNet.Sftp;
  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. private AsyncCallback _readAsyncCallback;
  24. private EventWaitHandle _disposeCompleted;
  25. [TestCleanup]
  26. public void TearDown()
  27. {
  28. _disposeCompleted?.Dispose();
  29. }
  30. protected override void SetupData()
  31. {
  32. var random = new Random();
  33. _handle = CreateByteArray(random, 5);
  34. _fileSize = 5000;
  35. _waitHandleArray = new WaitHandle[2];
  36. _operationTimeout = random.Next(10000, 20000);
  37. _closeAsyncResult = new SftpCloseAsyncResult(null, null);
  38. _disposeCompleted = new ManualResetEvent(false);
  39. _readAsyncCallback = null;
  40. }
  41. protected override void SetupMocks()
  42. {
  43. _seq = new MockSequence();
  44. _ = SftpSessionMock.InSequence(_seq)
  45. .Setup(p => p.CreateWaitHandleArray(It.IsNotNull<WaitHandle>(), It.IsNotNull<WaitHandle>()))
  46. .Returns<WaitHandle, WaitHandle>((disposingWaitHandle, semaphoreAvailableWaitHandle) =>
  47. {
  48. _waitHandleArray[0] = disposingWaitHandle;
  49. _waitHandleArray[1] = semaphoreAvailableWaitHandle;
  50. return _waitHandleArray;
  51. });
  52. _ = SftpSessionMock.InSequence(_seq)
  53. .Setup(p => p.OperationTimeout)
  54. .Returns(_operationTimeout);
  55. _ = SftpSessionMock.InSequence(_seq)
  56. .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout))
  57. .Returns(() => WaitAny(_waitHandleArray, _operationTimeout));
  58. _ = SftpSessionMock.InSequence(_seq)
  59. .Setup(p => p.BeginRead(_handle, 0, ChunkLength, It.IsNotNull<AsyncCallback>(), It.IsAny<BufferedRead>()))
  60. .Returns<byte[], ulong, uint, AsyncCallback, object>((handle, offset, length, callback, state) =>
  61. {
  62. _readAsyncCallback = callback;
  63. return null;
  64. });
  65. _ = SftpSessionMock.InSequence(_seq)
  66. .Setup(p => p.OperationTimeout)
  67. .Returns(_operationTimeout);
  68. _ = SftpSessionMock.InSequence(_seq)
  69. .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout))
  70. .Returns(() => WaitAny(_waitHandleArray, _operationTimeout));
  71. _ = SftpSessionMock.InSequence(_seq)
  72. .Setup(p => p.IsOpen)
  73. .Returns(true);
  74. _ = SftpSessionMock.InSequence(_seq)
  75. .Setup(p => p.BeginClose(_handle, null, null))
  76. .Returns(_closeAsyncResult);
  77. _ = SftpSessionMock.InSequence(_seq)
  78. .Setup(p => p.EndClose(_closeAsyncResult));
  79. }
  80. protected override void Arrange()
  81. {
  82. base.Arrange();
  83. _reader = new SftpFileReader(_handle, SftpSessionMock.Object, ChunkLength, 1, _fileSize);
  84. }
  85. protected override void Act()
  86. {
  87. ThreadAbstraction.ExecuteThread(() =>
  88. {
  89. Thread.Sleep(500);
  90. _reader.Dispose();
  91. _ = _disposeCompleted.Set();
  92. });
  93. try
  94. {
  95. _ = _reader.Read();
  96. Assert.Fail();
  97. }
  98. catch (ObjectDisposedException ex)
  99. {
  100. _actualException = ex;
  101. }
  102. // Dispose may unblock Read() before the dispose has fully completed, so
  103. // let's wait until it has completed
  104. _ = _disposeCompleted.WaitOne(500);
  105. }
  106. [TestMethod]
  107. public void ReadShouldHaveThrownObjectDisposedException()
  108. {
  109. Assert.IsNotNull(_actualException);
  110. Assert.AreEqual(typeof(SftpFileReader).FullName, _actualException.ObjectName);
  111. }
  112. [TestMethod]
  113. public void ReadAfterDisposeShouldThrowObjectDisposedException()
  114. {
  115. try
  116. {
  117. _ = _reader.Read();
  118. Assert.Fail();
  119. }
  120. catch (ObjectDisposedException ex)
  121. {
  122. Assert.IsNull(ex.InnerException);
  123. Assert.AreEqual(typeof(SftpFileReader).FullName, ex.ObjectName);
  124. }
  125. }
  126. [TestMethod]
  127. public void HandleShouldHaveBeenClosed()
  128. {
  129. SftpSessionMock.Verify(p => p.BeginClose(_handle, null, null), Times.Once);
  130. SftpSessionMock.Verify(p => p.EndClose(_closeAsyncResult), Times.Once);
  131. }
  132. [TestMethod]
  133. public void DisposeShouldCompleteImmediatelyAndNotAttemptToCloseHandleAgain()
  134. {
  135. var stopwatch = Stopwatch.StartNew();
  136. _reader.Dispose();
  137. stopwatch.Stop();
  138. Assert.IsTrue(stopwatch.ElapsedMilliseconds < 200, "Dispose took too long to complete: " + stopwatch.ElapsedMilliseconds);
  139. SftpSessionMock.Verify(p => p.BeginClose(_handle, null, null), Times.Once);
  140. SftpSessionMock.Verify(p => p.EndClose(_closeAsyncResult), Times.Once);
  141. }
  142. [TestMethod]
  143. public void InvokeOfReadAheadCallbackShouldCompleteImmediately()
  144. {
  145. Assert.IsNotNull(_readAsyncCallback);
  146. _readAsyncCallback(new SftpReadAsyncResult(null, null));
  147. }
  148. }
  149. }