Skip to content

Commit 82c288b

Browse files
committed
Harden test.
1 parent 590277f commit 82c288b

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

src/Renci.SshNet.Tests/Classes/Channels/ChannelTest_Close_SessionIsConnectedAndChannelIsOpen_EofReceived.cs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public class ChannelTest_Close_SessionIsConnectedAndChannelIsOpen_EofReceived
2020
private uint _remoteWindowSize;
2121
private uint _remotePacketSize;
2222
private ChannelStub _channel;
23-
private Stopwatch _closeTimer;
2423
private List<ChannelEventArgs> _channelClosedRegister;
2524
private IList<ExceptionEventArgs> _channelExceptionRegister;
25+
private ManualResetEvent _channelClosedReceived;
2626

2727
[TestInitialize]
2828
public void Initialize()
@@ -31,6 +31,16 @@ public void Initialize()
3131
Act();
3232
}
3333

34+
[TestCleanup]
35+
public void TearDown()
36+
{
37+
if (_channelClosedReceived != null)
38+
{
39+
_channelClosedReceived.Dispose();
40+
_channelClosedReceived = null;
41+
}
42+
}
43+
3444
private void Arrange()
3545
{
3646
var random = new Random();
@@ -40,9 +50,9 @@ private void Arrange()
4050
_remoteChannelNumber = (uint)random.Next(0, int.MaxValue);
4151
_remoteWindowSize = (uint)random.Next(0, int.MaxValue);
4252
_remotePacketSize = (uint)random.Next(0, int.MaxValue);
43-
_closeTimer = new Stopwatch();
4453
_channelClosedRegister = new List<ChannelEventArgs>();
4554
_channelExceptionRegister = new List<ExceptionEventArgs>();
55+
_channelClosedReceived = new ManualResetEvent(false);
4656

4757
_sessionMock = new Mock<ISession>(MockBehavior.Strict);
4858

@@ -58,19 +68,16 @@ private void Arrange()
5868
// raise ChannelCloseReceived event to set waithandle for receiving
5969
// SSH_MSG_CHANNEL_CLOSE message from server which is waited on after
6070
// sending the SSH_MSG_CHANNEL_CLOSE message to the server
61-
_sessionMock.Raise(s => s.ChannelCloseReceived += null,
62-
new MessageEventArgs<ChannelCloseMessage>(
63-
new ChannelCloseMessage(_localChannelNumber)));
71+
//
72+
// we're mocking the wait on the ChannelCloseMessage, but we still want
73+
// to get the channel in the state that it would have after actually receiving
74+
// the ChannelCloseMessage
75+
_sessionMock.Raise(s => s.ChannelCloseReceived += null, new MessageEventArgs<ChannelCloseMessage>(new ChannelCloseMessage(_localChannelNumber)));
76+
// signal that the ChannelCloseMessage was received; we use this to verify whether we've actually
77+
// waited on the EventWaitHandle to be set
78+
_channelClosedReceived.Set();
6479
}).Start();
65-
_closeTimer.Start();
66-
try
67-
{
68-
w.WaitOne();
69-
}
70-
finally
71-
{
72-
_closeTimer.Stop();
73-
}
80+
w.WaitOne();
7481
});
7582

7683
_channel = new ChannelStub(_sessionMock.Object, _localChannelNumber, _localWindowSize, _localPacketSize);
@@ -120,7 +127,7 @@ public void WaitOnHandleOnSessionShouldBeInvokedOnce()
120127
[TestMethod]
121128
public void WaitOnHandleOnSessionShouldWaitForChannelCloseMessageToBeReceived()
122129
{
123-
Assert.IsTrue(_closeTimer.ElapsedMilliseconds >= 100, "Elapsed milliseconds=" + _closeTimer.ElapsedMilliseconds);
130+
Assert.IsTrue(_channelClosedReceived.WaitOne(0));
124131
}
125132

126133
[TestMethod]

0 commit comments

Comments
 (0)