NetConfClientTest_Finalize_Connected.cs 3.2 KB

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