Skip to content

Commit 5ab8f3e

Browse files
authored
Stabilise some more tests (#1438)
* Stabilise some more tests * just increase the timeout
1 parent f512a41 commit 5ab8f3e

3 files changed

+36
-32
lines changed

test/Renci.SshNet.Tests/Classes/BaseClientTest_Connected_KeepAliveInterval_NotNegativeOne.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,7 @@ protected override void Act()
5959
{
6060
_client.KeepAliveInterval = _keepAliveInterval;
6161

62-
// allow keep-alive to be sent a few times. .NET 8 is faster and
63-
// we need to wait less because we want exactly three messages in a session.
64-
#if NETFRAMEWORK
65-
Thread.Sleep(195);
66-
#else
67-
Thread.Sleep(170);
68-
#endif
62+
Thread.Sleep(200);
6963
}
7064

7165
[TestMethod]
@@ -102,7 +96,9 @@ public void IsConnectedOnSessionShouldBeInvokedOnce()
10296
[TestMethod]
10397
public void SendMessageOnSessionShouldBeInvokedThreeTimes()
10498
{
105-
SessionMock.Verify(p => p.TrySendMessage(It.IsAny<IgnoreMessage>()), Times.Exactly(3));
99+
#pragma warning disable IDE0002 // Name can be simplified; "Ambiguous reference between Moq.Range and System.Range"
100+
SessionMock.Verify(p => p.TrySendMessage(It.IsAny<IgnoreMessage>()), Times.Between(2, 4, Moq.Range.Inclusive));
101+
#pragma warning restore IDE0002
106102
}
107103

108104
private class MyClient : BaseClient

test/Renci.SshNet.Tests/Classes/Channels/ChannelForwardedTcpipTest_Dispose_SessionIsConnectedAndChannelIsOpen.cs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public class ChannelForwardedTcpipTest_Dispose_SessionIsConnectedAndChannelIsOpe
3030
private TimeSpan _channelCloseTimeout;
3131
private IPEndPoint _remoteEndpoint;
3232
private AsyncSocketListener _remoteListener;
33-
private EventWaitHandle _channelBindFinishedWaitHandle;
3433
private Exception _channelException;
3534
private IList<Socket> _connectedRegister;
3635
private IList<Socket> _disconnectedRegister;
@@ -76,7 +75,6 @@ private void Arrange()
7675
_remoteWindowSize = (uint)random.Next(0, int.MaxValue);
7776
_remotePacketSize = (uint)random.Next(100, 200);
7877
_channelCloseTimeout = TimeSpan.FromSeconds(random.Next(10, 20));
79-
_channelBindFinishedWaitHandle = new ManualResetEvent(false);
8078
_channelException = null;
8179
_connectedRegister = new List<Socket>();
8280
_disconnectedRegister = new List<Socket>();
@@ -148,29 +146,27 @@ private void Arrange()
148146
_remotePacketSize);
149147

150148
_channelThread = new Thread(() =>
149+
{
150+
try
151+
{
152+
_channel.Bind(_remoteEndpoint, _forwardedPortMock.Object);
153+
}
154+
catch (Exception ex)
151155
{
152-
try
153-
{
154-
_channel.Bind(_remoteEndpoint, _forwardedPortMock.Object);
155-
}
156-
catch (Exception ex)
157-
{
158-
_channelException = ex;
159-
}
160-
finally
161-
{
162-
_ = _channelBindFinishedWaitHandle.Set();
163-
}
164-
});
156+
_channelException = ex;
157+
}
158+
});
165159
_channelThread.Start();
166160

167161
// give channel time to bind to remote endpoint
168-
Thread.Sleep(100);
162+
Thread.Sleep(300);
169163
}
170164

171165
private void Act()
172166
{
173167
_channel.Dispose();
168+
169+
Assert.IsTrue(_channelThread.Join(TimeSpan.FromSeconds(1)));
174170
}
175171

176172
[TestMethod]
@@ -185,7 +181,6 @@ public void ChannelShouldShutdownSocketToRemoteListener()
185181
public void BindShouldHaveFinishedWithoutException()
186182
{
187183
Assert.IsNull(_channelException, _channelException?.ToString());
188-
Assert.IsTrue(_channelBindFinishedWaitHandle.WaitOne(0));
189184
}
190185

191186
[TestMethod]

test/Renci.SshNet.Tests/Classes/SessionTest_Connected.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,29 @@ public void IncludeStrictKexPseudoAlgorithmInInitKex()
4747
[TestMethod]
4848
public void ShouldNotIncludeStrictKexPseudoAlgorithmInSubsequentKex()
4949
{
50-
ServerBytesReceivedRegister.Clear();
51-
Session.SendMessage(Session.ClientInitMessage);
50+
using var kexReceived = new ManualResetEventSlim();
51+
bool kexContainsPseudoAlg = true;
5252

53-
Thread.Sleep(100);
53+
ServerListener.BytesReceived += ServerListener_BytesReceived;
5454

55-
Assert.IsTrue(ServerBytesReceivedRegister.Count > 0);
55+
void ServerListener_BytesReceived(byte[] bytesReceived, System.Net.Sockets.Socket socket)
56+
{
57+
if (bytesReceived.Length > 5 && bytesReceived[5] == 20)
58+
{
59+
// SSH_MSG_KEXINIT = 20
60+
var kexInitMessage = new KeyExchangeInitMessage();
61+
kexInitMessage.Load(bytesReceived, 6, bytesReceived.Length - 6);
62+
kexContainsPseudoAlg = kexInitMessage.KeyExchangeAlgorithms.Contains("[email protected]");
63+
kexReceived.Set();
64+
}
65+
}
5666

57-
var kexInitMessage = new KeyExchangeInitMessage();
58-
kexInitMessage.Load(ServerBytesReceivedRegister[0], 4 + 1 + 1, ServerBytesReceivedRegister[0].Length - 4 - 1 - 1);
59-
Assert.IsFalse(kexInitMessage.KeyExchangeAlgorithms.Contains("[email protected]"));
67+
Session.SendMessage(Session.ClientInitMessage);
68+
69+
Assert.IsTrue(kexReceived.Wait(1000));
70+
Assert.IsFalse(kexContainsPseudoAlg);
71+
72+
ServerListener.BytesReceived -= ServerListener_BytesReceived;
6073
}
6174

6275
[TestMethod]

0 commit comments

Comments
 (0)