-
Notifications
You must be signed in to change notification settings - Fork 38
Amqp: reusable connections #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bobanco
wants to merge
1
commit into
akkadotnet:dev
Choose a base branch
from
bobanco:dev
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
256 changes: 256 additions & 0 deletions
256
Amqp/src/Akka.Streams.Amqp.Tests/AmqpConnectionProvidersTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,256 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using FluentAssertions; | ||
using RabbitMQ.Client; | ||
using RabbitMQ.Client.Exceptions; | ||
using Xunit; | ||
|
||
namespace Akka.Streams.Amqp.Tests | ||
{ | ||
public class AmqpConnectionProvidersTest : Akka.TestKit.Xunit2.TestKit | ||
{ | ||
private readonly ActorMaterializer _mat; | ||
public AmqpConnectionProvidersTest() | ||
{ | ||
_mat = ActorMaterializer.Create(Sys); | ||
} | ||
|
||
[Fact] | ||
public void | ||
The_AMQP_default_connection_providers_should_create_new_a_new_connection_per_invocation_of_LocalAmqpConnection() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "new_a_new" typo. |
||
{ | ||
var connectionProvider = AmqpLocalConnectionProvider.Instance; | ||
var connection1 = connectionProvider.Get(); | ||
var connection2 = connectionProvider.Get(); | ||
Assert.NotEqual(connection1, connection2); | ||
connectionProvider.Release(connection1); | ||
connectionProvider.Release(connection2); | ||
} | ||
|
||
[Fact] | ||
public void Not_error_if_releasing_already_closed_LocalAmqpCOnnection() | ||
{ | ||
var connectionProvider = AmqpLocalConnectionProvider.Instance; | ||
var connection1 = connectionProvider.Get(); | ||
var connection2 = connectionProvider.Get(); | ||
connectionProvider.Release(connection1); | ||
connectionProvider.Release(connection2); | ||
} | ||
|
||
[Fact] | ||
public void Create_new_connection_per_invocation_of_AmqpConnectionUri() | ||
{ | ||
var connectionProvider = AmqpUriConnectionProvider.Create("amqp://localhost:5672"); | ||
var connection1 = connectionProvider.Get(); | ||
var connection2 = connectionProvider.Get(); | ||
Assert.NotEqual(connection1, connection2); | ||
connectionProvider.Release(connection1); | ||
connectionProvider.Release(connection2); | ||
} | ||
|
||
[Fact] | ||
public void Not_error_if_releasing_already_closed_AmqpConnectionUri() | ||
{ | ||
var connectionProvider = AmqpUriConnectionProvider.Create("amqp://localhost:5672"); | ||
var connection1 = connectionProvider.Get(); | ||
var connection2 = connectionProvider.Get(); | ||
connectionProvider.Release(connection1); | ||
connectionProvider.Release(connection2); | ||
} | ||
|
||
[Fact] | ||
public void Create_new_connection_per_invocation_of_AmqpConnectionDetails() | ||
{ | ||
var connectionProvider = AmqpDetailsConnectionProvider.Create("localhost", 5672); | ||
var connection1 = connectionProvider.Get(); | ||
var connection2 = connectionProvider.Get(); | ||
Assert.NotEqual(connection1, connection2); | ||
connectionProvider.Release(connection1); | ||
connectionProvider.Release(connection2); | ||
} | ||
|
||
[Fact] | ||
public void Not_error_if_releasing_already_closed_AmqpConnectionDetails() | ||
{ | ||
var connectionProvider = AmqpDetailsConnectionProvider.Create("localhost", 5672); | ||
var connection1 = connectionProvider.Get(); | ||
var connection2 = connectionProvider.Get(); | ||
connectionProvider.Release(connection1); | ||
connectionProvider.Release(connection2); | ||
} | ||
|
||
[Fact] | ||
public void Create_new_connection_per_invocation_of_AmqpConnectionFactory() | ||
{ | ||
var connectionFactory = new ConnectionFactory(); | ||
var connectionProvider = AmqpConnectionFactoryConnectionProvider.Create(connectionFactory).WithHostsAndPorts(("localhost", 5672)); | ||
var connection1 = connectionProvider.Get(); | ||
var connection2 = connectionProvider.Get(); | ||
Assert.NotEqual(connection1, connection2); | ||
connectionProvider.Release(connection1); | ||
connectionProvider.Release(connection2); | ||
} | ||
|
||
[Fact] | ||
public void Not_error_if_releasing_already_closed_AmqpConnectionFactory() | ||
{ | ||
var connectionFactory = new ConnectionFactory(); | ||
var connectionProvider = AmqpConnectionFactoryConnectionProvider.Create(connectionFactory).WithHostsAndPorts(("localhost", 5672)); | ||
var connection1 = connectionProvider.Get(); | ||
var connection2 = connectionProvider.Get(); | ||
connectionProvider.Release(connection1); | ||
connectionProvider.Release(connection2); | ||
} | ||
|
||
[Fact] | ||
public void | ||
Reusable_connection_provider_with_automatic_release_should_reuse_the_same_connection_from_LocalAmqpConnection_and_release_it_when_last_client_disconnects() | ||
{ | ||
var connectionProvider = AmqpLocalConnectionProvider.Instance; | ||
var reusableConnectionProvider = AmqpCachedConnectionProvider.Create(connectionProvider); | ||
var connection1 = reusableConnectionProvider.Get(); | ||
var connection2 = reusableConnectionProvider.Get(); | ||
Assert.Equal(connection1, connection2); | ||
reusableConnectionProvider.Release(connection1); | ||
Assert.True(connection1.IsOpen); | ||
Assert.True(connection2.IsOpen); | ||
reusableConnectionProvider.Release(connection2); | ||
Assert.False(connection1.IsOpen); | ||
Assert.False(connection2.IsOpen); | ||
} | ||
|
||
[Fact] | ||
public void Reuse_the_same_connection_from_AmqpConnectionUri_and_release_it_when_last_client_disconnects() | ||
{ | ||
var connectionProvider = AmqpUriConnectionProvider.Create("amqp://localhost:5672"); | ||
var reusableConnectionProvider = AmqpCachedConnectionProvider.Create(connectionProvider); | ||
var connection1 = reusableConnectionProvider.Get(); | ||
var connection2 = reusableConnectionProvider.Get(); | ||
Assert.Equal(connection1, connection2); | ||
reusableConnectionProvider.Release(connection1); | ||
Assert.True(connection1.IsOpen); | ||
Assert.True(connection2.IsOpen); | ||
reusableConnectionProvider.Release(connection2); | ||
Assert.False(connection1.IsOpen); | ||
Assert.False(connection2.IsOpen); | ||
} | ||
|
||
[Fact] | ||
public void Reuse_the_same_connection_from_AmqpConnectionDetails_and_release_it_when_last_client_disconnects() | ||
{ | ||
var connectionProvider = AmqpDetailsConnectionProvider.Create("localhost", 5672); | ||
var reusableConnectionProvider = AmqpCachedConnectionProvider.Create(connectionProvider); | ||
var connection1 = reusableConnectionProvider.Get(); | ||
var connection2 = reusableConnectionProvider.Get(); | ||
Assert.Equal(connection1, connection2); | ||
reusableConnectionProvider.Release(connection1); | ||
Assert.True(connection1.IsOpen); | ||
Assert.True(connection2.IsOpen); | ||
reusableConnectionProvider.Release(connection2); | ||
Assert.False(connection1.IsOpen); | ||
Assert.False(connection2.IsOpen); | ||
} | ||
|
||
[Fact] | ||
public void Reuse_the_same_connection_from_AmqpConnectionFactory_and_release_it_when_last_client_disconnects() | ||
{ | ||
var connectionFactory = new ConnectionFactory(); | ||
var connectionProvider = AmqpConnectionFactoryConnectionProvider.Create(connectionFactory) | ||
.WithHostsAndPorts(("localhost", 5672)); | ||
var reusableConnectionProvider = AmqpCachedConnectionProvider.Create(connectionProvider); | ||
var connection1 = reusableConnectionProvider.Get(); | ||
var connection2 = reusableConnectionProvider.Get(); | ||
Assert.Equal(connection1, connection2); | ||
reusableConnectionProvider.Release(connection1); | ||
Assert.True(connection1.IsOpen); | ||
Assert.True(connection2.IsOpen); | ||
reusableConnectionProvider.Release(connection2); | ||
Assert.False(connection1.IsOpen); | ||
Assert.False(connection2.IsOpen); | ||
} | ||
|
||
|
||
[Fact] | ||
public void | ||
The_AMQP_reusable_connection_provider_without_automatic_release_should_reuse_same_connection_from_LocalAmqpConnection() | ||
{ | ||
var connectionProvider = AmqpLocalConnectionProvider.Instance; | ||
var reusableConnectionProvider = | ||
AmqpCachedConnectionProvider.Create(connectionProvider, automaticRelease: false); | ||
var connection1 = reusableConnectionProvider.Get(); | ||
var connection2 = reusableConnectionProvider.Get(); | ||
Assert.Equal(connection1, connection2); | ||
reusableConnectionProvider.Release(connection2); | ||
Assert.False(connection1.IsOpen); | ||
Assert.False(connection2.IsOpen); | ||
} | ||
|
||
[Fact] | ||
public void Reuse_the_same_connection_from_AmqpConnectionUri() | ||
{ | ||
var connectionProvider = AmqpUriConnectionProvider.Create("amqp://localhost:5672"); | ||
var reusableConnectionProvider = | ||
AmqpCachedConnectionProvider.Create(connectionProvider, automaticRelease: false); | ||
var connection1 = reusableConnectionProvider.Get(); | ||
var connection2 = reusableConnectionProvider.Get(); | ||
Assert.Equal(connection1, connection2); | ||
reusableConnectionProvider.Release(connection2); | ||
Assert.False(connection1.IsOpen); | ||
Assert.False(connection2.IsOpen); | ||
} | ||
|
||
[Fact] | ||
public void Reuse_the_same_connection_from_AmqpConnectionDetails() | ||
{ | ||
var connectionProvider = AmqpDetailsConnectionProvider.Create("localhost", 5672); | ||
var reusableConnectionProvider = | ||
AmqpCachedConnectionProvider.Create(connectionProvider, automaticRelease: false); | ||
var connection1 = reusableConnectionProvider.Get(); | ||
var connection2 = reusableConnectionProvider.Get(); | ||
Assert.Equal(connection1, connection2); | ||
reusableConnectionProvider.Release(connection2); | ||
Assert.False(connection1.IsOpen); | ||
Assert.False(connection2.IsOpen); | ||
} | ||
|
||
[Fact] | ||
public void Reuse_the_same_connection_from_AmqpConnectionFactory() | ||
{ | ||
var connectionFactory = new ConnectionFactory(); | ||
var connectionProvider = AmqpConnectionFactoryConnectionProvider.Create(connectionFactory) | ||
.WithHostsAndPorts(("localhost", 5672)); | ||
var reusableConnectionProvider = | ||
AmqpCachedConnectionProvider.Create(connectionProvider, automaticRelease: false); | ||
var connection1 = reusableConnectionProvider.Get(); | ||
var connection2 = reusableConnectionProvider.Get(); | ||
Assert.Equal(connection1, connection2); | ||
reusableConnectionProvider.Release(connection2); | ||
Assert.False(connection1.IsOpen); | ||
Assert.False(connection2.IsOpen); | ||
} | ||
|
||
[Fact] | ||
public void Not_leave_the_provider_in_an_invalid_state_if_getting_connection_fails() | ||
{ | ||
var connectionProvider = AmqpDetailsConnectionProvider.Create("localhost", 5673); | ||
var reusableConnectionProvider = AmqpCachedConnectionProvider.Create(connectionProvider, false); | ||
try | ||
{ | ||
reusableConnectionProvider.Get(); | ||
} | ||
catch (Exception e) | ||
{ | ||
e.Should().BeOfType<BrokerUnreachableException>(); | ||
} | ||
try | ||
{ | ||
reusableConnectionProvider.Get(); | ||
} | ||
catch (Exception e) | ||
{ | ||
e.Should().BeOfType<BrokerUnreachableException>(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_mat
is not used anywhere.