-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce ObjectPool and use it for ResilienceContext pooling (#1111)
- Loading branch information
Showing
26 changed files
with
235 additions
and
63 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
2 changes: 1 addition & 1 deletion
2
...olly.Core.Tests/Utils/FakeTimeProvider.cs → ...ly.Core.Tests/Helpers/FakeTimeProvider.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
2 changes: 1 addition & 1 deletion
2
src/Polly.Core.Tests/Utils/TestArguments.cs → ...Polly.Core.Tests/Helpers/TestArguments.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
2 changes: 1 addition & 1 deletion
2
...ore.Tests/Utils/TestResilienceStrategy.cs → ...e.Tests/Helpers/TestResilienceStrategy.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
2 changes: 1 addition & 1 deletion
2
src/Polly.Core.Tests/Utils/TestUtils.cs → src/Polly.Core.Tests/Helpers/TestUtils.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
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
using System; | ||
using Polly.Retry; | ||
|
||
namespace Polly.Core.Tests.Retry; | ||
|
1 change: 0 additions & 1 deletion
1
src/Polly.Core.Tests/Retry/RetryResilienceStrategyBuilderExtensionsTests.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Polly.Strategy; | ||
|
||
namespace Polly.Core.Tests.Strategy; | ||
|
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,5 +1,3 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Polly.Strategy; | ||
|
||
namespace Polly.Core.Tests.Strategy; | ||
|
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,5 +1,3 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Polly.Strategy; | ||
|
||
namespace Polly.Core.Tests.Strategy; | ||
|
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,4 +1,3 @@ | ||
using System; | ||
using Polly.Strategy; | ||
|
||
namespace Polly.Core.Tests.Strategy; | ||
|
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using Polly.Timeout; | ||
|
||
namespace Polly.Core.Tests.Timeout; | ||
|
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,111 @@ | ||
using Polly.Utils; | ||
|
||
namespace Polly.Core.Tests.Utils; | ||
|
||
public class ObjectPoolTests | ||
{ | ||
[Fact] | ||
public void GetAnd_ReturnObject_SameInstance() | ||
{ | ||
// Arrange | ||
var pool = new ObjectPool<object>(() => new object(), _ => true); | ||
|
||
var obj1 = pool.Get(); | ||
pool.Return(obj1); | ||
|
||
// Act | ||
var obj2 = pool.Get(); | ||
|
||
// Assert | ||
Assert.Same(obj1, obj2); | ||
} | ||
|
||
[Fact] | ||
public void MaxCapacity_Ok() | ||
{ | ||
ObjectPool<object>.MaxCapacity.Should().Be((Environment.ProcessorCount * 2) - 1); | ||
} | ||
|
||
[Fact] | ||
public void MaxCapacity_Respected() | ||
{ | ||
// Arrange | ||
var pool = new ObjectPool<object>(() => new object(), _ => true); | ||
var items1 = GetStoreReturn(pool); | ||
|
||
// Act | ||
var items2 = GetStoreReturn(pool); | ||
|
||
// Assert | ||
items1.Should().BeEquivalentTo(items2); | ||
} | ||
|
||
[Fact] | ||
public void MaxCapacityOverflow_Respected() | ||
{ | ||
// Arrange | ||
var count = ObjectPool<object>.MaxCapacity + 10; | ||
var pool = new ObjectPool<object>(() => new object(), _ => true); | ||
var items1 = GetStoreReturn(pool, count); | ||
|
||
// Act | ||
var items2 = GetStoreReturn(pool, count); | ||
|
||
// Assert | ||
items1.Last().Should().NotBeSameAs(items2.Last()); | ||
} | ||
|
||
[Fact] | ||
public void CreatedByPolicy() | ||
{ | ||
// Arrange | ||
var policy = new ListPolicy(); | ||
var pool = new ObjectPool<List<int>>(ListPolicy.Create, ListPolicy.Return); | ||
|
||
// Act | ||
var list = pool.Get(); | ||
|
||
// Assert | ||
Assert.Equal(17, list.Capacity); | ||
} | ||
|
||
[Fact] | ||
public void Return_RejectedByPolicy() | ||
{ | ||
// Arrange | ||
var policy = new ListPolicy(); | ||
var pool = new ObjectPool<List<int>>(ListPolicy.Create, ListPolicy.Return); | ||
var list1 = pool.Get(); | ||
list1.Capacity = 20; | ||
|
||
// Act | ||
pool.Return(list1); | ||
var list2 = pool.Get(); | ||
|
||
// Assert | ||
Assert.NotSame(list1, list2); | ||
} | ||
|
||
private static List<object> GetStoreReturn(ObjectPool<object> pool, int? count = null) | ||
{ | ||
var items = new List<object>(); | ||
for (int i = 0; i < (count ?? ObjectPool<object>.MaxCapacity); i++) | ||
{ | ||
items.Add(pool.Get()); | ||
} | ||
|
||
foreach (var item in items) | ||
{ | ||
pool.Return(item); | ||
} | ||
|
||
return items; | ||
} | ||
|
||
private class ListPolicy | ||
{ | ||
public static List<int> Create() => new(17); | ||
|
||
public static bool Return(List<int> obj) => obj.Capacity == 17; | ||
} | ||
} |
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,4 +1,3 @@ | ||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace Polly.Core.Tests.Utils; | ||
|
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
Oops, something went wrong.