### Question:
Learner { protected void connectToLeader(MultipleAddresses multiAddr, String hostname) throws IOException { this.leaderAddr = multiAddr; Set<InetSocketAddress> addresses; if (self.isMultiAddressReachabilityCheckEnabled()) { addresses = multiAddr.getAllReachableAddressesOrAll(); } else { addresses = multiAddr.getAllAddresses(); } ExecutorService executor = Executors.newFixedThreadPool(addresses.size()); CountDownLatch latch = new CountDownLatch(addresses.size()); AtomicReference<Socket> socket = new AtomicReference<>(null); addresses.stream().map(address -> new LeaderConnector(address, socket, latch)).forEach(executor::submit); try { latch.await(); } catch (InterruptedException e) { LOG.warn("Interrupted while trying to connect to Leader", e); } finally { executor.shutdown(); try { if (!executor.awaitTermination(1, TimeUnit.SECONDS)) { LOG.error("not all the LeaderConnector terminated properly"); } } catch (InterruptedException ie) { LOG.error("Interrupted while terminating LeaderConnector executor.", ie); } } if (socket.get() == null) { throw new IOException("Failed connect to " + multiAddr); } else { sock = socket.get(); sockBeingClosed.set(false); } self.authLearner.authenticate(sock, hostname); leaderIs = BinaryInputArchive.getArchive(new BufferedInputStream(sock.getInputStream())); bufferedOutput = new BufferedOutputStream(sock.getOutputStream()); leaderOs = BinaryOutputArchive.getArchive(bufferedOutput); if (asyncSending) { startSendingThread(); } }  Socket getSocket(); int getPendingRevalidationsCount(); void shutdown(); static final String LEARNER_ASYNC_SENDING; static final String LEARNER_CLOSE_SOCKET_ASYNC; static final boolean closeSocketAsync; }### Answer:
@Test public void connectionRetryTimeoutTest() throws Exception { assertThrows(IOException.class, () -> { Learner learner = new TestLearner(); learner.self = new QuorumPeer(); learner.self.setTickTime(2000); learner.self.setInitLimit(5); learner.self.setSyncLimit(2); InetSocketAddress addr = new InetSocketAddress(1111); learner.connectToLeader(new MultipleAddresses(addr), ""); }); }
@Test public void shouldTryMultipleAddresses() throws Exception { System.setProperty(QuorumPeer.CONFIG_KEY_MULTI_ADDRESS_ENABLED, "true"); TestLearner learner = new TestLearner(); learner.self = new QuorumPeer(); learner.self.setTickTime(2000); learner.self.setInitLimit(5); learner.self.setSyncLimit(2); InetSocketAddress addrA = new InetSocketAddress(1111); InetSocketAddress addrB = new InetSocketAddress(2222); InetSocketAddress addrC = new InetSocketAddress(3333); InetSocketAddress addrD = new InetSocketAddress(4444); learner.setPassConnectAttempt(100); try { learner.connectToLeader(new MultipleAddresses(asList(addrA, addrB, addrC, addrD)), ""); fail("should have thrown IOException!"); } catch (IOException e) { assertEquals(4 * 5, learner.getSockConnectAttempt()); } }