SftpFileReaderTest_ReadAheadEndInvokeException_PreventsFurtherReadAheads.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 byte[] _chunk1;
  20. private byte[] _chunk3;
  21. private SftpFileReader _reader;
  22. private ManualResetEvent _readAheadChunk2;
  23. private ManualResetEvent _readChunk2;
  24. private SshException _exception;
  25. private SshException _actualException;
  26. protected override void SetupData()
  27. {
  28. var random = new Random();
  29. _handle = CreateByteArray(random, 5);
  30. _chunk1 = CreateByteArray(random, ChunkLength);
  31. _chunk3 = CreateByteArray(random, ChunkLength);
  32. _fileSize = 3 * _chunk1.Length;
  33. _readAheadChunk2 = new ManualResetEvent(false);
  34. _readChunk2 = new ManualResetEvent(false);
  35. _exception = new SshException();
  36. }
  37. protected override void SetupMocks()
  38. {
  39. _seq = new MockSequence();
  40. SftpSessionMock.InSequence(_seq)
  41. .Setup(p => p.BeginRead(_handle, 0, ChunkLength, It.IsNotNull<AsyncCallback>(), It.IsAny<BufferedRead>()))
  42. .Callback<byte[], ulong, uint, AsyncCallback, object>((handle, offset, length, callback, state) =>
  43. {
  44. var asyncResult = new SftpReadAsyncResult(callback, state);
  45. asyncResult.SetAsCompleted(_chunk1, false);
  46. })
  47. .Returns((SftpReadAsyncResult)null);
  48. SftpSessionMock.InSequence(_seq)
  49. .Setup(p => p.BeginRead(_handle, ChunkLength, ChunkLength, It.IsNotNull<AsyncCallback>(), It.IsAny<BufferedRead>()))
  50. .Callback<byte[], ulong, uint, AsyncCallback, object>((handle, offset, length, callback, state) =>
  51. {
  52. ThreadAbstraction.ExecuteThread(() =>
  53. {
  54. // signal that we're in the read-ahead for chunk2
  55. _readAheadChunk2.Set();
  56. // wait for client to start reading this chunk
  57. _readChunk2.WaitOne(TimeSpan.FromSeconds(5));
  58. // sleep a short time to make sure the client is in the blocking wait
  59. Thread.Sleep(500);
  60. // complete async read of chunk2 with exception
  61. var asyncResult = new SftpReadAsyncResult(callback, state);
  62. asyncResult.SetAsCompleted(_exception, false);
  63. });
  64. })
  65. .Returns((SftpReadAsyncResult)null);
  66. SftpSessionMock.InSequence(_seq)
  67. .Setup(p => p.BeginRead(_handle, 2 * ChunkLength, ChunkLength, It.IsNotNull<AsyncCallback>(), It.IsAny<BufferedRead>()))
  68. .Callback<byte[], ulong, uint, AsyncCallback, object>((handle, offset, length, callback, state) =>
  69. {
  70. // this chunk should never be read
  71. Thread.Sleep(20000);
  72. var asyncResult = new SftpReadAsyncResult(callback, state);
  73. asyncResult.SetAsCompleted(_chunk3, false);
  74. })
  75. .Returns((SftpReadAsyncResult)null);
  76. }
  77. protected override void Arrange()
  78. {
  79. base.Arrange();
  80. // use a max. read-ahead of 1 to allow us to verify that the next read-ahead is not done
  81. // when a read-ahead has failed
  82. _reader = new SftpFileReader(_handle, SftpSessionMock.Object, ChunkLength, 1, _fileSize);
  83. }
  84. protected override void Act()
  85. {
  86. _reader.Read();
  87. // wait until SftpFileReader has starting reading ahead chunk 2
  88. Assert.IsTrue(_readAheadChunk2.WaitOne(TimeSpan.FromSeconds(5)));
  89. // signal that we are about to read chunk 2
  90. _readChunk2.Set();
  91. try
  92. {
  93. _reader.Read();
  94. Assert.Fail();
  95. }
  96. catch (SshException ex)
  97. {
  98. _actualException = ex;
  99. }
  100. }
  101. [TestMethod]
  102. public void ReadOfSecondChunkShouldThrowExceptionThatOccurredInReadAhead()
  103. {
  104. Assert.IsNotNull(_actualException);
  105. Assert.AreSame(_exception, _actualException);
  106. }
  107. [TestMethod]
  108. public void ReadAfterReadAheadExceptionShouldThrowObjectDisposedException()
  109. {
  110. try
  111. {
  112. _reader.Read();
  113. Assert.Fail();
  114. }
  115. catch (ObjectDisposedException ex)
  116. {
  117. Assert.IsNull(ex.InnerException);
  118. Assert.AreEqual(typeof(SftpFileReader).FullName, ex.ObjectName);
  119. }
  120. }
  121. [TestMethod]
  122. public void DisposeShouldCloseHandleAndCompleteImmediately()
  123. {
  124. SftpSessionMock.InSequence(_seq).Setup(p => p.RequestClose(_handle));
  125. var stopwatch = Stopwatch.StartNew();
  126. _reader.Dispose();
  127. stopwatch.Stop();
  128. Assert.IsTrue(stopwatch.ElapsedMilliseconds < 200, "Dispose took too long to complete: " + stopwatch.ElapsedMilliseconds);
  129. SftpSessionMock.Verify(p => p.RequestClose(_handle), Times.Once);
  130. }
  131. [TestMethod]
  132. public void ExceptionInReadAheadShouldPreventFurtherReadAheads()
  133. {
  134. SftpSessionMock.Verify(p => p.BeginRead(_handle, 2 * ChunkLength, ChunkLength, It.IsNotNull<AsyncCallback>(), It.IsAny<BufferedRead>()), Times.Never);
  135. }
  136. }
  137. }