Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions projects/Test/AsyncIntegration/TestConnectionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#if !NET6_0_OR_GREATER
using System;
#endif
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -57,17 +58,33 @@ protected override void SetUp()
}

[Fact]
public Task TestCreateConnectionAsync_WithAlreadyCanceledToken()
public async Task TestCreateConnectionAsync_WithAlreadyCanceledToken()
{
using var cts = new CancellationTokenSource();
cts.Cancel();

ConnectionFactory cf = CreateConnectionFactory();

return Assert.ThrowsAsync<TaskCanceledException>(() =>
bool passed = false;
/*
* If anyone wonders why TaskCanceledException is explicitly checked,
* even though it's a subclass of OperationCanceledException:
* https://github.com/rabbitmq/rabbitmq-dotnet-client/commit/383ca5c5f161edb717cf8fae7bf143c13143f634#r135400615
*/
try
{
await cf.CreateConnectionAsync(cts.Token);
}
catch (TaskCanceledException)
{
return cf.CreateConnectionAsync(cts.Token).AsTask();
});
passed = true;
}
catch (OperationCanceledException)
{
passed = true;
}

Assert.True(passed, "FAIL did not see TaskCanceledException nor OperationCanceledException");
}

[Fact]
Expand Down
46 changes: 27 additions & 19 deletions projects/Test/Common/IntegrationFixtureBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,18 @@ protected virtual void SetUp()
{
if (_connFactory == null)
{
/*
* https://github.com/rabbitmq/rabbitmq-dotnet-client/commit/120f9bfce627f704956e1008d095b853b459d45b#r135400345
*
* Integration tests must use CreateConnectionFactory so that ClientProvidedName is set for the connection.
* Tests that close connections via `rabbitmqctl` depend on finding the connection PID via its name.
*/
_connFactory = CreateConnectionFactory();
}

if (_conn == null)
{
_conn = _connFactory.CreateConnection();
_conn = CreateConnectionWithRetries(_connFactory);
_channel = _conn.CreateChannel();
AddCallbackHandlers();
}
Expand Down Expand Up @@ -209,27 +215,24 @@ internal AutorecoveringConnection CreateAutorecoveringConnection(IEnumerable<str
internal AutorecoveringConnection CreateAutorecoveringConnection(IEnumerable<string> hostnames,
TimeSpan requestedConnectionTimeout, TimeSpan networkRecoveryInterval, bool expectException = false)
{
ConnectionFactory ConnectionFactoryConfigurator(ConnectionFactory cf)
if (hostnames is null)
{
cf.AutomaticRecoveryEnabled = true;
// tests that use this helper will likely list unreachable hosts;
// make sure we time out quickly on those
cf.RequestedConnectionTimeout = requestedConnectionTimeout;
cf.NetworkRecoveryInterval = networkRecoveryInterval;
return cf;
throw new ArgumentNullException(nameof(hostnames));
}

return (AutorecoveringConnection)CreateConnectionWithRetries(hostnames, ConnectionFactoryConfigurator, expectException);
}
ConnectionFactory cf = CreateConnectionFactory();

protected IConnection CreateConnectionWithRetries(Func<ConnectionFactory, ConnectionFactory> connectionFactoryConfigurator)
{
var hostnames = new[] { "localhost" };
return CreateConnectionWithRetries(hostnames, connectionFactoryConfigurator);
cf.AutomaticRecoveryEnabled = true;
// tests that use this helper will likely list unreachable hosts;
// make sure we time out quickly on those
cf.RequestedConnectionTimeout = requestedConnectionTimeout;
cf.NetworkRecoveryInterval = networkRecoveryInterval;

return (AutorecoveringConnection)CreateConnectionWithRetries(cf, hostnames, expectException);
}

protected IConnection CreateConnectionWithRetries(IEnumerable<string> hostnames,
Func<ConnectionFactory, ConnectionFactory> connectionFactoryConfigurator, bool expectException = false)
protected IConnection CreateConnectionWithRetries(ConnectionFactory connectionFactory,
IEnumerable<string> hostnames = null, bool expectException = false)
{
bool shouldRetry = IsWindows;
ushort tries = 0;
Expand All @@ -238,9 +241,14 @@ protected IConnection CreateConnectionWithRetries(IEnumerable<string> hostnames,
{
try
{
ConnectionFactory cf0 = CreateConnectionFactory();
ConnectionFactory cf1 = connectionFactoryConfigurator(cf0);
return cf1.CreateConnection(hostnames);
if (hostnames is null)
{
return connectionFactory.CreateConnection();
}
else
{
return connectionFactory.CreateConnection(hostnames);
}
}
catch (BrokerUnreachableException ex)
{
Expand Down
79 changes: 33 additions & 46 deletions projects/Test/Integration/TestSsl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
// Copyright (c) 2007-2020 VMware, Inc. All rights reserved.
//---------------------------------------------------------------------------

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

ConnectionFactory ConnectionFactoryConfigurator(ConnectionFactory cf)
{
cf.Port = 5671;
cf.Ssl.ServerName = "*";
cf.Ssl.AcceptablePolicyErrors = SslPolicyErrors.RemoteCertificateNameMismatch;
cf.Ssl.Enabled = true;
return cf;
}
ConnectionFactory cf = CreateConnectionFactory();
cf.Port = 5671;
cf.Ssl.ServerName = "*";
cf.Ssl.AcceptablePolicyErrors = SslPolicyErrors.RemoteCertificateNameMismatch;
cf.Ssl.Enabled = true;

SendReceive(ConnectionFactoryConfigurator);
SendReceive(cf);
}

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

ConnectionFactory ConnectionFactoryConfigurator(ConnectionFactory cf)
{
cf.Port = 5671;
cf.Ssl.ServerName = _sslEnv.Hostname;
cf.Ssl.Enabled = true;
return cf;
}
ConnectionFactory cf = CreateConnectionFactory();
cf.Port = 5671;
cf.Ssl.ServerName = _sslEnv.Hostname;
cf.Ssl.Enabled = true;

SendReceive(ConnectionFactoryConfigurator);
SendReceive(cf);
}

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

ConnectionFactory ConnectionFactoryConfigurator(ConnectionFactory cf)
{
cf.Port = 5671;
cf.Ssl.ServerName = _sslEnv.Hostname;
cf.Ssl.CertPath = certPath;
cf.Ssl.CertPassphrase = _sslEnv.CertPassphrase;
cf.Ssl.Enabled = true;
return cf;
}
ConnectionFactory cf = CreateConnectionFactory();
cf.Port = 5671;
cf.Ssl.ServerName = _sslEnv.Hostname;
cf.Ssl.CertPath = certPath;
cf.Ssl.CertPassphrase = _sslEnv.CertPassphrase;
cf.Ssl.Enabled = true;

SendReceive(ConnectionFactoryConfigurator);
SendReceive(cf);
}

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

ConnectionFactory ConnectionFactoryConfigurator(ConnectionFactory cf)
ConnectionFactory cf = CreateConnectionFactory();
cf.Port = 5671;
cf.Ssl = new SslOption()
{
cf.Port = 5671;
cf.Ssl = new SslOption()
{
CertPath = null,
Enabled = true,
ServerName = _sslEnv.Hostname,
Version = SslProtocols.None,
AcceptablePolicyErrors =
SslPolicyErrors.RemoteCertificateNotAvailable |
SslPolicyErrors.RemoteCertificateNameMismatch
};
return cf;
}

SendReceive(ConnectionFactoryConfigurator);
CertPath = null,
Enabled = true,
ServerName = _sslEnv.Hostname,
Version = SslProtocols.None,
AcceptablePolicyErrors =
SslPolicyErrors.RemoteCertificateNotAvailable |
SslPolicyErrors.RemoteCertificateNameMismatch
};

SendReceive(cf);
}

private void SendReceive(Func<ConnectionFactory, ConnectionFactory> cfconfig)
private void SendReceive(ConnectionFactory connectionFactory)
{
using (IConnection conn = CreateConnectionWithRetries(cfconfig))
using (IConnection conn = CreateConnectionWithRetries(connectionFactory))
{
using (IChannel ch = conn.CreateChannel())
{
Expand Down