hadashiA / VContainer

The extra fast, minimum code size, GC-free DI (Dependency Injection) library running on Unity Game Engine.
https://vcontainer.hadashikick.jp
MIT License
2.02k stars 176 forks source link

A system registered using RegisterSystemIntoDefaultWorld remains even after going out of scope #705

Open Y-YoL opened 2 months ago

Y-YoL commented 2 months ago

I ran the code below in Play Mode test:

using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Unity.Entities;
using VContainer;
using VContainer.Unity;

public class NewTestScript
{
    private static IEnumerable<World> Worlds
    {
        get
        {
            foreach (var world in World.All)
            {
                yield return world;
            }
        }
    }

    public static IEnumerable<TestCaseData> TestCases => new[]
    {
        new TestCaseData((Action<IContainerBuilder>)(builder =>
        {
            builder.RegisterNewWorld("test", Lifetime.Scoped);
            builder.RegisterSystemIntoWorld<TestSystem>("test");
            builder.Register<Test1>(Lifetime.Transient);
        })).SetName("New World"),
        new TestCaseData((Action<IContainerBuilder>)(builder =>
        {
            builder.RegisterSystemIntoDefaultWorld<TestSystem>();
            builder.Register<Test1>(Lifetime.Transient);
        })).SetName("Default World"),
    };

    [Test]
    [TestCaseSource(nameof(TestCases))]
    public void EntitySystemLifetime(Action<IContainerBuilder> configuration)
    {
        using (var scope = LifetimeScope.Create(configuration))
        {
            Assert.NotNull(scope.Container.Resolve(typeof(Test1)));

            Assert.IsFalse(Worlds.All(world => world.GetExistingSystem<TestSystem>() == SystemHandle.Null), "generated");
        }

        // After exiting LifetimeScope, it is assumed that the items registered in the container will be deleted.
        Assert.IsTrue(Worlds.All(world => world.GetExistingSystem<TestSystem>() == SystemHandle.Null), "LifetimeScope exited.");
    }
}

[DisableAutoCreation]
public partial class TestSystem : SystemBase
{
    protected override void OnUpdate()
    {
    }
}

public class Test1
{
    [Inject]
    public Test1(TestSystem testSystem) { }
}

image

The lifespan of a System appears to be tied to the World, not the container.

Versions