Skip to content

Commit

Permalink
Additional integration tests (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kralizek committed Jan 7, 2019
1 parent 73024f0 commit 9f87e71
Show file tree
Hide file tree
Showing 17 changed files with 815 additions and 78 deletions.
16 changes: 16 additions & 0 deletions src/Nybus/InMemoryBusEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,30 @@ public Task StopAsync()

public Task NotifySuccessAsync(Message message)
{
OnMessageNotifySuccess?.Invoke(this, new MessageEventArgs(message));
return Task.CompletedTask;
}

public Task NotifyFailAsync(Message message)
{
OnMessageNotifyFail?.Invoke(this, new MessageEventArgs(message));
return Task.CompletedTask;
}

public bool IsTypeAccepted(Type type) => _acceptedTypes.Contains(type);

public event EventHandler<MessageEventArgs> OnMessageNotifySuccess;

public event EventHandler<MessageEventArgs> OnMessageNotifyFail;
}

public class MessageEventArgs : EventArgs
{
public MessageEventArgs(Message message)
{
Message = message ?? throw new ArgumentNullException(nameof(message));
}

public Message Message { get; }
}
}
25 changes: 21 additions & 4 deletions src/Nybus/NybusHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,16 @@ public async Task ExecuteCommandHandlerAsync<TCommand>(IDispatcher dispatcher, I
{
using (var scope = _serviceProvider.CreateScope())
{
var handler = (ICommandHandler<TCommand>)scope.ServiceProvider.GetRequiredService(handlerType);
await handler.HandleAsync(dispatcher, context).ConfigureAwait(false);
try
{
var handler = (ICommandHandler<TCommand>)scope.ServiceProvider.GetRequiredService(handlerType);
await handler.HandleAsync(dispatcher, context).ConfigureAwait(false);
}
catch (InvalidOperationException ex)
{
_logger.LogError(new { commandType = typeof(TCommand), handlerType}, s => $"No valid registration for {s.handlerType.FullName}");
throw new ConfigurationException($"No valid registration for {handlerType.FullName}", ex);
}
}
}

Expand All @@ -204,8 +212,17 @@ public async Task ExecuteEventHandlerAsync<TEvent>(IDispatcher dispatcher, IEven
{
using (var scope = _serviceProvider.CreateScope())
{
var handler = (IEventHandler<TEvent>)scope.ServiceProvider.GetRequiredService(handlerType);
await handler.HandleAsync(dispatcher, context).ConfigureAwait(false);
try
{
var handler = (IEventHandler<TEvent>)scope.ServiceProvider.GetRequiredService(handlerType);
await handler.HandleAsync(dispatcher, context).ConfigureAwait(false);

}
catch (InvalidOperationException ex)
{
_logger.LogError(new { eventType = typeof(TEvent), handlerType }, s => $"No valid registration for {s.handlerType.FullName}");
throw new ConfigurationException($"No valid registration for {handlerType.FullName}", ex);
}
}
}
}
Expand Down
21 changes: 18 additions & 3 deletions tests/TestUtils/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,28 @@ public class FirstTestCommand : ICommand
public string Message { get; set; }
}

public class SecondTestCommand : ICommand { }

public class FirstTestCommandHandler : ICommandHandler<FirstTestCommand>
{
public Task HandleAsync(IDispatcher dispatcher, ICommandContext<FirstTestCommand> incomingCommand)
public virtual Task HandleAsync(IDispatcher dispatcher, ICommandContext<FirstTestCommand> incomingCommand)
{
throw new NotImplementedException();
}
}

public class SecondTestCommand : ICommand { }

public class SecondTestCommandHandler : ICommandHandler<SecondTestCommand>
{
private readonly CommandReceived<SecondTestCommand> _commandReceived;

public SecondTestCommandHandler(CommandReceived<SecondTestCommand> commandReceived)
{
_commandReceived = commandReceived ?? throw new ArgumentNullException(nameof(commandReceived));
}

public virtual Task HandleAsync(IDispatcher dispatcher, ICommandContext<SecondTestCommand> incomingCommand)
{
return _commandReceived(dispatcher, incomingCommand);
}
}
}
21 changes: 18 additions & 3 deletions tests/TestUtils/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,28 @@ namespace Tests
{
public class FirstTestEvent : IEvent { }

public class SecondTestEvent : IEvent { }

public class FirstTestEventHandler : IEventHandler<FirstTestEvent>
{
public Task HandleAsync(IDispatcher dispatcher, IEventContext<FirstTestEvent> incomingEvent)
public virtual Task HandleAsync(IDispatcher dispatcher, IEventContext<FirstTestEvent> incomingEvent)
{
throw new NotImplementedException();
}
}

public class SecondTestEvent : IEvent { }

public class SecondTestEventHandler : IEventHandler<SecondTestEvent>
{
private readonly EventReceived<SecondTestEvent> _eventReceived;

public SecondTestEventHandler(EventReceived<SecondTestEvent> eventReceived)
{
_eventReceived = eventReceived ?? throw new ArgumentNullException(nameof(eventReceived));
}

public virtual Task HandleAsync(IDispatcher dispatcher, IEventContext<SecondTestEvent> incomingEvent)
{
return _eventReceived(dispatcher, incomingEvent);
}
}
}
16 changes: 16 additions & 0 deletions tests/TestUtils/ObservableTestExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;

