31
31
32
32
using System ;
33
33
using System . Collections . Generic ;
34
+ using System . Net . Sockets ;
34
35
using System . Text ;
35
36
using NUnit . Framework ;
36
-
37
37
using RabbitMQ . Client . Exceptions ;
38
+ using RabbitMQ . Client . Impl ;
38
39
39
40
namespace RabbitMQ . Client . Unit
40
41
{
@@ -44,12 +45,12 @@ public class TestConnectionFactory
44
45
[ Test ]
45
46
public void TestProperties ( )
46
47
{
47
- string u = "username" ;
48
+ string u = "username" ;
48
49
string pw = "password" ;
49
- string v = "vhost" ;
50
- string h = "192.168.0.1" ;
51
- int p = 5674 ;
52
- uint mms = 512 * 1024 * 1024 ;
50
+ string v = "vhost" ;
51
+ string h = "192.168.0.1" ;
52
+ int p = 5674 ;
53
+ uint mms = 512 * 1024 * 1024 ;
53
54
54
55
var cf = new ConnectionFactory
55
56
{
@@ -73,6 +74,35 @@ public void TestProperties()
73
74
Assert . AreEqual ( cf . Endpoint . MaxMessageSize , mms ) ;
74
75
}
75
76
77
+ [ Test ]
78
+ public void TestConnectionFactoryWithCustomSocketFactory ( )
79
+ {
80
+ const int defaultSocketBufsz = 8192 ; // From the docs
81
+ const int bufsz = 1024 ;
82
+
83
+ var cf = new ConnectionFactory
84
+ {
85
+ SocketFactory = ( AddressFamily af ) =>
86
+ {
87
+ var socket = new Socket ( af , SocketType . Stream , ProtocolType . Tcp )
88
+ {
89
+ SendBufferSize = bufsz ,
90
+ ReceiveBufferSize = bufsz ,
91
+ NoDelay = false
92
+ } ;
93
+ return new TcpClientAdapter ( socket ) ;
94
+ }
95
+ } ;
96
+
97
+ ITcpClient c = cf . SocketFactory ( AddressFamily . InterNetwork ) ;
98
+ Assert . IsAssignableFrom ( typeof ( TcpClientAdapter ) , c ) ;
99
+ TcpClientAdapter tcpClientAdapter = ( TcpClientAdapter ) c ;
100
+ Socket s = tcpClientAdapter . Client ;
101
+ Assert . AreNotEqual ( defaultSocketBufsz , s . ReceiveBufferSize ) ;
102
+ Assert . AreNotEqual ( defaultSocketBufsz , s . SendBufferSize ) ;
103
+ Assert . False ( s . NoDelay ) ;
104
+ }
105
+
76
106
[ Test ]
77
107
public void TestCreateConnectionUsesSpecifiedPort ( )
78
108
{
@@ -84,7 +114,7 @@ public void TestCreateConnectionUsesSpecifiedPort()
84
114
} ;
85
115
Assert . That ( ( ) =>
86
116
{
87
- using ( IConnection conn = cf . CreateConnection ( ) ) { }
117
+ using ( IConnection conn = cf . CreateConnection ( ) ) { }
88
118
} , Throws . TypeOf < BrokerUnreachableException > ( ) ) ;
89
119
}
90
120
@@ -99,7 +129,7 @@ public void TestCreateConnectionWithClientProvidedNameUsesSpecifiedPort()
99
129
} ;
100
130
Assert . That ( ( ) =>
101
131
{
102
- using ( IConnection conn = cf . CreateConnection ( "some_name" ) ) { }
132
+ using ( IConnection conn = cf . CreateConnection ( "some_name" ) ) { }
103
133
} , Throws . TypeOf < BrokerUnreachableException > ( ) ) ;
104
134
}
105
135
@@ -154,10 +184,10 @@ public void TestCreateConnectionAmqpTcpEndpointListAndClientProvidedName()
154
184
AutomaticRecoveryEnabled = true
155
185
} ;
156
186
var xs = new System . Collections . Generic . List < AmqpTcpEndpoint > { new AmqpTcpEndpoint ( "localhost" ) } ;
157
- using ( IConnection conn = cf . CreateConnection ( xs , "some_name" ) )
187
+ using ( IConnection conn = cf . CreateConnection ( xs , "some_name" ) )
158
188
{
159
- Assert . AreEqual ( "some_name" , conn . ClientProvidedName ) ;
160
- Assert . AreEqual ( "some_name" , conn . ClientProperties [ "connection_name" ] ) ;
189
+ Assert . AreEqual ( "some_name" , conn . ClientProvidedName ) ;
190
+ Assert . AreEqual ( "some_name" , conn . ClientProperties [ "connection_name" ] ) ;
161
191
}
162
192
}
163
193
@@ -169,7 +199,8 @@ public void TestCreateConnectionUsesDefaultPort()
169
199
AutomaticRecoveryEnabled = true ,
170
200
HostName = "localhost"
171
201
} ;
172
- using ( IConnection conn = cf . CreateConnection ( ) ) {
202
+ using ( IConnection conn = cf . CreateConnection ( ) )
203
+ {
173
204
Assert . AreEqual ( 5672 , conn . Endpoint . Port ) ;
174
205
}
175
206
}
@@ -217,7 +248,7 @@ public void TestCreateConnectionWithAutoRecoveryUsesAmqpTcpEndpoint()
217
248
Port = 1234
218
249
} ;
219
250
var ep = new AmqpTcpEndpoint ( "localhost" ) ;
220
- using ( IConnection conn = cf . CreateConnection ( new System . Collections . Generic . List < AmqpTcpEndpoint > { ep } ) ) { }
251
+ using ( IConnection conn = cf . CreateConnection ( new System . Collections . Generic . List < AmqpTcpEndpoint > { ep } ) ) { }
221
252
}
222
253
223
254
[ Test ]
@@ -230,7 +261,7 @@ public void TestCreateConnectionWithAutoRecoveryUsesInvalidAmqpTcpEndpoint()
230
261
var ep = new AmqpTcpEndpoint ( "localhost" , 1234 ) ;
231
262
Assert . That ( ( ) =>
232
263
{
233
- using ( IConnection conn = cf . CreateConnection ( new System . Collections . Generic . List < AmqpTcpEndpoint > { ep } ) ) { }
264
+ using ( IConnection conn = cf . CreateConnection ( new System . Collections . Generic . List < AmqpTcpEndpoint > { ep } ) ) { }
234
265
} , Throws . TypeOf < BrokerUnreachableException > ( ) ) ;
235
266
}
236
267
@@ -243,7 +274,7 @@ public void TestCreateConnectionUsesAmqpTcpEndpoint()
243
274
Port = 1234
244
275
} ;
245
276
var ep = new AmqpTcpEndpoint ( "localhost" ) ;
246
- using ( IConnection conn = cf . CreateConnection ( new System . Collections . Generic . List < AmqpTcpEndpoint > { ep } ) ) { }
277
+ using ( IConnection conn = cf . CreateConnection ( new System . Collections . Generic . List < AmqpTcpEndpoint > { ep } ) ) { }
247
278
}
248
279
249
280
[ Test ]
@@ -258,7 +289,7 @@ public void TestCreateConnectionWithForcedAddressFamily()
258
289
AddressFamily = System . Net . Sockets . AddressFamily . InterNetwork
259
290
} ;
260
291
cf . Endpoint = ep ;
261
- using ( IConnection conn = cf . CreateConnection ( ) ) { } ;
292
+ using ( IConnection conn = cf . CreateConnection ( ) ) { } ;
262
293
}
263
294
264
295
[ Test ]
@@ -268,7 +299,7 @@ public void TestCreateConnectionUsesInvalidAmqpTcpEndpoint()
268
299
var ep = new AmqpTcpEndpoint ( "localhost" , 1234 ) ;
269
300
Assert . That ( ( ) =>
270
301
{
271
- using ( IConnection conn = cf . CreateConnection ( new System . Collections . Generic . List < AmqpTcpEndpoint > { ep } ) ) { }
302
+ using ( IConnection conn = cf . CreateConnection ( new System . Collections . Generic . List < AmqpTcpEndpoint > { ep } ) ) { }
272
303
} , Throws . TypeOf < BrokerUnreachableException > ( ) ) ;
273
304
}
274
305
@@ -278,7 +309,7 @@ public void TestCreateConnectioUsesValidEndpointWhenMultipleSupplied()
278
309
var cf = new ConnectionFactory ( ) ;
279
310
var invalidEp = new AmqpTcpEndpoint ( "not_localhost" ) ;
280
311
var ep = new AmqpTcpEndpoint ( "localhost" ) ;
281
- using ( IConnection conn = cf . CreateConnection ( new List < AmqpTcpEndpoint > { invalidEp , ep } ) ) { } ;
312
+ using ( IConnection conn = cf . CreateConnection ( new List < AmqpTcpEndpoint > { invalidEp , ep } ) ) { } ;
282
313
}
283
314
284
315
[ Test ]
0 commit comments