-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Teach Service to run more than one task concurrently
- Loading branch information
Darran
committed
Sep 3, 2024
1 parent
8c51724
commit 9c0ffb1
Showing
13 changed files
with
184 additions
and
39 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 |
---|---|---|
|
@@ -34,4 +34,7 @@ _ReSharper.* | |
*.ncrunch* | ||
|
||
# NUNIT | ||
TestResult.xml | ||
TestResult.xml | ||
|
||
# Rider | ||
.idea |
29 changes: 29 additions & 0 deletions
29
DalSoft.Hosting.BackgroundQueue.Test/DalSoft.Hosting.BackgroundQueue.Test.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>netcoreapp3.1</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
|
||
<RootNamespace>DalSoft.Hosting.BackgroundQueue.Test</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentAssertions" Version="5.10.3" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="1.3.0"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\DalSoft.Hosting.BackgroundQueue\DalSoft.Hosting.BackgroundQueue.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
43 changes: 43 additions & 0 deletions
43
DalSoft.Hosting.BackgroundQueue.Test/MaxConcurrentCountTest.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,43 @@ | ||
using FluentAssertions; | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace DalSoft.Hosting.BackgroundQueue.Test | ||
{ | ||
public class MaxConcurrentCountTest | ||
{ | ||
[Fact] | ||
public async Task ServiceShouldRunMaxConcurrentCountTaskWhenExistInQueue() | ||
{ | ||
var tokenSource = new CancellationTokenSource(); | ||
var queue = new BackgroundQueue(ex => throw ex, 10, 10); ; | ||
var queueService = new BackgroundQueueService(queue); | ||
var highwaterMark = 0; | ||
|
||
|
||
// queue background tasks | ||
for (var i = 0; i < 20; i++) | ||
{ | ||
queue.Enqueue(async ct => | ||
{ | ||
highwaterMark = Math.Max(queue.ConcurrentCount, highwaterMark); | ||
await Task.Delay(5, ct); | ||
}); | ||
} | ||
|
||
// process background tasks | ||
var runningService = Task.Run(async () => await queueService.StartAsync(tokenSource.Token), tokenSource.Token); | ||
|
||
// wait for all tasks to be processed | ||
while(queue.Count > 0) | ||
{ | ||
await Task.Delay(20, tokenSource.Token); | ||
} | ||
|
||
// Check that tasks run concurrently up to the maxConcurrentCount. | ||
highwaterMark.Should().Be(11); | ||
} | ||
} | ||
} |
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,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2014 Darran | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
25 changes: 17 additions & 8 deletions
25
DalSoft.Hosting.BackgroundQueue/DalSoft.Hosting.BackgroundQueue.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 |
---|---|---|
@@ -1,26 +1,35 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<VersionPrefix>1.1.0</VersionPrefix> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<AssemblyName>DalSoft.Hosting.BackgroundQueue</AssemblyName> | ||
<IsPackable>true</IsPackable> | ||
<PackageId>DalSoft.Hosting.BackgroundQueue</PackageId> | ||
<PackageVersion>1.0.4</PackageVersion> | ||
<PackageReleaseNotes>Removed superfluous Task.Run and now pass the cancellation token to the background task.</PackageReleaseNotes> | ||
<Authors>DalSoft</Authors> | ||
<Title>DalSoft.Hosting.BackgroundQueue - lightweight .NET Core replacement for HostingEnvironment.QueueBackgroundWorkItem</Title> | ||
<Description>DalSoft.Hosting.BackgroundQueue is a very lightweight .NET Core replacement for HostingEnvironment.QueueBackgroundWorkItem using IHostedService</Description> | ||
<Copyright>DalSoft Ltd</Copyright> | ||
<PackageTags>asp.net core webbackgrounder environment.queuebackgroundworkitem IHostedService IHost IWebHost</PackageTags> | ||
<Copyright></Copyright> | ||
<RepositoryUrl>https://github.com/DalSoft/DalSoft.Hosting.BackgroundQueue</RepositoryUrl> | ||
<PackageLicenseUrl>https://github.com/DalSoft/DalSoft.Hosting.BackgroundQueue/blob/master/LICENSE</PackageLicenseUrl> | ||
<Owners>DalSoft</Owners> | ||
<Authors>DalSoft, JonasBr68</Authors> | ||
<PackageReleaseNotes>Added fix to run more than one task concurrently.</PackageReleaseNotes> | ||
<PackageIcon>icon.png</PackageIcon> | ||
<PackageLicenseFile>LICENSE</PackageLicenseFile> | ||
<PackageReadmeFile>README.md</PackageReadmeFile> | ||
<PackageProjectUrl>https://github.com/DalSoft/DalSoft.Hosting.BackgroundQueue</PackageProjectUrl> | ||
<PackageIconUrl>http://dalsoft.co.uk/images/icon.png</PackageIconUrl> | ||
<RepositoryUrl>https://github.com/DalSoft/DalSoft.Hosting.BackgroundQueue</RepositoryUrl> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="2.0.0" /> | ||
<None Include="Assets\icon.png" Pack="true" PackagePath="\"/> | ||
<None Include="Assets\LICENSE" Pack="true" PackagePath="\"/> | ||
<None Include="../README.md" Pack="true" PackagePath="\"/> | ||
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute"> | ||
<_Parameter1>DalSoft.Hosting.BackgroundQueue.Test</_Parameter1> | ||
</AssemblyAttribute> | ||
</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
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,19 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace DalSoft.Hosting.BackgroundQueue | ||
{ | ||
public interface IBackgroundQueue | ||
{ | ||
int Count { get; } | ||
int ConcurrentCount { get; } | ||
int MaxConcurrentCount { get; } | ||
|
||
int MillisecondsToWaitBeforePickingUpTask { get; } | ||
|
||
Task Dequeue(CancellationToken cancellationToken); | ||
|
||
void Enqueue(Func<CancellationToken, Task> task); | ||
} | ||
} |
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