SftpFileReaderTest_Read_ReadAheadExceptionInWaitOnHandle_NoChunkAvailable.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Moq;
  3. using Renci.SshNet.Common;
  4. using Renci.SshNet.Sftp;
  5. using System;
  6. using System.Diagnostics;
  7. using System.Threading;
  8. using BufferedRead = Renci.SshNet.Sftp.SftpFileReader.BufferedRead;
  9. namespace Renci.SshNet.Tests.Classes.Sftp
  10. {
  11. [TestClass]
  12. public class SftpFileReaderTest_Read_ReadAheadExceptionInWaitOnHandle_NoChunkAvailable : 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 SshException _exception;
  23. private SshException _actualException;
  24. protected override void SetupData()
  25. {
  26. var random = new Random();
  27. _handle = CreateByteArray(random, 5);
  28. _fileSize = 1234;
  29. _waitHandleArray = new WaitHandle[2];
  30. _operationTimeout = random.Next(10000, 20000);
  31. _closeAsyncResult = new SftpCloseAsyncResult(null, null);
  32. _exception = new SshException();
  33. }
  34. protected override void SetupMocks()
  35. {
  36. _seq = new MockSequence();
  37. SftpSessionMock.InSequence(_seq)
  38. .Setup(p => p.CreateWaitHandleArray(It.IsNotNull<WaitHandle>(), It.IsNotNull<WaitHandle>()))
  39. .Returns<WaitHandle, WaitHandle>((disposingWaitHandle, semaphoreAvailableWaitHandle) =>
  40. {
  41. _waitHandleArray[0] = disposingWaitHandle;
  42. _waitHandleArray[1] = semaphoreAvailableWaitHandle;
  43. return _waitHandleArray;
  44. });
  45. SftpSessionMock.InSequence(_seq).Setup(p => p.OperationTimeout).Returns(_operationTimeout);
  46. SftpSessionMock.InSequence(_seq)
  47. .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout))
  48. .Returns(() => WaitAny(_waitHandleArray, _operationTimeout));
  49. SftpSessionMock.InSequence(_seq)
  50. .Setup(p => p.BeginRead(_handle, 0, ChunkLength, It.IsNotNull<AsyncCallback>(), It.IsAny<BufferedRead>()))
  51. .Returns((SftpReadAsyncResult)null);
  52. SftpSessionMock.InSequence(_seq).Setup(p => p.OperationTimeout).Returns(_operationTimeout);
  53. SftpSessionMock.InSequence(_seq)
  54. .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout))
  55. .Throws(_exception);
  56. }
  57. protected override void Arrange()
  58. {
  59. base.Arrange();
  60. _reader = new SftpFileReader(_handle, SftpSessionMock.Object, ChunkLength, 1, _fileSize);
  61. }
  62. protected override void Act()
  63. {
  64. try
  65. {
  66. _reader.Read();
  67. Assert.Fail();
  68. }
  69. catch (SshException ex)
  70. {
  71. _actualException = ex;
  72. }
  73. }
  74. [TestMethod]
  75. public void ReadShouldHaveRethrownExceptionThrownByWaitOnHandle()
  76. {
  77. Assert.IsNotNull(_actualException);
  78. Assert.AreSame(_exception, _actualException);
  79. }
  80. [TestMethod]
  81. public void ReadShouldRethrowExceptionThrownByWaitOnHandle()
  82. {
  83. try
  84. {
  85. _reader.Read();
  86. Assert.Fail();
  87. }
  88. catch (SshException ex)
  89. {
  90. Assert.AreSame(_exception, ex);
  91. }
  92. }
  93. [TestMethod]
  94. public void DisposeShouldCloseHandleAndCompleteImmediately()
  95. {
  96. SftpSessionMock.InSequence(_seq).Setup(p => p.IsOpen).Returns(true);
  97. SftpSessionMock.InSequence(_seq).Setup(p => p.BeginClose(_handle, null, null)).Returns(_closeAsyncResult);
  98. SftpSessionMock.InSequence(_seq).Setup(p => p.EndClose(_closeAsyncResult));
  99. var stopwatch = Stopwatch.StartNew();
  100. _reader.Dispose();
  101. stopwatch.Stop();
  102. Assert.IsTrue(stopwatch.ElapsedMilliseconds < 200, "Dispose took too long to complete: " + stopwatch.ElapsedMilliseconds);
  103. SftpSessionMock.Verify(p => p.IsOpen, Times.Once);
  104. SftpSessionMock.Verify(p => p.BeginClose(_handle, null, null), Times.Once);
  105. SftpSessionMock.Verify(p => p.EndClose(_closeAsyncResult), Times.Once);
  106. }
  107. }
  108. }