Skip to content

Commit

Permalink
Merge pull request #4 from Kralizek/master
Browse files Browse the repository at this point in the history
BusExtensions and package dependencies constraints
  • Loading branch information
Kralizek committed Sep 29, 2015
2 parents f1019db + 078b818 commit 7f90c8f
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 4 deletions.
37 changes: 37 additions & 0 deletions src/Core/BusExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nybus
{
public static class BusExtensions
{
public static async Task InvokeManyCommands<TCommand>(this IBus bus, IEnumerable<TCommand> commands)
where TCommand : class, ICommand
{
if (bus == null)
{
throw new ArgumentNullException(nameof(bus));
}

if (commands == null) return;

await Task.WhenAll(commands.Select(bus.InvokeCommand)).ConfigureAwait(false);
}

public static async Task RaiseManyEvents<TEvent>(this IBus bus, IEnumerable<TEvent> events)
where TEvent : class, IEvent
{
if (bus == null)
{
throw new ArgumentNullException(nameof(bus));
}

if (events == null) return;

await Task.WhenAll(events.Select(bus.RaiseEvent)).ConfigureAwait(false);
}
}
}
1 change: 1 addition & 0 deletions src/Core/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="BusExtensions.cs" />
<Compile Include="Configuration\DelegateCommandHandler.cs" />
<Compile Include="Configuration\DelegateEventHandler.cs" />
<Compile Include="Configuration\IBusBuilder.cs" />
Expand Down
4 changes: 2 additions & 2 deletions src/MassTransit/MassTransit.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<description>Nybus bus based on MassTransit</description>
<language>en-US</language>
<dependencies>
<dependency id="MassTransit" version="2.10.1" />
<dependency id="MassTransit.RabbitMQ" version="2.10.1" />
<dependency id="MassTransit" version="[2,3)" />
<dependency id="MassTransit.RabbitMQ" version="[2,3)" />
</dependencies>
</metadata>
</package>
2 changes: 1 addition & 1 deletion src/NLog/NLog.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<description>NLog support for Nybus</description>
<language>en-US</language>
<dependencies>
<dependency id="NLog.Config" version="4.1.1" />
<dependency id="NLog.Config" version="[4.1,5)" />
</dependencies>
</metadata>
</package>
2 changes: 1 addition & 1 deletion src/Rx/Rx.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<description>Rx support for Nybus.</description>
<language>en-US</language>
<dependencies>
<dependency id="Rx-Linq" version="2.2.5" />
<dependency id="Rx-Linq" version="[2.2,3)" />
</dependencies>
</metadata>
</package>
85 changes: 85 additions & 0 deletions tests/Tests.Core/BusExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Moq;
using NUnit.Framework;
using Nybus;
using Ploeh.AutoFixture;
// ReSharper disable InvokeAsExtensionMethod

namespace Tests
{
[TestFixture]
public class BusExtensionsTests
{
private IFixture fixture;
private Mock<IBus> mockBus;

[SetUp]
public void Initialize()
{
fixture = new Fixture();

mockBus = new Mock<IBus>();
}

[Test]
public async Task InvokeManyCommands_forwards_to_IBus()
{
var commands = fixture.CreateMany<TestCommand>().ToArray();

await BusExtensions.InvokeManyCommands(mockBus.Object, commands);

mockBus.Verify(p => p.InvokeCommand(It.IsAny<TestCommand>()), Times.Exactly(commands.Length));
}

[Test, ExpectedException]
public async Task InvokeManyCommands_Bus_is_required()
{
var commands = fixture.CreateMany<TestCommand>().ToArray();

await BusExtensions.InvokeManyCommands(null, commands);
}

[Test]
public async Task InvokeManyCommands_no_exception_if_commands_is_null()
{
IEnumerable<TestCommand> commands = null;

await BusExtensions.InvokeManyCommands(mockBus.Object, commands);

mockBus.Verify(p => p.InvokeCommand(It.IsAny<TestCommand>()), Times.Never);

}

[Test]
public async Task RaiseManyEvents_forwards_to_IBus()
{
var events = fixture.CreateMany<TestEvent>().ToArray();

await BusExtensions.RaiseManyEvents(mockBus.Object, events);

mockBus.Verify(p => p.RaiseEvent(It.IsAny<TestEvent>()), Times.Exactly(events.Length));
}

[Test, ExpectedException]
public async Task RaiseManyEvents_Bus_is_required()
{
var events = fixture.CreateMany<TestEvent>().ToArray();

await BusExtensions.RaiseManyEvents(null, events);
}

[Test]
public async Task RaiseManyEvents_no_exception_if_commands_is_null()
{
IEnumerable<TestEvent> events = null;

await BusExtensions.RaiseManyEvents(mockBus.Object, events);

mockBus.Verify(p => p.RaiseEvent(It.IsAny<TestEvent>()), Times.Never);

}

}
}
1 change: 1 addition & 0 deletions tests/Tests.Core/Tests.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="BusExtensionsTests.cs" />
<Compile Include="Configuration\DefaultCommandContextFactoryTests.cs" />
<Compile Include="Configuration\DefaultCommandMessageFactoryTests.cs" />
<Compile Include="Configuration\DefaultEventContextFactoryTests.cs" />
Expand Down

0 comments on commit 7f90c8f

Please sign in to comment.