diff --git a/network/proxy/central-pack/build.gradle.kts b/network/proxy/central-pack/build.gradle.kts index 295741a1..7c5a1bf3 100644 --- a/network/proxy/central-pack/build.gradle.kts +++ b/network/proxy/central-pack/build.gradle.kts @@ -9,6 +9,11 @@ repositories { dependencies { compileOnly("com.velocitypowered:velocity-api:3.4.0-SNAPSHOT") annotationProcessor("com.velocitypowered:velocity-api:3.4.0-SNAPSHOT") + testImplementation("com.velocitypowered:velocity-api:3.4.0-SNAPSHOT") + testImplementation("org.junit.jupiter:junit-jupiter:5.11.4") + testRuntimeOnly("org.junit.platform:junit-platform-launcher") } java { toolchain.languageVersion.set(JavaLanguageVersion.of(21)) } tasks.withType { options.encoding = "UTF-8" } + +tasks.test { useJUnitPlatform() } diff --git a/network/proxy/central-pack/src/main/java/com/dunerealms/proxy/CentralPackPlugin.java b/network/proxy/central-pack/src/main/java/com/dunerealms/proxy/CentralPackPlugin.java index f4535d2d..e7f2f549 100644 --- a/network/proxy/central-pack/src/main/java/com/dunerealms/proxy/CentralPackPlugin.java +++ b/network/proxy/central-pack/src/main/java/com/dunerealms/proxy/CentralPackPlugin.java @@ -58,7 +58,13 @@ public final class CentralPackPlugin { ResourcePackInfo current=pack; Player player=event.getPlayer(); if(current!=null&&player.isActive()&&offered.put(player.getUniqueId(),player)!=player){ - player.sendResourcePackOffer(current); + try{ + player.sendResourcePackOffer(current); + }catch(RuntimeException failure){ + // No offer was queued. Permit a later attempt on this connection. + offered.remove(player.getUniqueId(),player); + logger.warn("Could not queue the central resource pack offer",failure); + } } } diff --git a/network/proxy/central-pack/src/test/java/com/dunerealms/proxy/CentralPackPluginTest.java b/network/proxy/central-pack/src/test/java/com/dunerealms/proxy/CentralPackPluginTest.java new file mode 100644 index 00000000..1a2357e9 --- /dev/null +++ b/network/proxy/central-pack/src/test/java/com/dunerealms/proxy/CentralPackPluginTest.java @@ -0,0 +1,113 @@ +package com.dunerealms.proxy; + +import com.velocitypowered.api.event.Subscribe; +import com.velocitypowered.api.event.connection.DisconnectEvent; +import com.velocitypowered.api.event.player.PlayerResourcePackStatusEvent; +import com.velocitypowered.api.event.player.PlayerResourcePackStatusEvent.Status; +import com.velocitypowered.api.event.player.ServerPostConnectEvent; +import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; +import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.ProxyServer; +import com.velocitypowered.api.proxy.player.ResourcePackInfo; +import com.velocitypowered.api.proxy.server.RegisteredServer; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Proxy; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.UUID; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.slf4j.Logger; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class CentralPackPluginTest { + @TempDir Path directory; + + @Test void packStatusNeverReoffersDuringTransfersButReconnectGetsOneNewOffer() throws Exception { + var backend = proxy(RegisteredServer.class, (mock, method, args) -> null); + for (Status[] replies : new Status[][] { + {Status.ACCEPTED, Status.DOWNLOADED, Status.SUCCESSFUL}, + {Status.DECLINED}, + {Status.ACCEPTED, Status.FAILED_DOWNLOAD} + }) { + var plugin = initializedPlugin(); + var first = new Connection(UUID.randomUUID()); + dispatch(plugin, new ServerPostConnectEvent(first.player, null)); + for (Status reply : replies) { + dispatch(plugin, new PlayerResourcePackStatusEvent(first.player, UUID.fromString("a7b5ab7d-d8d8-5b51-b016-71a5f4c00008"), reply, null)); + dispatch(plugin, new ServerPostConnectEvent(first.player, backend)); + } + assertEquals(1, first.offers); + dispatch(plugin, new DisconnectEvent(first.player, DisconnectEvent.LoginStatus.SUCCESSFUL_LOGIN)); + var reconnect = new Connection(first.id); + dispatch(plugin, new ServerPostConnectEvent(reconnect.player, null)); + dispatch(plugin, new DisconnectEvent(first.player, DisconnectEvent.LoginStatus.SUCCESSFUL_LOGIN)); + dispatch(plugin, new ServerPostConnectEvent(reconnect.player, null)); + assertEquals(1, reconnect.offers); + } + } + + @Test void aSynchronousQueueFailureDoesNotConsumeTheConnectionOffer() throws Exception { + var plugin = initializedPlugin(); + var connection = new Connection(UUID.randomUUID()); + connection.failOnce = true; + dispatch(plugin, new ServerPostConnectEvent(connection.player, null)); + assertEquals(0, connection.offers); + dispatch(plugin, new ServerPostConnectEvent(connection.player, null)); + dispatch(plugin, new ServerPostConnectEvent(connection.player, null)); + assertEquals(2, connection.attempts); + assertEquals(1, connection.offers); + } + + private CentralPackPlugin initializedPlugin() throws Exception { + Files.writeString(directory.resolve("pack.properties"), + "url=https://www.dunerealms.com/downloads/DuneRealms-Central-v2.zip\nsha1=" + "1".repeat(40) + "\n"); + var pack = proxy(ResourcePackInfo.class, (mock, method, args) -> null); + var builder = proxy(ResourcePackInfo.Builder.class, (mock, method, args) -> + method.getName().equals("build") ? pack : mock); + var server = proxy(ProxyServer.class, (mock, method, args) -> builder); + var log = proxy(Logger.class, (mock, method, args) -> null); + var plugin = new CentralPackPlugin(server, log, directory); + dispatch(plugin, new ProxyInitializeEvent()); + return plugin; + } + + private static void dispatch(CentralPackPlugin plugin, Object event) throws Exception { + for (var method : CentralPackPlugin.class.getDeclaredMethods()) + if (method.isAnnotationPresent(Subscribe.class) && method.getParameterTypes()[0].isInstance(event)) + method.invoke(plugin, event); + } + + private static T proxy(Class type, InvocationHandler handler) { + return type.cast(Proxy.newProxyInstance(type.getClassLoader(), new Class[]{type}, handler)); + } + + private static final class Connection { + final UUID id; + final Player player; + int attempts; + int offers; + boolean failOnce; + + Connection(UUID id) { + this.id = id; + this.player = proxy(Player.class, (mock, method, args) -> switch (method.getName()) { + case "isActive" -> true; + case "getUniqueId" -> this.id; + case "equals" -> mock == args[0]; + case "hashCode" -> System.identityHashCode(mock); + case "sendResourcePackOffer" -> { + attempts++; + if (failOnce) { + failOnce = false; + throw new IllegalStateException("fixture queue failure"); + } + offers++; + yield null; + } + default -> throw new UnsupportedOperationException(method.getName()); + }); + } + } +}