This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
using NUnit.Framework;
|
||||
using Unity.Burst;
|
||||
using Unity.Collections;
|
||||
using Unity.Jobs;
|
||||
|
||||
namespace ShrinkEventBus.Entities.Tests
|
||||
{
|
||||
internal struct EcsPingEvent : IShrinkEvent
|
||||
{
|
||||
public int Value;
|
||||
}
|
||||
|
||||
internal sealed class EcsPingSubscriber : IShrinkGeneratedSubscriber
|
||||
{
|
||||
public int Sum { get; private set; }
|
||||
|
||||
public IDisposable AttachGenerated(IShrinkBusResolver resolver, ShrinkBusKey? defaultBus = null)
|
||||
{
|
||||
return ShrinkGeneratedBinding.Subscribe<EcsPingEvent>(resolver, defaultBus,
|
||||
string.Empty, this, OnPing, ShrinkEventPriority.Normal, 0, false);
|
||||
}
|
||||
|
||||
private void OnPing(EcsPingEvent value) => Sum += value.Value;
|
||||
}
|
||||
|
||||
[BurstCompile]
|
||||
internal struct PostPingJob : IJob
|
||||
{
|
||||
public ShrinkEcsEventWriter<EcsPingEvent> Writer;
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
Writer.Post(new EcsPingEvent { Value = 11 });
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ShrinkEcsEventQueueTests
|
||||
{
|
||||
[Test]
|
||||
public void NativeWriterPlaybackUsesSameBusChannel()
|
||||
{
|
||||
using var host = new ShrinkEventBusHost();
|
||||
var bus = host.CreateBus(ShrinkBusKey.World("tests"), ShrinkBusOptions.Inline());
|
||||
var subscriber = new EcsPingSubscriber();
|
||||
using var binding = host.Attach(subscriber, ShrinkBusKey.World("tests"));
|
||||
using var queue = new ShrinkEcsEventQueue<EcsPingEvent>(Allocator.TempJob);
|
||||
|
||||
queue.Writer.Post(new EcsPingEvent { Value = 2 });
|
||||
queue.Writer.Post(new EcsPingEvent { Value = 5 });
|
||||
var played = queue.Playback(bus);
|
||||
|
||||
Assert.AreEqual(2, played);
|
||||
Assert.AreEqual(7, subscriber.Sum);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void BurstJobWritesThenPlaybackUsesManagedBus()
|
||||
{
|
||||
using var host = new ShrinkEventBusHost();
|
||||
var bus = host.CreateBus(ShrinkBusKey.World("burst-tests"), ShrinkBusOptions.Inline());
|
||||
var subscriber = new EcsPingSubscriber();
|
||||
using var binding = host.Attach(subscriber, ShrinkBusKey.World("burst-tests"));
|
||||
using var queue = new ShrinkEcsEventQueue<EcsPingEvent>(Allocator.TempJob);
|
||||
var job = new PostPingJob { Writer = queue.Writer };
|
||||
|
||||
job.Schedule().Complete();
|
||||
var played = queue.Playback(bus);
|
||||
|
||||
Assert.AreEqual(1, played);
|
||||
Assert.AreEqual(11, subscriber.Sum);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user