@Test public void closeStreams_ignores_failure_if_stream_fails_to_be_closed() throws Exception { InputStream stream = mock(InputStream.class); doThrow(new IOException("error")).when(stream).close(); Process process = mock(Process.class); when(process.getInputStream()).thenReturn(stream); ProcessCommandsProcessMonitor underTest = new ProcessCommandsProcessMonitor(process, null, mock(ProcessCommands.class, Mockito.RETURNS_MOCKS)); // no failures underTest.closeStreams(); } }
@Test public void closeStreams_ignores_null_stream() { ProcessCommands commands = mock(ProcessCommands.class); Process process = mock(Process.class); when(process.getInputStream()).thenReturn(null); ProcessCommandsProcessMonitor underTest = new ProcessCommandsProcessMonitor(process, null, commands); // no failures underTest.closeStreams(); }
@Test public void ProcessMonitorImpl_is_a_proxy_of_Process() throws Exception { Process process = mock(Process.class, RETURNS_DEEP_STUBS); ProcessCommands commands = mock(ProcessCommands.class, RETURNS_DEEP_STUBS); ProcessCommandsProcessMonitor underTest = new ProcessCommandsProcessMonitor(process, ProcessId.WEB_SERVER, commands); underTest.waitFor(); verify(process).waitFor(); underTest.closeStreams(); verify(process.getErrorStream()).close(); verify(process.getInputStream()).close(); verify(process.getOutputStream()).close(); underTest.destroyForcibly(); verify(process).destroyForcibly(); assertThat(underTest.getInputStream()).isNotNull(); underTest.isAlive(); verify(process).isAlive(); underTest.waitFor(123, TimeUnit.MILLISECONDS); verify(process).waitFor(123, TimeUnit.MILLISECONDS); }
} else { ProcessCommands commands = allProcessesCommands.createAfterClean(processId.getIpcIndex()); return new ProcessCommandsProcessMonitor(process, processId, commands);
@Test public void ProcessMonitorImpl_is_a_proxy_of_Commands() { Process process = mock(Process.class, RETURNS_DEEP_STUBS); ProcessCommands commands = mock(ProcessCommands.class, RETURNS_DEEP_STUBS); ProcessCommandsProcessMonitor underTest = new ProcessCommandsProcessMonitor(process, null, commands); underTest.askForStop(); verify(commands).askForStop(); underTest.acknowledgeAskForRestart(); verify(commands).acknowledgeAskForRestart(); underTest.askedForRestart(); verify(commands).askedForRestart(); underTest.isOperational(); verify(commands).isOperational(); }
public ProcessMonitor launch(AbstractCommand command) { EsInstallation fileSystem = command.getEsInstallation(); if (fileSystem != null) { cleanupOutdatedEsData(fileSystem); writeConfFiles(fileSystem); } Process process; if (command instanceof EsScriptCommand) { process = launchExternal((EsScriptCommand) command); } else if (command instanceof JavaCommand) { process = launchJava((JavaCommand) command); } else { throw new IllegalStateException("Unexpected type of command: " + command.getClass()); } ProcessId processId = command.getProcessId(); try { if (processId == ProcessId.ELASTICSEARCH) { return new EsProcessMonitor(process, processId, command.getEsInstallation(), new EsConnectorImpl()); } else { ProcessCommands commands = allProcessesCommands.createAfterClean(processId.getIpcIndex()); return new ProcessCommandsProcessMonitor(process, processId, commands); } } catch (Exception e) { // just in case if (process != null) { process.destroyForcibly(); } throw new IllegalStateException(format("Fail to launch monitor of process [%s]", processId.getKey()), e); } }