Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

Fix Readers not re-validating with AggregateSubscription #1326

Merged
merged 8 commits into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
- Fixed a bug where Reader/Writer/CommandSender/CommandReceiver fields would not have their state set to invalid when the underlying constraints were not met. [#1297](https://github.com/spatialos/gdk-for-unity/pull/1297)
- This bug would manifest itself in situations like a `Reader` reference attempting to read data that does not exist in your worker's view anymore.
- Fixed the Mobile Launcher being unable to find the Android SDK when using the embedded installation. [#1319](https://github.com/spatialos/gdk-for-unity/pull/1319)
- Fixed a bug where losing a `Reader` due to QBI would break the monobehaviour that required it. [#1326](https://github.com/spatialos/gdk-for-unity/pull/1326)

### Removed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Improbable.Gdk.Subscriptions;
using Improbable.Gdk.Test;
using Improbable.Gdk.TestUtils;
using Improbable.Tests;
using Improbable.Worker.CInterop;
using NUnit.Framework;
using UnityEngine;
Expand Down Expand Up @@ -38,6 +39,66 @@ public void Reader_is_disabled_if_component_removed()
});
}

[Test]
public void Reader_is_reenabled_if_component_regained()
{
World
.Step(world =>
{
world.Connection.CreateEntity(EntityId, GetTemplate());
})
.Step(world =>
{
var (_, readerBehaviour) = world.CreateGameObject<PositionReaderBehaviour>(EntityId);
return readerBehaviour;
})
.Step(world =>
{
world.Connection.RemoveComponent(EntityId, Position.ComponentId);
})
.Step(world =>
{
world.Connection.AddComponent(EntityId, Position.ComponentId, new Position.Update());
})
.Step((world, context) =>
{
var readerBehaviour = context;

Assert.IsNotNull(readerBehaviour.Reader);
Assert.IsTrue(readerBehaviour.Reader.IsValid);
});
}

[Test]
public void Reader_is_reenabled_if_component_regained_with_aggregate()
{
World
.Step(world =>
{
world.Connection.CreateEntity(EntityId, GetTemplate());
})
.Step(world =>
{
var (_, readerBehaviour) = world.CreateGameObject<MultipleComponentBehaviour>(EntityId);
return readerBehaviour;
})
.Step(world =>
{
world.Connection.RemoveComponent(EntityId, Position.ComponentId);
})
.Step(world =>
{
world.Connection.AddComponent(EntityId, Position.ComponentId, new Position.Update());
})
.Step((world, context) =>
{
var readerBehaviour = context;

Assert.IsNotNull(readerBehaviour.DependencyTestReader);
Assert.IsTrue(readerBehaviour.DependencyTestReader.IsValid);
});
}

[Test]
public void Writer_is_disabled_if_loses_auth()
{
Expand Down Expand Up @@ -148,6 +209,7 @@ private static EntityTemplate GetTemplate()
{
var template = new EntityTemplate();
template.AddComponent(new Position.Snapshot(), "worker");
template.AddComponent(new DependencyTest.Snapshot(), "worker");
template.AddComponent(new TestCommands.Snapshot(), "worker");
return template;
}
Expand Down Expand Up @@ -179,6 +241,12 @@ private class MultipleReaderBehaviour : MonoBehaviour
[Require] public PositionReader PositionReader;
[Require] public TestCommandsReader TestCommandsReader;
}

private class MultipleComponentBehaviour : MonoBehaviour
paulbalaji marked this conversation as resolved.
Show resolved Hide resolved
{
[Require] public PositionReader PositionReader;
[Require] public DependencyTestReader DependencyTestReader;
}

