Skip to content

Commit

Permalink
Integration tests for RabbitMQ (#38)
Browse files Browse the repository at this point in the history
* Improvements to RabbitMq engine
* Simplified IBusHost interface
* Changes to NybusDispatcher
* Add tests for RabbitMq via FakeRabbitMQ
* Add tests for RabbitMq to external server
  • Loading branch information
Kralizek committed Jan 13, 2019
1 parent 3f6f064 commit c639730
Show file tree
Hide file tree
Showing 103 changed files with 3,391 additions and 630 deletions.
52 changes: 35 additions & 17 deletions Nybus.sln
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,17 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SenderFeedback", "samples\R
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Types", "samples\RabbitMQ\Types\Types.csproj", "{5EADDCA9-0316-4596-85B1-6C91EDFE8460}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestUtils", "tests\TestUtils\TestUtils.csproj", "{6DAC88A4-9DCF-490B-81D3-B0AA72993EA4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestUtils", "tests\TestUtils\TestUtils.csproj", "{6DAC88A4-9DCF-490B-81D3-B0AA72993EA4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nybus.Engine.InMemory", "src\engines\Nybus.Engine.InMemory\Nybus.Engine.InMemory.csproj", "{C59D94B8-9869-4E12-B393-2645E9C38BF3}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nybus.Engine.InMemory", "src\engines\Nybus.Engine.InMemory\Nybus.Engine.InMemory.csproj", "{C59D94B8-9869-4E12-B393-2645E9C38BF3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.Nybus.Engine.InMemory", "tests\Tests.Nybus.Engine.InMemory\Tests.Nybus.Engine.InMemory.csproj", "{8AA01AE0-1F81-41E4-917C-682E22B645D2}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests.Nybus.Engine.InMemory", "tests\Tests.Nybus.Engine.InMemory\Tests.Nybus.Engine.InMemory.csproj", "{8AA01AE0-1F81-41E4-917C-682E22B645D2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.Integration.Nybus.Engine.InMemory", "tests\Tests.Integration.Nybus.Engine.InMemory\Tests.Integration.Nybus.Engine.InMemory.csproj", "{6B36CB22-F186-4AD9-8A6D-13E7A3E6D3A2}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "integration", "integration", "{DFE3C93F-B067-4F83-9964-5E8014007338}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.Integration.Nybus.Engine.InMemory", "tests\integration\Tests.Integration.Nybus.Engine.InMemory\Tests.Integration.Nybus.Engine.InMemory.csproj", "{B8998272-1E13-4FC0-BC5C-B012E85A2A78}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.Integration.Nybus.Engine.RabbitMq", "tests\integration\Tests.Integration.Nybus.Engine.RabbitMq\Tests.Integration.Nybus.Engine.RabbitMq.csproj", "{FBA8ABC5-9CDC-455D-9AA3-8249FC0F3A34}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -265,18 +269,30 @@ Global
{8AA01AE0-1F81-41E4-917C-682E22B645D2}.Release|x64.Build.0 = Release|Any CPU
{8AA01AE0-1F81-41E4-917C-682E22B645D2}.Release|x86.ActiveCfg = Release|Any CPU
{8AA01AE0-1F81-41E4-917C-682E22B645D2}.Release|x86.Build.0 = Release|Any CPU
{6B36CB22-F186-4AD9-8A6D-13E7A3E6D3A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6B36CB22-F186-4AD9-8A6D-13E7A3E6D3A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6B36CB22-F186-4AD9-8A6D-13E7A3E6D3A2}.Debug|x64.ActiveCfg = Debug|Any CPU
{6B36CB22-F186-4AD9-8A6D-13E7A3E6D3A2}.Debug|x64.Build.0 = Debug|Any CPU
{6B36CB22-F186-4AD9-8A6D-13E7A3E6D3A2}.Debug|x86.ActiveCfg = Debug|Any CPU
{6B36CB22-F186-4AD9-8A6D-13E7A3E6D3A2}.Debug|x86.Build.0 = Debug|Any CPU
{6B36CB22-F186-4AD9-8A6D-13E7A3E6D3A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6B36CB22-F186-4AD9-8A6D-13E7A3E6D3A2}.Release|Any CPU.Build.0 = Release|Any CPU
{6B36CB22-F186-4AD9-8A6D-13E7A3E6D3A2}.Release|x64.ActiveCfg = Release|Any CPU
{6B36CB22-F186-4AD9-8A6D-13E7A3E6D3A2}.Release|x64.Build.0 = Release|Any CPU
{6B36CB22-F186-4AD9-8A6D-13E7A3E6D3A2}.Release|x86.ActiveCfg = Release|Any CPU
{6B36CB22-F186-4AD9-8A6D-13E7A3E6D3A2}.Release|x86.Build.0 = Release|Any CPU
{B8998272-1E13-4FC0-BC5C-B012E85A2A78}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B8998272-1E13-4FC0-BC5C-B012E85A2A78}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B8998272-1E13-4FC0-BC5C-B012E85A2A78}.Debug|x64.ActiveCfg = Debug|Any CPU
{B8998272-1E13-4FC0-BC5C-B012E85A2A78}.Debug|x64.Build.0 = Debug|Any CPU
{B8998272-1E13-4FC0-BC5C-B012E85A2A78}.Debug|x86.ActiveCfg = Debug|Any CPU
{B8998272-1E13-4FC0-BC5C-B012E85A2A78}.Debug|x86.Build.0 = Debug|Any CPU
{B8998272-1E13-4FC0-BC5C-B012E85A2A78}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B8998272-1E13-4FC0-BC5C-B012E85A2A78}.Release|Any CPU.Build.0 = Release|Any CPU
{B8998272-1E13-4FC0-BC5C-B012E85A2A78}.Release|x64.ActiveCfg = Release|Any CPU
{B8998272-1E13-4FC0-BC5C-B012E85A2A78}.Release|x64.Build.0 = Release|Any CPU
{B8998272-1E13-4FC0-BC5C-B012E85A2A78}.Release|x86.ActiveCfg = Release|Any CPU
{B8998272-1E13-4FC0-BC5C-B012E85A2A78}.Release|x86.Build.0 = Release|Any CPU
{FBA8ABC5-9CDC-455D-9AA3-8249FC0F3A34}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FBA8ABC5-9CDC-455D-9AA3-8249FC0F3A34}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FBA8ABC5-9CDC-455D-9AA3-8249FC0F3A34}.Debug|x64.ActiveCfg = Debug|Any CPU
{FBA8ABC5-9CDC-455D-9AA3-8249FC0F3A34}.Debug|x64.Build.0 = Debug|Any CPU
{FBA8ABC5-9CDC-455D-9AA3-8249FC0F3A34}.Debug|x86.ActiveCfg = Debug|Any CPU
{FBA8ABC5-9CDC-455D-9AA3-8249FC0F3A34}.Debug|x86.Build.0 = Debug|Any CPU
{FBA8ABC5-9CDC-455D-9AA3-8249FC0F3A34}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FBA8ABC5-9CDC-455D-9AA3-8249FC0F3A34}.Release|Any CPU.Build.0 = Release|Any CPU
{FBA8ABC5-9CDC-455D-9AA3-8249FC0F3A34}.Release|x64.ActiveCfg = Release|Any CPU
{FBA8ABC5-9CDC-455D-9AA3-8249FC0F3A34}.Release|x64.Build.0 = Release|Any CPU
{FBA8ABC5-9CDC-455D-9AA3-8249FC0F3A34}.Release|x86.ActiveCfg = Release|Any CPU
{FBA8ABC5-9CDC-455D-9AA3-8249FC0F3A34}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -302,7 +318,9 @@ Global
{6DAC88A4-9DCF-490B-81D3-B0AA72993EA4} = {3566B926-8E32-4B0D-8DC0-6DB1EB82D2CB}
{C59D94B8-9869-4E12-B393-2645E9C38BF3} = {22CE8614-4057-44E0-8140-FFB46C642539}
{8AA01AE0-1F81-41E4-917C-682E22B645D2} = {3566B926-8E32-4B0D-8DC0-6DB1EB82D2CB}
{6B36CB22-F186-4AD9-8A6D-13E7A3E6D3A2} = {3566B926-8E32-4B0D-8DC0-6DB1EB82D2CB}
{DFE3C93F-B067-4F83-9964-5E8014007338} = {3566B926-8E32-4B0D-8DC0-6DB1EB82D2CB}
{B8998272-1E13-4FC0-BC5C-B012E85A2A78} = {DFE3C93F-B067-4F83-9964-5E8014007338}
{FBA8ABC5-9CDC-455D-9AA3-8249FC0F3A34} = {DFE3C93F-B067-4F83-9964-5E8014007338}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {087BE4A7-5329-4F83-84DF-7AA56A3944EE}
Expand Down
3 changes: 2 additions & 1 deletion build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ Task("RunTests")
{
NoBuild = true,
NoRestore = true,
Logger = $"trx;LogFileName={testResultFile.FullPath}"
Logger = $"trx;LogFileName={testResultFile.FullPath}",
Filter = "TestCategory!=External"
};
DotCoverCover(c => c.DotNetCoreTest(projectFile, settings), coverageResultFile, dotCoverSettings);
Expand Down
5 changes: 4 additions & 1 deletion samples/RabbitMQ/ReceiverSetup/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Nybus;
using Nybus.Configuration;
using RabbitMQ.Client;
using Types;

