mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-25 18:47:40 +01:00
Test to ensure instances & chunks to eventually be garbage collected
This commit is contained in:
parent
5685413fce
commit
7d752bce88
@ -6,6 +6,8 @@ import net.minestom.server.coordinate.Pos;
|
|||||||
import net.minestom.server.event.player.PlayerTickEvent;
|
import net.minestom.server.event.player.PlayerTickEvent;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.lang.ref.WeakReference;
|
||||||
|
|
||||||
@EnvTest
|
@EnvTest
|
||||||
public class InstanceUnregisterIntegrationTest {
|
public class InstanceUnregisterIntegrationTest {
|
||||||
|
|
||||||
@ -30,4 +32,40 @@ public class InstanceUnregisterIntegrationTest {
|
|||||||
listener.followup();
|
listener.followup();
|
||||||
env.tick();
|
env.tick();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void instanceGC(Env env) {
|
||||||
|
var instance = env.createFlatInstance();
|
||||||
|
var ref = new WeakReference<>(instance);
|
||||||
|
env.process().instance().unregisterInstance(instance);
|
||||||
|
|
||||||
|
//noinspection UnusedAssignment
|
||||||
|
instance = null;
|
||||||
|
waitUntilCleared(ref);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void chunkGC(Env env) {
|
||||||
|
// Ensure that unregistering an instance does release its chunks
|
||||||
|
var instance = env.createFlatInstance();
|
||||||
|
var chunk = instance.loadChunk(0, 0).join();
|
||||||
|
var ref = new WeakReference<>(chunk);
|
||||||
|
instance.unloadChunk(chunk);
|
||||||
|
env.process().instance().unregisterInstance(instance);
|
||||||
|
env.tick(); // Required to remove the chunk from the thread dispatcher
|
||||||
|
|
||||||
|
//noinspection UnusedAssignment
|
||||||
|
chunk = null;
|
||||||
|
waitUntilCleared(ref);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void waitUntilCleared(WeakReference<?> ref) {
|
||||||
|
while (ref.get() != null) {
|
||||||
|
System.gc();
|
||||||
|
try {
|
||||||
|
Thread.sleep(50);
|
||||||
|
} catch (InterruptedException ignore) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user