HostPorts { int nextAvailableBaseport(int numPorts) { int range = 0; int port = BASE_PORT; for (; port < BASE_PORT + MAX_PORTS && (range < numPorts); port++) { if (!isFree(port)) { range = 0; continue; } range++; } return range == numPorts ? port - range : 0; } HostPorts(String hostname); Optional<NetworkPorts> networkPorts(); void addNetworkPorts(NetworkPorts ports); void useLogger(DeployLogger logger); int requireNetworkPort(int port, NetworkPortRequestor service, String suffix); int wantNetworkPort(int port, NetworkPortRequestor service, String suffix); int wantNetworkPort(int port, NetworkPortRequestor service, String suffix, boolean forceRequired); int allocateNetworkPort(NetworkPortRequestor service, String suffix); void flushPortReservations(); @Override String toString(); final static int BASE_PORT; }
@Test public void next_available_baseport_is_BASE_PORT_when_no_ports_have_been_reserved() { HostPorts host = new HostPorts("myhostname"); assertThat(host.nextAvailableBaseport(1), is(HostPorts.BASE_PORT)); }