@Override @SuppressWarnings("unchecked") public T build() { return (T) new SshjTool(this); } }
@Override protected SshTool newSshTool(Map<String,?> flags) { return new SshjTool(flags); }
@Override protected SshTool newUnregisteredTool(Map<String,?> flags) { return new SshjTool(flags); }
@Test(groups = {"Integration"}) public void testReturnsOnSuccessWhenRetrying() throws Exception { final AtomicInteger callCount = new AtomicInteger(); final int successOnAttempt = 2; final SshTool localtool = new SshjTool(ImmutableMap.of("sshTries", 3, "host", "localhost", "privateKeyFile", "~/.ssh/id_rsa")) { @Override protected SshAction<Session> newSessionAction() { callCount.incrementAndGet(); if (callCount.incrementAndGet() >= successOnAttempt) { return super.newSessionAction(); } else { throw new RuntimeException("Simulating ssh execution failure"); } } }; tools.add(localtool); localtool.execScript(ImmutableMap.<String,Object>of(), ImmutableList.of("true")); assertEquals(callCount.get(), successOnAttempt); }
@Test(groups = {"Integration"}) public void testGivesUpAfterMaxRetries() throws Exception { final AtomicInteger callCount = new AtomicInteger(); final SshTool localtool = new SshjTool(ImmutableMap.of("sshTries", 3, "host", "localhost", "privateKeyFile", "~/.ssh/id_rsa")) { @Override protected SshAction<Session> newSessionAction() { callCount.incrementAndGet(); throw new RuntimeException("Simulating ssh execution failure"); } }; tools.add(localtool); try { localtool.execScript(ImmutableMap.<String,Object>of(), ImmutableList.of("true")); fail(); } catch (SshException e) { if (!e.toString().contains("out of retries")) throw e; assertEquals(callCount.get(), 3); } }
@Test(groups = {"Integration"}) public void testGivesUpAfterMaxTime() throws Exception { final AtomicInteger callCount = new AtomicInteger(); final SshTool localtool = new SshjTool(ImmutableMap.of("sshTriesTimeout", 1000, "host", "localhost", "privateKeyFile", "~/.ssh/id_rsa")) { @Override protected SshAction<Session> newSessionAction() { callCount.incrementAndGet(); try { Thread.sleep(600); } catch (InterruptedException e) { throw Exceptions.propagate(e); } throw new RuntimeException("Simulating ssh execution failure"); } }; tools.add(localtool); try { localtool.execScript(ImmutableMap.<String,Object>of(), ImmutableList.of("true")); fail(); } catch (RuntimeTimeoutException e) { if (!e.toString().contains("out of time")) throw e; assertEquals(callCount.get(), 2); } }