Skip to content

Commit ee2980e

Browse files
committed
Feat: Add ObjectPool extension library and tests
1 parent 9dec639 commit ee2980e

File tree

9 files changed

+206
-1
lines changed

9 files changed

+206
-1
lines changed

CodeOfChaos.Extensions.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeOfChaos.Extensions.Flue
4848
EndProject
4949
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.CodeOfChaos.Extensions.FluentValidation", "tests\Tests.CodeOfChaos.Extensions.FluentValidation\Tests.CodeOfChaos.Extensions.FluentValidation.csproj", "{0FFD99C9-3768-4E39-924D-D6847ABEC082}"
5050
EndProject
51+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeOfChaos.Extensions.ObjectPool", "src\CodeOfChaos.Extensions.ObjectPool\CodeOfChaos.Extensions.ObjectPool.csproj", "{090B05B4-FCE4-419C-8421-2D3CC896166E}"
52+
EndProject
53+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.CodeOfChaos.Extensions.ObjectPool", "tests\Tests.CodeOfChaos.Extensions.ObjectPool\Tests.CodeOfChaos.Extensions.ObjectPool.csproj", "{6BC1F7E4-353E-4A45-8343-C93097636B5F}"
54+
EndProject
5155
Global
5256
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5357
Debug|Any CPU = Debug|Any CPU
@@ -130,6 +134,14 @@ Global
130134
{0FFD99C9-3768-4E39-924D-D6847ABEC082}.Debug|Any CPU.Build.0 = Debug|Any CPU
131135
{0FFD99C9-3768-4E39-924D-D6847ABEC082}.Release|Any CPU.ActiveCfg = Release|Any CPU
132136
{0FFD99C9-3768-4E39-924D-D6847ABEC082}.Release|Any CPU.Build.0 = Release|Any CPU
137+
{090B05B4-FCE4-419C-8421-2D3CC896166E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
138+
{090B05B4-FCE4-419C-8421-2D3CC896166E}.Debug|Any CPU.Build.0 = Debug|Any CPU
139+
{090B05B4-FCE4-419C-8421-2D3CC896166E}.Release|Any CPU.ActiveCfg = Release|Any CPU
140+
{090B05B4-FCE4-419C-8421-2D3CC896166E}.Release|Any CPU.Build.0 = Release|Any CPU
141+
{6BC1F7E4-353E-4A45-8343-C93097636B5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
142+
{6BC1F7E4-353E-4A45-8343-C93097636B5F}.Debug|Any CPU.Build.0 = Debug|Any CPU
143+
{6BC1F7E4-353E-4A45-8343-C93097636B5F}.Release|Any CPU.ActiveCfg = Release|Any CPU
144+
{6BC1F7E4-353E-4A45-8343-C93097636B5F}.Release|Any CPU.Build.0 = Release|Any CPU
133145
EndGlobalSection
134146
GlobalSection(NestedProjects) = preSolution
135147
{26284571-0E09-4BAF-8C2B-DF87DCC1BA0B} = {8DD280D4-1E14-4D5E-AFE6-58DD8F079DCC}
@@ -152,5 +164,7 @@ Global
152164
{76717F23-6ED9-4C3B-81AD-89C79B7D36BE} = {197E72AD-DEAB-4350-AFC3-A3BB38720BF5}
153165
{6A293FAD-9E0C-4747-B2DE-AE6504E4CB4F} = {197E72AD-DEAB-4350-AFC3-A3BB38720BF5}
154166
{0FFD99C9-3768-4E39-924D-D6847ABEC082} = {8DD280D4-1E14-4D5E-AFE6-58DD8F079DCC}
167+
{090B05B4-FCE4-419C-8421-2D3CC896166E} = {197E72AD-DEAB-4350-AFC3-A3BB38720BF5}
168+
{6BC1F7E4-353E-4A45-8343-C93097636B5F} = {8DD280D4-1E14-4D5E-AFE6-58DD8F079DCC}
155169
EndGlobalSection
156170
EndGlobal
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<!-- Main package name -->
9+
<PackageId>CodeOfChaos.Extensions.ObjectPool</PackageId>
10+
<Version>0.60.0</Version>
11+
<Authors>Anna Sas</Authors>
12+
<Description>A Library of extensions for Microsoft.Extensions.ObjectPool</Description>
13+
<PackageProjectUrl>https://github.com/code-of-chaos/cs-code_of_chaos-extensions</PackageProjectUrl>
14+
<PackageTags>extensions objectpool</PackageTags>
15+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
16+
<PublishRepositoryUrl>true</PublishRepositoryUrl>
17+
<IncludeSymbols>true</IncludeSymbols>
18+
<DebugType>embedded</DebugType>
19+
<PackageLicenseFile>LICENSE</PackageLicenseFile>
20+
<PackageReadmeFile>README.md</PackageReadmeFile>
21+
<PackageIcon>icon.png</PackageIcon>
22+
</PropertyGroup>
23+
24+
<ItemGroup>
25+
<None Include="..\..\LICENSE" Pack="true" PackagePath="" Visible="false" />
26+
<None Include="README.md" Pack="true" PackagePath="" />
27+
<None Include="../../assets/icon.png" Pack="true" PackagePath="" Visible="false" />
28+
</ItemGroup>
29+
30+
<ItemGroup>
31+
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="9.0.4" />
32+
</ItemGroup>
33+
34+
</Project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using Microsoft.Extensions.ObjectPool;
5+
6+
namespace CodeOfChaos.Extensions.ObjectPool;
7+
// ---------------------------------------------------------------------------------------------------------------------
8+
// Code
9+
// ---------------------------------------------------------------------------------------------------------------------
10+
public class CollectionPoolPolicy<T, TItem> : IPooledObjectPolicy<T> where T : ICollection<TItem>, new() {
11+
public T Create() {
12+
return new T();
13+
}
14+
public bool Return(T obj) {
15+
obj.Clear();
16+
return obj.Count == 0;
17+
}
18+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# CodeOfChaos.Extensions.ObjectPool
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using Microsoft.Extensions.ObjectPool;
5+
6+
namespace CodeOfChaos.Extensions.ObjectPool;
7+
// ---------------------------------------------------------------------------------------------------------------------
8+
// Code
9+
// ---------------------------------------------------------------------------------------------------------------------
10+
public class ResettablePoolPolicy<T> : IPooledObjectPolicy<T> where T : IResettable, new() {
11+
public T Create() => new();
12+
public bool Return(T obj) => obj.TryReset();
13+
}

src/Tools.CodeOfChaos.Extensions/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public static async Task Main(string[] args) {
3030
"CodeOfChaos.Extensions.MicrosoftLogging",
3131
"CodeOfChaos.Extensions.DependencyInjection",
3232
"CodeOfChaos.Extensions.DependencyInjection.Generators",
33-
"CodeOfChaos.Extensions.FluentValidation"
33+
"CodeOfChaos.Extensions.FluentValidation",
34+
"CodeOfChaos.Extensions.ObjectPool"
3435
);
3536

3637
string oneLineArgs = InputHelper.ToOneLine(args).Replace("%PROJECTS%", projects);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using CodeOfChaos.Extensions.ObjectPool;
5+
6+
namespace Tests.CodeOfChaos.Extensions.ObjectPool;
7+
8+
// ---------------------------------------------------------------------------------------------------------------------
9+
// Code
10+
// ---------------------------------------------------------------------------------------------------------------------
11+
public class CollectionPoolPolicyTests {
12+
private readonly CollectionPoolPolicy<List<int>, int> _policy = new();
13+
14+
[Test]
15+
public async Task Create_ShouldReturnNewEmptyCollection() {
16+
// Arrange
17+
18+
// Act
19+
List<int> result = _policy.Create();
20+
21+
// Assert
22+
await Assert.That(result).IsNotNull()
23+
.And.IsEmpty();
24+
}
25+
26+
[Test]
27+
public async Task Reset_ShouldClearCollection() {
28+
// Arrange
29+
List<int> collection = [1, 2, 3];
30+
31+
// Act
32+
bool result = _policy.Return(collection);
33+
34+
// Assert
35+
await Assert.That(result).IsTrue();
36+
await Assert.That(collection).IsEmpty();
37+
}
38+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using CodeOfChaos.Extensions.ObjectPool;
5+
using Microsoft.Extensions.ObjectPool;
6+
7+
namespace Tests.CodeOfChaos.Extensions.ObjectPool;
8+
// ---------------------------------------------------------------------------------------------------------------------
9+
// Code
10+
// ---------------------------------------------------------------------------------------------------------------------
11+
public class ResettablePoolPolicyTests {
12+
public class TestResettable : IResettable {
13+
public bool ResetSuccessful { get; set; } = true;
14+
public int ResetCallCount { get; private set; }
15+
16+
public bool TryReset() {
17+
if (!ResetSuccessful) return ResetSuccessful;
18+
ResetCallCount++;
19+
return ResetSuccessful;
20+
}
21+
}
22+
23+
private readonly ResettablePoolPolicy<TestResettable> _policy = new();
24+
25+
[Test]
26+
public async Task Reset_ShouldCallReset() {
27+
// Arrange
28+
var resettable = new TestResettable();
29+
30+
// Act
31+
bool result = _policy.Return(resettable);
32+
33+
// Assert
34+
await Assert.That(result).IsTrue();
35+
await Assert.That(resettable.ResetCallCount).IsEqualTo(1);
36+
}
37+
38+
[Test]
39+
public async Task Reset_ShouldReturnFalse_IfResetFails() {
40+
// Arrange
41+
var resettable = new TestResettable { ResetSuccessful = false };
42+
43+
// Act
44+
bool result = _policy.Return(resettable);
45+
46+
// Assert
47+
await Assert.That(result).IsFalse();
48+
await Assert.That(resettable.ResetCallCount).IsEqualTo(0);
49+
}
50+
51+
[Test]
52+
public async Task Create_ShouldReturnNewInstance() {
53+
// Arrange
54+
55+
// Act
56+
TestResettable result = _policy.Create();
57+
58+
// Assert
59+
await Assert.That(result).IsNotNull();
60+
await Assert.That(result.ResetCallCount).IsEqualTo(0);
61+
}
62+
63+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
<IsTestProject>true</IsTestProject>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0"/>
14+
<PackageReference Include="Moq" Version="4.20.72"/>
15+
<PackageReference Include="TUnit" Version="0.19.86" />
16+
<PackageReference Include="Bogus" Version="35.6.3" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<ProjectReference Include="..\..\src\CodeOfChaos.Extensions.ObjectPool\CodeOfChaos.Extensions.ObjectPool.csproj" />
21+
</ItemGroup>
22+
23+
</Project>

0 commit comments

Comments
 (0)