diff --git a/RawRabbit.sln b/RawRabbit.sln index d61b399e..182d004c 100644 --- a/RawRabbit.sln +++ b/RawRabbit.sln @@ -70,6 +70,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RawRabbit.DependencyInjecti EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RawRabbit.Enrichers.Protobuf", "src\RawRabbit.Enrichers.Protobuf\RawRabbit.Enrichers.Protobuf.csproj", "{8D45F8AC-B65F-4A2B-9153-8A7F3D423575}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RawRabbit.Enrichers.Polly.Tests", "test\RawRabbit.Enrichers.Polly.Tests\RawRabbit.Enrichers.Polly.Tests.csproj", "{4B4C5936-D61E-4FD8-AEB7-154CEAF84E15}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -188,6 +190,10 @@ Global {8D45F8AC-B65F-4A2B-9153-8A7F3D423575}.Debug|Any CPU.Build.0 = Debug|Any CPU {8D45F8AC-B65F-4A2B-9153-8A7F3D423575}.Release|Any CPU.ActiveCfg = Release|Any CPU {8D45F8AC-B65F-4A2B-9153-8A7F3D423575}.Release|Any CPU.Build.0 = Release|Any CPU + {4B4C5936-D61E-4FD8-AEB7-154CEAF84E15}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4B4C5936-D61E-4FD8-AEB7-154CEAF84E15}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4B4C5936-D61E-4FD8-AEB7-154CEAF84E15}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4B4C5936-D61E-4FD8-AEB7-154CEAF84E15}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -221,6 +227,7 @@ Global {2788DED9-2558-496C-81B1-B471CD42AB64} = {2F91E22A-AEBA-4BEF-9A03-C8232830F697} {CF308330-735E-411D-BBA9-0018DD079AF1} = {7FCF8D3B-BA55-4C47-AC60-5CEF75418BEB} {8D45F8AC-B65F-4A2B-9153-8A7F3D423575} = {7FCF8D3B-BA55-4C47-AC60-5CEF75418BEB} + {4B4C5936-D61E-4FD8-AEB7-154CEAF84E15} = {2F91E22A-AEBA-4BEF-9A03-C8232830F697} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {6EC93B92-1319-44D3-A596-9FBD9BD23050} diff --git a/test/RawRabbit.Enrichers.Polly.Tests/Middleware/QueueDeclareMiddlewareTests.cs b/test/RawRabbit.Enrichers.Polly.Tests/Middleware/QueueDeclareMiddlewareTests.cs new file mode 100644 index 00000000..ab3527b3 --- /dev/null +++ b/test/RawRabbit.Enrichers.Polly.Tests/Middleware/QueueDeclareMiddlewareTests.cs @@ -0,0 +1,59 @@ +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Threading.Tasks; +using Moq; +using Polly; +using RabbitMQ.Client; +using RabbitMQ.Client.Exceptions; +using RawRabbit.Common; +using RawRabbit.Configuration.Queue; +using RawRabbit.Pipe; +using RawRabbit.Pipe.Middleware; +using Xunit; +using QueueDeclareMiddleware = RawRabbit.Enrichers.Polly.Middleware.QueueDeclareMiddleware; + +namespace RawRabbit.Enrichers.Polly.Tests.Middleware +{ + public class QueueDeclareMiddlewareTests + { + [Fact] + public async Task Should_Invoke_Queue_Declare_Policy_With_Correct_Context() + { + var topology = new Mock(); + var queueDeclaration = new QueueDeclaration(); + var policyCalled = false; + Context capturedContext = null; + + topology + .SetupSequence(t => t.DeclareQueueAsync(queueDeclaration)) + .Throws(new OperationInterruptedException(null)) + .Returns(Task.CompletedTask); + + var context = new PipeContext + { + Properties = new Dictionary + { + {PipeKey.QueueDeclaration, queueDeclaration} + } + }; + + context.UsePolicy(Policy + .Handle() + .RetryAsync((exception, retryCount, pollyContext) => + { + policyCalled = true; + capturedContext = pollyContext; + }), PolicyKeys.QueueDeclare); + var middleware = new QueueDeclareMiddleware(topology.Object) {Next = new NoOpMiddleware()}; + + /* Test */ + await middleware.InvokeAsync(context); + + /* Assert */ + Assert.True(policyCalled, "Should call policy"); + Assert.Equal(context, capturedContext[RetryKey.PipeContext]); + Assert.Equal(queueDeclaration, capturedContext[RetryKey.QueueDeclaration]); + Assert.Equal(topology.Object, capturedContext[RetryKey.TopologyProvider]); + } + } +} diff --git a/test/RawRabbit.Enrichers.Polly.Tests/Properties/AssemblyInfo.cs b/test/RawRabbit.Enrichers.Polly.Tests/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..0bee5348 --- /dev/null +++ b/test/RawRabbit.Enrichers.Polly.Tests/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("RawRabbit.Enrichers.Polly.Tests")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("RawRabbit.Enrichers.Polly.Tests")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("4b4c5936-d61e-4fd8-aeb7-154ceaf84e15")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/test/RawRabbit.Enrichers.Polly.Tests/RawRabbit.Enrichers.Polly.Tests.csproj b/test/RawRabbit.Enrichers.Polly.Tests/RawRabbit.Enrichers.Polly.Tests.csproj new file mode 100644 index 00000000..de265aee --- /dev/null +++ b/test/RawRabbit.Enrichers.Polly.Tests/RawRabbit.Enrichers.Polly.Tests.csproj @@ -0,0 +1,107 @@ + + + + + + + Debug + AnyCPU + {4B4C5936-D61E-4FD8-AEB7-154CEAF84E15} + Library + Properties + RawRabbit.Enrichers.Polly.Tests + RawRabbit.Enrichers.Polly.Tests + v4.6 + 512 + + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\..\packages\Castle.Core.4.2.0\lib\net45\Castle.Core.dll + + + ..\..\packages\Microsoft.Diagnostics.Tracing.EventSource.Redist.1.1.28\lib\net46\Microsoft.Diagnostics.Tracing.EventSource.dll + + + ..\..\packages\Moq.4.7.137\lib\net45\Moq.dll + + + ..\..\packages\Polly.5.3.1\lib\net45\Polly.dll + + + ..\..\packages\RabbitMQ.Client.5.0.1\lib\net451\RabbitMQ.Client.dll + + + + + + + + + + + + ..\..\packages\xunit.abstractions.2.0.1\lib\net35\xunit.abstractions.dll + True + + + ..\..\packages\xunit.assert.2.3.0\lib\netstandard1.1\xunit.assert.dll + + + ..\..\packages\xunit.extensibility.core.2.3.0\lib\netstandard1.1\xunit.core.dll + + + ..\..\packages\xunit.extensibility.execution.2.3.0\lib\net452\xunit.execution.desktop.dll + + + + + + + + + + + + + + + + {c95030ff-2d90-4c68-b382-061ce8542c3b} + RawRabbit.Enrichers.Polly + + + {14195f79-b374-4feb-ba3c-e24138542421} + RawRabbit + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + + \ No newline at end of file diff --git a/test/RawRabbit.Enrichers.Polly.Tests/Services/ChannelFactoryTests.cs b/test/RawRabbit.Enrichers.Polly.Tests/Services/ChannelFactoryTests.cs new file mode 100644 index 00000000..4a605ac3 --- /dev/null +++ b/test/RawRabbit.Enrichers.Polly.Tests/Services/ChannelFactoryTests.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Moq; +using Polly; +using RabbitMQ.Client; +using RabbitMQ.Client.Exceptions; +using RawRabbit.Configuration; +using RawRabbit.Enrichers.Polly.Services; +using Xunit; + +namespace RawRabbit.Enrichers.Polly.Tests.Services +{ + public class ChannelFactoryTests + { + [Fact] + public async Task Should_Use_Connect_Policy_When_Connecting_To_Broker() + { + /* Setup */ + var connection = new Mock(); + var connectionFactory = new Mock(); + connectionFactory + .Setup(f => f.CreateConnection()) + .Returns(connection.Object); + connectionFactory + .SetupSequence(c => c.CreateConnection( + It.IsAny>() + )) + .Throws(new BrokerUnreachableException(new Exception())) + .Throws(new BrokerUnreachableException(new Exception())) + .Throws(new BrokerUnreachableException(new Exception())) + .Returns(connection.Object); + + var policy = Policy + .Handle() + .WaitAndRetryAsync(new[] + { + TimeSpan.FromMilliseconds(1), + TimeSpan.FromMilliseconds(2), + TimeSpan.FromMilliseconds(4), + TimeSpan.FromMilliseconds(8), + TimeSpan.FromMilliseconds(16) + }); + + var factory = new ChannelFactory(connectionFactory.Object, RawRabbitConfiguration.Local, new ConnectionPolicies{ Connect = policy}); + + /* Test */ + /* Assert */ + await factory.ConnectAsync(); + } + + [Fact] + public async Task Should_Use_Create_Channel_Policy_When_Creaing_Channels() + { + /* Setup */ + var channel = new Mock(); + var connection = new Mock(); + var connectionFactory = new Mock(); + connectionFactory + .Setup(f => f.CreateConnection()) + .Returns(connection.Object); + connectionFactory + .Setup(c => c.CreateConnection( + It.IsAny>() + )) + .Returns(connection.Object); + connection + .Setup(c => c.IsOpen) + .Returns(true); + connection + .SetupSequence(c => c.CreateModel()) + .Throws(new TimeoutException()) + .Throws(new TimeoutException()) + .Returns(channel.Object); + + var policy = Policy + .Handle() + .WaitAndRetryAsync(new[] + { + TimeSpan.FromMilliseconds(1), + TimeSpan.FromMilliseconds(2), + TimeSpan.FromMilliseconds(4), + TimeSpan.FromMilliseconds(8), + TimeSpan.FromMilliseconds(16) + }); + + var factory = new ChannelFactory(connectionFactory.Object, RawRabbitConfiguration.Local, new ConnectionPolicies { CreateChannel = policy }); + + /* Test */ + var retrievedChannel = await factory.CreateChannelAsync(); + + /* Assert */ + Assert.Equal(channel.Object, retrievedChannel); + } + } +} diff --git a/test/RawRabbit.Enrichers.Polly.Tests/packages.config b/test/RawRabbit.Enrichers.Polly.Tests/packages.config new file mode 100644 index 00000000..769e4b9f --- /dev/null +++ b/test/RawRabbit.Enrichers.Polly.Tests/packages.config @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file