2
0

SftpFileReaderTest_ReadAheadEndInvokeException_PreventsFurtherReadAheads.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.Common;
  8. using Renci.SshNet.Sftp;
  9. using BufferedRead = Renci.SshNet.Sftp.SftpFileReader.BufferedRead;
  10. namespace Renci.SshNet.Tests.Classes.Sftp
  11. {
  12. [TestClass]
  13. public class SftpFileReaderTest_ReadAheadEndInvokeException_PreventsFurtherReadAheads : SftpFileReaderTestBase
  14. {
  15. private const int ChunkLength = 32 * 1024;
  16. private MockSequence _seq;
  17. private byte[] _handle;
  18. private int _fileSize;
  19. private WaitHandle[] _waitHandleArray;
  20. private int _operationTimeout;
  21. private SftpCloseAsyncResult _closeAsyncResult;
  22. private byte[] _chunk1;
  23. private SftpFileReader _reader;
  24. private ManualResetEvent _readAheadChunk2;
  25. private ManualResetEvent _readChunk2;
  26. private SshException _exception;
  27. private SshException _actualException;
  28. protected override void SetupData()
  29. {
  30. var random = new Random();
  31. _handle = CreateByteArray(random, 5);
  32. _chunk1 = CreateByteArray(random, ChunkLength);
  33. _fileSize = 3 * _chunk1.Length;
  34. _waitHandleArray = new WaitHandle[2];
  35. _operationTimeout = random.Next(10000, 20000);
  36. _closeAsyncResult = new SftpCloseAsyncResult(null, null);
  37. _readAheadChunk2 = new ManualResetEvent(false);
  38. _readChunk2 = new ManualResetEvent(false);
  39. _exception = new SshException();
  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. .Callback<byte[], ulong, uint, AsyncCallback, object>((handle, offset, length, callback, state) =>
  61. {
  62. var asyncResult = new SftpReadAsyncResult(callback, state);
  63. asyncResult.SetAsCompleted(_chunk1, false);
  64. })
  65. .Returns((SftpReadAsyncResult)null);
  66. _ = SftpSessionMock.InSequence(_seq)
  67. .Setup(p => p.OperationTimeout)
  68. .Returns(_operationTimeout);
  69. _ = SftpSessionMock.InSequence(_seq)
  70. .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout))
  71. .Returns(() => WaitAny(_waitHandleArray, _operationTimeout));
  72. _ = SftpSessionMock.InSequence(_seq)
  73. .Setup(p => p.BeginRead(_handle, ChunkLength, ChunkLength, It.IsNotNull<AsyncCallback>(), It.IsAny<BufferedRead>()))
  74. .Callback<byte[], ulong, uint, AsyncCallback, object>((handle, offset, length, callback, state) =>
  75. {
  76. ThreadAbstraction.ExecuteThread(() =>
  77. {
  78. // signal that we're in the read-ahead for chunk2
  79. _ = _readAheadChunk2.Set();
  80. // wait for client to start reading this chunk
  81. _ = _readChunk2.WaitOne(TimeSpan.FromSeconds(5));
  82. // sleep a short time to make sure the client is in the blocking wait
  83. Thread.Sleep(500);
  84. // complete async read of chunk2 with exception
  85. var asyncResult = new SftpReadAsyncResult(callback, state);
  86. asyncResult.SetAsCompleted(_exception, false);
  87. });
  88. })
  89. .Returns((SftpReadAsyncResult)null);
  90. _ = SftpSessionMock.InSequence(_seq)
  91. .Setup(p => p.OperationTimeout)
  92. .Returns(_operationTimeout);
  93. _ = SftpSessionMock.InSequence(_seq)
  94. .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout))
  95. .Returns(() => WaitAny(_waitHandleArray, _operationTimeout));
  96. }
  97. protected override void Arrange()
  98. {
  99. base.Arrange();
  100. // use a max. read-ahead of 1 to allow us to verify that the next read-ahead is not done
  101. // when a read-ahead has failed
  102. _reader = new SftpFileReader(_handle, SftpSessionMock.Object, ChunkLength, 1, _fileSize);
  103. }
  104. protected override void Act()
  105. {
  106. _ = _reader.Read();
  107. // wait until SftpFileReader has starting reading ahead chunk 2
  108. Assert.IsTrue(_readAheadChunk2.WaitOne(TimeSpan.FromSeconds(5)));
  109. // signal that we are about to read chunk 2
  110. _ = _readChunk2.Set();
  111. try
  112. {
  113. _ = _reader.Read();
  114. Assert.Fail();
  115. }
  116. catch (SshException ex)
  117. {
  118. _actualException = ex;
  119. }
  120. }
  121. [TestMethod]
  122. public void ReadOfSecondChunkShouldThrowExceptionThatOccurredInReadAhead()
  123. {
  124. Assert.IsNotNull(_actualException);
  125. Assert.AreSame(_exception, _actualException);
  126. }
  127. [TestMethod]
  128. public void ReadAfterReadAheadExceptionShouldRethrowExceptionThatOccurredInReadAhead()
  129. {
  130. try
  131. {
  132. _ = _reader.Read();
  133. Assert.Fail();
  134. }
  135. catch (SshException ex)
  136. {
  137. Assert.AreSame(_exception, ex);
  138. }
  139. }
  140. [TestMethod]
  141. public void DisposeShouldCloseHandleAndCompleteImmediately()
  142. {
  143. _ = SftpSessionMock.InSequence(_seq)
  144. .Setup(p => p.IsOpen)
  145. .Returns(true);
  146. _ = SftpSessionMock.InSequence(_seq)
  147. .Setup(p => p.BeginClose(_handle, null, null))
  148. .Returns(_closeAsyncResult);
  149. _ = SftpSessionMock.InSequence(_seq)
  150. .Setup(p => p.EndClose(_closeAsyncResult));
  151. var stopwatch = Stopwatch.StartNew();
  152. _reader.Dispose();
  153. stopwatch.Stop();
  154. Assert.IsTrue(stopwatch.ElapsedMilliseconds < 200, "Dispose took too long to complete: " + stopwatch.ElapsedMilliseconds);
  155. SftpSessionMock.Verify(p => p.IsOpen, Times.Once);
  156. SftpSessionMock.Verify(p => p.BeginClose(_handle, null, null), Times.Once);
  157. SftpSessionMock.Verify(p => p.EndClose(_closeAsyncResult), Times.Once);
  158. }
  159. [TestMethod]
  160. public void ExceptionInReadAheadShouldPreventFurtherReadAheads()
  161. {
  162. SftpSessionMock.Verify(p => p.BeginRead(_handle, 2 * ChunkLength, ChunkLength, It.IsNotNull<AsyncCallback>(), It.IsAny<BufferedRead>()), Times.Never);
  163. }
  164. }
  165. }