GatewayReceiverImpl implements GatewayReceiver { @Override public void start() { if (receiverServer == null) { receiverServer = cache.addGatewayReceiverServer(this); } if (receiverServer.isRunning()) { logger.warn("Gateway Receiver is already running"); return; } int loopStartPort = getPortToStart(); int port = loopStartPort; while (!tryToStart(port)) { if (port == endPort && startPort != endPort) { port = startPort; } else { port++; } if (port == loopStartPort || port > endPort) { throw new GatewayReceiverException("No available free port found in the given range (" + startPort + "-" + endPort + ")"); } } logger.info("The GatewayReceiver started on port : {}", this.port); InternalDistributedSystem system = cache.getInternalDistributedSystem(); system.handleResourceEvent(ResourceEvent.GATEWAYRECEIVER_START, this); } GatewayReceiverImpl(final InternalCache cache, final int startPort, final int endPort,
      final int maximumTimeBetweenPings, final int socketBufferSize, final String bindAddress,
      final List<GatewayTransportFilter> gatewayTransportFilters, final String hostnameForSenders,
      final boolean manualStart); @VisibleForTesting  GatewayReceiverImpl(final InternalCache cache, final int startPort, final int endPort,
      final int maximumTimeBetweenPings, final int socketBufferSize, final String bindAddress,
      final List<GatewayTransportFilter> gatewayTransportFilters, final String hostnameForSenders,
      final boolean manualStart, final boolean isPortAvailableResult,
      final int getRandomAvailablePortInRangeResult); private  GatewayReceiverImpl(final InternalCache cache, final int startPort, final int endPort,
      final int maximumTimeBetweenPings, final int socketBufferSize, final String bindAddress,
      final List<GatewayTransportFilter> gatewayTransportFilters, final String hostnameForSenders,
      final boolean manualStart, final Function<Integer, Boolean> isPortAvailableFunction,
      final Function<PortRange, Integer> getRandomAvailablePortInRangeFunction); @Override String getHostnameForSenders(); @Override String getHost(); @Override List<GatewayTransportFilter> getGatewayTransportFilters(); @Override int getMaximumTimeBetweenPings(); @Override int getPort(); @Override int getStartPort(); @Override int getEndPort(); @Override int getSocketBufferSize(); @Override boolean isManualStart(); @Override CacheServer getServer(); @Override void start(); @Override void stop(); @Override void destroy(); @Override String getBindAddress(); @Override boolean isRunning(); @Override String toString();  }
@Test public void startShouldThrowGatewayReceiverExceptionIfIOExceptionIsThrown() throws IOException { doThrow(new SocketException("Address already in use")).when(receiverServer).start(); GatewayReceiverImpl gateway = new GatewayReceiverImpl(cache, 2000, 2000, 5, 100, null, null, null, true, true, 2000); Throwable thrown = catchThrowable(() -> gateway.start()); assertThat(thrown) .isInstanceOf(GatewayReceiverException.class) .hasMessageContaining("No available free port found in the given range"); verify(receiverServer).start(); }
@Test public void startShouldTryTwoPortsInPortRangeIfIOExceptionIsThrown() throws IOException { doThrow(new SocketException("Address already in use")).when(receiverServer).start(); GatewayReceiverImpl gateway = new GatewayReceiverImpl(cache, 2000, 2001, 5, 100, null, null, null, true, true, 2000); Throwable thrown = catchThrowable(() -> gateway.start()); assertThat(thrown) .isInstanceOf(GatewayReceiverException.class) .hasMessageContaining("No available free port found in the given range"); verify(receiverServer, times(2)).start(); }