Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Y-YoL opened this issue Sep 5, 2024 · 0 comments

Comments

@Y-YoL
Copy link

Y-YoL commented Sep 5, 2024

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

  • Unity 2022.3.26f1
  • VContainer 1.16.2
  • Entities 1.2.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant