SubsystemSession_Dispose_Disconnected.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using Microsoft.Extensions.Logging.Abstractions;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using Moq;
  7. using Renci.SshNet.Channels;
  8. using Renci.SshNet.Common;
  9. namespace Renci.SshNet.Tests.Classes
  10. {
  11. [TestClass]
  12. public class SubsystemSession_Dispose_Disconnected
  13. {
  14. private Mock<ISession> _sessionMock;
  15. private Mock<IChannelSession> _channelMock;
  16. private string _subsystemName;
  17. private SubsystemSessionStub _subsystemSession;
  18. private int _operationTimeout;
  19. private IList<EventArgs> _disconnectedRegister;
  20. private IList<ExceptionEventArgs> _errorOccurredRegister;
  21. [TestInitialize]
  22. public void Setup()
  23. {
  24. Arrange();
  25. Act();
  26. }
  27. protected void Arrange()
  28. {
  29. var random = new Random();
  30. _subsystemName = random.Next().ToString(CultureInfo.InvariantCulture);
  31. _operationTimeout = 30000;
  32. _disconnectedRegister = new List<EventArgs>();
  33. _errorOccurredRegister = new List<ExceptionEventArgs>();
  34. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  35. _sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  36. _channelMock = new Mock<IChannelSession>(MockBehavior.Strict);
  37. var sequence = new MockSequence();
  38. _ = _sessionMock.InSequence(sequence)
  39. .Setup(p => p.CreateChannelSession())
  40. .Returns(_channelMock.Object);
  41. _ = _channelMock.InSequence(sequence)
  42. .Setup(p => p.Open());
  43. _ = _channelMock.InSequence(sequence)
  44. .Setup(p => p.SendSubsystemRequest(_subsystemName))
  45. .Returns(true);
  46. _ = _channelMock.InSequence(sequence)
  47. .Setup(p => p.Dispose());
  48. _subsystemSession = new SubsystemSessionStub(
  49. _sessionMock.Object,
  50. _subsystemName,
  51. _operationTimeout);
  52. _subsystemSession.Disconnected += (sender, args) => _disconnectedRegister.Add(args);
  53. _subsystemSession.ErrorOccurred += (sender, args) => _errorOccurredRegister.Add(args);
  54. _subsystemSession.Connect();
  55. _subsystemSession.Disconnect();
  56. }
  57. protected void Act()
  58. {
  59. _subsystemSession.Dispose();
  60. }
  61. [TestMethod]
  62. public void DisconnectHasNeverFired()
  63. {
  64. Assert.AreEqual(0, _disconnectedRegister.Count);
  65. }
  66. [TestMethod]
  67. public void ErrorOccurredHasNeverFired()
  68. {
  69. Assert.AreEqual(0, _errorOccurredRegister.Count);
  70. }
  71. [TestMethod]
  72. public void IsOpenShouldReturnFalse()
  73. {
  74. Assert.IsFalse(_subsystemSession.IsOpen);
  75. }
  76. [TestMethod]
  77. public void DisposeOnChannelShouldBeInvokedOnce()
  78. {
  79. _channelMock.Verify(p => p.Dispose(), Times.Once);
  80. }
  81. }
  82. }