Files
RobustToolbox/Robust.Shared.IntegrationTests/GameObjects/EntitySystemManagerOrderTest.cs
DrSmugleaf fe1648d290 Make EntitySystemManager.DependencyCollection inject EntityQuery, make BUIs inject systems and entity queries (#6394)
* Make EntitySystemManager.DependencyCollection inject EntityQuery

* Make BUIs inject systems and entity queries

* Fix import

* We parallelize those

* RIDER I BEG YOU

* Mocked unit tests are my passion

* Perhaps we do not care about fractional milliseconds

* Forgor to make it debug only

* Use Parallel.For instead of ForEach

* Rider I am going to become the joker

* Fix EntMan resolve

* Now with lazy resolve technology

* Use GetOrAdd
2026-02-05 21:35:52 +01:00

143 lines
5.1 KiB
C#

using Moq;
using NUnit.Framework;
using Robust.Server.Configuration;
using Robust.Shared.Configuration;
using Robust.Shared.ContentPack;
using Robust.Shared.Exceptions;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Network;
using Robust.Shared.Profiling;
using Robust.Shared.Reflection;
using Robust.Shared.Replays;
using Robust.Shared.Timing;
namespace Robust.UnitTesting.Shared.GameObjects
{
[TestFixture]
[TestOf(typeof(EntitySystemManager))]
internal sealed class EntitySystemManagerOrderTest
{
private sealed class Counter
{
public int X;
}
[Reflect(false)]
private abstract class TestSystemBase : IEntitySystem
{
public Counter? Counter;
public int LastUpdate;
public virtual IEnumerable<Type> UpdatesAfter => Enumerable.Empty<Type>();
public virtual IEnumerable<Type> UpdatesBefore => Enumerable.Empty<Type>();
public bool UpdatesOutsidePrediction => true;
public void Initialize() { }
public void Shutdown() { }
public void Update(float frameTime)
{
LastUpdate = Counter!.X++;
}
public void FrameUpdate(float frameTime) { }
}
// Expected update order is is A -> D -> C -> B
[Reflect(false)]
private sealed class TestSystemA : TestSystemBase
{
}
[Reflect(false)]
private sealed class TestSystemB : TestSystemBase
{
public override IEnumerable<Type> UpdatesAfter => new[] {typeof(TestSystemA)};
}
[Reflect(false)]
private sealed class TestSystemC : TestSystemBase
{
public override IEnumerable<Type> UpdatesBefore => new[] {typeof(TestSystemB)};
}
[Reflect(false)]
private sealed class TestSystemD : TestSystemBase
{
public override IEnumerable<Type> UpdatesAfter => new[] {typeof(TestSystemA)};
public override IEnumerable<Type> UpdatesBefore => new[] {typeof(TestSystemC)};
}
[Test]
public void Test()
{
var deps = new DependencyCollection();
deps.Register<IRuntimeLog, RuntimeLog>();
deps.Register<ILogManager, LogManager>();
deps.Register<IGameTiming, GameTiming>();
deps.RegisterInstance<INetManager>(new Mock<INetManager>().Object);
deps.Register<IConfigurationManager, ServerNetConfigurationManager>();
deps.Register<IServerNetConfigurationManager, ServerNetConfigurationManager>();
deps.Register<ProfManager, ProfManager>();
deps.Register<IDynamicTypeFactory, DynamicTypeFactory>();
deps.Register<IDynamicTypeFactoryInternal, DynamicTypeFactory>();
deps.RegisterInstance<IModLoader>(new Mock<IModLoader>().Object);
deps.Register<IEntitySystemManager, EntitySystemManager>();
// WHEN WILL THE SUFFERING END
deps.RegisterInstance<IReplayRecordingManager>(new Mock<IReplayRecordingManager>().Object);
var reflectionMock = new Mock<IReflectionManager>();
reflectionMock.Setup(p => p.GetAllChildren<IEntitySystem>(false))
.Returns(new[]
{
typeof(TestSystemA),
typeof(TestSystemB),
typeof(TestSystemC),
typeof(TestSystemD),
});
deps.RegisterInstance<IReflectionManager>(reflectionMock.Object);
// Never
var componentFactoryMock = new Mock<IComponentFactory>();
componentFactoryMock.Setup(p => p.AllRegisteredTypes).Returns(Enumerable.Empty<Type>());
deps.RegisterInstance<IComponentFactory>(componentFactoryMock.Object);
var entityManagerMock = new Mock<IEntityManager>();
entityManagerMock.Setup(p => p.ComponentFactory).Returns(componentFactoryMock.Object);
deps.RegisterInstance<IEntityManager>(entityManagerMock.Object);
deps.BuildGraph();
IoCManager.InitThread(deps, true);
var systems = deps.Resolve<IEntitySystemManager>();
systems.Initialize();
var counter = new Counter();
systems.GetEntitySystem<TestSystemA>().Counter = counter;
systems.GetEntitySystem<TestSystemB>().Counter = counter;
systems.GetEntitySystem<TestSystemC>().Counter = counter;
systems.GetEntitySystem<TestSystemD>().Counter = counter;
systems.TickUpdate(1, noPredictions: false);
Assert.That(counter.X, Is.EqualTo(4));
Assert.That(systems.GetEntitySystem<TestSystemA>().LastUpdate, Is.EqualTo(0));
Assert.That(systems.GetEntitySystem<TestSystemB>().LastUpdate, Is.EqualTo(3));
Assert.That(systems.GetEntitySystem<TestSystemC>().LastUpdate, Is.EqualTo(2));
Assert.That(systems.GetEntitySystem<TestSystemD>().LastUpdate, Is.EqualTo(1));
}
[TearDown]
public void TearDown()
{
IoCManager.Clear();
}
}
}