3
3
using System . Net ;
4
4
using System . Net . Sockets ;
5
5
using System . Text ;
6
+ using System . Threading ;
6
7
using Microsoft . VisualStudio . TestTools . UnitTesting ;
7
8
using Moq ;
8
9
using Renci . SshNet . Channels ;
@@ -21,6 +22,8 @@ public class ForwardedPortDynamicTest_Started_SocketSendShutdownImmediately
21
22
private IList < EventArgs > _closingRegister ;
22
23
private IList < ExceptionEventArgs > _exceptionRegister ;
23
24
private TimeSpan _connectionTimeout ;
25
+ private ManualResetEvent _channelDisposed ;
26
+ private IPEndPoint _forwardedPortEndPoint ;
24
27
25
28
[ TestInitialize ]
26
29
public void Initialize ( )
@@ -38,6 +41,7 @@ public void Cleanup()
38
41
_connectionInfoMock . Setup ( p => p . Timeout ) . Returns ( TimeSpan . FromSeconds ( 5 ) ) ;
39
42
_forwardedPort . Stop ( ) ;
40
43
}
44
+
41
45
if ( _client != null )
42
46
{
43
47
if ( _client . Connected )
@@ -47,13 +51,28 @@ public void Cleanup()
47
51
_client = null ;
48
52
}
49
53
}
54
+
55
+ if ( _channelDisposed != null )
56
+ {
57
+ _channelDisposed . Dispose ( ) ;
58
+ _channelDisposed = null ;
59
+ }
50
60
}
51
61
52
62
private void SetupData ( )
53
63
{
54
64
_closingRegister = new List < EventArgs > ( ) ;
55
65
_exceptionRegister = new List < ExceptionEventArgs > ( ) ;
56
66
_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 ) ;
57
76
}
58
77
59
78
private void CreateMocks ( )
@@ -72,30 +91,26 @@ private void SetupMocks()
72
91
_sessionMock . InSequence ( seq ) . Setup ( p => p . ConnectionInfo ) . Returns ( _connectionInfoMock . Object ) ;
73
92
_connectionInfoMock . InSequence ( seq ) . Setup ( p => p . Timeout ) . Returns ( _connectionTimeout ) ;
74
93
_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 ( ) ) ;
76
95
}
77
96
78
97
private void Arrange ( )
79
98
{
80
- SetupData ( ) ;
81
99
CreateMocks ( ) ;
100
+ SetupData ( ) ;
82
101
SetupMocks ( ) ;
83
102
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 ;
88
103
_forwardedPort . Start ( ) ;
89
104
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 ) ;
94
106
}
95
107
96
108
private void Act ( )
97
109
{
98
110
_client . Shutdown ( SocketShutdown . Send ) ;
111
+
112
+ // wait for channel to be disposed
113
+ _channelDisposed . WaitOne ( TimeSpan . FromMilliseconds ( 200 ) ) ;
99
114
}
100
115
101
116
[ TestMethod ]
0 commit comments