public void testRetriesLoggedAtInfoWithCount() throws Exception {
    SSHClientConnection mockConnection = createMock(SSHClientConnection.class);
    net.schmizz.sshj.SSHClient mockClient = createMock(net.schmizz.sshj.SSHClient.class);

    mockConnection.clear();
    expectLastCall();
    mockConnection.create();
    expectLastCall().andThrow(new ConnectionException("test1"));
    mockConnection.clear();
    expectLastCall();
    // currently does two clears, one on failure (above) and one on next iteration (below)
    mockConnection.clear();
    expectLastCall();
    mockConnection.create();
    expectLastCall().andReturn(mockClient);
    replay(mockConnection);
    replay(mockClient);

    ssh.sshClientConnection = mockConnection;
    BufferLogger logcheck = new BufferLogger(ssh.getClass().getCanonicalName());
    ssh.logger = logcheck;
    logcheck.setLevel(Level.INFO);

    ssh.connect();

    Assert.assertEquals(ssh.sshClientConnection, mockConnection);
    verify(mockConnection);
    verify(mockClient);
    Record r = logcheck.assertLogContains("attempt 1 of 5");
    logcheck.assertLogDoesntContain("attempt 2 of 5");
    Assert.assertEquals(Level.INFO, r.getLevel());
  }
Example #2
0
 protected <T, C extends Connection<T>> T acquire(C connection) {
   String errorMessage = String.format("(%s) error acquiring %s", toString(), connection);
   for (int i = 0; i < sshRetries; i++) {
     try {
       connection.clear();
       logger.debug(">> (%s) acquiring %s", toString(), connection);
       T returnVal = connection.create();
       logger.debug("<< (%s) acquired %s", toString(), returnVal);
       return returnVal;
     } catch (Exception from) {
       try {
         connection.clear();
       } catch (Exception e1) {
         logger.warn(from, "<< (%s) error closing connection", toString());
       }
       if (i + 1 == sshRetries) {
         logger.error(from, "<< " + errorMessage + ": out of retries %d", sshRetries);
         throw propagate(from, errorMessage);
       } else if (Throwables2.getFirstThrowableOfType(from, IllegalStateException.class) != null) {
         logger.warn(from, "<< " + errorMessage + ": " + from.getMessage());
         disconnect();
         backoffForAttempt(i + 1, errorMessage + ": " + from.getMessage());
         connect();
         continue;
       } else if (shouldRetry(from)) {
         logger.warn(from, "<< " + errorMessage + ": " + from.getMessage());
         backoffForAttempt(i + 1, errorMessage + ": " + from.getMessage());
         continue;
       } else {
         logger.error(from, "<< " + errorMessage + ": exception not retryable");
         throw propagate(from, errorMessage);
       }
     }
   }
   assert false : "should not reach here";
   return null;
 }