2
0

SftpClientTest_Finalize_Connected.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Moq;
  4. namespace Renci.SshNet.Tests.Classes
  5. {
  6. [TestClass]
  7. public class SftpClientTest_Finalize_Connected : SftpClientTestBase
  8. {
  9. private SftpClient _sftpClient;
  10. private ConnectionInfo _connectionInfo;
  11. private int _operationTimeout;
  12. private WeakReference _sftpClientWeakRefence;
  13. protected override void SetupData()
  14. {
  15. _connectionInfo = new ConnectionInfo("host", "user", new NoneAuthenticationMethod("userauth"));
  16. _operationTimeout = new Random().Next(1000, 10000);
  17. _sftpClient = new SftpClient(_connectionInfo, false, ServiceFactoryMock.Object)
  18. {
  19. OperationTimeout = TimeSpan.FromMilliseconds(_operationTimeout)
  20. };
  21. _sftpClientWeakRefence = new WeakReference(_sftpClient);
  22. }
  23. protected override void SetupMocks()
  24. {
  25. _ = ServiceFactoryMock.Setup(p => p.CreateSocketFactory())
  26. .Returns(SocketFactoryMock.Object);
  27. _ = ServiceFactoryMock.Setup(p => p.CreateSession(_connectionInfo, SocketFactoryMock.Object))
  28. .Returns(SessionMock.Object);
  29. _ = SessionMock.Setup(p => p.Connect());
  30. _ = ServiceFactoryMock.Setup(p => p.CreateSftpResponseFactory())
  31. .Returns(SftpResponseFactoryMock.Object);
  32. _ = ServiceFactoryMock.Setup(p => p.CreateSftpSession(SessionMock.Object, _operationTimeout, _connectionInfo.Encoding, SftpResponseFactoryMock.Object))
  33. .Returns(SftpSessionMock.Object);
  34. _ = SftpSessionMock.Setup(p => p.Connect());
  35. }
  36. protected override void Arrange()
  37. {
  38. base.Arrange();
  39. _sftpClient.Connect();
  40. _sftpClient = null;
  41. // We need to dereference all mocks as they might otherwise hold the target alive
  42. //(through recorded invocations?)
  43. CreateMocks();
  44. }
  45. protected override void Act()
  46. {
  47. GC.Collect();
  48. GC.WaitForPendingFinalizers();
  49. }
  50. [TestMethod]
  51. public void DisconnectOnSftpSessionShouldNeverBeInvoked()
  52. {
  53. // Since we recreated the mocks, this test has no value
  54. // We'll leaving ths test just in case we have a solution that does not require us
  55. // to recreate the mocks
  56. SftpSessionMock.Verify(p => p.Disconnect(), Times.Never);
  57. }
  58. [TestMethod]
  59. public void DisposeOnSftpSessionShouldNeverBeInvoked()
  60. {
  61. // Since we recreated the mocks, this test has no value
  62. // We'll leaving ths test just in case we have a solution that does not require us
  63. // to recreate the mocks
  64. SftpSessionMock.Verify(p => p.Dispose(), Times.Never);
  65. }
  66. [TestMethod]
  67. public void SftpClientShouldHaveBeenFinalized()
  68. {
  69. Assert.IsNull(_sftpClientWeakRefence.Target);
  70. }
  71. }
  72. }