/** * Request for graceful stop then blocks until process is stopped. * Returns immediately if the process is disabled in configuration. */ private void stopProcess(ProcessId processId) { SQProcess process = processesById.get(processId); if (process != null) { process.stop(1, TimeUnit.MINUTES); } }
@Test public void process_is_stopped_forcibly_if_graceful_stop_is_too_long() throws Exception { ProcessLifecycleListener listener = mock(ProcessLifecycleListener.class); SQProcess underTest = SQProcess.builder(A_PROCESS_ID) .addProcessLifecycleListener(listener) .build(); try (TestProcess testProcess = new TestProcess()) { underTest.start(() -> testProcess); underTest.stop(1L, TimeUnit.MILLISECONDS); testProcess.waitFor(); assertThat(testProcess.askedForStop).isTrue(); assertThat(testProcess.destroyedForcibly).isTrue(); assertThat(testProcess.isAlive()).isFalse(); assertThat(underTest.getState()).isEqualTo(Lifecycle.State.STOPPED); verify(listener).onProcessState(A_PROCESS_ID, Lifecycle.State.STOPPED); } }
@Test public void process_stops_after_graceful_request_for_stop() throws Exception { ProcessLifecycleListener listener = mock(ProcessLifecycleListener.class); SQProcess underTest = SQProcess.builder(A_PROCESS_ID) .addProcessLifecycleListener(listener) .build(); try (TestProcess testProcess = new TestProcess()) { underTest.start(() -> testProcess); Thread stopperThread = new Thread(() -> underTest.stop(1, TimeUnit.HOURS)); stopperThread.start(); // thread is blocked until process stopped assertThat(stopperThread.isAlive()).isTrue(); // wait for the stopper thread to ask graceful stop while (!testProcess.askedForStop) { Thread.sleep(1L); } assertThat(underTest.getState()).isEqualTo(Lifecycle.State.STOPPING); verify(listener).onProcessState(A_PROCESS_ID, Lifecycle.State.STOPPING); // process stopped testProcess.close(); // waiting for stopper thread to detect and handle the stop stopperThread.join(); assertThat(underTest.getState()).isEqualTo(Lifecycle.State.STOPPED); verify(listener).onProcessState(A_PROCESS_ID, Lifecycle.State.STOPPED); } }
/** * Request for graceful stop then blocks until process is stopped. * Returns immediately if the process is disabled in configuration. */ private void stopProcess(ProcessId processId) { SQProcess process = processesById.get(processId); if (process != null) { process.stop(1, TimeUnit.MINUTES); } }