### Question:
ConsoleConnectionFactory { public ConsoleConnection connect(String host, int port) throws ConsoleConnectionException { try { final ClientSender sender = ClientSender.connect( new Connector(host, port, ConnectionType.CONSOLE_CLIENT), null); final Runnable keepAlive = new Runnable() { @Override public void run() { try { sender.sendKeepAlive(); } catch (CommunicationException e) { throw new RuntimeException(e); } } }; m_executor.scheduleWithFixedDelay(keepAlive, 0, 1, TimeUnit.SECONDS); return new ConsoleConnectionImplementation(sender); } catch (CommunicationException e) { throw new ConsoleConnectionException("Failed to connect", e); } } ConsoleConnectionFactory(); protected  ConsoleConnectionFactory(ScheduledExecutorService executor); ConsoleConnection connect(String host, int port);  }### Answer:
@Test public void testConnect() throws Exception { final SocketAcceptorThread socketAcceptor = SocketAcceptorThread.create(); final ConsoleConnection consoleConnection = new ConsoleConnectionFactory().connect(socketAcceptor.getHostName(), socketAcceptor.getPort()); assertNotNull(consoleConnection); socketAcceptor.close(); final InputStream socketInput = socketAcceptor.getAcceptedSocket().getInputStream(); final ObjectInputStream objectInputStream = new ObjectInputStream(socketInput); assertEquals(ConnectionType.CONSOLE_CLIENT, objectInputStream.readObject()); }
@Test(expected = ConsoleConnectionException.class) public void testConnectBadAcceptor() throws Exception { final SocketAcceptorThread socketAcceptor = SocketAcceptorThread.create(); final ConsoleConnectionFactory consoleConnectionFactory = new ConsoleConnectionFactory(); final ConsoleConnection consoleConnection = consoleConnectionFactory.connect( socketAcceptor.getHostName(), socketAcceptor.getPort()); assertNotNull(consoleConnection); socketAcceptor.close(); consoleConnectionFactory.connect(socketAcceptor.getHostName(), socketAcceptor.getPort()); }
@Test public void testKeepAlive() throws Exception { final ConsoleConnectionFactory consoleConnection = new ConsoleConnectionFactory(m_scheduler); final SocketAcceptorThread socketAcceptor = SocketAcceptorThread.create(); consoleConnection.connect(socketAcceptor.getHostName(), socketAcceptor.getPort()); socketAcceptor.close(); verify(m_scheduler).scheduleWithFixedDelay(m_runnableCaptor.capture(), isA(Long.class), isA(Long.class), isA(TimeUnit.class)); final InputStream socketInput = socketAcceptor.getAcceptedSocket().getInputStream(); final ObjectInputStream objectInputStream = new ObjectInputStream(socketInput); assertEquals(ConnectionType.CONSOLE_CLIENT, objectInputStream.readObject()); assertNull(objectInputStream.readObject()); assertEquals(0, socketInput.available()); final Runnable keepAlive = m_runnableCaptor.getValue(); keepAlive.run(); final ObjectInputStream objectInputStream2 = new ObjectInputStream(socketInput); assertTrue(objectInputStream2.readObject() instanceof KeepAliveMessage); socketAcceptor.getAcceptedSocket().close(); try { keepAlive.run(); keepAlive.run(); fail("Expected RuntimeException"); } catch (RuntimeException e) { assertTrue(e.getCause() instanceof CommunicationException); } }