Skip to content

Commit 56bd2c9

Browse files
committed
Make TcpClientAdapter public
Improve test for SocketFactory Follow-up to: * #1414 * #1415 * #1416 https://groups.google.com/g/rabbitmq-users/c/9_ohuUbX9NY
1 parent b2586ed commit 56bd2c9

File tree

7 files changed

+51
-43
lines changed

7 files changed

+51
-43
lines changed

projects/RabbitMQ.Client/client/api/ITcpClient.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
namespace RabbitMQ.Client
77
{
88
/// <summary>
9-
/// Wrapper interface for standard TCP-client. Provides socket for socket frame handler class.
9+
/// Wrapper interface for <see cref="Socket"/>.
10+
/// Provides the socket for socket frame handler class.
1011
/// </summary>
1112
/// <remarks>Contains all methods that are currently in use in rabbitmq client.</remarks>
1213
public interface ITcpClient : IDisposable

projects/RabbitMQ.Client/client/impl/TcpClientAdapter.cs renamed to projects/RabbitMQ.Client/client/api/TcpClientAdapter.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
24
using System.Net;
35
using System.Net.Sockets;
46
using System.Threading.Tasks;
57

6-
namespace RabbitMQ.Client.Impl
8+
namespace RabbitMQ.Client
79
{
810
/// <summary>
9-
/// Simple wrapper around TcpClient.
11+
/// Simple wrapper around <see cref="Socket"/>.
1012
/// </summary>
11-
internal class TcpClientAdapter : ITcpClient
13+
public class TcpClientAdapter : ITcpClient
1214
{
1315
private Socket _sock;
1416

@@ -21,7 +23,7 @@ public virtual async Task ConnectAsync(string host, int port)
2123
{
2224
AssertSocket();
2325
IPAddress[] adds = await Dns.GetHostAddressesAsync(host).ConfigureAwait(false);
24-
IPAddress ep = TcpClientAdapterHelper.GetMatchingHost(adds, _sock.AddressFamily);
26+
IPAddress ep = GetMatchingHost(adds, _sock.AddressFamily);
2527
if (ep == default(IPAddress))
2628
{
2729
throw new ArgumentException($"No ip address could be resolved for {host}");
@@ -38,12 +40,11 @@ public virtual Task ConnectAsync(IPAddress ep, int port)
3840

3941
public virtual void Close()
4042
{
41-
_sock?.Dispose();
43+
_sock.Dispose();
4244
_sock = null;
4345
}
4446

45-
[Obsolete("Override Dispose(bool) instead.")]
46-
public virtual void Dispose()
47+
public void Dispose()
4748
{
4849
Dispose(true);
4950
}
@@ -52,11 +53,8 @@ protected virtual void Dispose(bool disposing)
5253
{
5354
if (disposing)
5455
{
55-
// dispose managed resources
5656
Close();
5757
}
58-
59-
// dispose unmanaged resources
6058
}
6159

6260
public virtual NetworkStream GetStream()
@@ -106,5 +104,15 @@ private void AssertSocket()
106104
throw new InvalidOperationException("Cannot perform operation as socket is null");
107105
}
108106
}
107+
108+
public static IPAddress GetMatchingHost(IReadOnlyCollection<IPAddress> addresses, AddressFamily addressFamily)
109+
{
110+
IPAddress ep = addresses.FirstOrDefault(a => a.AddressFamily == addressFamily);
111+
if (ep is null && addresses.Count == 1 && addressFamily == AddressFamily.Unspecified)
112+
{
113+
return addresses.Single();
114+
}
115+
return ep;
116+
}
109117
}
110118
}

projects/RabbitMQ.Client/client/impl/SocketFrameHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public SocketFrameHandler(AmqpTcpEndpoint endpoint,
117117

118118
// Resolve the hostname to know if it's even possible to even try IPv6
119119
IPAddress[] adds = Dns.GetHostAddresses(endpoint.HostName);
120-
IPAddress ipv6 = TcpClientAdapterHelper.GetMatchingHost(adds, AddressFamily.InterNetworkV6);
120+
IPAddress ipv6 = TcpClientAdapter.GetMatchingHost(adds, AddressFamily.InterNetworkV6);
121121

122122
if (ipv6 == default(IPAddress))
123123
{
@@ -141,7 +141,7 @@ public SocketFrameHandler(AmqpTcpEndpoint endpoint,
141141

142142
if (_socket is null)
143143
{
144-
IPAddress ipv4 = TcpClientAdapterHelper.GetMatchingHost(adds, AddressFamily.InterNetwork);
144+
IPAddress ipv4 = TcpClientAdapter.GetMatchingHost(adds, AddressFamily.InterNetwork);
145145
if (ipv4 == default(IPAddress))
146146
{
147147
throw new ConnectFailureException("Connection failed", new ArgumentException($"No ip address could be resolved for {endpoint.HostName}"));

projects/RabbitMQ.Client/client/impl/TcpClientAdapterHelper.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

projects/Unit/APIApproval.Approve.verified.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,20 @@ namespace RabbitMQ.Client
765765
public string ServerName { get; set; }
766766
public System.Security.Authentication.SslProtocols Version { get; set; }
767767
}
768+
public class TcpClientAdapter : RabbitMQ.Client.ITcpClient, System.IDisposable
769+
{
770+
public TcpClientAdapter(System.Net.Sockets.Socket socket) { }
771+
public virtual System.Net.Sockets.Socket Client { get; }
772+
public virtual bool Connected { get; }
773+
public virtual System.TimeSpan ReceiveTimeout { get; set; }
774+
public virtual void Close() { }
775+
public virtual System.Threading.Tasks.Task ConnectAsync(System.Net.IPAddress ep, int port) { }
776+
public virtual System.Threading.Tasks.Task ConnectAsync(string host, int port) { }
777+
public void Dispose() { }
778+
protected virtual void Dispose(bool disposing) { }
779+
public virtual System.Net.Sockets.NetworkStream GetStream() { }
780+
public static System.Net.IPAddress GetMatchingHost(System.Collections.Generic.IReadOnlyCollection<System.Net.IPAddress> addresses, System.Net.Sockets.AddressFamily addressFamily) { }
781+
}
768782
public class TimerBasedCredentialRefresher : RabbitMQ.Client.ICredentialsRefresher
769783
{
770784
public TimerBasedCredentialRefresher() { }

projects/Unit/TestConnectionFactory.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
using System.Collections.Generic;
3333
using System.Net.Sockets;
3434
using RabbitMQ.Client.Exceptions;
35-
using RabbitMQ.Client.Impl;
3635
using Xunit;
3736

3837
namespace RabbitMQ.Client.Unit
@@ -74,16 +73,23 @@ public void TestProperties()
7473
[Fact]
7574
public void TestConnectionFactoryWithCustomSocketFactory()
7675
{
77-
const int bufsz = 1024;
76+
const int testBufsz = 1024;
77+
int defaultReceiveBufsz = 0;
78+
int defaultSendBufsz = 0;
79+
using (var defaultSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP))
80+
{
81+
defaultReceiveBufsz = defaultSocket.ReceiveBufferSize;
82+
defaultSendBufsz = defaultSocket.SendBufferSize;
83+
}
7884

7985
ConnectionFactory cf = new()
8086
{
8187
SocketFactory = (AddressFamily af) =>
8288
{
8389
var socket = new Socket(af, SocketType.Stream, ProtocolType.Tcp)
8490
{
85-
SendBufferSize = bufsz,
86-
ReceiveBufferSize = bufsz,
91+
SendBufferSize = testBufsz,
92+
ReceiveBufferSize = testBufsz,
8793
NoDelay = false
8894
};
8995
return new TcpClientAdapter(socket);
@@ -94,8 +100,8 @@ public void TestConnectionFactoryWithCustomSocketFactory()
94100
Assert.IsType<TcpClientAdapter>(c);
95101
TcpClientAdapter tcpClientAdapter = (TcpClientAdapter)c;
96102
Socket s = tcpClientAdapter.Client;
97-
Assert.Equal(bufsz, s.ReceiveBufferSize);
98-
Assert.Equal(bufsz, s.SendBufferSize);
103+
Assert.NotEqual(defaultReceiveBufsz, s.ReceiveBufferSize);
104+
Assert.NotEqual(defaultSendBufsz, s.SendBufferSize);
99105
Assert.False(s.NoDelay);
100106
}
101107

projects/Unit/TestTcpClientAdapter.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
using System.Net;
3333
using System.Net.Sockets;
34-
using RabbitMQ.Client.Impl;
3534
using Xunit;
3635

3736
namespace RabbitMQ.Client.Unit
@@ -42,15 +41,15 @@ public class TestTcpClientAdapter
4241
public void TcpClientAdapterHelperGetMatchingHostReturnNoAddressIfFamilyDoesNotMatch()
4342
{
4443
var address = IPAddress.Parse("127.0.0.1");
45-
IPAddress matchingAddress = TcpClientAdapterHelper.GetMatchingHost(new[] { address }, AddressFamily.InterNetworkV6);
44+
IPAddress matchingAddress = TcpClientAdapter.GetMatchingHost(new[] { address }, AddressFamily.InterNetworkV6);
4645
Assert.Null(matchingAddress);
4746
}
4847

4948
[Fact]
5049
public void TcpClientAdapterHelperGetMatchingHostReturnsSingleAddressIfFamilyIsUnspecified()
5150
{
5251
var address = IPAddress.Parse("1.1.1.1");
53-
IPAddress matchingAddress = TcpClientAdapterHelper.GetMatchingHost(new[] { address }, AddressFamily.Unspecified);
52+
IPAddress matchingAddress = TcpClientAdapter.GetMatchingHost(new[] { address }, AddressFamily.Unspecified);
5453
Assert.Equal(address, matchingAddress);
5554
}
5655

@@ -59,7 +58,7 @@ public void TcpClientAdapterHelperGetMatchingHostReturnNoAddressIfFamilyIsUnspec
5958
{
6059
var address = IPAddress.Parse("1.1.1.1");
6160
var address2 = IPAddress.Parse("2.2.2.2");
62-
IPAddress matchingAddress = TcpClientAdapterHelper.GetMatchingHost(new[] { address, address2 }, AddressFamily.Unspecified);
61+
IPAddress matchingAddress = TcpClientAdapter.GetMatchingHost(new[] { address, address2 }, AddressFamily.Unspecified);
6362
Assert.Null(matchingAddress);
6463
}
6564
}

0 commit comments

Comments
 (0)