namespace ReceiverConfig
namespace ReceiverSetup
{
class Program
{
Expand Down Expand Up @@ -46,6 +47,8 @@ public async Task OnExecuteAsync()
Password = Password,
VirtualHost = VirtualHost
};
c.CommandQueueFactory = new StaticQueueFactory("my-test-queue");
});
});
Expand Down
9 changes: 9 additions & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@

<NoWarn>NU5105</NoWarn>
</PropertyGroup>


<PropertyGroup>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-*" PrivateAssets="All"/>
<PackageReference Include="SourceLink.Copy.PdbFiles" Version="2.8.3" PrivateAssets="All" />
</ItemGroup>
</Project>
6 changes: 1 addition & 5 deletions src/Nybus.Abstractions/IBusHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ public interface IBusHost

Task StopAsync();

void SubscribeToCommand<TCommand>(CommandReceived<TCommand> commandReceived) where TCommand : class, ICommand;

void SubscribeToEvent<TEvent>(EventReceived<TEvent> eventReceived) where TEvent : class, IEvent;

IBusExecutionEnvironment ExecutionEnvironment { get; }
IBus Bus { get; }
}

public delegate Task CommandReceived<TCommand>(IDispatcher dispatcher, ICommandContext<TCommand> context) where TCommand : class, ICommand;
Expand Down
2 changes: 2 additions & 0 deletions src/Nybus.Abstractions/IContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public interface IContext
DateTimeOffset SentOn { get; }

