|
| 1 | +package dev.objz.commandbridge.velocity.api; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 6 | + |
| 7 | +import dev.objz.commandbridge.api.platform.Platform; |
| 8 | +import dev.objz.commandbridge.net.Endpoint; |
| 9 | +import dev.objz.commandbridge.net.ResponseAwaiter; |
| 10 | +import dev.objz.commandbridge.net.SendOperation; |
| 11 | +import dev.objz.commandbridge.net.payloads.PluginMessage; |
| 12 | +import dev.objz.commandbridge.net.proto.Envelope; |
| 13 | +import dev.objz.commandbridge.security.AuthStatus; |
| 14 | +import dev.objz.commandbridge.scripting.model.enums.Location; |
| 15 | +import dev.objz.commandbridge.velocity.net.EndpointServer; |
| 16 | +import dev.objz.commandbridge.velocity.net.session.ClientSession; |
| 17 | +import dev.objz.commandbridge.velocity.net.session.SessionHub; |
| 18 | +import dev.objz.commandbridge.velocity.util.PlayerTracker; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import java.lang.reflect.Method; |
| 22 | +import java.time.Duration; |
| 23 | +import java.util.List; |
| 24 | +import java.util.UUID; |
| 25 | +import java.util.concurrent.CompletableFuture; |
| 26 | +import java.util.concurrent.CompletionException; |
| 27 | +import java.util.concurrent.atomic.AtomicInteger; |
| 28 | + |
| 29 | +class VelocityCommandBridgeImplTest { |
| 30 | + |
| 31 | + @Test |
| 32 | + void requirePlayerDropsSendWhenAbsent() { |
| 33 | + SessionHub sessions = new SessionHub(); |
| 34 | + PlayerTracker tracker = new PlayerTracker(); |
| 35 | + PluginMessageQueue queue = new PluginMessageQueue(); |
| 36 | + CountingEndpointServer endpointServer = new CountingEndpointServer(); |
| 37 | + VelocityCommandBridgeImpl bridge = new VelocityCommandBridgeImpl( |
| 38 | + sessions, |
| 39 | + tracker, |
| 40 | + "velocity", |
| 41 | + endpointServer, |
| 42 | + queue); |
| 43 | + |
| 44 | + PluginMessage payload = new PluginMessage("test", null, false, UUID.randomUUID(), null, null); |
| 45 | + |
| 46 | + invokeSend(bridge, Platform.BACKEND.target("backend-1"), payload).join(); |
| 47 | + |
| 48 | + assertEquals(0, endpointServer.sendCount()); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void requirePlayerDeliversSendWhenPresent() { |
| 53 | + SessionHub sessions = new SessionHub(); |
| 54 | + PlayerTracker tracker = new PlayerTracker(); |
| 55 | + PluginMessageQueue queue = new PluginMessageQueue(); |
| 56 | + CountingEndpointServer endpointServer = new CountingEndpointServer(); |
| 57 | + VelocityCommandBridgeImpl bridge = new VelocityCommandBridgeImpl( |
| 58 | + sessions, |
| 59 | + tracker, |
| 60 | + "velocity", |
| 61 | + endpointServer, |
| 62 | + queue); |
| 63 | + |
| 64 | + UUID player = UUID.randomUUID(); |
| 65 | + tracker.addPlayer("backend-1", player); |
| 66 | + ClientSession session = sessions.add("backend-1", new TestEndpoint()); |
| 67 | + session.location(Location.BACKEND); |
| 68 | + session.status(AuthStatus.AUTH_OK); |
| 69 | + |
| 70 | + PluginMessage payload = new PluginMessage("test", null, false, player, null, null); |
| 71 | + |
| 72 | + invokeSend(bridge, Platform.BACKEND.target("backend-1"), payload).join(); |
| 73 | + |
| 74 | + assertEquals(1, endpointServer.sendCount()); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void requirePlayerFailsRequestWhenAbsent() { |
| 79 | + SessionHub sessions = new SessionHub(); |
| 80 | + PlayerTracker tracker = new PlayerTracker(); |
| 81 | + PluginMessageQueue queue = new PluginMessageQueue(); |
| 82 | + CountingEndpointServer endpointServer = new CountingEndpointServer(); |
| 83 | + VelocityCommandBridgeImpl bridge = new VelocityCommandBridgeImpl( |
| 84 | + sessions, |
| 85 | + tracker, |
| 86 | + "velocity", |
| 87 | + endpointServer, |
| 88 | + queue); |
| 89 | + |
| 90 | + PluginMessage payload = new PluginMessage("test", null, true, UUID.randomUUID(), null, null); |
| 91 | + CompletableFuture<PluginMessage> future = invokeRequest( |
| 92 | + bridge, |
| 93 | + Platform.BACKEND.target("backend-1"), |
| 94 | + payload, |
| 95 | + Duration.ofSeconds(1)); |
| 96 | + |
| 97 | + CompletionException ex = assertThrows(CompletionException.class, future::join); |
| 98 | + Throwable cause = assertInstanceOf(IllegalStateException.class, ex.getCause()); |
| 99 | + assertEquals("Player not on target server: backend-1", cause.getMessage()); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void whenOnlineQueuesSendWhenAbsent() { |
| 104 | + SessionHub sessions = new SessionHub(); |
| 105 | + PlayerTracker tracker = new PlayerTracker(); |
| 106 | + PluginMessageQueue queue = new PluginMessageQueue(); |
| 107 | + CountingEndpointServer endpointServer = new CountingEndpointServer(); |
| 108 | + VelocityCommandBridgeImpl bridge = new VelocityCommandBridgeImpl( |
| 109 | + sessions, |
| 110 | + tracker, |
| 111 | + "velocity", |
| 112 | + endpointServer, |
| 113 | + queue); |
| 114 | + |
| 115 | + UUID player = UUID.randomUUID(); |
| 116 | + PluginMessage payload = new PluginMessage("test", null, false, null, player, null); |
| 117 | + |
| 118 | + invokeSend(bridge, Platform.BACKEND.target("backend-1"), payload).join(); |
| 119 | + |
| 120 | + List<PluginMessageQueue.QueuedMessage> drained = queue.drain("backend-1", player); |
| 121 | + assertEquals(1, drained.size()); |
| 122 | + assertEquals(0, endpointServer.sendCount()); |
| 123 | + } |
| 124 | + |
| 125 | + @SuppressWarnings("unchecked") |
| 126 | + private static CompletableFuture<Void> invokeSend( |
| 127 | + VelocityCommandBridgeImpl bridge, |
| 128 | + Platform.ServerTarget target, |
| 129 | + PluginMessage payload) { |
| 130 | + try { |
| 131 | + Method method = VelocityCommandBridgeImpl.class.getDeclaredMethod( |
| 132 | + "sendPluginMessage", |
| 133 | + Platform.ServerTarget.class, |
| 134 | + PluginMessage.class); |
| 135 | + method.setAccessible(true); |
| 136 | + return (CompletableFuture<Void>) method.invoke(bridge, target, payload); |
| 137 | + } catch (Exception e) { |
| 138 | + throw new RuntimeException(e); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @SuppressWarnings("unchecked") |
| 143 | + private static CompletableFuture<PluginMessage> invokeRequest( |
| 144 | + VelocityCommandBridgeImpl bridge, |
| 145 | + Platform.ServerTarget target, |
| 146 | + PluginMessage payload, |
| 147 | + Duration timeout) { |
| 148 | + try { |
| 149 | + Method method = VelocityCommandBridgeImpl.class.getDeclaredMethod( |
| 150 | + "requestPluginMessage", |
| 151 | + Platform.ServerTarget.class, |
| 152 | + PluginMessage.class, |
| 153 | + Duration.class); |
| 154 | + method.setAccessible(true); |
| 155 | + return (CompletableFuture<PluginMessage>) method.invoke(bridge, target, payload, timeout); |
| 156 | + } catch (Exception e) { |
| 157 | + throw new RuntimeException(e); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private static final class CountingEndpointServer implements EndpointServer { |
| 162 | + private final AtomicInteger sendCount = new AtomicInteger(); |
| 163 | + |
| 164 | + @Override |
| 165 | + public void start() { |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public void stop() { |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public SendOperation send(Endpoint endpoint, Envelope request) { |
| 174 | + sendCount.incrementAndGet(); |
| 175 | + return new SendOperation(endpoint, request, new ResponseAwaiter()); |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public void close(Endpoint endpoint) { |
| 180 | + } |
| 181 | + |
| 182 | + private int sendCount() { |
| 183 | + return sendCount.get(); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + private static final class TestEndpoint implements Endpoint { |
| 188 | + @Override |
| 189 | + public CompletableFuture<Void> send(Envelope env) { |
| 190 | + return CompletableFuture.completedFuture(null); |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + public boolean isOpen() { |
| 195 | + return true; |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments