-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from Kralizek/master
BusExtensions and package dependencies constraints
- Loading branch information
Showing
7 changed files
with
128 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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,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); | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,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); | ||
|
||
} | ||
|
||
} | ||
} |
This file contains 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