2
0

SshCommandTest_BeginExecute_EndExecuteInvokedOnAsyncResultFromPreviousInvocation.cs 2.8 KB

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