diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanProxyBuilder.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanProxyBuilder.java index 8d25d21d87..9f0a1de4a8 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanProxyBuilder.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanProxyBuilder.java @@ -15,16 +15,21 @@ *******************************************************************************/ package org.eclipse.leshan.integration.tests.util; +import static org.eclipse.leshan.core.util.TestToolBox.uriHandler; + import java.net.InetSocketAddress; +import java.util.List; +import java.util.stream.Collectors; -import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; public class LeshanProxyBuilder { public static ReverseProxy givenReverseProxyFor(LeshanTestServer server, Protocol protocol) { - EndpointUri serverEndpointUri = server.getEndpoint(protocol).getURI(); - return new ReverseProxy(new InetSocketAddress("localhost", 0), - new InetSocketAddress(serverEndpointUri.getHost(), serverEndpointUri.getPort())); + List endpoints = server.getEndpoints().stream() // + .map(e -> uriHandler.getSocketAddr(e.getURI())) // + .collect(Collectors.toList()); + + return new ReverseProxy(new InetSocketAddress("localhost", 0), endpoints); } } diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/ReverseProxy.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/ReverseProxy.java index cb561201b3..4818ea2dbb 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/ReverseProxy.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/ReverseProxy.java @@ -21,6 +21,9 @@ import java.nio.channels.DatagramChannel; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import java.util.Set; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -58,9 +61,12 @@ public class ReverseProxy { private static final int BUFFER_SIZE = 2048; private final InetSocketAddress clientSideProxyAddress; - private final InetSocketAddress serverAddress; + private InetSocketAddress serverAddress; private InetSocketAddress clientAddress; + private final List possibleServerAddress; + private volatile int currentServerAddressIndex; + private DatagramChannel clientToProxyChannel; private DatagramChannel proxyToServerChannel; private Selector selector; @@ -69,10 +75,17 @@ public class ReverseProxy { private volatile boolean running = false; // true if reserver proxy is running private volatile boolean stop = false; // true if we asked to stop reserve proxy private volatile boolean changeServerSideProxyAddress = false; // true if we ask to change server side proxy address + private volatile int askedServerAddressIndex; // value of the requested server address index public ReverseProxy(InetSocketAddress clientSideProxyAddress, InetSocketAddress serverAddress) { + this(clientSideProxyAddress, Collections.singletonList(serverAddress)); + } + + public ReverseProxy(InetSocketAddress clientSideProxyAddress, List possibleServerAddress) { this.clientSideProxyAddress = clientSideProxyAddress; - this.serverAddress = serverAddress; + this.possibleServerAddress = Collections.unmodifiableList(new ArrayList<>(possibleServerAddress)); + this.currentServerAddressIndex = 0; + this.serverAddress = possibleServerAddress.get(currentServerAddressIndex); } public void start() { @@ -111,6 +124,12 @@ public void start() { if (changeServerSideProxyAddress) { reassignServerSideProxyAddress(); } + // change server address if needed + int addr = askedServerAddressIndex; + if (addr != currentServerAddressIndex) { + serverAddress = possibleServerAddress.get(addr); + currentServerAddressIndex = addr; + } } // proxy is stopped LOGGER.trace("Stopping Reverse Proxy"); @@ -170,8 +189,8 @@ private void handlerServerPackets() throws IOException { if (sourceAddress == null) { return; } - if (!sourceAddress.equals(serverAddress)) { - logAndRaiseException(String.format("We should only receive data from server %s", serverAddress)); + if (!possibleServerAddress.contains(sourceAddress)) { + logAndRaiseException(String.format("We should only receive data from server %s", possibleServerAddress)); } if (clientAddress == null) { logAndRaiseException("Client should send data first before sever send data"); @@ -236,6 +255,27 @@ public InetSocketAddress getServerAddress() { return serverAddress; } + /** + * @return address of server to proxified + */ + public void useNextServerAddress() { + if (possibleServerAddress.size() == 1) { + throw new IllegalStateException("there is only 1 possible server address"); + } + + askedServerAddressIndex = (askedServerAddressIndex + 1) % possibleServerAddress.size(); + + selector.wakeup(); + // Wait address effectively changed (we wait 10x100 ms max) + for (int i = 0; i < 10 && askedServerAddressIndex != currentServerAddressIndex; i++) { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + LOGGER.debug("Change Server Side Proxy Address was interrupted", e); + } + } + } + /** * @return will assign a new server side proxy address which can be used to simulate client address change. */