SshCommandTest_BeginExecute_EndExecuteNotInvokedOnAsyncResultFromPreviousInvocation.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Globalization;
  3. using System.Text;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using Moq;
  6. using Renci.SshNet.Channels;
  7. using Renci.SshNet.Tests.Common;
  8. namespace Renci.SshNet.Tests.Classes
  9. {
  10. [TestClass]
  11. public class SshCommandTest_BeginExecute_EndExecuteNotInvokedOnAsyncResultFromPreviousInvocation : TestBase
  12. {
  13. private Mock<ISession> _sessionMock;
  14. private Mock<IChannelSession> _channelSessionMock;
  15. private string _commandText;
  16. private Encoding _encoding;
  17. private SshCommand _sshCommand;
  18. private IAsyncResult _asyncResult;
  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. _channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
  31. _commandText = random.Next().ToString(CultureInfo.InvariantCulture);
  32. _encoding = Encoding.UTF8;
  33. _asyncResult = null;
  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));
  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. }