diff --git a/src/test/java/net/minestom/server/api/TestUtils.java b/src/test/java/net/minestom/server/api/TestUtils.java new file mode 100644 index 000000000..a834d7afd --- /dev/null +++ b/src/test/java/net/minestom/server/api/TestUtils.java @@ -0,0 +1,15 @@ +package net.minestom.server.api; + +import java.lang.ref.WeakReference; + +public class TestUtils { + public static void waitUntilCleared(WeakReference ref) { + while (ref.get() != null) { + System.gc(); + try { + Thread.sleep(50); + } catch (InterruptedException ignore) { + } + } + } +} diff --git a/src/test/java/net/minestom/server/entity/EntityRemovalIntegrationTest.java b/src/test/java/net/minestom/server/entity/EntityRemovalIntegrationTest.java index 7dea5a2d8..ef0b74bf9 100644 --- a/src/test/java/net/minestom/server/entity/EntityRemovalIntegrationTest.java +++ b/src/test/java/net/minestom/server/entity/EntityRemovalIntegrationTest.java @@ -7,9 +7,11 @@ import net.minestom.server.network.packet.server.play.DestroyEntitiesPacket; import net.minestom.server.utils.time.TimeUnit; import org.junit.jupiter.api.Test; +import java.lang.ref.WeakReference; import java.time.temporal.TemporalUnit; import java.util.List; +import static net.minestom.server.api.TestUtils.waitUntilCleared; import static org.junit.jupiter.api.Assertions.*; @EnvTest @@ -61,6 +63,21 @@ public class EntityRemovalIntegrationTest { assertEquals(1, entity.getAliveTicks()); } + @Test + public void entityGC(Env env) { + // Ensure that entities do not stay in memory after they are removed + var instance = env.createFlatInstance(); + var entity = new Entity(EntityType.ZOMBIE); + entity.setInstance(instance, new Pos(0, 40, 0)).join(); + entity.remove(); + + var ref = new WeakReference<>(entity); + //noinspection UnusedAssignment + entity = null; + env.tick(); // Required to remove the entity from the thread dispatcher + waitUntilCleared(ref); + } + static final class TestEntity extends Entity { public TestEntity(long delay, TemporalUnit unit) { super(EntityType.ZOMBIE); diff --git a/src/test/java/net/minestom/server/event/EventNodeMapTest.java b/src/test/java/net/minestom/server/event/EventNodeMapTest.java index 457054b7f..e9ee2894d 100644 --- a/src/test/java/net/minestom/server/event/EventNodeMapTest.java +++ b/src/test/java/net/minestom/server/event/EventNodeMapTest.java @@ -10,6 +10,7 @@ import org.junit.jupiter.api.Test; import java.lang.ref.WeakReference; import java.util.concurrent.atomic.AtomicBoolean; +import static net.minestom.server.api.TestUtils.waitUntilCleared; import static org.junit.jupiter.api.Assertions.*; public class EventNodeMapTest { @@ -81,14 +82,4 @@ public class EventNodeMapTest { item = null; waitUntilCleared(ref); } - - private static void waitUntilCleared(WeakReference ref) { - while (ref.get() != null) { - System.gc(); - try { - Thread.sleep(50); - } catch (InterruptedException ignore) { - } - } - } } diff --git a/src/test/java/net/minestom/server/instance/InstanceUnregisterIntegrationTest.java b/src/test/java/net/minestom/server/instance/InstanceUnregisterIntegrationTest.java index 1e9a74e69..2b0dc2a65 100644 --- a/src/test/java/net/minestom/server/instance/InstanceUnregisterIntegrationTest.java +++ b/src/test/java/net/minestom/server/instance/InstanceUnregisterIntegrationTest.java @@ -8,6 +8,8 @@ import org.junit.jupiter.api.Test; import java.lang.ref.WeakReference; +import static net.minestom.server.api.TestUtils.waitUntilCleared; + @EnvTest public class InstanceUnregisterIntegrationTest { @@ -58,14 +60,4 @@ public class InstanceUnregisterIntegrationTest { chunk = null; waitUntilCleared(ref); } - - private static void waitUntilCleared(WeakReference ref) { - while (ref.get() != null) { - System.gc(); - try { - Thread.sleep(50); - } catch (InterruptedException ignore) { - } - } - } }