Files
usdt/ruoyi-system/src/test/java/com/ruoyi/system/service/TronGrpcConnectivityTest.java
T
2026-11-15 09:54:17 -05:00

38 lines
1.4 KiB
Java

package com.ruoyi.system.service;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import org.junit.Assume;
import org.junit.Test;
import org.tron.trident.api.GrpcAPI;
import org.tron.trident.api.WalletGrpc;
import org.tron.trident.proto.Chain;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertTrue;
/** 真实网络检查,默认跳过;使用 -Dtron.grpc.integration=true 显式启用。 */
public class TronGrpcConnectivityTest {
@Test
public void shouldQueryLatestBlockFromAllNetworks() throws Exception {
Assume.assumeTrue(Boolean.getBoolean("tron.grpc.integration"));
assertLatestBlock("grpc.trongrid.io");
assertLatestBlock("grpc.nile.trongrid.io");
assertLatestBlock("grpc.shasta.trongrid.io");
}
private void assertLatestBlock(String host) throws Exception {
ManagedChannel channel = ManagedChannelBuilder.forAddress(host, 50051).usePlaintext().build();
try {
Chain.Block block = WalletGrpc.newBlockingStub(channel)
.withDeadlineAfter(60, TimeUnit.SECONDS)
.getNowBlock(GrpcAPI.EmptyMessage.getDefaultInstance());
assertTrue(host + " 未返回有效区块", block.hasBlockHeader()
&& block.getBlockHeader().getRawData().getNumber() > 0L);
} finally {
channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS);
}
}
}