Skip to content

Commit 590277f

Browse files
committed
Harden test.
1 parent 954db2f commit 590277f

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

src/Renci.SshNet.Tests/Classes/ForwardedPortDynamicTest_Started_SocketSendShutdownImmediately.cs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Net;
44
using System.Net.Sockets;
55
using System.Text;
6+
using System.Threading;
67
using Microsoft.VisualStudio.TestTools.UnitTesting;
78
using Moq;
89
using Renci.SshNet.Channels;
@@ -21,6 +22,8 @@ public class ForwardedPortDynamicTest_Started_SocketSendShutdownImmediately
2122
private IList<EventArgs> _closingRegister;
2223
private IList<ExceptionEventArgs> _exceptionRegister;
2324
private TimeSpan _connectionTimeout;
25+
private ManualResetEvent _channelDisposed;
26+
private IPEndPoint _forwardedPortEndPoint;
2427

2528
[TestInitialize]
2629
public void Initialize()
@@ -38,6 +41,7 @@ public void Cleanup()
3841
_connectionInfoMock.Setup(p => p.Timeout).Returns(TimeSpan.FromSeconds(5));
3942
_forwardedPort.Stop();
4043
}
44+
4145
if (_client != null)
4246
{
4347
if (_client.Connected)
@@ -47,13 +51,28 @@ public void Cleanup()
4751
_client = null;
4852
}
4953
}
54+
55+
if (_channelDisposed != null)
56+
{
57+
_channelDisposed.Dispose();
58+
_channelDisposed = null;
59+
}
5060
}
5161

5262
private void SetupData()
5363
{
5464
_closingRegister = new List<EventArgs>();
5565
_exceptionRegister = new List<ExceptionEventArgs>();
5666
_connectionTimeout = TimeSpan.FromSeconds(5);
67+
_channelDisposed = new ManualResetEvent(false);
68+
_forwardedPortEndPoint = new IPEndPoint(IPAddress.Loopback, 8122);
69+
70+
_forwardedPort = new ForwardedPortDynamic((uint) _forwardedPortEndPoint.Port);
71+
_forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
72+
_forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
73+
_forwardedPort.Session = _sessionMock.Object;
74+
75+
_client = new Socket(_forwardedPortEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
5776
}
5877

5978
private void CreateMocks()
@@ -72,30 +91,26 @@ private void SetupMocks()
7291
_sessionMock.InSequence(seq).Setup(p => p.ConnectionInfo).Returns(_connectionInfoMock.Object);
7392
_connectionInfoMock.InSequence(seq).Setup(p => p.Timeout).Returns(_connectionTimeout);
7493
_channelMock.InSequence(seq).Setup(p => p.Close());
75-
_channelMock.InSequence(seq).Setup(p => p.Dispose());
94+
_channelMock.InSequence(seq).Setup(p => p.Dispose()).Callback(() => _channelDisposed.Set());
7695
}
7796

7897
private void Arrange()
7998
{
80-
SetupData();
8199
CreateMocks();
100+
SetupData();
82101
SetupMocks();
83102

84-
_forwardedPort = new ForwardedPortDynamic(8122);
85-
_forwardedPort.Closing += (sender, args) => _closingRegister.Add(args);
86-
_forwardedPort.Exception += (sender, args) => _exceptionRegister.Add(args);
87-
_forwardedPort.Session = _sessionMock.Object;
88103
_forwardedPort.Start();
89104

90-
var endPoint = new IPEndPoint(IPAddress.Loopback, 8122);
91-
92-
_client = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
93-
_client.Connect(endPoint);
105+
_client.Connect(_forwardedPortEndPoint);
94106
}
95107

96108
private void Act()
97109
{
98110
_client.Shutdown(SocketShutdown.Send);
111+
112+
// wait for channel to be disposed
113+
_channelDisposed.WaitOne(TimeSpan.FromMilliseconds(200));
99114
}
100115

101116
[TestMethod]

0 commit comments

Comments
 (0)