SocketInternetObservingStrategy implements InternetObservingStrategy { protected boolean isConnected(final String host, final int port, final int timeoutInMs, final ErrorHandler errorHandler) { final Socket socket = new Socket(); return isConnected(socket, host, port, timeoutInMs, errorHandler); }  @Override String getDefaultPingHost(); @Override Observable<Boolean> observeInternetConnectivity(final int initialIntervalInMs,
      final int intervalInMs, final String host, final int port, final int timeoutInMs,
      final int httpResponse, final ErrorHandler errorHandler); @Override Single<Boolean> checkInternetConnectivity(final String host, final int port,
      final int timeoutInMs, final int httpResponse, final ErrorHandler errorHandler);  }
@Test public void shouldNotBeConnectedToTheInternetWhenSocketThrowsAnExceptionOnConnect() throws IOException { final InetSocketAddress address = new InetSocketAddress(getHost(), PORT); doThrow(new IOException()).when(socket).connect(address, TIMEOUT_IN_MS); final boolean isConnected = strategy.isConnected(socket, getHost(), PORT, TIMEOUT_IN_MS, errorHandler); assertThat(isConnected).isFalse(); }
@Test public void shouldHandleAnExceptionThrownDuringClosingTheSocket() throws IOException { final String errorMsg = "Could not close the socket"; final IOException givenException = new IOException(errorMsg); doThrow(givenException).when(socket).close(); strategy.isConnected(socket, getHost(), PORT, TIMEOUT_IN_MS, errorHandler); verify(errorHandler, times(1)).handleError(givenException, errorMsg); }