Files
RobustToolbox/Robust.UnitTesting/Shared/GameObjects/EntityEventBusTests.RefBroadcastEvents.cs
T
e5013d9e3f Add support for struct directed events, add by-ref directed/broadcast events. (#1931)
* Add support for struct directed events.
- Turn transform events into readonly structs

* TransformComponent events have readonly fields.

* Reduce boxing allocations, add DirectedRegistration readonly struct.

* Use typeof in a few places instead of GetType

* Add overloads to allow passing directed events by ref.

* Raise transform events by ref.

* Fix occluder AnchorStateChangedEvent subscription

* Fix broadphase subscriptions

* Fix bug oops

* Fix transform system raising moveevents by value

* Don't reflect the test entity systems.

* Directed EventBus uses ref everywhere, internally
Deduplicates a bunch of code. Whoo!

* Add broadcast by-ref events

* Fix by-ref events bug using Unsafe, add new tests for sorted by-ref events.

* Port broadcast events to by-ref.

* Speed, more correctness, reduce generics bloat.

* Clean up subscription code some.

* Remove bad test.

Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
2021-08-21 11:37:02 +02:00

148 lines
4.8 KiB
C#

using System;
using NUnit.Framework;
using Robust.Shared.GameObjects;
using Robust.Shared.Reflection;
using Robust.UnitTesting.Server;
namespace Robust.UnitTesting.Shared.GameObjects
{
[TestFixture]
public partial class EntityEventBusTests
{
[Test]
public void SubscribeCompRefBroadcastEvent()
{
// Arrange.
var simulation = RobustServerSimulation
.NewSimulation()
.RegisterEntitySystems(factory => factory.LoadExtraSystemType<SubscribeCompRefBroadcastSystem>())
.InitializeInstance();
var ev = new TestStructEvent() {TestNumber = 5};
simulation.Resolve<IEntityManager>().EventBus.RaiseEvent(EventSource.Local, ref ev);
Assert.That(ev.TestNumber, Is.EqualTo(15));
}
[Reflect(false)]
public class SubscribeCompRefBroadcastSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<TestStructEvent>(OnTestEvent);
}
private void OnTestEvent(ref TestStructEvent ev)
{
Assert.That(ev.TestNumber, Is.EqualTo(5));
ev.TestNumber += 10;
}
}
[Test]
public void SubscriptionNoMixedRefValueBroadcastEvent()
{
// Arrange.
var simulation = RobustServerSimulation
.NewSimulation()
.RegisterEntitySystems(factory =>
factory.LoadExtraSystemType<SubscriptionNoMixedRefValueBroadcastEventSystem>());
// Act. No mixed ref and value subscriptions are allowed.
Assert.Throws(typeof(InvalidOperationException), () => simulation.InitializeInstance());
}
[Reflect(false)]
private class SubscriptionNoMixedRefValueBroadcastEventSystem : EntitySystem
{
public override void Initialize()
{
// The below is not allowed, as you're subscribing by-ref and by-value to the same event...
SubscribeLocalEvent<TestStructEvent>(MyRefHandler);
SubscribeLocalEvent<TestStructEvent>(MyValueHandler);
}
private void MyValueHandler(TestStructEvent args) { }
private void MyRefHandler(ref TestStructEvent args) { }
}
[Test]
public void SortedBroadcastRefEvents()
{
// Arrange.
var simulation = RobustServerSimulation
.NewSimulation()
.RegisterEntitySystems(factory =>
{
factory.LoadExtraSystemType<BroadcastOrderASystem>();
factory.LoadExtraSystemType<BroadcastOrderBSystem>();
factory.LoadExtraSystemType<BroadcastOrderCSystem>();
})
.InitializeInstance();
// Act.
var testEvent = new TestStructEvent {TestNumber = 5};
var eventBus = simulation.Resolve<IEntityManager>().EventBus;
eventBus.RaiseEvent(EventSource.Local, ref testEvent);
// Check that the entity systems changed the value correctly
Assert.That(testEvent.TestNumber, Is.EqualTo(15));
}
[Reflect(false)]
private class BroadcastOrderASystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<TestStructEvent>(OnA, new[]{typeof(BroadcastOrderBSystem)}, new[]{typeof(BroadcastOrderCSystem)});
}
private void OnA(ref TestStructEvent args)
{
// Second handler being ran.
Assert.That(args.TestNumber, Is.EqualTo(0));
args.TestNumber = 10;
}
}
[Reflect(false)]
private class BroadcastOrderBSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<TestStructEvent>(OnB, null, new []{typeof(BroadcastOrderASystem)});
}
private void OnB(ref TestStructEvent args)
{
// Last handler being ran.
Assert.That(args.TestNumber, Is.EqualTo(10));
args.TestNumber = 15;
}
}
[Reflect(false)]
private class BroadcastOrderCSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<TestStructEvent>(OnC);
}
private void OnC(ref TestStructEvent args)
{
// First handler being ran.
Assert.That(args.TestNumber, Is.EqualTo(5));
args.TestNumber = 0;
}
}
}
}