#pragma warning restore 649

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ internal DependentComponentReader(World world, Entity entity, EntityId entityId)
{
add
{
if (!IsValid)
{
throw new InvalidOperationException("Cannot add field update callback when Reader is not valid.");
}

if (aUpdateCallbackToCallbackKey == null)
{
aUpdateCallbackToCallbackKey = new Dictionary<Action<global::Improbable.TestSchema.ExhaustiveRepeatedData>, ulong>();
Expand Down Expand Up @@ -87,6 +92,11 @@ internal DependentComponentReader(World world, Entity entity, EntityId entityId)
{
add
{
if (!IsValid)
{
throw new InvalidOperationException("Cannot add field update callback when Reader is not valid.");
}

if (bUpdateCallbackToCallbackKey == null)
{
bUpdateCallbackToCallbackKey = new Dictionary<Action<global::Improbable.TestSchema.SomeEnum>, ulong>();
Expand Down Expand Up @@ -117,6 +127,11 @@ internal DependentComponentReader(World world, Entity entity, EntityId entityId)
{
add
{
if (!IsValid)
{
throw new InvalidOperationException("Cannot add field update callback when Reader is not valid.");
}

if (cUpdateCallbackToCallbackKey == null)
{
cUpdateCallbackToCallbackKey = new Dictionary<Action<global::Improbable.TestSchema.SomeEnum?>, ulong>();
Expand Down Expand Up @@ -147,6 +162,11 @@ internal DependentComponentReader(World world, Entity entity, EntityId entityId)
{
add
{
if (!IsValid)
{
throw new InvalidOperationException("Cannot add field update callback when Reader is not valid.");
}

if (dUpdateCallbackToCallbackKey == null)
{
dUpdateCallbackToCallbackKey = new Dictionary<Action<global::System.Collections.Generic.List<global::Improbable.TestSchema.SomeType>>, ulong>();
Expand Down Expand Up @@ -177,6 +197,11 @@ internal DependentComponentReader(World world, Entity entity, EntityId entityId)
{
add
{
if (!IsValid)
{
throw new InvalidOperationException("Cannot add field update callback when Reader is not valid.");
}

if (eUpdateCallbackToCallbackKey == null)
{
eUpdateCallbackToCallbackKey = new Dictionary<Action<global::System.Collections.Generic.Dictionary<global::Improbable.TestSchema.SomeEnum, global::Improbable.TestSchema.SomeType>>, ulong>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void Clean(World world)

private void AddComponent(EntityId entityId)
{
workerSystem.TryGetEntity(entityId, out var entity);
var entity = workerSystem.GetEntity(entityId);
var component = new global::Improbable.DependentSchema.DependentComponent.Component();

component.aHandle = global::Improbable.DependentSchema.DependentComponent.ReferenceTypeProviders.AProvider.Allocate(world);
Expand All @@ -106,7 +106,7 @@ private void AddComponent(EntityId entityId)

private void RemoveComponent(EntityId entityId)
{
workerSystem.TryGetEntity(entityId, out var entity);
var entity = workerSystem.GetEntity(entityId);
entityManager.RemoveComponent<ComponentAuthority>(entity);

var data = entityManager.GetComponentData<global::Improbable.DependentSchema.DependentComponent.Component>(entity);
Expand All @@ -124,7 +124,7 @@ private void RemoveComponent(EntityId entityId)

private void ApplyUpdate(in ComponentUpdateReceived<Update> update, ComponentDataFromEntity<Component> dataFromEntity)
{
workerSystem.TryGetEntity(update.EntityId, out var entity);
var entity = workerSystem.GetEntity(update.EntityId);
if (!dataFromEntity.Exists(entity))
{
return;
Expand Down Expand Up @@ -167,13 +167,13 @@ private void SetAuthority(EntityId entityId, Authority authority)
{
case Authority.NotAuthoritative:
{
workerSystem.TryGetEntity(entityId, out var entity);
var entity = workerSystem.GetEntity(entityId);
entityManager.SetSharedComponentData(entity, ComponentAuthority.NotAuthoritative);
break;
}
case Authority.Authoritative:
{
workerSystem.TryGetEntity(entityId, out var entity);
var entity = workerSystem.GetEntity(entityId);
entityManager.SetSharedComponentData(entity, ComponentAuthority.Authoritative);
break;
}
Expand Down
Loading