Skip to content

Commit

Permalink
Fix: ObjectPool uses PoolSettings.IsPoolingEnabled (#400)
Browse files Browse the repository at this point in the history
  • Loading branch information
axunonb authored May 24, 2024
1 parent 6488209 commit 050fe28
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/SmartFormat.Tests/Core/FormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,4 +344,4 @@ public void Parallel_Smart_Format()
// Restore to saved value
ThreadSafeMode.SwitchTo(savedMode);
}
}
}
4 changes: 3 additions & 1 deletion src/SmartFormat.Tests/Pooling/ObjectPoolClassesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public void Should_Not_Exceed_Maximum_Pool_Size(object poolAsObj)
public void Dispose_Should_Clear_The_Pool(object poolAsObj)
{
const int shouldBeCreated = 5;

var pool = (ObjectPool<SomePoolObject>) poolAsObj;

for (var i = 1; i <= shouldBeCreated; i++)
Expand All @@ -255,6 +255,7 @@ public void Dispose_Should_Clear_The_Pool(object poolAsObj)
public void Disabled_Pooling_Should_Only_Return_New_Instances(object poolAsObj)
{
var pool = (ObjectPool<SomePoolObject>) poolAsObj;
var savedPoolingEnabled = pool.IsPoolingEnabled;
pool.IsPoolingEnabled = false;

var active = new List<SomePoolObject>();
Expand All @@ -267,6 +268,7 @@ public void Disabled_Pooling_Should_Only_Return_New_Instances(object poolAsObj)
{
pool.Return(a);
}
pool.IsPoolingEnabled = savedPoolingEnabled;

Assert.Multiple(() =>
{
Expand Down
4 changes: 2 additions & 2 deletions src/SmartFormat.Tests/Pooling/PoolBalanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void Pooling_Disabled_Leaves_Pools_Empty()

foreach (var p in pools)
{
Assert.That(p.Counters!.CountAll, Is.EqualTo(0), "CountAll");
Assert.That(p.Counters!.CountAll, Is.EqualTo(0), $"{p.Type?.Name}: CountAll");
}
}
}
}
2 changes: 1 addition & 1 deletion src/SmartFormat/Core/Formatting/FormattingInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public FormattingInfo Initialize (FormattingInfo? parent, FormatDetails formatDe
public void ReturnToPool()
{
Parent = null;
// Assign new value, but leave existing references untouched
// Assign new value, instance is returned to the pool elsewhere
FormatDetails = InitializationObject.FormatDetails;
Placeholder = null;
Selector = null;
Expand Down
8 changes: 6 additions & 2 deletions src/SmartFormat/Pooling/ObjectPools/ObjectPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ protected ObjectPool(PoolPolicy<T> poolPolicy)
/// Indicates whether object pooling is enabled (<see langword="true"/>).
/// <para>Default is taken from <see cref="PoolSettings.IsPoolingEnabled"/></para>
/// </summary>
public bool IsPoolingEnabled { get; internal set; } = PoolSettings.IsPoolingEnabled;
public bool IsPoolingEnabled
{
get => PoolSettings.IsPoolingEnabled;
internal set => PoolSettings.IsPoolingEnabled = value;
}

/// <summary>
/// The configuration of how an <see cref="IObjectPool{U}"/> works.
Expand Down Expand Up @@ -118,4 +122,4 @@ public void Dispose()
}

#endregion
}
}

0 comments on commit 050fe28

Please sign in to comment.