Skip to content

Commit 99359a8

Browse files
authored
Merge pull request #1449 from rabbitmq/lukebakken/fix-socketexception-on-windows-for-good
Retry more connections in test suite
2 parents 16d1744 + a76e281 commit 99359a8

File tree

3 files changed

+81
-69
lines changed

3 files changed

+81
-69
lines changed

projects/Test/AsyncIntegration/TestConnectionFactory.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#if !NET6_0_OR_GREATER
3333
using System;
3434
#endif
35+
using System;
3536
using System.Collections.Generic;
3637
using System.Threading;
3738
using System.Threading.Tasks;
@@ -57,17 +58,33 @@ protected override void SetUp()
5758
}
5859

5960
[Fact]
60-
public Task TestCreateConnectionAsync_WithAlreadyCanceledToken()
61+
public async Task TestCreateConnectionAsync_WithAlreadyCanceledToken()
6162
{
6263
using var cts = new CancellationTokenSource();
6364
cts.Cancel();
6465

6566
ConnectionFactory cf = CreateConnectionFactory();
6667

67-
return Assert.ThrowsAsync<TaskCanceledException>(() =>
68+
bool passed = false;
69+
/*
70+
* If anyone wonders why TaskCanceledException is explicitly checked,
71+
* even though it's a subclass of OperationCanceledException:
72+
* https://github.com/rabbitmq/rabbitmq-dotnet-client/commit/383ca5c5f161edb717cf8fae7bf143c13143f634#r135400615
73+
*/
74+
try
75+
{
76+
await cf.CreateConnectionAsync(cts.Token);
77+
}
78+
catch (TaskCanceledException)
6879
{
69-
return cf.CreateConnectionAsync(cts.Token).AsTask();
70-
});
80+
passed = true;
81+
}
82+
catch (OperationCanceledException)
83+
{
84+
passed = true;
85+
}
86+
87+
Assert.True(passed, "FAIL did not see TaskCanceledException nor OperationCanceledException");
7188
}
7289

7390
[Fact]

