Files
ssh.net/test/Renci.SshNet.Tests/Classes/SshCommandTest_EndExecute_AsyncResultFromOtherInstance.cs
Bart Vries d40bc43ac1 Refactor logging to allow a loggerfactory per session (#1673)
* Refactor logging to allow a loggerfactory per session specified in the ConnectionInfo.

This commit introduces an `ILoggerFactory` to various classes, replacing the static logger factory with an instance-based approach for more flexible and session-specific logging. These changes improve the logging framework's flexibility and maintainability and allow unit testing of logging.

* Improvements bases on feedback. Fixed tests. Added documentation.

* Update src/Renci.SshNet/ConnectionInfo.cs

---------

Co-authored-by: Rob Hague <rob.hague00@gmail.com>
2025-07-26 21:24:48 +02:00

94 lines
3.3 KiB
C#

using System;
using System.Globalization;
using System.Text;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Channels;
using Renci.SshNet.Tests.Common;
namespace Renci.SshNet.Tests.Classes
{
[TestClass]
public class SshCommandTest_EndExecute_AsyncResultFromOtherInstance : TestBase
{
private Mock<ISession> _sessionMock;
private Mock<IChannelSession> _channelSessionAMock;
private Mock<IChannelSession> _channelSessionBMock;
private string _commandText;
private Encoding _encoding;
private SshCommand _sshCommandA;
private SshCommand _sshCommandB;
private ArgumentException _actualException;
private IAsyncResult _asyncResultB;
protected override void OnInit()
{
base.OnInit();
Arrange();
Act();
}
private void Arrange()
{
_sessionMock = new Mock<ISession>(MockBehavior.Strict);
_sessionMock.Setup(p => p.SessionLoggerFactory).Returns(NullLoggerFactory.Instance);
_channelSessionAMock = new Mock<IChannelSession>(MockBehavior.Strict);
_channelSessionBMock = new Mock<IChannelSession>(MockBehavior.Strict);
_commandText = new Random().Next().ToString(CultureInfo.InvariantCulture);
_encoding = Encoding.UTF8;
_asyncResultB = null;
var seq = new MockSequence();
_ = _sessionMock.InSequence(seq)
.Setup(p => p.CreateChannelSession())
.Returns(_channelSessionAMock.Object);
_ = _channelSessionAMock.InSequence(seq)
.Setup(p => p.Open());
_ = _channelSessionAMock.InSequence(seq)
.Setup(p => p.SendExecRequest(_commandText))
.Returns(true);
_ = _sessionMock.InSequence(seq)
.Setup(p => p.CreateChannelSession())
.Returns(_channelSessionBMock.Object);
_ = _channelSessionBMock.InSequence(seq)
.Setup(p => p.Open());
_ = _channelSessionBMock.InSequence(seq)
.Setup(p => p.SendExecRequest(_commandText))
.Returns(true);
_sshCommandA = new SshCommand(_sessionMock.Object, _commandText, _encoding);
_ = _sshCommandA.BeginExecute();
_sshCommandB = new SshCommand(_sessionMock.Object, _commandText, _encoding);
_asyncResultB = _sshCommandB.BeginExecute();
}
private void Act()
{
try
{
_ = _sshCommandA.EndExecute(_asyncResultB);
Assert.Fail();
}
catch (ArgumentException ex)
{
_actualException = ex;
}
}
[TestMethod]
public void EndExecuteShouldHaveThrownArgumentException()
{
Assert.IsNotNull(_actualException);
Assert.IsNull(_actualException.InnerException);
Assert.AreEqual("asyncResult", _actualException.ParamName);
}
}
}