2
0

SubsystemSession_Dispose_Disposed.cs 3.0 KB

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