SshCommandTest_BeginExecute_EndExecuteInvokedOnAsyncResultFromPreviousInvocation.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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.Common;
  8. using Renci.SshNet.Tests.Common;
  9. namespace Renci.SshNet.Tests.Classes
  10. {
  11. [TestClass]
  12. public class SshCommandTest_BeginExecute_EndExecuteInvokedOnAsyncResultFromPreviousInvocation : TestBase
  13. {
  14. private Mock<ISession> _sessionMock;
  15. private Mock<IChannelSession> _channelSessionAMock;
  16. private Mock<IChannelSession> _channelSessionBMock;
  17. private string _commandText;
  18. private Encoding _encoding;
  19. private SshCommand _sshCommand;
  20. private IAsyncResult _asyncResultA;
  21. private IAsyncResult _asyncResultB;
  22. protected override void OnInit()
  23. {
  24. base.OnInit();
  25. Arrange();
  26. Act();
  27. }
  28. private void Arrange()
  29. {
  30. var random = new Random();
  31. _sessionMock = new Mock<ISession>(MockBehavior.Strict);
  32. _channelSessionAMock = new Mock<IChannelSession>(MockBehavior.Strict);
  33. _channelSessionBMock = new Mock<IChannelSession>(MockBehavior.Strict);
  34. _commandText = random.Next().ToString(CultureInfo.InvariantCulture);
  35. _encoding = Encoding.UTF8;
  36. _asyncResultA = null;
  37. _asyncResultB = null;
  38. var seq = new MockSequence();
  39. _sessionMock.InSequence(seq).Setup(p => p.CreateChannelSession()).Returns(_channelSessionAMock.Object);
  40. _channelSessionAMock.InSequence(seq).Setup(p => p.Open());
  41. _channelSessionAMock.InSequence(seq).Setup(p => p.SendExecRequest(_commandText))
  42. .Returns(true)
  43. .Raises(c => c.Closed += null, new ChannelEventArgs(5));
  44. _channelSessionAMock.InSequence(seq).Setup(p => p.Dispose());
  45. _sshCommand = new SshCommand(_sessionMock.Object, _commandText, _encoding);
  46. _asyncResultA = _sshCommand.BeginExecute();
  47. _sshCommand.EndExecute(_asyncResultA);
  48. _sessionMock.InSequence(seq).Setup(p => p.CreateChannelSession()).Returns(_channelSessionBMock.Object);
  49. _channelSessionBMock.InSequence(seq).Setup(p => p.Open());
  50. _channelSessionBMock.InSequence(seq).Setup(p => p.SendExecRequest(_commandText)).Returns(true);
  51. }
  52. private void Act()
  53. {
  54. _asyncResultB = _sshCommand.BeginExecute();
  55. }
  56. [TestMethod]
  57. public void BeginExecuteShouldReturnNewAsyncResult()
  58. {
  59. Assert.IsNotNull(_asyncResultB);
  60. Assert.AreNotSame(_asyncResultA, _asyncResultB);
  61. }
  62. }
  63. }