]> source.dussan.org Git - sonarqube.git/blob
3fbe5f8a52c65a2ea06d1672b678b86066bbcafe
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.application.process;
21
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;
34
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;
43
44 public class StopRequestWatcherImplTest {
45
46   @Rule
47   public TemporaryFolder temp = new TemporaryFolder();
48   @Rule
49   public TestRule safeGuard = new DisableOnDebug(Timeout.seconds(10));
50
51   private AppSettings settings = mock(AppSettings.class, RETURNS_DEEP_STUBS);
52   private ProcessCommands commands = mock(ProcessCommands.class);
53   private Scheduler scheduler = mock(Scheduler.class);
54
55   @Test
56   public void do_not_watch_command_if_disabled() throws IOException {
57     enableSetting(false);
58     StopRequestWatcherImpl underTest = new StopRequestWatcherImpl(settings, scheduler, commands);
59
60     underTest.startWatching();
61     assertThat(underTest.isAlive()).isFalse();
62
63     underTest.stopWatching();
64     verifyZeroInteractions(commands, scheduler);
65   }
66
67   @Test
68   public void watch_stop_command_if_enabled() throws Exception {
69     enableSetting(true);
70     StopRequestWatcherImpl underTest = new StopRequestWatcherImpl(settings, scheduler, commands);
71     underTest.setDelayMs(1L);
72
73     underTest.startWatching();
74     assertThat(underTest.isAlive()).isTrue();
75     verify(scheduler, never()).terminate();
76
77     when(commands.askedForStop()).thenReturn(true);
78     verify(scheduler, timeout(1_000L)).terminate();
79
80     underTest.stopWatching();
81     while (underTest.isAlive()) {
82       Thread.sleep(1L);
83     }
84   }
85
86   @Test
87   public void create_instance_with_default_delay() throws IOException {
88     FileSystem fs = mock(FileSystem.class);
89     when(fs.getTempDir()).thenReturn(temp.newFolder());
90
91     StopRequestWatcherImpl underTest = StopRequestWatcherImpl.create(settings, scheduler, fs);
92
93     assertThat(underTest.getDelayMs()).isEqualTo(500L);
94   }
95
96   @Test
97   public void stop_watching_commands_if_thread_is_interrupted() throws Exception {
98     enableSetting(true);
99     StopRequestWatcherImpl underTest = new StopRequestWatcherImpl(settings, scheduler, commands);
100
101     underTest.startWatching();
102     underTest.interrupt();
103
104     while (underTest.isAlive()) {
105       Thread.sleep(1L);
106     }
107     assertThat(underTest.isAlive()).isFalse();
108   }
109
110   private void enableSetting(boolean b) {
111     when(settings.getProps().valueAsBoolean(ProcessProperties.ENABLE_STOP_COMMAND)).thenReturn(b);
112   }
113
114 }