SpawnAndDeleteEntityCountTest improvements (#34069)

* Minor improvements to SpawnAndDeleteEntityCountTest Assert messages

* * Now lists added/removed entities on test failure.
* Wrapped entity loop in MultipleAsync so all failing ents are reported.
* Removed Multiple wrappers on client/server pairs, as they made result formatting ugly.

---------

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
Tayrtahn
2025-04-17 07:43:28 -04:00
committed by GitHub
parent 9b7af5dfcf
commit db40bce58f

View File

@@ -1,6 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Numerics; using System.Numerics;
using System.Text;
using Robust.Shared; using Robust.Shared;
using Robust.Shared.Audio.Components; using Robust.Shared.Audio.Components;
using Robust.Shared.Configuration; using Robust.Shared.Configuration;
@@ -270,7 +271,10 @@ namespace Content.IntegrationTests.Tests
// We consider only non-audio entities, as some entities will just play sounds when they spawn. // We consider only non-audio entities, as some entities will just play sounds when they spawn.
int Count(IEntityManager ent) => ent.EntityCount - ent.Count<AudioComponent>(); int Count(IEntityManager ent) => ent.EntityCount - ent.Count<AudioComponent>();
IEnumerable<EntityUid> Entities(IEntityManager entMan) => entMan.GetEntities().Where(entMan.HasComponent<AudioComponent>);
await Assert.MultipleAsync(async () =>
{
foreach (var protoId in protoIds) foreach (var protoId in protoIds)
{ {
// TODO fix ninja // TODO fix ninja
@@ -280,6 +284,8 @@ namespace Content.IntegrationTests.Tests
var count = Count(server.EntMan); var count = Count(server.EntMan);
var clientCount = Count(client.EntMan); var clientCount = Count(client.EntMan);
var serverEntities = new HashSet<EntityUid>(Entities(server.EntMan));
var clientEntities = new HashSet<EntityUid>(Entities(client.EntMan));
EntityUid uid = default; EntityUid uid = default;
await server.WaitPost(() => uid = server.EntMan.SpawnEntity(protoId, coords)); await server.WaitPost(() => uid = server.EntMan.SpawnEntity(protoId, coords));
await pair.RunTicksSync(3); await pair.RunTicksSync(3);
@@ -287,53 +293,59 @@ namespace Content.IntegrationTests.Tests
// If the entity deleted itself, check that it didn't spawn other entities // If the entity deleted itself, check that it didn't spawn other entities
if (!server.EntMan.EntityExists(uid)) if (!server.EntMan.EntityExists(uid))
{ {
if (Count(server.EntMan) != count) Assert.That(Count(server.EntMan), Is.EqualTo(count), $"Server prototype {protoId} failed on deleting itself\n" +
{ BuildDiffString(serverEntities, Entities(server.EntMan), server.EntMan));
Assert.Fail($"Server prototype {protoId} failed on deleting itself"); Assert.That(Count(client.EntMan), Is.EqualTo(clientCount), $"Client prototype {protoId} failed on deleting itself\n" +
} $"Expected {clientCount} and found {client.EntMan.EntityCount}.\n" +
$"Server count was {count}.\n" +
if (Count(client.EntMan) != clientCount) BuildDiffString(clientEntities, Entities(client.EntMan), client.EntMan));
{
Assert.Fail($"Client prototype {protoId} failed on deleting itself\n" +
$"Expected {clientCount} and found {Count(client.EntMan)}.\n" +
$"Server was {count}.");
}
continue; continue;
} }
// Check that the number of entities has increased. // Check that the number of entities has increased.
if (Count(server.EntMan) <= count) Assert.That(Count(server.EntMan), Is.GreaterThan(count), $"Server prototype {protoId} failed on spawning as entity count didn't increase\n" +
{ BuildDiffString(serverEntities, Entities(server.EntMan), server.EntMan));
Assert.Fail($"Server prototype {protoId} failed on spawning as entity count didn't increase"); Assert.That(Count(client.EntMan), Is.GreaterThan(clientCount), $"Client prototype {protoId} failed on spawning as entity count didn't increase\n" +
} $"Expected at least {clientCount} and found {client.EntMan.EntityCount}. " +
$"Server count was {count}.\n" +
if (Count(client.EntMan) <= clientCount) BuildDiffString(clientEntities, Entities(client.EntMan), client.EntMan));
{
Assert.Fail($"Client prototype {protoId} failed on spawning as entity count didn't increase" +
$"Expected at least {clientCount} and found {Count(client.EntMan)}. " +
$"Server was {count}");
}
await server.WaitPost(() => server.EntMan.DeleteEntity(uid)); await server.WaitPost(() => server.EntMan.DeleteEntity(uid));
await pair.RunTicksSync(3); await pair.RunTicksSync(3);
// Check that the number of entities has gone back to the original value. // Check that the number of entities has gone back to the original value.
if (Count(server.EntMan) != count) Assert.That(Count(server.EntMan), Is.EqualTo(count), $"Server prototype {protoId} failed on deletion: count didn't reset properly\n" +
{ BuildDiffString(serverEntities, Entities(server.EntMan), server.EntMan));
Assert.Fail($"Server prototype {protoId} failed on deletion count didn't reset properly"); Assert.That(client.EntMan.EntityCount, Is.EqualTo(clientCount), $"Client prototype {protoId} failed on deletion: count didn't reset properly:\n" +
} $"Expected {clientCount} and found {client.EntMan.EntityCount}.\n" +
$"Server count was {count}.\n" +
if (Count(client.EntMan) != clientCount) BuildDiffString(clientEntities, Entities(client.EntMan), client.EntMan));
{
Assert.Fail($"Client prototype {protoId} failed on deletion count didn't reset properly:\n" +
$"Expected {clientCount} and found {Count(client.EntMan)}.\n" +
$"Server was {count}.");
}
} }
});
await pair.CleanReturnAsync(); await pair.CleanReturnAsync();
} }
private static string BuildDiffString(IEnumerable<EntityUid> oldEnts, IEnumerable<EntityUid> newEnts, IEntityManager entMan)
{
var sb = new StringBuilder();
var addedEnts = newEnts.Except(oldEnts);
var removedEnts = oldEnts.Except(newEnts);
if (addedEnts.Any())
sb.AppendLine("Listing new entities:");
foreach (var addedEnt in addedEnts)
{
sb.AppendLine(entMan.ToPrettyString(addedEnt));
}
if (removedEnts.Any())
sb.AppendLine("Listing removed entities:");
foreach (var removedEnt in removedEnts)
{
sb.AppendLine("\t" + entMan.ToPrettyString(removedEnt));
}
return sb.ToString();
}
private static bool HasRequiredDataField(Component component) private static bool HasRequiredDataField(Component component)
{ {
foreach (var field in component.GetType().GetFields()) foreach (var field in component.GetType().GetFields())