3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info 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.application.process;
22 import java.io.IOException;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.DisableOnDebug;
26 import org.junit.rules.TemporaryFolder;
27 import org.junit.rules.TestRule;
28 import org.junit.rules.Timeout;
29 import org.sonar.application.FileSystem;
30 import org.sonar.application.Scheduler;
31 import org.sonar.application.config.AppSettings;
32 import org.sonar.process.ProcessCommands;
33 import org.sonar.process.ProcessProperties;
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.never;
39 import static org.mockito.Mockito.timeout;
40 import static org.mockito.Mockito.verify;
41 import static org.mockito.Mockito.verifyZeroInteractions;
42 import static org.mockito.Mockito.when;
44 public class StopRequestWatcherImplTest {
47 public TemporaryFolder temp = new TemporaryFolder();
49 public TestRule safeGuard = new DisableOnDebug(Timeout.seconds(10));
51 private AppSettings settings = mock(AppSettings.class, RETURNS_DEEP_STUBS);
52 private ProcessCommands commands = mock(ProcessCommands.class);
53 private Scheduler scheduler = mock(Scheduler.class);
56 public void do_not_watch_command_if_disabled() throws IOException {
58 StopRequestWatcherImpl underTest = new StopRequestWatcherImpl(settings, scheduler, commands);
60 underTest.startWatching();
61 assertThat(underTest.isAlive()).isFalse();
63 underTest.stopWatching();
64 verifyZeroInteractions(commands, scheduler);
68 public void watch_stop_command_if_enabled() throws Exception {
70 StopRequestWatcherImpl underTest = new StopRequestWatcherImpl(settings, scheduler, commands);
71 underTest.setDelayMs(1L);
73 underTest.startWatching();
74 assertThat(underTest.isAlive()).isTrue();
75 verify(scheduler, never()).terminate();
77 when(commands.askedForStop()).thenReturn(true);
78 verify(scheduler, timeout(1_000L)).terminate();
80 underTest.stopWatching();
81 while (underTest.isAlive()) {
87 public void create_instance_with_default_delay() throws IOException {
88 FileSystem fs = mock(FileSystem.class);
89 when(fs.getTempDir()).thenReturn(temp.newFolder());
91 StopRequestWatcherImpl underTest = StopRequestWatcherImpl.create(settings, scheduler, fs);
93 assertThat(underTest.getDelayMs()).isEqualTo(500L);
97 public void stop_watching_commands_if_thread_is_interrupted() throws Exception {
99 StopRequestWatcherImpl underTest = new StopRequestWatcherImpl(settings, scheduler, commands);
101 underTest.startWatching();
102 underTest.interrupt();
104 while (underTest.isAlive()) {
107 assertThat(underTest.isAlive()).isFalse();
110 private void enableSetting(boolean b) {
111 when(settings.getProps().valueAsBoolean(ProcessProperties.ENABLE_STOP_COMMAND)).thenReturn(b);