mirror of
https://github.com/sshnet/SSH.NET.git
synced 2026-09-10 09:15:47 +00:00
7d52787ce3
* make ConnectShouldActivateKeepAliveIfSessionIs test less flaky wait longer and expect at least two instead of exactly two. example: https://ci.appveyor.com/project/drieseng/ssh-net/builds/49849877/job/9rrtw6j8eu3i5p8o?fullLog=true * fix net462 build
87 lines
2.8 KiB
C#
87 lines
2.8 KiB
C#
using System;
|
|
using System.Threading;
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
using Moq;
|
|
|
|
using Renci.SshNet.Messages.Transport;
|
|
|
|
namespace Renci.SshNet.Tests.Classes
|
|
{
|
|
[TestClass]
|
|
public class BaseClientTest_NotConnected_KeepAliveInterval_NotNegativeOne : BaseClientTestBase
|
|
{
|
|
private BaseClient _client;
|
|
private ConnectionInfo _connectionInfo;
|
|
private TimeSpan _keepAliveInterval;
|
|
private int _keepAliveCount;
|
|
|
|
protected override void SetupData()
|
|
{
|
|
_connectionInfo = new ConnectionInfo("host", "user", new PasswordAuthenticationMethod("user", "pwd"));
|
|
_keepAliveInterval = TimeSpan.FromMilliseconds(100d);
|
|
_keepAliveCount = 0;
|
|
}
|
|
|
|
protected override void Arrange()
|
|
{
|
|
base.Arrange();
|
|
|
|
_client = new MyClient(_connectionInfo, false, ServiceFactoryMock.Object);
|
|
}
|
|
|
|
protected override void TearDown()
|
|
{
|
|
if (_client != null)
|
|
{
|
|
SessionMock.Setup(p => p.OnDisconnecting());
|
|
SessionMock.Setup(p => p.Dispose());
|
|
_client.Dispose();
|
|
}
|
|
}
|
|
|
|
protected override void Act()
|
|
{
|
|
_client.KeepAliveInterval = _keepAliveInterval;
|
|
|
|
// allow keep-alive to be sent at least once
|
|
Thread.Sleep(150);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void KeepAliveIntervalShouldReturnConfiguredValue()
|
|
{
|
|
Assert.AreEqual(_keepAliveInterval, _client.KeepAliveInterval);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ConnectShouldActivateKeepAliveIfSessionIs()
|
|
{
|
|
ServiceFactoryMock.Setup(p => p.CreateSocketFactory())
|
|
.Returns(SocketFactoryMock.Object);
|
|
ServiceFactoryMock.Setup(p => p.CreateSession(_connectionInfo, SocketFactoryMock.Object))
|
|
.Returns(SessionMock.Object);
|
|
SessionMock.Setup(p => p.Connect());
|
|
SessionMock.Setup(p => p.TrySendMessage(It.IsAny<IgnoreMessage>()))
|
|
.Returns(true)
|
|
.Callback(() => Interlocked.Increment(ref _keepAliveCount));
|
|
|
|
_client.Connect();
|
|
|
|
// allow keep-alive to be sent at least twice with some margin for error
|
|
Thread.Sleep(5 * (int)_keepAliveInterval.TotalMilliseconds);
|
|
|
|
// At least two keep-alives should be sent
|
|
SessionMock.Verify(p => p.TrySendMessage(It.IsAny<IgnoreMessage>()), Times.AtLeast(2));
|
|
}
|
|
|
|
private class MyClient : BaseClient
|
|
{
|
|
public MyClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServiceFactory serviceFactory) : base(connectionInfo, ownsConnectionInfo, serviceFactory)
|
|
{
|
|
}
|
|
}
|
|
}
|
|
}
|