-
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
Showing
13 changed files
with
301 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=backi/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=timediatr/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary> |
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,7 @@ | ||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> | ||
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=0c682e2c_002D1af8_002D41e4_002D9940_002Da3dcb1d4afe0/@EntryIndexedValue"><SessionState ContinuousTestingMode="0" IsActive="True" Name="CreateHandlerEachTime" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> | ||
<TestAncestor> | ||
<TestId>MSTest::2243861D-99F1-427E-80A6-DDED312872D7::net8.0::Backi.Tests.SendMediatrRequestShould.CreateHandlerEachTime</TestId> | ||
<TestId>MSTest::C47EDCC3-FF07-4B1E-BDC4-06544C6264F5::net8.0::Backi.Tests.TimediatrShould.SendCommandOneFewTimes</TestId> | ||
</TestAncestor> | ||
</SessionState></s:String></wpf:ResourceDictionary> |
25 changes: 25 additions & 0 deletions
25
mediator/dotnet/Backi.Mediatr.Tests/Backi.Mediatr.Tests.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,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentAssertions" Version="6.12.2" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.1" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/> | ||
<PackageReference Include="MSTest.TestAdapter" Version="3.0.4"/> | ||
<PackageReference Include="MSTest.TestFramework" Version="3.0.4"/> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\lib\Backi.Mediatr.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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 @@ | ||
global using Microsoft.VisualStudio.TestTools.UnitTesting; |
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,59 @@ | ||
using MediatR; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Backi.Tests; | ||
|
||
public static class ServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddMediatrTestingInfrastructure(this IServiceCollection services) | ||
{ | ||
services.AddSingleton<CounterCollection>(); | ||
services.AddMediatR(m => m.RegisterServicesFromAssembly(typeof(CounterCollection).Assembly)); | ||
services.AddLogging(l => | ||
{ | ||
l.AddSimpleConsole(c => c.SingleLine = true); | ||
l.SetMinimumLevel(LogLevel.Debug); | ||
}); | ||
|
||
return services; | ||
} | ||
} | ||
|
||
public class CounterCollection | ||
{ | ||
public readonly Dictionary<string, int> items = new (); | ||
|
||
public void Increment(string key) | ||
{ | ||
if (!items.TryAdd(key, 1)) | ||
{ | ||
items[key] += 1; | ||
} | ||
} | ||
|
||
public int Get(string key) => items.GetValueOrDefault(key); | ||
} | ||
|
||
public class CommandOne : IRequest | ||
{ | ||
public class Handler : IRequestHandler<CommandOne> | ||
{ | ||
readonly CounterCollection counters; | ||
public const string ConstructorCounterKey = "CommandA.Handler.Constructor"; | ||
public const string HandleCounterKey = "CommandA.Handler.Handle"; | ||
|
||
public Handler(CounterCollection counters) | ||
{ | ||
this.counters = counters; | ||
|
||
counters.Increment(ConstructorCounterKey); | ||
} | ||
|
||
public Task Handle(CommandOne request, CancellationToken cancellationToken) | ||
{ | ||
counters.Increment(HandleCounterKey); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
mediator/dotnet/Backi.Mediatr.Tests/SendMediatrRequestShould.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,32 @@ | ||
using FluentAssertions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Backi.Tests; | ||
|
||
[TestClass] | ||
public class SendMediatrRequestShould | ||
{ | ||
[TestMethod] | ||
public async Task CreateHandlerEachTime() | ||
{ | ||
var services = new ServiceCollection(); | ||
|
||
services.AddMediatrTestingInfrastructure(); | ||
|
||
var provider = services.BuildServiceProvider(); | ||
|
||
var scopeFactory = provider.GetRequiredService<IServiceScopeFactory>(); | ||
var logger = provider.GetRequiredService<ILogger<SendMediatrRequestShould>>(); | ||
|
||
var commandInstance = new CommandOne(); | ||
|
||
await scopeFactory.SendMediatorRequest(commandInstance, logger); | ||
await scopeFactory.SendMediatorRequest(commandInstance, logger); | ||
|
||
var counters = provider.GetRequiredService<CounterCollection>(); | ||
|
||
counters.Get(CommandOne.Handler.ConstructorCounterKey).Should().Be(2); | ||
counters.Get(CommandOne.Handler.HandleCounterKey).Should().Be(2); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
mediator/dotnet/Backi.Timediatr.Tests/Backi.Timediatr.Tests.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,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.1" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.1" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/> | ||
<PackageReference Include="MSTest.TestAdapter" Version="3.0.4"/> | ||
<PackageReference Include="MSTest.TestFramework" Version="3.0.4"/> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Backi.Mediatr.Tests\Backi.Mediatr.Tests.csproj" /> | ||
<ProjectReference Include="..\Backi.Timediatr\Backi.Timediatr.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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 @@ | ||
global using Microsoft.VisualStudio.TestTools.UnitTesting; |
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,34 @@ | ||
using FluentAssertions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace Backi.Tests; | ||
|
||
[TestClass] | ||
public class TimediatrShould | ||
{ | ||
[TestMethod] | ||
public async Task SendCommandOneFewTimes() | ||
{ | ||
var services = new ServiceCollection(); | ||
|
||
services.AddMediatrTestingInfrastructure(); | ||
services.AddTimediatr(timediatr => timediatr.Configure(o => | ||
{ | ||
o.Schedule.Add(new CommandOne(), TimeSpan.FromSeconds(3)); | ||
})); | ||
|
||
var provider = services.BuildServiceProvider(); | ||
|
||
var backgroundService = (TimediatrBackgroundService)provider.GetRequiredService<IHostedService>(); | ||
|
||
await backgroundService.StartAsync(CancellationToken.None); | ||
await Task.Delay(TimeSpan.FromSeconds(10)); | ||
await backgroundService.StopAsync(CancellationToken.None); | ||
|
||
var counter = provider.GetRequiredService<CounterCollection>(); | ||
|
||
counter.Get(CommandOne.Handler.HandleCounterKey).Should().Be(4); | ||
counter.Get(CommandOne.Handler.ConstructorCounterKey).Should().Be(4); | ||
} | ||
} |
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,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Backi.Timers" Version="2024.110.129.4" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\lib\Backi.Mediatr.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,65 @@ | ||
using Backi.Timers; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Backi; | ||
|
||
public class TimediatrBackgroundService( | ||
IOptions<TimediatrConfiguration> options, | ||
IServiceScopeFactory serviceScopeFactory, | ||
ILogger<TimediatrBackgroundService> logger) : IHostedService | ||
{ | ||
readonly List<SafeTimer> timers = []; | ||
|
||
public Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
var schedule = options.Value.Schedule; | ||
|
||
foreach (var timer in schedule) | ||
{ | ||
logger.LogDebug("Setting {requestType} to run with interval {interval}", timer.Key.GetType(), timer.Value); | ||
|
||
timers.Add(SafeTimer.RunNowAndPeriodically( | ||
timer.Value, | ||
() => serviceScopeFactory.SendMediatorRequest(timer.Key, logger, cancellationToken), | ||
ex => logger.LogError(ex, "Error while processing {requestType}", timer.Key.GetType()) | ||
)); | ||
|
||
logger.LogInformation("Set {requestType} to run with interval {interval}", timer.Key.GetType(), timer.Value); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
logger.LogDebug("Stopping all timers"); | ||
|
||
foreach (var timer in timers) | ||
{ | ||
timer.Stop(); | ||
} | ||
|
||
logger.LogInformation("Stopped all timers"); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
|
||
public class TimediatrConfiguration | ||
{ | ||
public Dictionary<object, TimeSpan> Schedule { get; set; } = new(); | ||
} | ||
|
||
public static class TimediatrServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddTimediatr(this IServiceCollection services, Action<OptionsBuilder<TimediatrConfiguration>> configuration) | ||
{ | ||
services.AddHostedService<TimediatrBackgroundService>(); | ||
var optionsBuilder = services.AddOptions<TimediatrConfiguration>(); | ||
configuration(optionsBuilder); | ||
|
||
return services; | ||
} | ||
} |
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