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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d3be15e44ff9405e947ecaaac7905527
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "ShrinkEventBus.Entities.Tests",
|
||||
"rootNamespace": "ShrinkEventBus.Entities.Tests",
|
||||
"references": [
|
||||
"ShrinkEventBus.Entities.Runtime",
|
||||
"ShrinkEventBus.Runtime",
|
||||
"Unity.Burst",
|
||||
"Unity.Collections",
|
||||
"Unity.Jobs",
|
||||
"UniTask",
|
||||
"UnityEngine.TestRunner",
|
||||
"UnityEditor.TestRunner"
|
||||
],
|
||||
"includePlatforms": ["Editor"],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"autoReferenced": false,
|
||||
"defineConstraints": ["UNITY_INCLUDE_TESTS"]
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9ef47a51870047fda3659747535d685a
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user