ObjectPoolTests.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright (c) Craftwork Games. All rights reserved.
  2. // Licensed under the MIT license.
  3. // See LICENSE file in the project root for full license information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using MonoGame.Extended.Collections;
  10. namespace MonoGame.Extended.Tests.Collections;
  11. public class ObjectPoolTests
  12. {
  13. private class TestPoolable : IPoolable
  14. {
  15. public Action<IPoolable> ReturnAction { get; private set; }
  16. public IPoolable NextNode { get; set; }
  17. public IPoolable PreviousNode { get; set; }
  18. public void Initialize(Action<IPoolable> returnAction)
  19. {
  20. ReturnAction = returnAction;
  21. }
  22. public void Return()
  23. {
  24. ReturnAction(this);
  25. }
  26. }
  27. [Fact]
  28. public void ObjectPool_ThrowsNullReferenceException_WhenAllItemsReturnedAndNewCalled()
  29. {
  30. // Arrange
  31. var pool = new ObjectPool<TestPoolable>(() => new TestPoolable(), 2);
  32. // Act & Assert
  33. var item1 = pool.New();
  34. var item2 = pool.New();
  35. // Return all items to the pool
  36. item1.Return();
  37. item2.Return();
  38. var exception = Record.Exception(() => pool.New());
  39. Assert.Null(exception);
  40. }
  41. }