|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.seata.core.rpc.netty; |
| 18 | + |
| 19 | +import org.apache.seata.common.ConfigurationKeys; |
| 20 | +import org.apache.seata.common.ConfigurationTestHelper; |
| 21 | +import org.apache.seata.common.XID; |
| 22 | +import org.apache.seata.common.util.NetUtil; |
| 23 | +import org.apache.seata.common.util.UUIDGenerator; |
| 24 | +import org.apache.seata.server.coordinator.DefaultCoordinator; |
| 25 | +import org.apache.seata.server.session.SessionHolder; |
| 26 | +import org.junit.jupiter.api.AfterEach; |
| 27 | +import org.slf4j.Logger; |
| 28 | +import org.slf4j.LoggerFactory; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | +import java.lang.management.ManagementFactory; |
| 32 | +import java.net.ServerSocket; |
| 33 | +import java.util.concurrent.LinkedBlockingQueue; |
| 34 | +import java.util.concurrent.ThreadPoolExecutor; |
| 35 | +import java.util.concurrent.TimeUnit; |
| 36 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 37 | + |
| 38 | +/** |
| 39 | + * Base test class for Netty client tests to eliminate code duplication |
| 40 | + */ |
| 41 | +public abstract class BaseNettyClientTest { |
| 42 | + |
| 43 | + private static final Logger LOGGER = LoggerFactory.getLogger(BaseNettyClientTest.class); |
| 44 | + |
| 45 | + /** |
| 46 | + * Get a dynamic available port |
| 47 | + */ |
| 48 | + protected static int getDynamicPort() throws IOException { |
| 49 | + try (ServerSocket serverSocket = new ServerSocket(0)) { |
| 50 | + return serverSocket.getLocalPort(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Initialize message executor thread pool |
| 56 | + */ |
| 57 | + protected static ThreadPoolExecutor initMessageExecutor() { |
| 58 | + return new ThreadPoolExecutor( |
| 59 | + 5, |
| 60 | + 5, |
| 61 | + 500, |
| 62 | + TimeUnit.SECONDS, |
| 63 | + new LinkedBlockingQueue<>(20000), |
| 64 | + new ThreadPoolExecutor.CallerRunsPolicy()); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Start Seata server with dynamic port and intelligent waiting |
| 69 | + */ |
| 70 | + protected ServerInstance startServer(int port) throws Exception { |
| 71 | + ThreadPoolExecutor workingThreads = initMessageExecutor(); |
| 72 | + NettyServerConfig serverConfig = new NettyServerConfig(); |
| 73 | + serverConfig.setServerListenPort(port); |
| 74 | + NettyRemotingServer nettyRemotingServer = new NettyRemotingServer(workingThreads, serverConfig); |
| 75 | + |
| 76 | + AtomicBoolean serverStatus = new AtomicBoolean(); |
| 77 | + Thread thread = new Thread(() -> { |
| 78 | + try { |
| 79 | + SessionHolder.init(null); |
| 80 | + nettyRemotingServer.setHandler(DefaultCoordinator.getInstance(nettyRemotingServer)); |
| 81 | + // set registry |
| 82 | + XID.setIpAddress(NetUtil.getLocalIp()); |
| 83 | + XID.setPort(port); |
| 84 | + // init snowflake for transactionId, branchId |
| 85 | + UUIDGenerator.init(1L); |
| 86 | + System.out.println( |
| 87 | + "pid info: " + ManagementFactory.getRuntimeMXBean().getName()); |
| 88 | + nettyRemotingServer.init(); |
| 89 | + serverStatus.set(true); |
| 90 | + } catch (Throwable t) { |
| 91 | + serverStatus.set(false); |
| 92 | + LOGGER.error("The seata-server failed to start", t); |
| 93 | + } |
| 94 | + }); |
| 95 | + thread.start(); |
| 96 | + |
| 97 | + // Wait for the seata-server to start with intelligent waiting |
| 98 | + long start = System.nanoTime(); |
| 99 | + long maxWaitNanoTime = 10 * 1000 * 1000 * 1000L; // 10s |
| 100 | + while (System.nanoTime() - start < maxWaitNanoTime) { |
| 101 | + Thread.sleep(100); |
| 102 | + if (serverStatus.get()) { |
| 103 | + break; |
| 104 | + } |
| 105 | + } |
| 106 | + if (!serverStatus.get()) { |
| 107 | + throw new RuntimeException("Waiting for a while, but the seata-server did not start successfully."); |
| 108 | + } |
| 109 | + |
| 110 | + return new ServerInstance(nettyRemotingServer, port); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Start server with simpler logic (for some tests that don't need intelligent waiting) |
| 115 | + */ |
| 116 | + protected ServerInstance startServerSimple(int port) throws Exception { |
| 117 | + ThreadPoolExecutor workingThreads = initMessageExecutor(); |
| 118 | + NettyServerConfig serverConfig = new NettyServerConfig(); |
| 119 | + serverConfig.setServerListenPort(port); |
| 120 | + NettyRemotingServer nettyRemotingServer = new NettyRemotingServer(workingThreads, serverConfig); |
| 121 | + |
| 122 | + new Thread(() -> { |
| 123 | + SessionHolder.init(null); |
| 124 | + nettyRemotingServer.setHandler(DefaultCoordinator.getInstance(nettyRemotingServer)); |
| 125 | + // set registry |
| 126 | + XID.setIpAddress(NetUtil.getLocalIp()); |
| 127 | + XID.setPort(port); |
| 128 | + // init snowflake for transactionId, branchId |
| 129 | + UUIDGenerator.init(1L); |
| 130 | + nettyRemotingServer.init(); |
| 131 | + }) |
| 132 | + .start(); |
| 133 | + |
| 134 | + Thread.sleep(3000); // Simple wait |
| 135 | + return new ServerInstance(nettyRemotingServer, port); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Configure client to use the specified port |
| 140 | + */ |
| 141 | + protected void configureClient(int port) { |
| 142 | + ConfigurationTestHelper.putConfig("service.default.grouplist", "127.0.0.1:" + port); |
| 143 | + ConfigurationTestHelper.putConfig(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL, String.valueOf(port)); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Clean up client configuration |
| 148 | + */ |
| 149 | + protected void cleanupClientConfig() { |
| 150 | + ConfigurationTestHelper.removeConfig("service.default.grouplist"); |
| 151 | + ConfigurationTestHelper.removeConfig(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Server instance wrapper to hold server and port information |
| 156 | + */ |
| 157 | + protected static class ServerInstance { |
| 158 | + private final NettyRemotingServer server; |
| 159 | + private final int port; |
| 160 | + |
| 161 | + public ServerInstance(NettyRemotingServer server, int port) { |
| 162 | + this.server = server; |
| 163 | + this.port = port; |
| 164 | + } |
| 165 | + |
| 166 | + public NettyRemotingServer getServer() { |
| 167 | + return server; |
| 168 | + } |
| 169 | + |
| 170 | + public int getPort() { |
| 171 | + return port; |
| 172 | + } |
| 173 | + |
| 174 | + public String getAddress() { |
| 175 | + return "127.0.0.1:" + port; |
| 176 | + } |
| 177 | + |
| 178 | + public void destroy() { |
| 179 | + if (server != null) { |
| 180 | + server.destroy(); |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Clean up configuration after each test |
| 187 | + */ |
| 188 | + @AfterEach |
| 189 | + public void cleanupAfterTest() { |
| 190 | + cleanupClientConfig(); |
| 191 | + } |
| 192 | +} |
0 commit comments