1
1
using System ;
2
2
using System . Collections . Generic ;
3
- using System . Diagnostics ;
4
3
using System . Globalization ;
5
4
using System . Net ;
6
5
using System . Threading ;
@@ -22,8 +21,7 @@ public class ForwardedPortRemoteTest_Dispose_PortStarted_ChannelBound
22
21
private IList < ExceptionEventArgs > _exceptionRegister ;
23
22
private IPEndPoint _bindEndpoint ;
24
23
private IPEndPoint _remoteEndpoint ;
25
- private TimeSpan _expectedElapsedTime ;
26
- private TimeSpan _elapsedTimeOfStop ;
24
+ private TimeSpan _bindSleepTime ;
27
25
private uint _remoteChannelNumberWhileClosing ;
28
26
private uint _remoteWindowSizeWhileClosing ;
29
27
private uint _remotePacketSizeWhileClosing ;
@@ -32,23 +30,16 @@ public class ForwardedPortRemoteTest_Dispose_PortStarted_ChannelBound
32
30
private uint _remotePacketSizeStarted ;
33
31
private string _originatorAddress ;
34
32
private uint _originatorPort ;
33
+ private ManualResetEvent _channelBindStarted ;
34
+ private ManualResetEvent _channelBindCompleted ;
35
35
36
36
protected ForwardedPortRemote ForwardedPort { get ; private set ; }
37
37
38
38
[ TestInitialize ]
39
39
public void Setup ( )
40
40
{
41
41
Arrange ( ) ;
42
-
43
- // calculate stop time here instead of in Act() because that method can be overridden
44
-
45
- var stopwatch = new Stopwatch ( ) ;
46
- stopwatch . Start ( ) ;
47
-
48
42
Act ( ) ;
49
-
50
- stopwatch . Stop ( ) ;
51
- _elapsedTimeOfStop = stopwatch . Elapsed ;
52
43
}
53
44
54
45
[ TestCleanup ]
@@ -59,30 +50,59 @@ public void Cleanup()
59
50
ForwardedPort . Dispose ( ) ;
60
51
ForwardedPort = null ;
61
52
}
53
+
54
+ if ( _channelBindStarted != null )
55
+ {
56
+ _channelBindStarted . Dispose ( ) ;
57
+ _channelBindStarted = null ;
58
+ }
59
+
60
+ if ( _channelBindCompleted != null )
61
+ {
62
+ _channelBindCompleted . Dispose ( ) ;
63
+ _channelBindCompleted = null ;
64
+ }
62
65
}
63
66
64
- protected void Arrange ( )
67
+ private void CreateMocks ( )
68
+ {
69
+ _connectionInfoMock = new Mock < IConnectionInfo > ( MockBehavior . Strict ) ;
70
+ _sessionMock = new Mock < ISession > ( MockBehavior . Strict ) ;
71
+ _channelMock = new Mock < IChannelForwardedTcpip > ( MockBehavior . Strict ) ;
72
+ }
73
+
74
+ private void SetUpData ( )
65
75
{
66
76
var random = new Random ( ) ;
77
+
67
78
_closingRegister = new List < EventArgs > ( ) ;
68
79
_exceptionRegister = new List < ExceptionEventArgs > ( ) ;
69
80
_bindEndpoint = new IPEndPoint ( IPAddress . Any , random . Next ( IPEndPoint . MinPort , IPEndPoint . MaxPort ) ) ;
70
81
_remoteEndpoint = new IPEndPoint ( IPAddress . Parse ( "193.168.1.5" ) , random . Next ( IPEndPoint . MinPort , IPEndPoint . MaxPort ) ) ;
71
- _expectedElapsedTime = TimeSpan . FromMilliseconds ( random . Next ( 100 , 500 ) ) ;
72
- ForwardedPort = new ForwardedPortRemote ( _bindEndpoint . Address , ( uint ) _bindEndpoint . Port , _remoteEndpoint . Address , ( uint ) _remoteEndpoint . Port ) ;
82
+ _bindSleepTime = TimeSpan . FromMilliseconds ( random . Next ( 100 , 500 ) ) ;
73
83
_remoteChannelNumberWhileClosing = ( uint ) random . Next ( 0 , 1000 ) ;
74
84
_remoteWindowSizeWhileClosing = ( uint ) random . Next ( 0 , int . MaxValue ) ;
75
85
_remotePacketSizeWhileClosing = ( uint ) random . Next ( 0 , int . MaxValue ) ;
76
- _remoteChannelNumberStarted = ( uint ) random . Next ( 0 , 1000 ) ;
77
- _remoteWindowSizeStarted = ( uint ) random . Next ( 0 , int . MaxValue ) ;
78
- _remotePacketSizeStarted = ( uint ) random . Next ( 0 , int . MaxValue ) ;
86
+ _remoteChannelNumberStarted = ( uint ) random . Next ( 0 , 1000 ) ;
87
+ _remoteWindowSizeStarted = ( uint ) random . Next ( 0 , int . MaxValue ) ;
88
+ _remotePacketSizeStarted = ( uint ) random . Next ( 0 , int . MaxValue ) ;
79
89
_originatorAddress = random . Next ( ) . ToString ( CultureInfo . InvariantCulture ) ;
80
- _originatorPort = ( uint ) random . Next ( 0 , int . MaxValue ) ;
90
+ _originatorPort = ( uint ) random . Next ( 0 , int . MaxValue ) ;
91
+ _channelBindStarted = new ManualResetEvent ( false ) ;
92
+ _channelBindCompleted = new ManualResetEvent ( false ) ;
81
93
82
- _connectionInfoMock = new Mock < IConnectionInfo > ( MockBehavior . Strict ) ;
83
- _sessionMock = new Mock < ISession > ( MockBehavior . Strict ) ;
84
- _channelMock = new Mock < IChannelForwardedTcpip > ( MockBehavior . Strict ) ;
94
+ ForwardedPort = new ForwardedPortRemote ( _bindEndpoint . Address , ( uint ) _bindEndpoint . Port , _remoteEndpoint . Address , ( uint ) _remoteEndpoint . Port ) ;
95
+ ForwardedPort . Closing += ( sender , args ) =>
96
+ {
97
+ _closingRegister . Add ( args ) ;
98
+ _sessionMock . Raise ( p => p . ChannelOpenReceived += null , new MessageEventArgs < ChannelOpenMessage > ( new ChannelOpenMessage ( _remoteChannelNumberWhileClosing , _remoteWindowSizeWhileClosing , _remotePacketSizeWhileClosing , new ForwardedTcpipChannelInfo ( ForwardedPort . BoundHost , ForwardedPort . BoundPort , _originatorAddress , _originatorPort ) ) ) ) ;
99
+ } ;
100
+ ForwardedPort . Exception += ( sender , args ) => _exceptionRegister . Add ( args ) ;
101
+ ForwardedPort . Session = _sessionMock . Object ;
102
+ }
85
103
104
+ private void SetupMocks ( )
105
+ {
86
106
_connectionInfoMock . Setup ( p => p . Timeout ) . Returns ( TimeSpan . FromSeconds ( 15 ) ) ;
87
107
_sessionMock . Setup ( p => p . IsConnected ) . Returns ( true ) ;
88
108
_sessionMock . Setup ( p => p . ConnectionInfo ) . Returns ( _connectionInfoMock . Object ) ;
@@ -109,7 +129,12 @@ protected void Arrange()
109
129
p . Bind (
110
130
It . Is < IPEndPoint > (
111
131
ep => ep . Address . Equals ( _remoteEndpoint . Address ) && ep . Port == _remoteEndpoint . Port ) ,
112
- ForwardedPort ) ) . Callback ( ( ) => Thread . Sleep ( _expectedElapsedTime ) ) ;
132
+ ForwardedPort ) ) . Callback ( ( ) =>
133
+ {
134
+ _channelBindStarted . Set ( ) ;
135
+ Thread . Sleep ( _bindSleepTime ) ;
136
+ _channelBindCompleted . Set ( ) ;
137
+ } ) ;
113
138
_channelMock . Setup ( p => p . Close ( ) ) ;
114
139
_channelMock . Setup ( p => p . Dispose ( ) ) ;
115
140
_sessionMock . Setup (
@@ -118,37 +143,38 @@ protected void Arrange()
118
143
It . Is < GlobalRequestMessage > (
119
144
g =>
120
145
g . RequestName == GlobalRequestName . CancelTcpIpForward &&
121
- g . AddressToBind == ForwardedPort . BoundHost && g . PortToBind == ForwardedPort . BoundPort ) ) ) ;
122
- _sessionMock . Setup ( p => p . MessageListenerCompleted ) . Returns ( new ManualResetEvent ( true ) ) ;
146
+ g . AddressToBind == ForwardedPort . BoundHost && g . PortToBind == ForwardedPort . BoundPort ) ) ) . Callback (
147
+ ( ) =>
148
+ {
149
+ // raise event confirming that forwarded port was cancelled
150
+ _sessionMock . Raise ( p => p . RequestSuccessReceived += null , new MessageEventArgs < RequestSuccessMessage > ( new RequestSuccessMessage ( ) ) ) ;
151
+ } ) ;
152
+ _sessionMock . Setup ( p => p . MessageListenerCompleted ) . Returns ( new ManualResetEvent ( false ) ) ;
153
+ }
154
+
155
+ protected void Arrange ( )
156
+ {
157
+ CreateMocks ( ) ;
158
+ SetUpData ( ) ;
159
+ SetupMocks ( ) ;
123
160
124
- ForwardedPort . Closing += ( sender , args ) =>
125
- {
126
- _closingRegister . Add ( args ) ;
127
- _sessionMock . Raise ( p => p . ChannelOpenReceived += null , new MessageEventArgs < ChannelOpenMessage > ( new ChannelOpenMessage ( _remoteChannelNumberWhileClosing , _remoteWindowSizeWhileClosing , _remotePacketSizeWhileClosing , new ForwardedTcpipChannelInfo ( ForwardedPort . BoundHost , ForwardedPort . BoundPort , _originatorAddress , _originatorPort ) ) ) ) ;
128
- } ;
129
- ForwardedPort . Exception += ( sender , args ) => _exceptionRegister . Add ( args ) ;
130
- ForwardedPort . Session = _sessionMock . Object ;
131
161
ForwardedPort . Start ( ) ;
132
162
133
163
_sessionMock . Raise ( p => p . ChannelOpenReceived += null , new MessageEventArgs < ChannelOpenMessage > ( new ChannelOpenMessage ( _remoteChannelNumberStarted , _remoteWindowSizeStarted , _remotePacketSizeStarted , new ForwardedTcpipChannelInfo ( ForwardedPort . BoundHost , ForwardedPort . BoundPort , _originatorAddress , _originatorPort ) ) ) ) ;
164
+
165
+ // wait until channel is bound
166
+ Assert . IsTrue ( _channelBindStarted . WaitOne ( TimeSpan . FromMilliseconds ( 200 ) ) ) ;
134
167
}
135
168
136
169
protected virtual void Act ( )
137
170
{
138
- var stopwatch = new Stopwatch ( ) ;
139
- stopwatch . Start ( ) ;
140
-
141
171
ForwardedPort . Dispose ( ) ;
142
-
143
- stopwatch . Stop ( ) ;
144
- _elapsedTimeOfStop = stopwatch . Elapsed ;
145
172
}
146
173
147
174
[ TestMethod ]
148
- public void StopShouldBlockUntilBoundChannelHasClosed ( )
175
+ public void ShouldBlockUntilBindHasCompleted ( )
149
176
{
150
- Assert . IsTrue ( _elapsedTimeOfStop >= _expectedElapsedTime , string . Format ( "Expected {0} or greater but was {1}." , _expectedElapsedTime . TotalMilliseconds , _elapsedTimeOfStop . TotalMilliseconds ) ) ;
151
- Assert . IsTrue ( _elapsedTimeOfStop < _expectedElapsedTime . Add ( TimeSpan . FromMilliseconds ( 200 ) ) ) ;
177
+ Assert . IsTrue ( _channelBindCompleted . WaitOne ( 0 ) ) ;
152
178
}
153
179
154
180
[ TestMethod ]
0 commit comments