Skip to content

Commit

Permalink
Merge pull request #502 from acraven/ResetConnectionFix
Browse files Browse the repository at this point in the history
Suppress and log BrokerUnreachableException during ResetConnection
  • Loading branch information
iancooper authored May 13, 2019
2 parents 539348e + fbc2de1 commit dd99734
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ THE SOFTWARE. */
using System.Collections.Generic;
using Paramore.Brighter.MessagingGateway.RMQ.Logging;
using RabbitMQ.Client;
using RabbitMQ.Client.Exceptions;

namespace Paramore.Brighter.MessagingGateway.RMQ
{
Expand Down Expand Up @@ -80,9 +81,19 @@ public void ResetConnection(ConnectionFactory connectionFactory)

lock (s_lock)
{
var connection = s_connectionPool[connectionId];
TryRemoveConnection(connectionId);
CreateConnection(connectionFactory);

try
{
CreateConnection(connectionFactory);
}
catch (BrokerUnreachableException exception)
{
s_logger.Value.ErrorException(
"RMQMessagingGateway: Failed to reset connection to Rabbit MQ endpoint {0}",
exception,
connectionFactory.Endpoint);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#region Licence
/* The MIT License (MIT)
Copyright © 2014 Ian Cooper <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */

#endregion

using System;
using FluentAssertions;
using Paramore.Brighter.MessagingGateway.RMQ;
using RabbitMQ.Client;
using Xunit;

namespace Paramore.Brighter.Tests.MessagingGateway.RMQ
{
[Collection("RMQ")]
[Trait("Category", "RMQ")]
public class RMQMessageGatewayConnectionPoolResetConnectionDoesNotExist
{
private readonly RMQMessageGatewayConnectionPool _connectionPool;

public RMQMessageGatewayConnectionPoolResetConnectionDoesNotExist()
{
_connectionPool = new RMQMessageGatewayConnectionPool("MyConnectionName", 7);
}

[Fact]
public void When_resetting_a_connection_that_does_not_exist()
{
var connectionFactory = new ConnectionFactory {HostName = "invalidhost"};

Action resetConnection = () => _connectionPool.ResetConnection(connectionFactory);

resetConnection.Should().NotThrow();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#region Licence
/* The MIT License (MIT)
Copyright © 2014 Ian Cooper <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */

#endregion

using FakeItEasy;
using FluentAssertions;
using Paramore.Brighter.MessagingGateway.RMQ;
using RabbitMQ.Client;
using Xunit;

namespace Paramore.Brighter.Tests.MessagingGateway.RMQ
{
[Collection("RMQ")]
[Trait("Category", "RMQ")]
public class RMQMessageGatewayConnectionPoolResetConnectionExists
{
private readonly RMQMessageGatewayConnectionPool _connectionPool;
private readonly IConnection _originalConnection;

public RMQMessageGatewayConnectionPoolResetConnectionExists()
{
_connectionPool = new RMQMessageGatewayConnectionPool("MyConnectionName", 7);

var connectionFactory = new ConnectionFactory { HostName = "localhost" };

_originalConnection = _connectionPool.GetConnection(connectionFactory);
}

[Fact]
public void When_resetting_a_connection_that_exists()
{
var connectionFactory = new ConnectionFactory{HostName = "localhost"};

_connectionPool.ResetConnection(connectionFactory);

_connectionPool.GetConnection(connectionFactory).Should().NotBeSameAs(_originalConnection);
}
}
}

0 comments on commit dd99734

Please sign in to comment.