Guid CorrelationId { get; }

Message Message { get; }
}
}
2 changes: 1 addition & 1 deletion src/Nybus.Abstractions/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public abstract class Message

public abstract Type Type { get; }

public MessageDescriptor Descriptor => MessageDescriptor.CreateFromType(Type);
public MessageDescriptor Descriptor => new MessageDescriptor(Type);

protected object Item { get; set; }
}
Expand Down
43 changes: 31 additions & 12 deletions src/Nybus.Abstractions/Utils/IMessageDescriptorStore.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
using System;
using System.Collections.Generic;
using System.Reflection;

namespace Nybus.Utils
{
public interface IMessageDescriptorStore
{
bool RegisterType(Type type);
bool RegisterCommandType<TCommand>()
where TCommand : class, ICommand;

bool TryGetDescriptorForType(Type type, out MessageDescriptor descriptor);
bool RegisterEventType<TEvent>()
where TEvent : class, IEvent;

bool TryGetTypeForDescriptor(MessageDescriptor descriptor, out Type type);
bool FindCommandTypeForDescriptor(MessageDescriptor descriptor, out Type type);

bool FindEventTypeForDescriptor(MessageDescriptor descriptor, out Type type);

bool HasCommands();

bool HasEvents();

IEnumerable<Type> Commands { get; }

IEnumerable<Type> Events { get; }
}

public class MessageDescriptor
Expand All @@ -21,24 +34,26 @@ public static MessageDescriptor CreateFromType(Type type)
throw new ArgumentNullException(nameof(type));
}

return new MessageDescriptor(type.Name, type.Namespace);
}
var attribute = type.GetCustomAttribute<MessageAttribute>();

public static MessageDescriptor CreateFromAttribute (MessageAttribute attribute)
{
if (attribute == null)
{
throw new ArgumentNullException(nameof(attribute));
return new MessageDescriptor(type);
}

return new MessageDescriptor(attribute.Name, attribute.Namespace);
return new MessageDescriptor(attribute);
}


