-
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.
Added Tests to the AsyncProcessing package
(#release-note: Added Tests to the AsyncProcessing package#)
- Loading branch information
Farshad DASHTI
authored and
Farshad DASHTI
committed
Oct 9, 2024
1 parent
10e017a
commit 193cff6
Showing
11 changed files
with
166 additions
and
8 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
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
3 changes: 3 additions & 0 deletions
3
src/DfE.CoreLibs.Contracts/Academies/V1/EducationalPerformance/SchoolAbsenceDataDto.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
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
5 changes: 4 additions & 1 deletion
5
src/DfE.CoreLibs.Contracts/Academies/V4/Establishments/EstablishmentDto.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
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
29 changes: 29 additions & 0 deletions
29
src/Tests/DfE.CoreLibs.AsyncProcessing.Tests/DfE.CoreLibs.AsyncProcessing.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,29 @@ | ||
<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="coverlet.collector" Version="6.0.0" /> | ||
<PackageReference Include="MediatR" Version="12.4.1" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="xunit" Version="2.5.3" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\DfE.CoreLibs.AsyncProcessing\DfE.CoreLibs.AsyncProcessing.csproj" /> | ||
<ProjectReference Include="..\..\DfE.CoreLibs.Testing\DfE.CoreLibs.Testing.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit" /> | ||
</ItemGroup> | ||
|
||
</Project> |
109 changes: 109 additions & 0 deletions
109
src/Tests/DfE.CoreLibs.AsyncProcessing.Tests/Services/BackgroundServiceFactoryTests.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,109 @@ | ||
using DfE.CoreLibs.AsyncProcessing.Interfaces; | ||
using DfE.CoreLibs.AsyncProcessing.Services; | ||
using DfE.CoreLibs.Testing.AutoFixture.Attributes; | ||
using MediatR; | ||
using NSubstitute; | ||
|
||
namespace DfE.CoreLibs.AsyncProcessing.Tests.Services | ||
{ | ||
public class BackgroundServiceFactoryTests | ||
{ | ||
private readonly IMediator _mediator; | ||
private readonly BackgroundServiceFactory _backgroundServiceFactory; | ||
|
||
public BackgroundServiceFactoryTests() | ||
{ | ||
_mediator = Substitute.For<IMediator>(); | ||
_backgroundServiceFactory = new BackgroundServiceFactory(_mediator); | ||
} | ||
|
||
[Theory] | ||
[CustomAutoData] | ||
public async Task EnqueueTask_ShouldProcessTaskInQueue( | ||
Func<Task<int>> taskFunc, | ||
IBackgroundServiceEvent eventMock) | ||
{ | ||
// Arrange | ||
Func<int, IBackgroundServiceEvent> eventFactory = _ => eventMock; | ||
|
||
var semaphore = new SemaphoreSlim(0, 1); | ||
bool taskExecuted = false; | ||
|
||
Func<Task<int>> wrappedTaskFunc = async () => | ||
{ | ||
taskExecuted = true; | ||
semaphore.Release(); | ||
return await taskFunc(); | ||
}; | ||
|
||
// Act | ||
_backgroundServiceFactory.EnqueueTask(wrappedTaskFunc, eventFactory); | ||
|
||
// Ensure the task gets processed | ||
await semaphore.WaitAsync(1000); | ||
|
||
// Assert | ||
Assert.True(taskExecuted, "Task in the queue should have been processed."); | ||
} | ||
|
||
[Theory] | ||
[CustomAutoData] | ||
public async Task EnqueueTask_ShouldPublishEvent_WhenEventFactoryIsProvided( | ||
int taskResult, | ||
IBackgroundServiceEvent eventMock) | ||
{ | ||
// Arrange | ||
Func<int, IBackgroundServiceEvent> eventFactory = _ => eventMock; | ||
|
||
// Act | ||
_backgroundServiceFactory.EnqueueTask(() => Task.FromResult(taskResult), eventFactory); | ||
|
||
// Trigger processing | ||
await Task.Delay(100); | ||
|
||
// Assert | ||
await _mediator.Received(1).Publish(eventMock, Arg.Any<CancellationToken>()); | ||
} | ||
|
||
[Theory] | ||
[CustomAutoData] | ||
public async Task StartProcessingQueue_ShouldProcessTasksSequentially( | ||
Func<Task<int>> taskFunc, | ||
IBackgroundServiceEvent eventMock) | ||
{ | ||
// Arrange | ||
Func<int, IBackgroundServiceEvent> eventFactory = _ => eventMock; | ||
|
||
int taskCount = 0; | ||
Func<Task<int>> wrappedTaskFunc = async () => | ||
{ | ||
Interlocked.Increment(ref taskCount); | ||
return await taskFunc(); | ||
}; | ||
|
||
_backgroundServiceFactory.EnqueueTask(wrappedTaskFunc, eventFactory); | ||
_backgroundServiceFactory.EnqueueTask(wrappedTaskFunc, eventFactory); | ||
|
||
// Act | ||
await Task.Delay(100); // Allow time for processing | ||
|
||
// Assert | ||
Assert.Equal(2, taskCount); | ||
} | ||
|
||
[Fact] | ||
public async Task ExecuteAsync_ShouldStopProcessing_WhenCancellationRequested() | ||
{ | ||
// Arrange | ||
using var cts = new CancellationTokenSource(); | ||
var task = _backgroundServiceFactory.StartAsync(cts.Token); | ||
|
||
// Act | ||
await cts.CancelAsync(); | ||
await task; | ||
|
||
// Assert | ||
Assert.True(task.IsCompletedSuccessfully); | ||
} | ||
} | ||
} |