Pump native chunk tasks while server stop waits for pregen

On Paper only the server thread advances chunk tasks, so a stop that blocks on a running pregen can wait forever. Stop now polls the native chunk queue while joining the pregen worker, and pregen skips eviction and flush scheduling once the server is stopping.

Upstream: 083e68986
This commit is contained in:
piexel committed 2026-11-10 01:27:07 +01:00
1 parent e87e6d9fae
commit ae60b14402
8 files changed
+226 -3

No files matched your search

@@ -0,0 +1,129 @@
package art.arcane.iris.core.gui;
import art.arcane.iris.core.pregenerator.IrisPregenerator;
import org.junit.Test;
import java.lang.reflect.Field;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.CALLS_REAL_METHODS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
public class PregeneratorJobOwnerDrainTest {
@Test
public void ownerPollingCompletesTheRequestThatKeepsTheWorkerAlive() throws Exception {
try (Fixture fixture = new Fixture()) {
Thread owner = Thread.currentThread();
AtomicInteger polls = new AtomicInteger();
assertTrue(PregeneratorJob.shutdownAndWait(1_000L, () -> {
assertSame(owner, Thread.currentThread());
polls.incrementAndGet();
fixture.completion.complete(null);
}));
assertTrue(polls.get() > 0);
assertFalse(fixture.worker.isAlive());
assertNull(PregeneratorJob.getInstance());
verify(fixture.pregenerator).close();
}
}
@Test
public void timeoutKeepsThePendingJobAndDoesNotCompleteItsRequest() throws Exception {
try (Fixture fixture = new Fixture()) {
AtomicInteger polls = new AtomicInteger();
IllegalStateException failure = assertThrows(IllegalStateException.class,
() -> PregeneratorJob.shutdownAndWait(20L, polls::incrementAndGet));
assertEquals("Timed out while stopping the Iris pregenerator after 20ms.", failure.getMessage());
assertTrue(polls.get() > 0);
assertTrue(fixture.worker.isAlive());
assertFalse(fixture.completion.isDone());
assertSame(fixture.job, PregeneratorJob.getInstance());
}
}
@Test
public void interruptedOwnerKeepsTheJobAndOriginalInterruption() throws Exception {
try (Fixture fixture = new Fixture()) {
try {
IllegalStateException failure = assertThrows(IllegalStateException.class,
() -> PregeneratorJob.shutdownAndWait(1_000L, () -> Thread.currentThread().interrupt()));
assertTrue(failure.getCause() instanceof InterruptedException);
assertTrue(Thread.currentThread().isInterrupted());
assertSame(fixture.job, PregeneratorJob.getInstance());
assertFalse(fixture.completion.isDone());
} finally {
Thread.interrupted();
}
}
}
@Test
public void ownerTaskFailureEscapesWithItsCauseAndRetainsTheJob() throws Exception {
try (Fixture fixture = new Fixture()) {
IllegalStateException expected = new IllegalStateException("Native task failed", new Exception("Chunk cause"));
IllegalStateException actual = assertThrows(IllegalStateException.class,
() -> PregeneratorJob.shutdownAndWait(1_000L, () -> {
throw expected;
}));
assertSame(expected, actual);
assertSame(expected.getCause(), actual.getCause());
assertSame(fixture.job, PregeneratorJob.getInstance());
assertFalse(fixture.completion.isDone());
}
}
private static final class Fixture implements AutoCloseable {
private final PregeneratorJob job = mock(PregeneratorJob.class, CALLS_REAL_METHODS);
private final IrisPregenerator pregenerator = mock(IrisPregenerator.class);
private final CompletableFuture<Void> completion = new CompletableFuture<>();
private final AtomicReference<PregeneratorJob> instance;
private final PregeneratorJob previous;
private final Thread worker;
private Fixture() throws Exception {
CountDownLatch started = new CountDownLatch(1);
worker = new Thread(() -> {
started.countDown();
completion.join();
}, "Pregen owner drain test");
set("worker", worker);
set("pregenerator", pregenerator);
instance = instance();
previous = instance.getAndSet(job);
worker.start();
assertTrue(started.await(5L, TimeUnit.SECONDS));
}
@Override
public void close() throws Exception {
completion.complete(null);
worker.join(5_000L);
instance.set(previous);
assertFalse(worker.isAlive());
}
private void set(String name, Object value) throws Exception {
Field field = PregeneratorJob.class.getDeclaredField(name);
field.setAccessible(true);
field.set(job, value);
}
@SuppressWarnings("unchecked")
private static AtomicReference<PregeneratorJob> instance() throws Exception {
Field field = PregeneratorJob.class.getDeclaredField("instance");
field.setAccessible(true);
return (AtomicReference<PregeneratorJob>) field.get(null);
}
}
}