Files
MoonBridge/testing/smoke/ChannelJava8Probe.java
layue13andClaude Opus 5.5 789648c82f refactor: 仓库根目录按用途分组
- proxy/:core、plugin-api、luckperms 及其上游子模块 vendor/luckperms
- messaging/、backend/ 保持在根目录(messaging 是代理与后端共用的 API)
- build-logic/(原 proxy-build-logic)
- testing/:smoke/ 与 benchmarks/,不进入发行物

Gradle 项目名不变(settings.gradle.kts 映射目录),任务路径与 Maven 坐标照旧。
脚本的仓库根定位、文档与记录中的链接同步更新。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 14:34:01 +08:00

69 lines
4.2 KiB
Java

import dev.moonbridge.backendchannel.BackendChannelClient;
import dev.moonbridge.messaging.Endpoint;
import dev.moonbridge.messaging.Message;
import dev.moonbridge.messaging.MessageChannel;
import dev.moonbridge.messaging.PublishResult;
import dev.moonbridge.messaging.SendResult;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
/** Run with an actual Java 8 VM against ChannelRouterFixture; uses no players. */
public final class ChannelJava8Probe {
public static void main(String[] args) throws Exception {
if (!System.getProperty("java.specification.version").equals("1.8")) {
throw new IllegalStateException("This probe must run on Java 8");
}
int port = Integer.parseInt(args[0]);
byte[] bytes = "java8-payload".getBytes(StandardCharsets.UTF_8);
try (BackendChannelClient a = client(port, "a", 25565);
BackendChannelClient b = client(port, "b", 25566)) {
require(a.awaitRegistered(8, TimeUnit.SECONDS), "backend a did not register");
require(b.awaitRegistered(8, TimeUnit.SECONDS), "backend b did not register");
MessageChannel sender = a.messaging("probe-a").channel("probe:channel");
MessageChannel receiver = b.messaging("probe-b").channel("probe:channel");
AtomicReference<Message> received = new AtomicReference<Message>();
receiver.onRequest(message -> {
received.set(message);
return CompletableFuture.completedFuture(message.payload());
});
Message proxyReply = sender.request(Endpoint.proxy(), bytes).toCompletableFuture().get(5, TimeUnit.SECONDS);
require(Endpoint.proxy().equals(proxyReply.source()), "wrong proxy reply source");
Message reply = sender.request(Endpoint.backend("b"), bytes).toCompletableFuture().get(5, TimeUnit.SECONDS);
require(Endpoint.backend("a").equals(received.get().source()), "wrong forwarded source");
require(Endpoint.backend("b").equals(reply.source()), "wrong reply source");
require(received.get().id().equals(reply.replyTo()), "reply correlation changed in transit");
require(!reply.id().equals(reply.replyTo()), "reply lacks an independent ID");
CountDownLatch notifications = new CountDownLatch(3);
AtomicReference<UUID> publishedId = new AtomicReference<UUID>();
sender.subscribe(message -> notifications.countDown());
receiver.subscribe(message -> { publishedId.set(message.id()); notifications.countDown(); });
b.messaging("probe-second-plugin").channel("probe:channel")
.subscribe(message -> notifications.countDown());
PublishResult publication = sender.publish(bytes).toCompletableFuture().get(5, TimeUnit.SECONDS);
require(publication.results().size() == 3, "publication missed a node");
require(publication.results().get(Endpoint.backend("a")) == SendResult.ACCEPTED, "sender subscriber missing");
require(publication.results().get(Endpoint.backend("b")) == SendResult.ACCEPTED, "backend subscribers missing");
require(notifications.await(5, TimeUnit.SECONDS), "publication missed a plugin");
require(publication.messageId().equals(publishedId.get()), "publication ID changed in transit");
System.out.println("JAVA8_CHANNEL_PASS java=" + System.getProperty("java.version")
+ " requests=2 backendSubscribers=3 endToEndIdentity=true");
}
}
private static BackendChannelClient client(int port, String name, int gamePort) {
return new BackendChannelClient("127.0.0.1", port, "probe-" + name, name,
"tcp://127.0.0.1:" + gamePort, UUID.randomUUID().toString(), "primary",
"isolated-channel-probe-secret-at-least-32-bytes".getBytes(StandardCharsets.UTF_8));
}
private static void require(boolean value, String message) {
if (!value) throw new AssertionError(message);
}
}