SshCommandTest_BeginExecute_EndExecuteNotInvokedOnAsyncResultFromPreviousInvocation.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Globalization;
  3. using System.Text;
  4. using Microsoft.Extensions.Logging.Abstractions;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using Moq;
  7. using Renci.SshNet.Channels;
  8. using Renci.SshNet.Tests.Common;
  9. namespace Renci.SshNet.Tests.Classes
  10. {
  11. [TestClass]
  12. public class SshCommandTest_BeginExecute_EndExecuteNotInvokedOnAsyncResultFromPreviousInvocation : TestBase
  13. {
  14. private Mock<ISession> _sessionMock;
  15. private Mock<IChannelSession> _channelSessionMock;
  16. private string _commandText;
  17. private Encoding _encoding;
  18. private SshCommand _sshCommand;
  19. private InvalidOperationException _actualException;
  20. protected override void OnInit()
  21. {
  22. base.OnInit();
  23. Arrange();
  24. Act();
  25. }
  26. private void Arrange()
  27. {
  28. var random = new Random();
  29. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  30. _sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
  31. _channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
  32. _commandText = random.Next().ToString(CultureInfo.InvariantCulture);
  33. _encoding = Encoding.UTF8;
  34. var seq = new MockSequence();
  35. _sessionMock.InSequence(seq).Setup(p => p.CreateChannelSession()).Returns(_channelSessionMock.Object);
  36. _channelSessionMock.InSequence(seq).Setup(p => p.Open());
  37. _channelSessionMock.InSequence(seq).Setup(p => p.SendExecRequest(_commandText)).Returns(true);
  38. _sshCommand = new SshCommand(_sessionMock.Object, _commandText, _encoding);
  39. _sshCommand.BeginExecute();
  40. }
  41. private void Act()
  42. {
  43. try
  44. {
  45. _sshCommand.BeginExecute();
  46. Assert.Fail();
  47. }
  48. catch (InvalidOperationException ex)
  49. {
  50. _actualException = ex;
  51. }
  52. }
  53. [TestMethod]
  54. public void BeginExecuteShouldThrowInvalidOperationException()
  55. {
  56. Assert.IsNotNull(_actualException);
  57. Assert.IsNull(_actualException.InnerException);
  58. Assert.AreEqual("Asynchronous operation is already in progress.", _actualException.Message);
  59. }
  60. }
  61. }