3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-12-25 15:50:19 +01:00

Fix event ordering.

Dieser Commit ist enthalten in:
Andrew Steinborn 2021-07-12 08:37:26 -04:00
Ursprung 31aa996623
Commit f79736b8b7
2 geänderte Dateien mit 31 neuen und 1 gelöschten Zeilen

Datei anzeigen

@ -22,7 +22,9 @@ import static java.util.Objects.requireNonNull;
import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache; import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.common.base.VerifyException; import com.google.common.base.VerifyException;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashMultimap; import com.google.common.collect.HashMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import com.google.common.reflect.TypeToken; import com.google.common.reflect.TypeToken;
import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.google.common.util.concurrent.ThreadFactoryBuilder;
@ -87,7 +89,7 @@ public class VelocityEventManager implements EventManager {
private final ExecutorService asyncExecutor; private final ExecutorService asyncExecutor;
private final PluginManager pluginManager; private final PluginManager pluginManager;
private final Multimap<Class<?>, HandlerRegistration> handlersByType = HashMultimap.create(); private final ListMultimap<Class<?>, HandlerRegistration> handlersByType = ArrayListMultimap.create();
private final LoadingCache<Class<?>, HandlersCache> handlersCache = private final LoadingCache<Class<?>, HandlersCache> handlersCache =
Caffeine.newBuilder().build(this::bakeHandlers); Caffeine.newBuilder().build(this::bakeHandlers);

Datei anzeigen

@ -27,6 +27,7 @@ import com.velocitypowered.api.event.PostOrder;
import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.proxy.testutil.FakePluginManager; import com.velocitypowered.proxy.testutil.FakePluginManager;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -59,6 +60,33 @@ public class EventTest {
} }
} }
@Test
void listenerOrderPreserved() throws Exception {
final AtomicLong listenerAInvoked = new AtomicLong();
final AtomicLong listenerBInvoked = new AtomicLong();
final AtomicLong listenerCInvoked = new AtomicLong();
eventManager.register(FakePluginManager.PLUGIN_A, TestEvent.class, event -> {
listenerAInvoked.set(System.nanoTime());
});
eventManager.register(FakePluginManager.PLUGIN_B, TestEvent.class, event -> {
listenerBInvoked.set(System.nanoTime());
});
eventManager.register(FakePluginManager.PLUGIN_A, TestEvent.class, event -> {
listenerCInvoked.set(System.nanoTime());
});
try {
eventManager.fire(new TestEvent()).get();
} finally {
eventManager.unregisterListeners(FakePluginManager.PLUGIN_A);
}
// Check that the order is A < B < C. Check only that A < B and B < C as B < C and A < B => A < C.
assertTrue(listenerAInvoked.get() < listenerBInvoked.get(), "Listener B invoked before A!");
assertTrue(listenerBInvoked.get() < listenerCInvoked.get(), "Listener C invoked before B!");
}
@Test @Test
void testAlwaysAsync() throws Exception { void testAlwaysAsync() throws Exception {
final AlwaysAsyncListener listener = new AlwaysAsyncListener(); final AlwaysAsyncListener listener = new AlwaysAsyncListener();