SshConnectionCache implements SshUtilities { protected int execute(Connection connection, String command, OutputStream stdout) throws IOException { int exitCode = RemoteProcess.UNDEFINED_EXIT_STATUS; RemoteProcess proc = null; try { proc = invoke(connection, command, stdout, null); long deadline = System.currentTimeMillis() + INPUTSTREAM_TIMEOUT_MILLIS; do { try { exitCode = proc.waitFor(INPUTSTREAM_TIMEOUT_MILLIS); if (exitCode != RemoteProcess.UNDEFINED_EXIT_STATUS) { break; } } catch (InterruptedException e) { _log.info("VHM: unexpected interruption while waiting for remote command to complete"); } } while (deadline > System.currentTimeMillis()); if (stdout != null) { try { stdout.flush(); } catch (IOException e) { } } } catch (Exception e) { String msg = "VHM: "+connection.hostname+" - exception executing command on remote host"; _log.log(Level.WARNING, msg+" - "+e.getMessage()); _log.log(Level.INFO, msg, e); } finally { if (proc != null) { proc.cleanup(); } } _log.log(Level.FINE, "Exit status from exec is: " + exitCode); return exitCode; } SshConnectionCache(int capacity); @Override int copy(String remote, int port, Credentials credentials, byte[] data, String remoteDirectory, String remoteName, String permissions); @Override int execute(String remote, int port, Credentials credentials, String command, OutputStream stdout); RemoteProcess invoke(Connection connection, String command, OutputStream stdout, InputStream stdin); @Override RemoteProcess invoke(String remote, int port, Credentials credentials, String command, OutputStream stdout);  }
@Ignore @Test public void evictedConnectionDoesntCloseWhileInUse() throws IOException { int cacheCapacity = 1; cache = new TestSshConnectionCache(cacheCapacity); assertEquals(0, cache.getCacheSize()); Connection firstConnection = new Connection(aliases[0], SshUtilities.DEFAULT_SSH_PORT, credentials); cache.runInvokeNotReturn(firstConnection); assertEquals(1, cache.getCacheSize()); cache.assertSessionCachedAndConnected(firstConnection); Connection secondConnection = new Connection(aliases[1], SshUtilities.DEFAULT_SSH_PORT, credentials); int returnVal = cache.execute(secondConnection, "date", null); assertEquals("Expected ssh'd date command to return without error", 0, returnVal); cache.assertSessionCachedAndConnected(secondConnection); assertEquals(1, cache.getCacheSize()); cache.assertEvictedConnectionDoesntClose(firstConnection); cache.cleanupUnclosedEvictedConnection(); }
@Ignore @Test public void evictedConnectionCloseAfterUse() throws IOException, InterruptedException { int cacheCapacity = 1; cache = new TestSshConnectionCache(cacheCapacity); assertEquals(0, cache.getCacheSize()); Connection firstConnection = new Connection(aliases[0], SshUtilities.DEFAULT_SSH_PORT, credentials); cache.runInvokeNotReturn(firstConnection); assertEquals(1, cache.getCacheSize()); cache.assertSessionCachedAndConnected(firstConnection); Connection secondConnection = new Connection(aliases[1], SshUtilities.DEFAULT_SSH_PORT, credentials); int returnVal = cache.execute(secondConnection, "date", null); assertEquals("Expected ssh'd date command to return without error", 0, returnVal); cache.assertSessionCachedAndConnected(secondConnection); assertEquals(1, cache.getCacheSize()); cache.assertEvictedConnectionDoesntClose(firstConnection); cache.runInvokeResume(firstConnection); }