private static readonly char[] Separators = new []{':'};

public static bool TryParse(string descriptorName, out MessageDescriptor descriptor)
{
if (descriptorName == null)
{
descriptor = null;
return false;
}

var strings = descriptorName.Split(':');

if (strings.Length != 2)
Expand All @@ -51,6 +66,10 @@ public static bool TryParse(string descriptorName, out MessageDescriptor descrip
return true;
}

public MessageDescriptor(MessageAttribute attribute) : this (attribute.Name, attribute.Namespace) { }

public MessageDescriptor(Type type) : this (type.Name, type.Namespace) { }

public MessageDescriptor(string name, string @namespace)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
Expand All @@ -63,7 +82,7 @@ public MessageDescriptor(string name, string @namespace)

public override string ToString() => $"{Namespace}:{Name}";

public static implicit operator MessageDescriptor(Type type) => CreateFromType(type);
public static implicit operator string(MessageDescriptor descriptor) => descriptor.ToString();

public static readonly IEqualityComparer<MessageDescriptor> EqualityComparer = new MessageDescriptorEqualityComparer();

Expand Down
2 changes: 1 addition & 1 deletion src/Nybus/NybusContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected NybusContext (Message message)

public Guid CorrelationId { get; }

protected Message Message { get; }
public Message Message { get; }
}

public class NybusCommandContext<TCommand> : NybusContext, ICommandContext<TCommand> where TCommand : class, ICommand
Expand Down
10 changes: 5 additions & 5 deletions src/Nybus/NybusDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ namespace Nybus
public class NybusDispatcher : IDispatcher
{
private readonly IBus _bus;
private readonly Guid _correlationId;
private readonly Message _message;

public NybusDispatcher(IBus innerBus, Guid correlationId)
public NybusDispatcher(IBus innerBus, Message message)
{
_bus = innerBus ?? throw new ArgumentNullException(nameof(innerBus));
_correlationId = correlationId;
_message = message ?? throw new ArgumentNullException(nameof(message));
}

public Task InvokeCommandAsync<TCommand>(TCommand command)
where TCommand : class, ICommand
{
return _bus.InvokeCommandAsync(command, _correlationId);
return _bus.InvokeCommandAsync(command, _message.Headers.CorrelationId);
}

public Task RaiseEventAsync<TEvent>(TEvent @event)
where TEvent : class, IEvent
{
return _bus.RaiseEventAsync(@event, _correlationId);
return _bus.RaiseEventAsync(@event, _message.Headers.CorrelationId);
}
}
}
6 changes: 4 additions & 2 deletions src/Nybus/NybusHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public NybusHost(IBusEngine busEngine, INybusConfiguration configuration, IServi
_serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
}

public IBus Bus => this;

public Task InvokeCommandAsync<TCommand>(TCommand command, Guid correlationId) where TCommand : class, ICommand
{
var message = new CommandMessage<TCommand>
Expand Down Expand Up @@ -123,7 +125,7 @@ async Task ProcessMessage(Message message)

async Task ExecuteHandler(ICommandContext<TCommand> context)
{
var dispatcher = new NybusDispatcher(this, context.CorrelationId);
var dispatcher = new NybusDispatcher(this, context.Message);
await commandReceived(dispatcher, context).ConfigureAwait(false);
}

Expand Down Expand Up @@ -165,7 +167,7 @@ async Task ProcessMessage(Message message)

async Task ExecuteHandler(IEventContext<TEvent> context)
{
var dispatcher = new NybusDispatcher(this, context.CorrelationId);
var dispatcher = new NybusDispatcher(this, context.Message);
await eventReceived(dispatcher, context).ConfigureAwait(false);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Nybus/NybusHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Nybus
public class NybusHostBuilder : ISubscriptionBuilder
{
private readonly ILoggerFactory _loggerFactory;
private readonly IList<Action<IBusHost>> _subscriptions = new List<Action<IBusHost>>();
private readonly IList<Action<NybusHost>> _subscriptions = new List<Action<NybusHost>>();

public NybusHostBuilder(ILoggerFactory loggerFactory)
{
Expand Down
Loading

0 comments on commit c639730

Please sign in to comment.