projects/Test/Common/IntegrationFixtureBase.cs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,18 @@ protected virtual void SetUp()
113113
{
114114
if (_connFactory == null)
115115
{
116+
/*
117+
* https://github.com/rabbitmq/rabbitmq-dotnet-client/commit/120f9bfce627f704956e1008d095b853b459d45b#r135400345
118+
*
119+
* Integration tests must use CreateConnectionFactory so that ClientProvidedName is set for the connection.
120+
* Tests that close connections via `rabbitmqctl` depend on finding the connection PID via its name.
121+
*/
116122
_connFactory = CreateConnectionFactory();
117123
}
118124

119125
if (_conn == null)
120126
{
121-
_conn = _connFactory.CreateConnection();
127+
_conn = CreateConnectionWithRetries(_connFactory);
122128
_channel = _conn.CreateChannel();
123129
AddCallbackHandlers();
124130
}
@@ -209,27 +215,24 @@ internal AutorecoveringConnection CreateAutorecoveringConnection(IEnumerable<str
209215
internal AutorecoveringConnection CreateAutorecoveringConnection(IEnumerable<string> hostnames,
210216
TimeSpan requestedConnectionTimeout, TimeSpan networkRecoveryInterval, bool expectException = false)
211217
{
212-
ConnectionFactory ConnectionFactoryConfigurator(ConnectionFactory cf)
218+
if (hostnames is null)
213219
{
214-
cf.AutomaticRecoveryEnabled = true;
215-
// tests that use this helper will likely list unreachable hosts;
216-
// make sure we time out quickly on those
217-
cf.RequestedConnectionTimeout = requestedConnectionTimeout;
218-
cf.NetworkRecoveryInterval = networkRecoveryInterval;
219-
return cf;
220+
throw new ArgumentNullException(nameof(hostnames));
220221
}
221222

222-
return (AutorecoveringConnection)CreateConnectionWithRetries(hostnames, ConnectionFactoryConfigurator, expectException);
223-
}
223+
ConnectionFactory cf = CreateConnectionFactory();
224224

225-
protected IConnection CreateConnectionWithRetries(Func<ConnectionFactory, ConnectionFactory> connectionFactoryConfigurator)
226-
{
227-
var hostnames = new[] { "localhost" };
228-
return CreateConnectionWithRetries(hostnames, connectionFactoryConfigurator);
225+
cf.AutomaticRecoveryEnabled = true;
226+
// tests that use this helper will likely list unreachable hosts;
227+
// make sure we time out quickly on those
228+
cf.RequestedConnectionTimeout = requestedConnectionTimeout;
229+
cf.NetworkRecoveryInterval = networkRecoveryInterval;
230+
231+
return (AutorecoveringConnection)CreateConnectionWithRetries(cf, hostnames, expectException);
229232
}
230233

231-
protected IConnection CreateConnectionWithRetries(IEnumerable<string> hostnames,
232-
Func<ConnectionFactory, ConnectionFactory> connectionFactoryConfigurator, bool expectException = false)
234+
protected IConnection CreateConnectionWithRetries(ConnectionFactory connectionFactory,
235+
IEnumerable<string> hostnames = null, bool expectException = false)
233236
{
234237
bool shouldRetry = IsWindows;
235238
ushort tries = 0;
@@ -238,9 +241,14 @@ protected IConnection CreateConnectionWithRetries(IEnumerable<string> hostnames,
238241
{
239242
try
240243
{
241-
ConnectionFactory cf0 = CreateConnectionFactory();
242-
ConnectionFactory cf1 = connectionFactoryConfigurator(cf0);
243-
return cf1.CreateConnection(hostnames);
244+
if (hostnames is null)
245+
{
246+
return connectionFactory.CreateConnection();
247+
}
248+
else
249+
{
250+
return connectionFactory.CreateConnection(hostnames);
251+
}
244252
}
245253
catch (BrokerUnreachableException ex)
246254
{

projects/Test/Integration/TestSsl.cs

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
// Copyright (c) 2007-2020 VMware, Inc. All rights reserved.
3030
//---------------------------------------------------------------------------
3131

32-
using System;
3332
using System.IO;
3433
using System.Net.Security;
3534
using System.Security.Authentication;
@@ -60,32 +59,26 @@ public void TestServerVerifiedIgnoringNameMismatch()
6059
{
6160
Skip.IfNot(_sslEnv.IsSslConfigured, "SSL_CERTS_DIR and/or PASSWORD are not configured, skipping test");
6261

63-
ConnectionFactory ConnectionFactoryConfigurator(ConnectionFactory cf)
64-
{
65-
cf.Port = 5671;
66-
cf.Ssl.ServerName = "*";
67-
cf.Ssl.AcceptablePolicyErrors = SslPolicyErrors.RemoteCertificateNameMismatch;
68-
cf.Ssl.Enabled = true;
69-
return cf;
70-
}
62+
ConnectionFactory cf = CreateConnectionFactory();
63+
cf.Port = 5671;
64+
cf.Ssl.ServerName = "*";
65+
cf.Ssl.AcceptablePolicyErrors = SslPolicyErrors.RemoteCertificateNameMismatch;
66+
cf.Ssl.Enabled = true;
7167

72-
SendReceive(ConnectionFactoryConfigurator);
68+
SendReceive(cf);
7369
}
7470

7571
[SkippableFact]
7672
public void TestServerVerified()
7773
{
7874
Skip.IfNot(_sslEnv.IsSslConfigured, "SSL_CERTS_DIR and/or PASSWORD are not configured, skipping test");
7975

80-
ConnectionFactory ConnectionFactoryConfigurator(ConnectionFactory cf)
81-
{
82-
cf.Port = 5671;
83-
cf.Ssl.ServerName = _sslEnv.Hostname;
84-
cf.Ssl.Enabled = true;
85-
return cf;
86-
}
76+
ConnectionFactory cf = CreateConnectionFactory();
77+
cf.Port = 5671;
78+
cf.Ssl.ServerName = _sslEnv.Hostname;
79+
cf.Ssl.Enabled = true;
8780

88-
SendReceive(ConnectionFactoryConfigurator);
81+
SendReceive(cf);
8982
}
9083

9184
[SkippableFact]
@@ -96,17 +89,14 @@ public void TestClientAndServerVerified()
9689
string certPath = _sslEnv.CertPath;
9790
Assert.True(File.Exists(certPath));
9891

99-
ConnectionFactory ConnectionFactoryConfigurator(ConnectionFactory cf)
100-
{
101-
cf.Port = 5671;
102-
cf.Ssl.ServerName = _sslEnv.Hostname;
103-
cf.Ssl.CertPath = certPath;
104-
cf.Ssl.CertPassphrase = _sslEnv.CertPassphrase;
105-
cf.Ssl.Enabled = true;
106-
return cf;
107-
}
92+
ConnectionFactory cf = CreateConnectionFactory();
93+
cf.Port = 5671;
94+
cf.Ssl.ServerName = _sslEnv.Hostname;
95+
cf.Ssl.CertPath = certPath;
96+
cf.Ssl.CertPassphrase = _sslEnv.CertPassphrase;
97+
cf.Ssl.Enabled = true;
10898

109-
SendReceive(ConnectionFactoryConfigurator);
99+
SendReceive(cf);
110100
}
111101

112102
// rabbitmq/rabbitmq-dotnet-client#46, also #44 and #45
@@ -115,28 +105,25 @@ public void TestNoClientCertificate()
115105
{
116106
Skip.IfNot(_sslEnv.IsSslConfigured, "SSL_CERTS_DIR and/or PASSWORD are not configured, skipping test");
117107

118-
ConnectionFactory ConnectionFactoryConfigurator(ConnectionFactory cf)
108+
ConnectionFactory cf = CreateConnectionFactory();
109+
cf.Port = 5671;
110+
cf.Ssl = new SslOption()
119111
{
120-
cf.Port = 5671;
121-
cf.Ssl = new SslOption()
122-
{
123-
CertPath = null,
124-
Enabled = true,
125-
ServerName = _sslEnv.Hostname,
126-
Version = SslProtocols.None,
127-
AcceptablePolicyErrors =
128-
SslPolicyErrors.RemoteCertificateNotAvailable |
129-
SslPolicyErrors.RemoteCertificateNameMismatch
130-
};
131-
return cf;
132-
}
133-
134-
SendReceive(ConnectionFactoryConfigurator);
112+
CertPath = null,
113+
Enabled = true,
114+
ServerName = _sslEnv.Hostname,
115+
Version = SslProtocols.None,
116+
AcceptablePolicyErrors =
117+
SslPolicyErrors.RemoteCertificateNotAvailable |
118+
SslPolicyErrors.RemoteCertificateNameMismatch
119+
};
120+
121+
SendReceive(cf);
135122
}
136123

137-
private void SendReceive(Func<ConnectionFactory, ConnectionFactory> cfconfig)
124+
private void SendReceive(ConnectionFactory connectionFactory)
138125
{
139-
using (IConnection conn = CreateConnectionWithRetries(cfconfig))
126+
using (IConnection conn = CreateConnectionWithRetries(connectionFactory))
140127
{
141128
using (IChannel ch = conn.CreateChannel())
142129
{

0 commit comments

Comments
 (0)