nzw921rx commented on PR #11574:
URL: https://github.com/apache/seatunnel/pull/11574#issuecomment-5127046318

   Follow-up to the earlier local-test note: the final implementation has now 
been pushed in `5ae330484`.
   
   ### Final structure
   
   - Renamed `HostPortForwarder` to `ContainerTcpProxy`.
   - Moved it from `common.util` to `common.container`.
   - Added inherited `TestSuiteBase.startContainerTcpProxy(...)` methods plus a 
final `@AfterAll` cleanup hook.
   - Redis Cluster and TiDB now use the inherited lifecycle management.
   - `HbaseCluster` keeps explicit ownership because it is a composed helper 
rather than a `TestSuiteBase` subclass.
   - No production SeaTunnel code or existing assertion logic was changed.
   
   The temporary socket regression test is intentionally **not committed** to 
the E2E tree. For reproducibility, this is the final-name/package version of 
the local test source:
   
   <details>
   <summary>ContainerTcpProxyTest.java</summary>
   
   ```java
   /*
    * Licensed to the Apache Software Foundation (ASF) under one or more
    * contributor license agreements.  See the NOTICE file distributed with
    * this work for additional information regarding copyright ownership.
    * The ASF licenses this file to You under the Apache License, Version 2.0
    * (the "License"); you may not use this file except in compliance with
    * the License.  You may obtain a copy of the License at
    *
    *    http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
    */
   
   package org.apache.seatunnel.e2e.common.container;
   
   import org.junit.jupiter.api.Test;
   
   import java.io.DataInputStream;
   import java.io.IOException;
   import java.io.UncheckedIOException;
   import java.net.InetAddress;
   import java.net.InetSocketAddress;
   import java.net.ServerSocket;
   import java.net.Socket;
   import java.nio.charset.StandardCharsets;
   import java.util.Collections;
   import java.util.concurrent.ExecutorService;
   import java.util.concurrent.Executors;
   import java.util.concurrent.Future;
   import java.util.concurrent.TimeUnit;
   
   import static org.junit.jupiter.api.Assertions.assertArrayEquals;
   import static org.junit.jupiter.api.Assertions.assertNotEquals;
   
   class ContainerTcpProxyTest {
   
       @Test
       void shouldIsolateSamePublishedPortAndReleaseBindings() throws Exception 
{
           int publishedPort = findAvailablePort();
           ExecutorService targetExecutor = Executors.newFixedThreadPool(2);
   
           try (ServerSocket firstTarget = new ServerSocket(0);
                   ServerSocket secondTarget = new ServerSocket(0)) {
               byte[] firstRequest = "first 
proxy".getBytes(StandardCharsets.UTF_8);
               byte[] secondRequest = "second 
proxy".getBytes(StandardCharsets.UTF_8);
               Future<?> firstEcho =
                       startEchoServer(targetExecutor, firstTarget, 
firstRequest.length);
               Future<?> secondEcho =
                       startEchoServer(targetExecutor, secondTarget, 
secondRequest.length);
   
               String firstAddress;
               String secondAddress;
               try (ContainerTcpProxy firstProxy =
                               ContainerTcpProxy.start(
                                       Collections.singletonList(
                                               ContainerTcpProxy.PortMapping.of(
                                                       publishedPort,
                                                       "127.0.0.1",
                                                       
firstTarget.getLocalPort())));
                       ContainerTcpProxy secondProxy =
                               ContainerTcpProxy.start(
                                       Collections.singletonList(
                                               ContainerTcpProxy.PortMapping.of(
                                                       publishedPort,
                                                       "127.0.0.1",
                                                       
secondTarget.getLocalPort())))) {
                   firstAddress = firstProxy.getLoopbackAddress();
                   secondAddress = secondProxy.getLoopbackAddress();
   
                   assertNotEquals(firstAddress, secondAddress);
                   assertRoundTrip(firstAddress, publishedPort, firstRequest);
                   assertRoundTrip(secondAddress, publishedPort, secondRequest);
               }
   
               firstEcho.get(5, TimeUnit.SECONDS);
               secondEcho.get(5, TimeUnit.SECONDS);
               assertBindingReleased(firstAddress, publishedPort);
               assertBindingReleased(secondAddress, publishedPort);
           } finally {
               targetExecutor.shutdownNow();
           }
       }
   
       private static Future<?> startEchoServer(
               ExecutorService executor, ServerSocket targetServer, int 
requestLength) {
           return executor.submit(
                   () -> {
                       try (Socket socket = targetServer.accept()) {
                           byte[] request = new byte[requestLength];
                           new 
DataInputStream(socket.getInputStream()).readFully(request);
                           socket.getOutputStream().write(request);
                           socket.getOutputStream().flush();
                       } catch (IOException e) {
                           throw new UncheckedIOException(e);
                       }
                   });
       }
   
       private static void assertRoundTrip(String host, int port, byte[] 
request) throws IOException {
           try (Socket socket = new Socket(host, port)) {
               socket.getOutputStream().write(request);
               socket.getOutputStream().flush();
               byte[] response = new byte[request.length];
               new DataInputStream(socket.getInputStream()).readFully(response);
               assertArrayEquals(request, response);
           }
       }
   
       private static int findAvailablePort() throws IOException {
           try (ServerSocket socket = new ServerSocket(0)) {
               return socket.getLocalPort();
           }
       }
   
       private static void assertBindingReleased(String host, int port) throws 
IOException {
           try (ServerSocket socket = new ServerSocket()) {
               socket.bind(new InetSocketAddress(InetAddress.getByName(host), 
port));
           }
       }
   }
   ```
   
   </details>
   
   ### Validation
   
   - The equivalent local behavioral test passed in a Linux JDK 8 environment 
before the class/package-only rename: `Tests run: 1, Failures: 0, Errors: 0, 
Skipped: 0`.
   - The final renamed implementation passed targeted Spotless formatting.
   - The final renamed implementation passed targeted `test-compile` for 
`seatunnel-e2e-common`, Redis, TiDB CDC, and HBase.
   - Per the requested validation scope, the full `mvnw verify` command was not 
run.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to