HostPorts { List<Integer> allocatePorts(NetworkPortRequestor service, int wantedPort) { PortAllocBridge allocator = new PortAllocBridge(this, service); service.allocatePorts(wantedPort, allocator); return allocator.result(); } 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 port_above_vespas_port_range_can_be_reserved() { HostPorts host = new HostPorts("myhostname"); MockRoot root = new MockRoot(); host.allocatePorts(new TestService(root, 1), HostPorts.BASE_PORT + HostPorts.MAX_PORTS + 1); }
@Test(expected = RuntimeException.class) public void allocating_same_port_throws_exception() { HostPorts host = new HostPorts("myhostname"); MockRoot root = new MockRoot(); TestService service1 = new TestService(root, 1); TestService service2 = new TestService(root, 1); host.allocatePorts(service1, HostPorts.BASE_PORT); host.allocatePorts(service2, HostPorts.BASE_PORT); }
@Test(expected = RuntimeException.class) public void allocating_overlapping_ports_throws_exception() { HostPorts host = new HostPorts("myhostname"); MockRoot root = new MockRoot(); TestService service2 = new TestService(root, 2); TestService service1 = new TestService(root, 1); host.allocatePorts(service2, HostPorts.BASE_PORT); host.allocatePorts(service1, HostPorts.BASE_PORT + 1); }