ChannelTest_OnSessionErrorOccurred_OnErrorOccurred_Exception.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using Moq;
  5. using Renci.SshNet.Common;
  6. namespace Renci.SshNet.Tests.Classes.Channels
  7. {
  8. [TestClass]
  9. public class ChannelTest_OnSessionErrorOccurred_OnErrorOccurred_Exception
  10. {
  11. private Mock<ISession> _sessionMock;
  12. private uint _localWindowSize;
  13. private uint _localPacketSize;
  14. private uint _localChannelNumber;
  15. private ChannelStub _channel;
  16. private IList<ExceptionEventArgs> _channelExceptionRegister;
  17. private Exception _onErrorOccurredException;
  18. private Exception _errorOccurredException;
  19. [TestInitialize]
  20. public void Initialize()
  21. {
  22. Arrange();
  23. Act();
  24. }
  25. private void Arrange()
  26. {
  27. var random = new Random();
  28. _localWindowSize = (uint)random.Next(1000, int.MaxValue);
  29. _localPacketSize = _localWindowSize - 1;
  30. _localChannelNumber = (uint)random.Next(0, int.MaxValue);
  31. _onErrorOccurredException = new SystemException();
  32. _channelExceptionRegister = new List<ExceptionEventArgs>();
  33. _errorOccurredException = new SystemException();
  34. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  35. _channel = new ChannelStub(_sessionMock.Object, _localChannelNumber, _localWindowSize, _localPacketSize);
  36. _channel.Exception += (sender, args) => _channelExceptionRegister.Add(args);
  37. _channel.OnErrorOccurredException = _onErrorOccurredException;
  38. }
  39. private void Act()
  40. {
  41. _sessionMock.Raise(s => s.ErrorOccured += null, new ExceptionEventArgs(_errorOccurredException));
  42. }
  43. [TestMethod]
  44. public void ExceptionEventShouldHaveFiredOnce()
  45. {
  46. Assert.AreEqual(1, _channelExceptionRegister.Count);
  47. Assert.AreSame(_onErrorOccurredException, _channelExceptionRegister[0].Exception);
  48. }
  49. [TestMethod]
  50. public void OnErrorOccuredShouldBeInvokedOnce()
  51. {
  52. Assert.AreEqual(1, _channel.OnErrorOccurredInvocations.Count);
  53. Assert.AreSame(_errorOccurredException, _channel.OnErrorOccurredInvocations[0]);
  54. }
  55. }
  56. }