1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
/*
* SonarQube
* Copyright (C) 2009-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.application;
import java.io.IOException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.DisableOnDebug;
import org.junit.rules.TemporaryFolder;
import org.junit.rules.TestRule;
import org.junit.rules.Timeout;
import org.sonar.application.config.TestAppSettings;
import org.sonar.process.sharedmemoryfile.ProcessCommands;
import static com.google.common.collect.ImmutableMap.of;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.sonar.process.ProcessProperties.Property.ENABLE_STOP_COMMAND;
public class StopRequestWatcherImplTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();
@Rule
public TestRule safeguardTimeout = new DisableOnDebug(Timeout.seconds(60));
private final ProcessCommands commands = mock(ProcessCommands.class);
private final Scheduler scheduler = mock(Scheduler.class);
@Test
public void startWatching_does_not_start_thread_if_stop_command_is_disabled() {
TestAppSettings appSettings = new TestAppSettings();
StopRequestWatcherImpl underTest = new StopRequestWatcherImpl(appSettings, scheduler, commands);
underTest.startWatching();
assertThat(underTest.isAlive()).isFalse();
}
@Test
public void watch_stop_command_if_stop_command_is_enabled() {
TestAppSettings appSettings = new TestAppSettings(of(ENABLE_STOP_COMMAND.getKey(), "true"));
StopRequestWatcherImpl underTest = new StopRequestWatcherImpl(appSettings, scheduler, commands);
underTest.startWatching();
assertThat(underTest.isAlive()).isTrue();
verify(scheduler, never()).stop();
when(commands.askedForStop()).thenReturn(true);
verify(scheduler, timeout(1_000L)).stop();
underTest.stopWatching();
await().until(() -> !underTest.isAlive());
assertThat(underTest.isAlive()).isFalse();
}
@Test
public void create_instance_with_default_delay() throws IOException {
TestAppSettings appSettings = new TestAppSettings();
FileSystem fs = mock(FileSystem.class);
when(fs.getTempDir()).thenReturn(temp.newFolder());
StopRequestWatcherImpl underTest = StopRequestWatcherImpl.create(appSettings, scheduler, fs);
assertThat(underTest.getDelayMs()).isEqualTo(500L);
}
@Test
public void stop_watching_commands_if_thread_is_interrupted() {
TestAppSettings appSettings = new TestAppSettings();
StopRequestWatcherImpl underTest = new StopRequestWatcherImpl(appSettings, scheduler, commands);
underTest.startWatching();
underTest.interrupt();
await().until(() -> !underTest.isAlive());
assertThat(underTest.isAlive()).isFalse();
}
}
|