Skip to content

Commit 59d86ae

Browse files
committed
Add connection retries for SocketException to SSL tests. Uncovered with windows 2019.
1 parent 54f470a commit 59d86ae

File tree

3 files changed

+77
-47
lines changed

3 files changed

+77
-47
lines changed

.ci/windows/versions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"erlang": "26.1.2",
2+
"erlang": "26.2.1",
33
"rabbitmq": "3.12.10"
44
}

projects/Test/Common/IntegrationFixtureBase.cs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,36 @@ protected static bool IsVerbose
200200
get { return s_isVerbose; }
201201
}
202202

203-
internal AutorecoveringConnection CreateAutorecoveringConnection(IList<string> hostnames, bool expectException = false)
203+
internal AutorecoveringConnection CreateAutorecoveringConnection(IEnumerable<string> hostnames, bool expectException = false)
204204
{
205205

206206
return CreateAutorecoveringConnection(hostnames, RequestedConnectionTimeout, RecoveryInterval, expectException);
207207
}
208208

209209
internal AutorecoveringConnection CreateAutorecoveringConnection(IEnumerable<string> hostnames,
210210
TimeSpan requestedConnectionTimeout, TimeSpan networkRecoveryInterval, bool expectException = false)
211+
{
212+
ConnectionFactory ConnectionFactoryConfigurator(ConnectionFactory cf)
213+
{
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+
}
221+
222+
return (AutorecoveringConnection)CreateConnectionWithRetries(hostnames, ConnectionFactoryConfigurator, expectException);
223+
}
224+
225+
protected IConnection CreateConnectionWithRetries(Func<ConnectionFactory, ConnectionFactory> connectionFactoryConfigurator)
226+
{
227+
var hostnames = new[] { "localhost" };
228+
return CreateConnectionWithRetries(hostnames, connectionFactoryConfigurator);
229+
}
230+
231+
protected IConnection CreateConnectionWithRetries(IEnumerable<string> hostnames,
232+
Func<ConnectionFactory, ConnectionFactory> connectionFactoryConfigurator, bool expectException = false)
211233
{
212234
bool shouldRetry = IsWindows;
213235
ushort tries = 0;
@@ -216,13 +238,9 @@ internal AutorecoveringConnection CreateAutorecoveringConnection(IEnumerable<str
216238
{
217239
try
218240
{
219-
ConnectionFactory cf = CreateConnectionFactory();
220-
cf.AutomaticRecoveryEnabled = true;
221-
// tests that use this helper will likely list unreachable hosts;
222-
// make sure we time out quickly on those
223-
cf.RequestedConnectionTimeout = requestedConnectionTimeout;
224-
cf.NetworkRecoveryInterval = networkRecoveryInterval;
225-
return (AutorecoveringConnection)cf.CreateConnection(hostnames);
241+
ConnectionFactory cf0 = CreateConnectionFactory();
242+
ConnectionFactory cf1 = connectionFactoryConfigurator(cf0);
243+
return cf1.CreateConnection(hostnames);
226244
}
227245
catch (BrokerUnreachableException ex)
228246
{
@@ -251,7 +269,7 @@ internal AutorecoveringConnection CreateAutorecoveringConnection(IEnumerable<str
251269
if (ioex.InnerException is SocketException)
252270
{
253271
tries++;
254-
_output.WriteLine($"WARNING: {nameof(CreateAutorecoveringConnection)} retrying, caught exception: {ioex.InnerException}");
272+
_output.WriteLine($"WARNING: {nameof(CreateConnectionWithRetries)} retrying ({tries}), caught exception: {ioex.InnerException}");
255273
}
256274
else
257275
{
@@ -263,7 +281,7 @@ internal AutorecoveringConnection CreateAutorecoveringConnection(IEnumerable<str
263281
}
264282
while (shouldRetry && tries < 5);
265283

266-
Assert.Fail($"FAIL: {nameof(CreateAutorecoveringConnection)} could not open connection");
284+
Assert.Fail($"FAIL: {nameof(CreateConnectionWithRetries)} could not open connection");
267285
return null;
268286
}
269287

projects/Test/Integration/TestSsl.cs

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

32+
using System;
3233
using System.IO;
33-
using System.Net;
3434
using System.Net.Security;
3535
using System.Security.Authentication;
3636
using RabbitMQ.Client;
@@ -50,8 +50,6 @@ public TestSsl(ITestOutputHelper output) : base(output)
5050

5151
protected override void SetUp()
5252
{
53-
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
54-
5553
Assert.Null(_connFactory);
5654
Assert.Null(_conn);
5755
Assert.Null(_channel);
@@ -62,25 +60,32 @@ public void TestServerVerifiedIgnoringNameMismatch()
6260
{
6361
Skip.IfNot(_sslEnv.IsSslConfigured, "SSL_CERTS_DIR and/or PASSWORD are not configured, skipping test");
6462

65-
var cf = CreateConnectionFactory();
66-
cf.Port = 5671;
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+
}
6771

68-
cf.Ssl.ServerName = "*";
69-
cf.Ssl.AcceptablePolicyErrors = SslPolicyErrors.RemoteCertificateNameMismatch;
70-
cf.Ssl.Enabled = true;
71-
SendReceive(cf);
72+
SendReceive(ConnectionFactoryConfigurator);
7273
}
7374

7475
[SkippableFact]
7576
public void TestServerVerified()
7677
{
7778
Skip.IfNot(_sslEnv.IsSslConfigured, "SSL_CERTS_DIR and/or PASSWORD are not configured, skipping test");
7879

79-
var cf = CreateConnectionFactory();
80-
cf.Port = 5671;
81-
cf.Ssl.ServerName = _sslEnv.Hostname;
82-
cf.Ssl.Enabled = true;
83-
SendReceive(cf);
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+
}
87+
88+
SendReceive(ConnectionFactoryConfigurator);
8489
}
8590

8691
[SkippableFact]
@@ -91,13 +96,17 @@ public void TestClientAndServerVerified()
9196
string certPath = _sslEnv.CertPath;
9297
Assert.True(File.Exists(certPath));
9398

94-
var cf = CreateConnectionFactory();
95-
cf.Port = 5671;
96-
cf.Ssl.ServerName = _sslEnv.Hostname;
97-
cf.Ssl.CertPath = certPath;
98-
cf.Ssl.CertPassphrase = _sslEnv.CertPassphrase;
99-
cf.Ssl.Enabled = true;
100-
SendReceive(cf);
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+
}
108+
109+
SendReceive(ConnectionFactoryConfigurator);
101110
}
102111

103112
// rabbitmq/rabbitmq-dotnet-client#46, also #44 and #45
@@ -106,25 +115,28 @@ public void TestNoClientCertificate()
106115
{
107116
Skip.IfNot(_sslEnv.IsSslConfigured, "SSL_CERTS_DIR and/or PASSWORD are not configured, skipping test");
108117

109-
var cf = CreateConnectionFactory();
110-
cf.Port = 5671;
111-
cf.Ssl = new SslOption()
118+
ConnectionFactory ConnectionFactoryConfigurator(ConnectionFactory cf)
112119
{
113-
CertPath = null,
114-
Enabled = true,
115-
ServerName = _sslEnv.Hostname,
116-
Version = SslProtocols.None,
117-
AcceptablePolicyErrors =
118-
SslPolicyErrors.RemoteCertificateNotAvailable |
119-
SslPolicyErrors.RemoteCertificateNameMismatch
120-
};
121-
122-
SendReceive(cf);
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);
123135
}
124136

125-
private void SendReceive(ConnectionFactory cf)
137+
private void SendReceive(Func<ConnectionFactory, ConnectionFactory> cfconfig)
126138
{
127-
using (IConnection conn = cf.CreateConnection(_testDisplayName))
139+
using (IConnection conn = CreateConnectionWithRetries(cfconfig))
128140
{
129141
using (IChannel ch = conn.CreateChannel())
130142
{

0 commit comments

Comments
 (0)