### Question:
MultipleAddresses { public InetSocketAddress getReachableAddress() throws NoRouteToHostException { return addresses.parallelStream() .filter(this::checkIfAddressIsReachable) .findAny() .orElseThrow(() -> new NoRouteToHostException("No valid address among " + addresses)); } MultipleAddresses();  MultipleAddresses(Collection<InetSocketAddress> addresses);  MultipleAddresses(InetSocketAddress address);  MultipleAddresses(Collection<InetSocketAddress> addresses, Duration timeout); boolean isEmpty(); Set<InetSocketAddress> getAllAddresses(); Set<InetSocketAddress> getWildcardAddresses(); List<Integer> getAllPorts(); List<String> getAllHostStrings(); void addAddress(InetSocketAddress address); InetSocketAddress getReachableAddress(); Set<InetSocketAddress> getAllReachableAddresses(); Set<InetSocketAddress> getAllReachableAddressesOrAll(); InetSocketAddress getReachableOrOne(); void recreateSocketAddresses(); InetSocketAddress getOne(); int size(); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); static final Duration DEFAULT_TIMEOUT; }### Answer:
@Test public void testGetValidAddress() throws NoRouteToHostException { List<InetSocketAddress> addresses = getAddressList(); MultipleAddresses multipleAddresses = new MultipleAddresses(addresses); assertTrue(addresses.contains(multipleAddresses.getReachableAddress())); }
@Test public void testGetValidAddressWithNotValid() { assertThrows(NoRouteToHostException.class, () -> { MultipleAddresses multipleAddresses = new MultipleAddresses(new InetSocketAddress("10.0.0.1", 22)); multipleAddresses.getReachableAddress(); }); }
@Test public void testAlwaysGetReachableAddress() throws Exception{ InetSocketAddress reachableHost = new InetSocketAddress("127.0.0.1", 1234); InetSocketAddress unreachableHost1 = new InetSocketAddress("unreachable1.address.zookeeper.apache.com", 1234); InetSocketAddress unreachableHost2 = new InetSocketAddress("unreachable2.address.zookeeper.apache.com", 1234); InetSocketAddress unreachableHost3 = new InetSocketAddress("unreachable3.address.zookeeper.apache.com", 1234); MultipleAddresses multipleAddresses = new MultipleAddresses( Arrays.asList(unreachableHost1, unreachableHost2, unreachableHost3, reachableHost)); for (int i = 0; i < 10; i++) { assertEquals(reachableHost, multipleAddresses.getReachableAddress()); } }