From: Tayrtahn Date: Thu, 17 Apr 2025 11:43:28 +0000 (-0400) Subject: SpawnAndDeleteEntityCountTest improvements (#34069) X-Git-Url: https://git.smokeofanarchy.ru/gitweb.cgi?a=commitdiff_plain;h=db40bce58f7844901902a7f15e14cbcd094933ca;p=space-station-14.git 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> --- diff --git a/Content.IntegrationTests/Tests/EntityTest.cs b/Content.IntegrationTests/Tests/EntityTest.cs index 26365d8d96..3992272b5b 100644 --- a/Content.IntegrationTests/Tests/EntityTest.cs +++ b/Content.IntegrationTests/Tests/EntityTest.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using System.Numerics; +using System.Text; using Robust.Shared; using Robust.Shared.Audio.Components; using Robust.Shared.Configuration; @@ -270,68 +271,79 @@ namespace Content.IntegrationTests.Tests // We consider only non-audio entities, as some entities will just play sounds when they spawn. int Count(IEntityManager ent) => ent.EntityCount - ent.Count(); + IEnumerable Entities(IEntityManager entMan) => entMan.GetEntities().Where(entMan.HasComponent); - foreach (var protoId in protoIds) + await Assert.MultipleAsync(async () => { - // TODO fix ninja - // Currently ninja fails to equip their own loadout. - if (protoId == "MobHumanSpaceNinja") - continue; - - var count = Count(server.EntMan); - var clientCount = Count(client.EntMan); - EntityUid uid = default; - await server.WaitPost(() => uid = server.EntMan.SpawnEntity(protoId, coords)); - await pair.RunTicksSync(3); - - // If the entity deleted itself, check that it didn't spawn other entities - if (!server.EntMan.EntityExists(uid)) + foreach (var protoId in protoIds) { - if (Count(server.EntMan) != count) - { - Assert.Fail($"Server prototype {protoId} failed on deleting itself"); - } + // TODO fix ninja + // Currently ninja fails to equip their own loadout. + if (protoId == "MobHumanSpaceNinja") + continue; - if (Count(client.EntMan) != clientCount) + var count = Count(server.EntMan); + var clientCount = Count(client.EntMan); + var serverEntities = new HashSet(Entities(server.EntMan)); + var clientEntities = new HashSet(Entities(client.EntMan)); + EntityUid uid = default; + await server.WaitPost(() => uid = server.EntMan.SpawnEntity(protoId, coords)); + await pair.RunTicksSync(3); + + // If the entity deleted itself, check that it didn't spawn other entities + if (!server.EntMan.EntityExists(uid)) { - Assert.Fail($"Client prototype {protoId} failed on deleting itself\n" + - $"Expected {clientCount} and found {Count(client.EntMan)}.\n" + - $"Server was {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.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" + + BuildDiffString(clientEntities, Entities(client.EntMan), client.EntMan)); + continue; } - continue; - } - // Check that the number of entities has increased. - if (Count(server.EntMan) <= count) - { - Assert.Fail($"Server prototype {protoId} failed on spawning as entity count didn't increase"); - } - - if (Count(client.EntMan) <= clientCount) - { - 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}"); + // Check that the number of entities has increased. + 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.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" + + BuildDiffString(clientEntities, Entities(client.EntMan), client.EntMan)); + + await server.WaitPost(() => server.EntMan.DeleteEntity(uid)); + await pair.RunTicksSync(3); + + // Check that the number of entities has gone back to the original value. + 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.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" + + BuildDiffString(clientEntities, Entities(client.EntMan), client.EntMan)); } + }); - await server.WaitPost(() => server.EntMan.DeleteEntity(uid)); - await pair.RunTicksSync(3); - - // Check that the number of entities has gone back to the original value. - if (Count(server.EntMan) != count) - { - Assert.Fail($"Server prototype {protoId} failed on deletion count didn't reset properly"); - } + await pair.CleanReturnAsync(); + } - if (Count(client.EntMan) != clientCount) - { - 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}."); - } + private static string BuildDiffString(IEnumerable oldEnts, IEnumerable 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)); } - - await pair.CleanReturnAsync(); + 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)