namespace Tests {
public static class ObservableTestExtensions
{
public static IReadOnlyList<T> DumpInList<T>(this IObservable<T> sequence)
{
var incomingItems = new List<T>();

sequence.Subscribe(incomingItems.Add);

return incomingItems;
}
}
}
6 changes: 5 additions & 1 deletion tests/TestUtils/TestUtils.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Reactive.Linq" Version="3.1.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Nybus.Abstractions\Nybus.Abstractions.csproj" />
</ItemGroup>
Expand Down
79 changes: 79 additions & 0 deletions tests/Tests.Integration.Nybus/AutomaticHandlerBareSetupTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
using Nybus;

namespace Tests
{
[TestFixture]
public class AutomaticHandlerBareSetupTests
{
[Test, AutoMoqData]
public async Task Host_can_loopback_commands(ServiceCollection services, SecondTestCommand testCommand)
{
var commandReceived = Mock.Of<CommandReceived<SecondTestCommand>>();

services.AddLogging(l => l.AddDebug());

services.AddSingleton(commandReceived);
services.AddSingleton<ICommandHandler<SecondTestCommand>, SecondTestCommandHandler>();

services.AddNybus(nybus =>
{
nybus.UseInMemoryBusEngine();
nybus.SubscribeToCommand<SecondTestCommand>();
});

var serviceProvider = services.BuildServiceProvider();

var host = serviceProvider.GetRequiredService<IBusHost>();

var bus = serviceProvider.GetRequiredService<IBus>();

await host.StartAsync();

await bus.InvokeCommandAsync(testCommand);

await host.StopAsync();

Mock.Get(commandReceived).Verify(p => p(It.IsAny<IDispatcher>(), It.IsAny<ICommandContext<SecondTestCommand>>()));
}

[Test, AutoMoqData]
public async Task Host_can_loopback_events(ServiceCollection services, SecondTestEvent testEvent)
{
var eventReceived = Mock.Of<EventReceived<SecondTestEvent>>();

services.AddLogging(l => l.AddDebug());

services.AddSingleton(eventReceived);
services.AddSingleton<IEventHandler<SecondTestEvent>, SecondTestEventHandler>();

services.AddNybus(nybus =>
{
nybus.UseInMemoryBusEngine();
nybus.SubscribeToEvent<SecondTestEvent>();
});

var serviceProvider = services.BuildServiceProvider();

var host = serviceProvider.GetRequiredService<IBusHost>();

var bus = serviceProvider.GetRequiredService<IBus>();

await host.StartAsync();

await bus.RaiseEventAsync(testEvent);

await host.StopAsync();

Mock.Get(eventReceived).Verify(p => p(It.IsAny<IDispatcher>(), It.IsAny<IEventContext<SecondTestEvent>>()));

}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
using Nybus;

namespace Tests
{
[TestFixture]
public class AutomaticHandlerConfigurationSetupTests
{
[Test, AutoMoqData]
public async Task Host_can_loopback_commands(ServiceCollection services, SecondTestCommand testCommand)
{
var settings = new Dictionary<string, string>
{
["Nybus:ErrorPolicy:ProviderName"] = "retry",
["Nybus:ErrorPolicy:MaxRetries"] = "5",
};

var configurationBuilder = new ConfigurationBuilder().AddInMemoryCollection(settings);
var configuration = configurationBuilder.Build();

var commandReceived = Mock.Of<CommandReceived<SecondTestCommand>>();

services.AddLogging(l => l.AddDebug());

services.AddSingleton(commandReceived);
services.AddSingleton<ICommandHandler<SecondTestCommand>, SecondTestCommandHandler>();

services.AddNybus(nybus =>
{
nybus.UseInMemoryBusEngine();
nybus.UseConfiguration(configuration);
nybus.SubscribeToCommand<SecondTestCommand>();
});

var serviceProvider = services.BuildServiceProvider();

var host = serviceProvider.GetRequiredService<IBusHost>();

var bus = serviceProvider.GetRequiredService<IBus>();

await host.StartAsync();

await bus.InvokeCommandAsync(testCommand);

await host.StopAsync();

Mock.Get(commandReceived).Verify(p => p(It.IsAny<IDispatcher>(), It.IsAny<ICommandContext<SecondTestCommand>>()));
}

[Test, AutoMoqData]
public async Task Host_can_loopback_events(ServiceCollection services, SecondTestEvent testEvent)
{
var settings = new Dictionary<string, string>
{
["Nybus:ErrorPolicy:ProviderName"] = "retry",
["Nybus:ErrorPolicy:MaxRetries"] = "5",
};

var configurationBuilder = new ConfigurationBuilder().AddInMemoryCollection(settings);
var configuration = configurationBuilder.Build();

var eventReceived = Mock.Of<EventReceived<SecondTestEvent>>();

services.AddLogging(l => l.AddDebug());

services.AddSingleton(eventReceived);
services.AddSingleton<IEventHandler<SecondTestEvent>, SecondTestEventHandler>();

services.AddNybus(nybus =>
{
nybus.UseInMemoryBusEngine();
nybus.UseConfiguration(configuration);
nybus.SubscribeToEvent<SecondTestEvent>();
});

var serviceProvider = services.BuildServiceProvider();

var host = serviceProvider.GetRequiredService<IBusHost>();

var bus = serviceProvider.GetRequiredService<IBus>();

await host.StartAsync();

await bus.RaiseEventAsync(testEvent);

await host.StopAsync();

Mock.Get(eventReceived).Verify(p => p(It.IsAny<IDispatcher>(), It.IsAny<IEventContext<SecondTestEvent>>()));

}

}
}
Loading

0 comments on commit 9f87e71

Please sign in to comment.