2
0

SftpFileReaderTest_ReadAheadEndInvokeException_PreventsFurtherReadAheads.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Moq;
  3. using Renci.SshNet.Abstractions;
  4. using Renci.SshNet.Common;
  5. using Renci.SshNet.Sftp;
  6. using System;
  7. using System.Diagnostics;
  8. using System.Threading;
  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 byte[] _chunk3;
  24. private SftpFileReader _reader;
  25. private ManualResetEvent _readAheadChunk2;
  26. private ManualResetEvent _readChunk2;
  27. private SshException _exception;
  28. private SshException _actualException;
  29. protected override void SetupData()
  30. {
  31. var random = new Random();
  32. _handle = CreateByteArray(random, 5);
  33. _chunk1 = CreateByteArray(random, ChunkLength);
  34. _chunk3 = CreateByteArray(random, ChunkLength);
  35. _fileSize = 3 * _chunk1.Length;
  36. _waitHandleArray = new WaitHandle[2];
  37. _operationTimeout = random.Next(10000, 20000);
  38. _closeAsyncResult = new SftpCloseAsyncResult(null, null);
  39. _readAheadChunk2 = new ManualResetEvent(false);
  40. _readChunk2 = new ManualResetEvent(false);
  41. _exception = new SshException();
  42. }
  43. protected override void SetupMocks()
  44. {
  45. _seq = new MockSequence();
  46. SftpSessionMock.InSequence(_seq)
  47. .Setup(p => p.CreateWaitHandleArray(It.IsNotNull<WaitHandle>(), It.IsNotNull<WaitHandle>()))
  48. .Returns<WaitHandle, WaitHandle>((disposingWaitHandle, semaphoreAvailableWaitHandle) =>
  49. {
  50. _waitHandleArray[0] = disposingWaitHandle;
  51. _waitHandleArray[1] = semaphoreAvailableWaitHandle;
  52. return _waitHandleArray;
  53. });
  54. SftpSessionMock.InSequence(_seq).Setup(p => p.OperationTimeout).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).Setup(p => p.OperationTimeout).Returns(_operationTimeout);
  67. SftpSessionMock.InSequence(_seq)
  68. .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout))
  69. .Returns(() => WaitAny(_waitHandleArray, _operationTimeout));
  70. SftpSessionMock.InSequence(_seq)
  71. .Setup(p => p.BeginRead(_handle, ChunkLength, ChunkLength, It.IsNotNull<AsyncCallback>(), It.IsAny<BufferedRead>()))
  72. .Callback<byte[], ulong, uint, AsyncCallback, object>((handle, offset, length, callback, state) =>
  73. {
  74. ThreadAbstraction.ExecuteThread(() =>
  75. {
  76. // signal that we're in the read-ahead for chunk2
  77. _readAheadChunk2.Set();
  78. // wait for client to start reading this chunk
  79. _readChunk2.WaitOne(TimeSpan.FromSeconds(5));
  80. // sleep a short time to make sure the client is in the blocking wait
  81. Thread.Sleep(500);
  82. // complete async read of chunk2 with exception
  83. var asyncResult = new SftpReadAsyncResult(callback, state);
  84. asyncResult.SetAsCompleted(_exception, false);
  85. });
  86. })
  87. .Returns((SftpReadAsyncResult)null);
  88. SftpSessionMock.InSequence(_seq).Setup(p => p.OperationTimeout).Returns(_operationTimeout);
  89. SftpSessionMock.InSequence(_seq)
  90. .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout))
  91. .Returns(() => WaitAny(_waitHandleArray, _operationTimeout));
  92. }
  93. protected override void Arrange()
  94. {
  95. base.Arrange();
  96. // use a max. read-ahead of 1 to allow us to verify that the next read-ahead is not done
  97. // when a read-ahead has failed
  98. _reader = new SftpFileReader(_handle, SftpSessionMock.Object, ChunkLength, 1, _fileSize);
  99. }
  100. protected override void Act()
  101. {
  102. _reader.Read();
  103. // wait until SftpFileReader has starting reading ahead chunk 2
  104. Assert.IsTrue(_readAheadChunk2.WaitOne(TimeSpan.FromSeconds(5)));
  105. // signal that we are about to read chunk 2
  106. _readChunk2.Set();
  107. try
  108. {
  109. _reader.Read();
  110. Assert.Fail();
  111. }
  112. catch (SshException ex)
  113. {
  114. _actualException = ex;
  115. }
  116. }
  117. [TestMethod]
  118. public void ReadOfSecondChunkShouldThrowExceptionThatOccurredInReadAhead()
  119. {
  120. Assert.IsNotNull(_actualException);
  121. Assert.AreSame(_exception, _actualException);
  122. }
  123. [TestMethod]
  124. public void ReadAfterReadAheadExceptionShouldRethrowExceptionThatOccurredInReadAhead()
  125. {
  126. try
  127. {
  128. _reader.Read();
  129. Assert.Fail();
  130. }
  131. catch (SshException ex)
  132. {
  133. Assert.AreSame(_exception, ex);
  134. }
  135. }
  136. [TestMethod]
  137. public void DisposeShouldCloseHandleAndCompleteImmediately()
  138. {
  139. SftpSessionMock.InSequence(_seq).Setup(p => p.BeginClose(_handle, null, null)).Returns(_closeAsyncResult);
  140. SftpSessionMock.InSequence(_seq).Setup(p => p.EndClose(_closeAsyncResult));
  141. var stopwatch = Stopwatch.StartNew();
  142. _reader.Dispose();
  143. stopwatch.Stop();
  144. Assert.IsTrue(stopwatch.ElapsedMilliseconds < 200, "Dispose took too long to complete: " + stopwatch.ElapsedMilliseconds);
  145. SftpSessionMock.Verify(p => p.BeginClose(_handle, null, null), Times.Once);
  146. SftpSessionMock.Verify(p => p.EndClose(_closeAsyncResult), Times.Once);
  147. }
  148. [TestMethod]
  149. public void ExceptionInReadAheadShouldPreventFurtherReadAheads()
  150. {
  151. SftpSessionMock.Verify(p => p.BeginRead(_handle, 2 * ChunkLength, ChunkLength, It.IsNotNull<AsyncCallback>(), It.IsAny<BufferedRead>()), Times.Never);
  152. }
  153. }
  154. }