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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
/*
* 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.process;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
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.mockito.ArgumentCaptor;
import org.sonar.process.Lifecycle.State;
import org.sonar.process.sharedmemoryfile.ProcessCommands;
import org.sonar.process.test.StandardProcess;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.sonar.process.ProcessEntryPoint.PROPERTY_GRACEFUL_STOP_TIMEOUT_MS;
import static org.sonar.process.ProcessEntryPoint.PROPERTY_PROCESS_INDEX;
import static org.sonar.process.ProcessEntryPoint.PROPERTY_PROCESS_KEY;
import static org.sonar.process.ProcessEntryPoint.PROPERTY_SHARED_PATH;
public class ProcessEntryPointTest {
private SystemExit exit = mock(SystemExit.class);
@Rule
public TestRule safeguardTimeout = new DisableOnDebug(Timeout.seconds(60));
@Rule
public TemporaryFolder temp = new TemporaryFolder();
private ProcessCommands commands = new OperationalFlagOnlyProcessCommands();
private Runtime runtime;
@Before
public void setUp() {
runtime = mock(Runtime.class);
}
@Test
public void load_properties_from_file() throws Exception {
File propsFile = temp.newFile();
FileUtils.write(propsFile, "sonar.foo=bar\nprocess.key=web\nprocess.index=1\nprocess.sharedDir=" + temp.newFolder().getAbsolutePath().replaceAll("\\\\", "/"));
ProcessEntryPoint entryPoint = ProcessEntryPoint.createForArguments(new String[] {propsFile.getAbsolutePath()});
assertThat(entryPoint.getProps().value("sonar.foo")).isEqualTo("bar");
assertThat(entryPoint.getProps().value("process.key")).isEqualTo("web");
}
@Test
public void test_initial_state() throws Exception {
Props props = createProps();
ProcessEntryPoint entryPoint = new ProcessEntryPoint(props, exit, commands, runtime);
assertThat(entryPoint.getProps()).isSameAs(props);
}
@Test
public void fail_to_launch_multiple_times() throws IOException {
Props props = createProps();
ProcessEntryPoint entryPoint = new ProcessEntryPoint(props, exit, commands, runtime);
entryPoint.launch(new NoopProcess());
try {
entryPoint.launch(new NoopProcess());
fail();
} catch (IllegalStateException e) {
assertThat(e).hasMessage("Already started");
}
}
@Test
public void launch_then_request_graceful_stop() throws Exception {
Props props = createProps();
final ProcessEntryPoint entryPoint = new ProcessEntryPoint(props, exit, commands, runtime);
final StandardProcess process = new StandardProcess();
new Thread(() -> entryPoint.launch(process)).start();
waitForOperational(process, commands);
// requests for graceful stop -> waits until down
// Should terminate before the timeout of 30s
entryPoint.stop();
assertThat(process.getState()).isEqualTo(State.STOPPED);
assertThat(process.wasStopped()).isTrue();
assertThat(process.wasHardStopped()).isFalse();
}
@Test
public void launch_then_request_hard_stop() throws Exception {
Props props = createProps();
final ProcessEntryPoint entryPoint = new ProcessEntryPoint(props, exit, commands, runtime);
final StandardProcess process = new StandardProcess();
// starts and waits until terminated
Thread runner = new Thread(() -> entryPoint.launch(process));
runner.start();
waitForOperational(process, commands);
// requests for stop hardly waiting
entryPoint.hardStop();
assertThat(process.getState()).isEqualTo(State.STOPPED);
assertThat(process.wasHardStopped()).isTrue();
}
@Test
public void terminate_if_unexpected_shutdown() throws Exception {
Props props = createProps();
final ProcessEntryPoint entryPoint = new ProcessEntryPoint(props, exit, commands, runtime);
final StandardProcess process = new StandardProcess();
Thread runner = new Thread(() -> {
// starts and waits until terminated
entryPoint.launch(process);
});
runner.start();
waitForOperational(process, commands);
// emulate signal to shutdown process
ArgumentCaptor<Thread> shutdownHookCaptor = ArgumentCaptor.forClass(Thread.class);
verify(runtime).addShutdownHook(shutdownHookCaptor.capture());
shutdownHookCaptor.getValue().start();
while (process.getState() != State.STOPPED) {
Thread.sleep(10L);
}
// exit before test timeout, ok !
}
@Test
public void terminate_if_startup_error() throws IOException {
Props props = createProps();
final ProcessEntryPoint entryPoint = new ProcessEntryPoint(props, exit, commands, runtime);
final Monitored process = mock(Monitored.class);
doThrow(IllegalStateException.class).when(process).start();
entryPoint.launch(process);
}
private static void waitForOperational(StandardProcess process, ProcessCommands commands) throws InterruptedException {
while (!(process.getState() == State.STARTED && commands.isOperational())) {
Thread.sleep(10L);
}
}
private Props createProps() throws IOException {
Props props = new Props(new Properties());
props.set(PROPERTY_SHARED_PATH, temp.newFolder().getAbsolutePath());
props.set(PROPERTY_PROCESS_INDEX, "1");
props.set(PROPERTY_PROCESS_KEY, ProcessId.COMPUTE_ENGINE.getKey());
props.set(PROPERTY_GRACEFUL_STOP_TIMEOUT_MS, "30000");
return props;
}
private static class NoopProcess implements Monitored {
@Override
public void start() {
}
@Override
public Status getStatus() {
return Status.OPERATIONAL;
}
@Override
public void awaitStop() {
}
@Override
public void stop() {
}
@Override
public void hardStop() {
}
}
private static class OperationalFlagOnlyProcessCommands implements ProcessCommands {
private final AtomicBoolean operational = new AtomicBoolean(false);
@Override
public boolean isUp() {
return false;
}
@Override
public void setUp() {
}
@Override
public boolean isOperational() {
return operational.get();
}
@Override
public void setOperational() {
operational.set(true);
}
@Override
public void setHttpUrl(String s) {
}
@Override
public String getHttpUrl() {
return null;
}
@Override
public void askForStop() {
}
@Override
public boolean askedForStop() {
return false;
}
@Override
public void askForHardStop() {
}
@Override
public boolean askedForHardStop() {
return false;
}
@Override
public void askForRestart() {
}
@Override
public boolean askedForRestart() {
return false;
}
@Override
public void acknowledgeAskForRestart() {
}
@Override
public void endWatch() {
}
}
}
|