-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
149248d
commit 029d2fa
Showing
6 changed files
with
165 additions
and
12 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
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
45 changes: 45 additions & 0 deletions
45
tests/ReflectionEventing.UnitTests/EventBusBuilderTests.cs
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,45 @@ | ||
// This Source Code Form is subject to the terms of the MIT License. | ||
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT. | ||
// Copyright (C) Leszek Pomianowski and ReflectionEventing Contributors. | ||
// All Rights Reserved. | ||
|
||
using FluentAssertions; | ||
|
||
namespace ReflectionEventing.UnitTests; | ||
|
||
public sealed class EventBusBuilderTests | ||
{ | ||
private readonly EventBusBuilder _eventBusBuilder = new EventBusBuilder(); | ||
|
||
[Fact] | ||
public void AddConsumer_ShouldAddConsumerToDictionary() | ||
{ | ||
Type consumerType = typeof(MySampleConsumer); | ||
|
||
_eventBusBuilder.AddConsumer(consumerType); | ||
|
||
IDictionary<Type, IEnumerable<Type>> consumers = _eventBusBuilder.GetConsumers(); | ||
_ = consumers.Should().ContainKey(consumerType); | ||
} | ||
|
||
[Fact] | ||
public void AddConsumer_ShouldAddEventTypeToConsumerInDictionary() | ||
{ | ||
Type consumerType = typeof(MySampleConsumer); | ||
|
||
_eventBusBuilder.AddConsumer(consumerType); | ||
|
||
IDictionary<Type, IEnumerable<Type>> consumers = _eventBusBuilder.GetConsumers(); | ||
_ = consumers[consumerType].Should().Contain(typeof(TestEvent)); | ||
} | ||
|
||
public sealed record TestEvent; | ||
|
||
public sealed record MySampleConsumer : IConsumer<TestEvent> | ||
{ | ||
public Task ConsumeAsync(TestEvent payload, CancellationToken cancellationToken) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
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,52 @@ | ||
// This Source Code Form is subject to the terms of the MIT License. | ||
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT. | ||
// Copyright (C) Leszek Pomianowski and ReflectionEventing Contributors. | ||
// All Rights Reserved. | ||
|
||
namespace ReflectionEventing.UnitTests; | ||
|
||
public sealed class EventBusTests | ||
{ | ||
private readonly IConsumerProvider _consumerProvider; | ||
private readonly IConsumerTypesProvider _consumerTypesProvider; | ||
private readonly EventBus _eventBus; | ||
|
||
public EventBusTests() | ||
{ | ||
_consumerProvider = Substitute.For<IConsumerProvider>(); | ||
_consumerTypesProvider = Substitute.For<IConsumerTypesProvider>(); | ||
_eventBus = new EventBus(_consumerProvider, _consumerTypesProvider); | ||
} | ||
|
||
[Fact] | ||
public async Task PublishAsync_ShouldCallConsumeAsyncOnAllConsumers() | ||
{ | ||
TestEvent testEvent = new(); | ||
Type consumerType = typeof(IConsumer<TestEvent>); | ||
IConsumer<TestEvent> consumer = Substitute.For<IConsumer<TestEvent>>(); | ||
|
||
_ = _consumerTypesProvider.GetConsumerTypes<TestEvent>().Returns([consumerType]); | ||
_ = _consumerProvider.GetConsumerTypes(consumerType).Returns([consumer]); | ||
|
||
await _eventBus.PublishAsync(testEvent, CancellationToken.None); | ||
|
||
await consumer.Received().ConsumeAsync(testEvent, Arg.Any<CancellationToken>()); | ||
} | ||
|
||
[Fact] | ||
public async Task Publish_ShouldCallPublishAsync() | ||
{ | ||
TestEvent testEvent = new(); | ||
Type consumerType = typeof(IConsumer<TestEvent>); | ||
IConsumer<TestEvent> consumer = Substitute.For<IConsumer<TestEvent>>(); | ||
|
||
_ = _consumerTypesProvider.GetConsumerTypes<TestEvent>().Returns([consumerType]); | ||
_ = _consumerProvider.GetConsumerTypes(consumerType).Returns([consumer]); | ||
|
||
_eventBus.Publish(testEvent); | ||
|
||
await consumer.Received().ConsumeAsync(testEvent, Arg.Any<CancellationToken>()); | ||
} | ||
|
||
public sealed record TestEvent; | ||
} |
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,11 @@ | ||
// This Source Code Form is subject to the terms of the MIT License. | ||
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT. | ||
// Copyright (C) Leszek Pomianowski and ReflectionEventing Contributors. | ||
// All Rights Reserved. | ||
|
||
global using NSubstitute; | ||
global using System; | ||
global using System.Collections.Generic; | ||
global using System.Threading; | ||
global using System.Threading.Tasks; | ||
global using Xunit; |
28 changes: 28 additions & 0 deletions
28
tests/ReflectionEventing.UnitTests/ReflectionEventing.UnitTests.csproj
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,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentAssertions"/> | ||
<PackageReference Include="NSubstitute"/> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk"/> | ||
<PackageReference Include="xunit" /> | ||
<PackageReference Include="xunit.runner.visualstudio"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\ReflectionEventing\ReflectionEventing.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |