Beispiel #1
0
    public void DoesNotReturnOnDestroyWhenFalse()
    {
        var pool = new ElementPool <Element>(returnsWhenDestroyed: false);

        var element1 = pool.GetOrCreateElement(() => new Element());

        element1.Destroy();

        var element2 = pool.GetOrCreateElement(() => new Element());

        element2.Should().NotBe(element1);
    }
Beispiel #2
0
    public void ReturnsOnDestroyWhenTrue()
    {
        var pool = new ElementPool <Element>(returnsWhenDestroyed: true);

        var element1 = pool.GetOrCreateElement(() => new Element());

        element1.Destroy();

        var element2 = pool.GetOrCreateElement(() => new Element());

        element2.Should().Be(element1);
    }
Beispiel #3
0
    public void GetOrCreate_DoesNotCallCleanWhenEmpty()
    {
        var pool = new ElementPool <Element>();

        bool isCalled = false;

        pool.GetOrCreateElement(() => new Element(), element => isCalled = true);

        isCalled.Should().BeFalse();
    }
Beispiel #4
0
    public void GetOrCreate_CallsCleanWhenReturningDirtyElement()
    {
        var pool = new ElementPool <Element>();

        pool.ReturnElement(new Element());

        bool isCalled = false;

        pool.GetOrCreateElement(() => new Element(), element => isCalled = true);

        isCalled.Should().BeTrue();
    }
Beispiel #5
0
    public void GetOrCreate_CreatesWhenEmpty()
    {
        var pool = new ElementPool <Element>();

        bool isCalled = false;

        pool.GetOrCreateElement(() =>
        {
            isCalled = true;
            return(new Element());
        });

        isCalled.Should().BeTrue();
    }
Beispiel #6
0
    public void GetOrCreate_DoesNotCreateWhenContainingElement()
    {
        var pool = new ElementPool <Element>();

        pool.ReturnElement(new Element());

        bool isCalled = false;

        pool.GetOrCreateElement(() =>
        {
            isCalled = true;
            return(new Element());
        });

        isCalled.Should().BeFalse();
    }
Beispiel #7
0
    public void StoresMaximumAmountOfPooledElements()
    {
        var pool = new ElementPool <Element>(5);

        for (int i = 0; i < 10; i++)
        {
            pool.ReturnElement(new Element());
        }

        var callCount = 0;

        for (int i = 0; i < 10; i++)
        {
            pool.GetOrCreateElement(() =>
            {
                callCount++;
                return(new Element());
            });
        }

        callCount.Should().Be(5);
    }