2
0

NetConfClientTest_Finalize_Connected.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. {
  19. OperationTimeout = TimeSpan.FromMilliseconds(_operationTimeout)
  20. };
  21. _netConfClientWeakRefence = new WeakReference(_netConfClient);
  22. }
  23. protected override void SetupMocks()
  24. {
  25. var sequence = new MockSequence();
  26. _ = ServiceFactoryMock.InSequence(sequence)
  27. .Setup(p => p.CreateSocketFactory())
  28. .Returns(SocketFactoryMock.Object);
  29. _ = ServiceFactoryMock.InSequence(sequence)
  30. .Setup(p => p.CreateSession(_connectionInfo, SocketFactoryMock.Object))
  31. .Returns(SessionMock.Object);
  32. _ = SessionMock.InSequence(sequence)
  33. .Setup(p => p.Connect());
  34. _ = ServiceFactoryMock.InSequence(sequence)
  35. .Setup(p => p.CreateNetConfSession(SessionMock.Object, _operationTimeout))
  36. .Returns(NetConfSessionMock.Object);
  37. _ = NetConfSessionMock.InSequence(sequence)
  38. .Setup(p => p.Connect());
  39. }
  40. protected override void Arrange()
  41. {
  42. base.Arrange();
  43. _netConfClient.Connect();
  44. _netConfClient = null;
  45. // We need to dereference all mocks as they might otherwise hold the target alive
  46. //(through recorded invocations?)
  47. CreateMocks();
  48. }
  49. protected override void Act()
  50. {
  51. GC.Collect();
  52. GC.WaitForPendingFinalizers();
  53. }
  54. [TestMethod]
  55. public void DisconnectOnNetConfSessionShouldBeInvokedOnce()
  56. {
  57. // Since we recreated the mocks, this test has no value
  58. // We'll leaving ths test just in case we have a solution that does not require us
  59. // to recreate the mocks
  60. NetConfSessionMock.Verify(p => p.Disconnect(), Times.Never);
  61. }
  62. [TestMethod]
  63. public void DisposeOnNetConfSessionShouldBeInvokedOnce()
  64. {
  65. // Since we recreated the mocks, this test has no value
  66. // We'll leaving ths test just in case we have a solution that does not require us
  67. // to recreate the mocks
  68. NetConfSessionMock.Verify(p => p.Dispose(), Times.Never);
  69. }
  70. [TestMethod]
  71. public void NetConfClientShouldHaveBeenFinalized()
  72. {
  73. Assert.IsNull(_netConfClientWeakRefence.Target);
  74. }
  75. }
  76. }