fix(Pool): Return now checks for null entries to avoid nulls creeping into the pool

This commit is contained in:
mischa 2024-04-17 12:33:03 +08:00 committed by MrGadget
parent 5f0c2467fb
commit e01b618dcb
2 changed files with 18 additions and 1 deletions

View File

@ -32,7 +32,15 @@ public Pool(Func<T> objectGenerator, int initialCapacity)
// return an element to the pool
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Return(T item) => objects.Push(item);
public void Return(T item)
{
// make sure we can't accidentally insert null values into the pool.
// debugging this would be hard since it would only show on get().
if (item == null)
throw new ArgumentNullException(nameof(item));
objects.Push(item);
}
// count to see how many objects are in the pool. useful for tests.
public int Count => objects.Count;

View File

@ -34,6 +34,15 @@ public void ReturnAndTake()
Assert.That(pool.Get(), Is.EqualTo("returned"));
}
[Test]
public void ReturnNull()
{
// make sure we can't accidentally insert null values into the pool.
// debugging this would be hard since it would only show on get().
Assert.That(() => pool.Return(null), Throws.ArgumentNullException);
Assert.That(pool.Count, Is.EqualTo(0));
}
[Test]
public void Count()
{