SftpFileReaderTest_Read_ReadAheadExceptionInWaitOnHandle_ChunkAvailable.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using Moq;
  6. using Renci.SshNet.Common;
  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_Read_ReadAheadExceptionInWaitOnHandle_ChunkAvailable : 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 byte[] _chunk1;
  22. private byte[] _chunk2;
  23. private SftpFileReader _reader;
  24. private SshException _exception;
  25. private ManualResetEvent _exceptionSignaled;
  26. private SshException _actualException;
  27. protected override void SetupData()
  28. {
  29. var random = new Random();
  30. _handle = CreateByteArray(random, 5);
  31. _chunk1 = CreateByteArray(random, ChunkLength);
  32. _chunk2 = CreateByteArray(random, ChunkLength);
  33. _fileSize = _chunk1.Length + _chunk2.Length + 1;
  34. _waitHandleArray = new WaitHandle[2];
  35. _operationTimeout = random.Next(10000, 20000);
  36. _closeAsyncResult = new SftpCloseAsyncResult(null, null);
  37. _exception = new SshException();
  38. _exceptionSignaled = new ManualResetEvent(false);
  39. }
  40. protected override void SetupMocks()
  41. {
  42. _seq = new MockSequence();
  43. SftpSessionMock.InSequence(_seq)
  44. .Setup(p => p.CreateWaitHandleArray(It.IsNotNull<WaitHandle>(), It.IsNotNull<WaitHandle>()))
  45. .Returns<WaitHandle, WaitHandle>((disposingWaitHandle, semaphoreAvailableWaitHandle) =>
  46. {
  47. _waitHandleArray[0] = disposingWaitHandle;
  48. _waitHandleArray[1] = semaphoreAvailableWaitHandle;
  49. return _waitHandleArray;
  50. });
  51. SftpSessionMock.InSequence(_seq).Setup(p => p.OperationTimeout).Returns(_operationTimeout);
  52. SftpSessionMock.InSequence(_seq)
  53. .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout))
  54. .Returns(() => WaitAny(_waitHandleArray, _operationTimeout));
  55. SftpSessionMock.InSequence(_seq)
  56. .Setup(p => p.BeginRead(_handle, 0, ChunkLength, It.IsNotNull<AsyncCallback>(), It.IsAny<BufferedRead>()))
  57. .Callback<byte[], ulong, uint, AsyncCallback, object>((handle, offset, length, callback, state) =>
  58. {
  59. var asyncResult = new SftpReadAsyncResult(callback, state);
  60. asyncResult.SetAsCompleted(_chunk1, false);
  61. })
  62. .Returns((SftpReadAsyncResult)null);
  63. SftpSessionMock.InSequence(_seq).Setup(p => p.OperationTimeout).Returns(_operationTimeout);
  64. SftpSessionMock.InSequence(_seq)
  65. .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout))
  66. .Callback(() => _exceptionSignaled.Set())
  67. .Throws(_exception);
  68. }
  69. protected override void Arrange()
  70. {
  71. base.Arrange();
  72. _reader = new SftpFileReader(_handle, SftpSessionMock.Object, ChunkLength, 2, _fileSize);
  73. }
  74. protected override void Act()
  75. {
  76. // wait for the exception to be signaled by the second call to WaitAny
  77. _exceptionSignaled.WaitOne(5000);
  78. // allow a little time to allow SftpFileReader to process exception
  79. Thread.Sleep(100);
  80. try
  81. {
  82. _reader.Read();
  83. Assert.Fail();
  84. }
  85. catch (SshException ex)
  86. {
  87. _actualException = ex;
  88. }
  89. }
  90. [TestMethod]
  91. public void ReadShouldHaveRethrownExceptionThrownByWaitAny()
  92. {
  93. Assert.IsNotNull(_actualException);
  94. Assert.AreSame(_exception, _actualException);
  95. }
  96. [TestMethod]
  97. public void ReadShouldRethrowExceptionThrownByWaitAny()
  98. {
  99. try
  100. {
  101. _reader.Read();
  102. Assert.Fail();
  103. }
  104. catch (SshException ex)
  105. {
  106. Assert.AreSame(_exception, ex);
  107. }
  108. }
  109. [TestMethod]
  110. public void DisposeShouldCloseHandleAndCompleteImmediately()
  111. {
  112. SftpSessionMock.InSequence(_seq).Setup(p => p.IsOpen).Returns(true);
  113. SftpSessionMock.InSequence(_seq).Setup(p => p.BeginClose(_handle, null, null)).Returns(_closeAsyncResult);
  114. SftpSessionMock.InSequence(_seq).Setup(p => p.EndClose(_closeAsyncResult));
  115. var stopwatch = Stopwatch.StartNew();
  116. _reader.Dispose();
  117. stopwatch.Stop();
  118. Assert.IsTrue(stopwatch.ElapsedMilliseconds < 200, "Dispose took too long to complete: " + stopwatch.ElapsedMilliseconds);
  119. SftpSessionMock.Verify(p => p.IsOpen, Times.Once);
  120. SftpSessionMock.Verify(p => p.BeginClose(_handle, null, null), Times.Once);
  121. SftpSessionMock.Verify(p => p.EndClose(_closeAsyncResult), Times.Once);
  122. }
  123. }
  124. }