mirror of
https://github.com/AlexMacocian/MonoGame.Extended.git
synced 2026-07-21 17:59:31 +00:00
1f3f1c5aa0
* Replaced delegate with Action<T> * Change delegate to Action<T> * Add unit test to confirm issue * Fix NullReferenceException in ObjectPool when reusing all returned items The ObjectPool's Use() method was throwing a NullReferenceException when all items had been returned to the pool and New() was called again. This was due to _tailNode being null in this scenario. Added a null check for _tailNode in Use() method to properly reinitialize the linked list structure when the pool has been emptied and refilled.
52 lines
1.3 KiB
C#
52 lines
1.3 KiB
C#
// Copyright (c) Craftwork Games. All rights reserved.
|
|
// Licensed under the MIT license.
|
|
// See LICENSE file in the project root for full license information.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using MonoGame.Extended.Collections;
|
|
|
|
namespace MonoGame.Extended.Tests.Collections;
|
|
|
|
public class ObjectPoolTests
|
|
{
|
|
private class TestPoolable : IPoolable
|
|
{
|
|
public Action<IPoolable> ReturnAction { get; private set; }
|
|
public IPoolable NextNode { get; set; }
|
|
public IPoolable PreviousNode { get; set; }
|
|
|
|
public void Initialize(Action<IPoolable> returnAction)
|
|
{
|
|
ReturnAction = returnAction;
|
|
}
|
|
|
|
public void Return()
|
|
{
|
|
ReturnAction(this);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void ObjectPool_ThrowsNullReferenceException_WhenAllItemsReturnedAndNewCalled()
|
|
{
|
|
// Arrange
|
|
var pool = new ObjectPool<TestPoolable>(() => new TestPoolable(), 2);
|
|
|
|
// Act & Assert
|
|
var item1 = pool.New();
|
|
var item2 = pool.New();
|
|
|
|
// Return all items to the pool
|
|
item1.Return();
|
|
item2.Return();
|
|
|
|
|
|
var exception = Record.Exception(() => pool.New());
|
|
Assert.Null(exception);
|
|
}
|
|
}
|