2 * SonarQube :: Process Monitor
3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.process.monitor;
22 import java.io.IOException;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.Random;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.ExpectedException;
29 import org.junit.rules.TemporaryFolder;
30 import org.sonar.process.DefaultProcessCommands;
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.verify;
36 public class RestartRequestWatcherThreadTest {
37 private static final long TEST_DELAYS_MS = 5L;
40 public ExpectedException expectedException = ExpectedException.none();
42 public TemporaryFolder temp = new TemporaryFolder();
44 private Monitor monitor = mock(Monitor.class);
47 public void constructor_throws_NPE_if_monitor_arg_is_null() {
48 expectedException.expect(NullPointerException.class);
49 expectedException.expectMessage("monitor can not be null");
51 new RestartRequestWatcherThread(null, Collections.<ProcessRef>emptyList());
55 public void constructor_throws_NPE_if_processes_arg_is_null() {
56 expectedException.expect(NullPointerException.class);
57 expectedException.expectMessage("processes can not be null");
59 new RestartRequestWatcherThread(monitor, null);
63 public void each_RestartRequestWatcherThread_instance_get_a_unique_thread_name() {
64 assertThat(newSingleProcessRefRestartWatcher().getName())
65 .isNotEqualTo(newSingleProcessRefRestartWatcher().getName());
69 public void does_not_stop_watching_when_no_processRef_requests_restart() throws Exception {
70 RestartRequestWatcherThread underTest = newSingleProcessRefRestartWatcher();
76 assertThat(underTest.isWatching()).isTrue();
77 assertThat(underTest.isAlive()).isTrue();
81 public void stops_watching_when_any_processRef_requests_restart() throws Exception {
82 ProcessRef processRef1 = newProcessRef(1);
83 ProcessRef processRef2 = newProcessRef(2);
84 RestartRequestWatcherThread underTest = newSingleProcessRefRestartWatcher(processRef1, processRef2);
90 if (new Random().nextInt() % 2 == 1) {
91 processRef1.getCommands().askForRestart();
93 processRef2.getCommands().askForRestart();
98 assertThat(underTest.isWatching()).isFalse();
99 verify(monitor).restartAsync();
102 private RestartRequestWatcherThread newSingleProcessRefRestartWatcher(ProcessRef... processRefs) {
103 return new RestartRequestWatcherThread(monitor, Arrays.asList(processRefs), TEST_DELAYS_MS);
106 private ProcessRef newProcessRef(int id) {
108 return new ProcessRef(String.valueOf(id), new DefaultProcessCommands(temp.newFolder(), id), mock(Process.class), mock(StreamGobbler.class));
109 } catch (IOException e) {
110 throw new RuntimeException(e);