mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-09-16 15:22:27 +02:00
* Isolate net messages in integration tests. Integration tests don't use Lidgren to connect client and send, instead they just use some in-process channels to communicate. Because of this, the original implementation of net messages *directly* passed the net message instances between client and server instances. This caused issues whenever content would mutate data in a NetMessage after it "passed through". Now we run the messages through WriteToBuffer() and ReadFromBuffer() so they pass through binary serialization. This means there's no more implicit sharing of the objects. Note that this requires some trickery: Lidgren's message types have internal constructors. Really ideally we'd change the engine to make this more testable... but that's a content breaking change. Instead I just added InternalsVisibleTo to Lidgren so we can mess with it. We maintain the library ourselves anyways I can do what I want. Fixes #4836 * Register Robust.UnitTesting as assembly for reflection. This is necessary so that serialized types in the assembly can be picked up by NetSerializer. Have to disable automatic reflection on all entity systems/components that tests register manually right now, because otherwise tests break. * Stop shallow cloning specific net messages in integration tests. This isn't necessary anymore now that we have a thorough fix. * Wow I really forgot to copy-paste that line to the other side huh. * Add test that serializer hash matches. * Another test one I missed earlier. * Changelog
77 lines
2.6 KiB
C#
77 lines
2.6 KiB
C#
using System.Collections.Generic;
|
|
using NUnit.Framework;
|
|
using Robust.Shared.GameObjects;
|
|
using Robust.Shared.Map;
|
|
using Robust.Shared.Reflection;
|
|
using Robust.UnitTesting.Server;
|
|
|
|
namespace Robust.UnitTesting.Shared.GameObjects;
|
|
|
|
public sealed partial class EntityEventBusTests
|
|
{
|
|
// Explanation of what bug this is testing:
|
|
// Because event ordering is keyed on system type, we have a problem.
|
|
// If you register to a directed event like FooEvent twice for different components,
|
|
// you now have different subscriptions with the same key.
|
|
//
|
|
// To trigger this, at least one subscription to this event (possibly another system entirely)
|
|
// needs to demand some ordering calculation to happen.
|
|
|
|
[Test]
|
|
public void TestDifferentComponentsOrderedSameKeySub()
|
|
{
|
|
var simulation = RobustServerSimulation
|
|
.NewSimulation()
|
|
.RegisterEntitySystems(factory =>
|
|
{
|
|
factory.LoadExtraSystemType<DifferentComponentsSameKeySubSystem>();
|
|
factory.LoadExtraSystemType<DifferentComponentsSameKeySubSystem2>();
|
|
})
|
|
.RegisterComponents(factory => factory.RegisterClass<FooComponent>())
|
|
.InitializeInstance();
|
|
|
|
var map = new MapId(1);
|
|
simulation.AddMap(map);
|
|
|
|
var entity = simulation.SpawnEntity(null, new MapCoordinates(0, 0, map));
|
|
simulation.Resolve<IEntityManager>().AddComponent<FooComponent>(entity);
|
|
|
|
var foo = new FooEvent();
|
|
simulation.Resolve<IEntityManager>().EventBus.RaiseLocalEvent(entity, foo, true);
|
|
|
|
Assert.That(foo.EventOrder, Is.EquivalentTo(new[]{"Foo", "Transform", "Metadata"}).Or.EquivalentTo(new[]{"Foo", "Metadata", "Transform"}));
|
|
}
|
|
|
|
[Reflect(false)]
|
|
private sealed class DifferentComponentsSameKeySubSystem : EntitySystem
|
|
{
|
|
public override void Initialize()
|
|
{
|
|
SubscribeLocalEvent<TransformComponent, FooEvent>((_, _, e) => { e.EventOrder.Add("Transform"); });
|
|
SubscribeLocalEvent<MetaDataComponent, FooEvent>((_, _, e) => { e.EventOrder.Add("Metadata"); });
|
|
}
|
|
}
|
|
|
|
[Reflect(false)]
|
|
private sealed class DifferentComponentsSameKeySubSystem2 : EntitySystem
|
|
{
|
|
public override void Initialize()
|
|
{
|
|
SubscribeLocalEvent<FooComponent, FooEvent>(
|
|
(_, _, e) => e.EventOrder.Add("Foo"),
|
|
before: new[] {typeof(DifferentComponentsSameKeySubSystem)});
|
|
}
|
|
}
|
|
|
|
[Reflect(false)]
|
|
private sealed partial class FooComponent : Component
|
|
{
|
|
|
|
}
|
|
|
|
private sealed class FooEvent
|
|
{
|
|
public List<string> EventOrder = new();
|
|
}
|
|
}
|