diff --git a/Content.IntegrationTests/Tests/EntityTest.cs b/Content.IntegrationTests/Tests/EntityTest.cs
index c82535dd36..59164972c7 100644
--- a/Content.IntegrationTests/Tests/EntityTest.cs
+++ b/Content.IntegrationTests/Tests/EntityTest.cs
@@ -13,6 +13,7 @@ using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.Manager.Attributes;
+using Robust.Shared.Spawners;
namespace Content.IntegrationTests.Tests
{
@@ -290,6 +291,8 @@ namespace Content.IntegrationTests.Tests
// If the entity deleted itself, check that it didn't spawn other entities
if (!server.EntMan.EntityExists(uid))
{
+ await CleanupTransientEntities(pair, serverEntities);
+
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" +
@@ -309,6 +312,7 @@ namespace Content.IntegrationTests.Tests
await server.WaitPost(() => server.EntMan.DeleteEntity(uid));
await pair.RunTicksSync(3);
+ await CleanupTransientEntities(pair, serverEntities);
// 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" +
@@ -321,6 +325,33 @@ namespace Content.IntegrationTests.Tests
});
}
+ ///
+ /// Deletes any entities with that were not present in the baseline snapshot.
+ /// Some entities spawn transient side-effects on deletion (e.g. explosion visuals). These side-effect entities
+ /// use TimedDespawn and would persist across test iterations, corrupting baseline entity counts and causing
+ /// cascading assertion failures.
+ ///
+ private static async Task CleanupTransientEntities(Pair.TestPair pair, HashSet baselineEntities)
+ {
+ var server = pair.Server;
+ await server.WaitPost(() =>
+ {
+ var toRemove = new List();
+ var query = server.EntMan.AllEntityQueryEnumerator();
+ while (query.MoveNext(out var uid, out _))
+ {
+ if (!baselineEntities.Contains(uid))
+ toRemove.Add(uid);
+ }
+
+ foreach (var uid in toRemove)
+ {
+ server.EntMan.DeleteEntity(uid);
+ }
+ });
+ await pair.RunTicksSync(3);
+ }
+
private static string BuildDiffString(IEnumerable oldEnts, IEnumerable newEnts, IEntityManager entMan)
{
var sb = new StringBuilder();