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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
/*
* 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.process;
import java.io.InputStream;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.awaitility.Awaitility;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.DisableOnDebug;
import org.junit.rules.TestRule;
import org.junit.rules.Timeout;
import org.mockito.Mockito;
import org.sonar.process.ProcessId;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.sonar.application.process.ManagedProcessHandler.Timeout.newTimeout;
public class ManagedProcessHandlerTest {
private static final ProcessId A_PROCESS_ID = ProcessId.ELASTICSEARCH;
@Rule
public TestRule safeguardTimeout = new DisableOnDebug(Timeout.seconds(60));
@Test
public void initial_state_is_INIT() {
ManagedProcessHandler underTest = newHanderBuilder(A_PROCESS_ID).build();
assertThat(underTest.getProcessId()).isEqualTo(A_PROCESS_ID);
assertThat(underTest.getState()).isEqualTo(ManagedProcessLifecycle.State.INIT);
}
@Test
public void start_and_stop_process() {
ProcessLifecycleListener listener = mock(ProcessLifecycleListener.class);
ManagedProcessHandler underTest = newHanderBuilder(A_PROCESS_ID)
.addProcessLifecycleListener(listener)
.build();
try (TestManagedProcess testProcess = new TestManagedProcess()) {
assertThat(underTest.start(() -> testProcess)).isTrue();
assertThat(underTest.getState()).isEqualTo(ManagedProcessLifecycle.State.STARTED);
assertThat(testProcess.isAlive()).isTrue();
assertThat(testProcess.streamsClosed).isFalse();
verify(listener).onProcessState(A_PROCESS_ID, ManagedProcessLifecycle.State.STARTED);
testProcess.close();
Awaitility.await()
.atMost(10, TimeUnit.SECONDS)
.until(() -> underTest.getState() == ManagedProcessLifecycle.State.STOPPED);
assertThat(testProcess.isAlive()).isFalse();
assertThat(testProcess.streamsClosed).isTrue();
verify(listener).onProcessState(A_PROCESS_ID, ManagedProcessLifecycle.State.STOPPED);
}
}
private ManagedProcessHandler.Builder newHanderBuilder(ProcessId aProcessId) {
return ManagedProcessHandler.builder(aProcessId)
.setStopTimeout(newTimeout(1, TimeUnit.SECONDS))
.setHardStopTimeout(newTimeout(1, TimeUnit.SECONDS));
}
@Test
public void start_does_not_nothing_if_already_started_once() {
ManagedProcessHandler underTest = newHanderBuilder(A_PROCESS_ID).build();
try (TestManagedProcess testProcess = new TestManagedProcess()) {
assertThat(underTest.start(() -> testProcess)).isTrue();
assertThat(underTest.getState()).isEqualTo(ManagedProcessLifecycle.State.STARTED);
assertThat(underTest.start(() -> {
throw new IllegalStateException();
})).isFalse();
assertThat(underTest.getState()).isEqualTo(ManagedProcessLifecycle.State.STARTED);
}
}
@Test
public void start_throws_exception_and_move_to_state_STOPPED_if_execution_of_command_fails() {
ManagedProcessHandler underTest = newHanderBuilder(A_PROCESS_ID).build();
assertThatThrownBy(() -> {
underTest.start(() -> {
throw new IllegalStateException("error");
});
assertThat(underTest.getState()).isEqualTo(ManagedProcessLifecycle.State.STOPPED);
})
.isInstanceOf(IllegalStateException.class)
.hasMessage("error");
}
@Test
public void send_event_when_process_is_operational() {
ManagedProcessEventListener listener = mock(ManagedProcessEventListener.class);
ManagedProcessHandler underTest = newHanderBuilder(A_PROCESS_ID)
.addEventListener(listener)
.build();
try (TestManagedProcess testProcess = new TestManagedProcess()) {
underTest.start(() -> testProcess);
testProcess.operational = true;
underTest.refreshState();
verify(listener).onManagedProcessEvent(A_PROCESS_ID, ManagedProcessEventListener.Type.OPERATIONAL);
}
verifyNoMoreInteractions(listener);
}
@Test
public void operational_event_is_sent_once() {
ManagedProcessEventListener listener = mock(ManagedProcessEventListener.class);
ManagedProcessHandler underTest = newHanderBuilder(A_PROCESS_ID)
.addEventListener(listener)
.build();
try (TestManagedProcess testProcess = new TestManagedProcess()) {
underTest.start(() -> testProcess);
testProcess.operational = true;
underTest.refreshState();
verify(listener).onManagedProcessEvent(A_PROCESS_ID, ManagedProcessEventListener.Type.OPERATIONAL);
// second run
underTest.refreshState();
verifyNoMoreInteractions(listener);
}
}
@Test
public void send_event_when_process_requests_for_restart() {
ManagedProcessEventListener listener = mock(ManagedProcessEventListener.class);
ManagedProcessHandler underTest = newHanderBuilder(A_PROCESS_ID)
.addEventListener(listener)
.setWatcherDelayMs(1L)
.build();
try (TestManagedProcess testProcess = new TestManagedProcess()) {
underTest.start(() -> testProcess);
testProcess.askedForRestart = true;
verify(listener, timeout(10_000)).onManagedProcessEvent(A_PROCESS_ID, ManagedProcessEventListener.Type.ASK_FOR_RESTART);
// flag is reset so that next run does not trigger again the event
underTest.refreshState();
verifyNoMoreInteractions(listener);
assertThat(testProcess.askedForRestart).isFalse();
}
}
@Test
public void process_stops_after_graceful_request_for_stop() throws Exception {
ProcessLifecycleListener listener = mock(ProcessLifecycleListener.class);
ManagedProcessHandler underTest = newHanderBuilder(A_PROCESS_ID)
.addProcessLifecycleListener(listener)
.setHardStopTimeout(newTimeout(1, TimeUnit.HOURS))
.build();
try (TestManagedProcess testProcess = new TestManagedProcess()) {
underTest.start(() -> testProcess);
Thread stopperThread = new Thread(() -> {
try {
underTest.hardStop();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
stopperThread.start();
// thread is blocked until process stopped
assertThat(stopperThread.isAlive()).isTrue();
// wait for the stopper thread to ask graceful stop
while (!testProcess.askedForHardStop) {
Thread.sleep(1L);
}
assertThat(underTest.getState()).isEqualTo(ManagedProcessLifecycle.State.HARD_STOPPING);
verify(listener).onProcessState(A_PROCESS_ID, ManagedProcessLifecycle.State.HARD_STOPPING);
// process stopped
testProcess.close();
// waiting for stopper thread to detect and handle the stop
stopperThread.join();
assertThat(underTest.getState()).isEqualTo(ManagedProcessLifecycle.State.STOPPED);
verify(listener).onProcessState(A_PROCESS_ID, ManagedProcessLifecycle.State.STOPPED);
}
}
@Test
public void process_is_hard_stopped_if_graceful_stop_is_too_long() throws Exception {
ProcessLifecycleListener listener = mock(ProcessLifecycleListener.class);
ManagedProcessHandler underTest = newHanderBuilder(A_PROCESS_ID)
.addProcessLifecycleListener(listener)
.setStopTimeout(newTimeout(1, TimeUnit.MILLISECONDS))
.setHardStopTimeout(newTimeout(1, TimeUnit.MILLISECONDS))
.build();
try (TestManagedProcess testProcess = new TestManagedProcess()) {
underTest.start(() -> testProcess);
underTest.stop();
testProcess.waitFor();
assertThat(testProcess.askedForHardStop).isTrue();
assertThat(testProcess.askedForStop).isTrue();
assertThat(testProcess.destroyedForcibly).isTrue();
assertThat(testProcess.isAlive()).isFalse();
assertThat(underTest.getState()).isEqualTo(ManagedProcessLifecycle.State.STOPPED);
verify(listener).onProcessState(A_PROCESS_ID, ManagedProcessLifecycle.State.STOPPED);
}
}
@Test
public void process_is_stopped_forcibly_if_hard_stop_is_too_long() throws Exception {
ProcessLifecycleListener listener = mock(ProcessLifecycleListener.class);
ManagedProcessHandler underTest = newHanderBuilder(A_PROCESS_ID)
.addProcessLifecycleListener(listener)
.setHardStopTimeout(newTimeout(1, TimeUnit.MILLISECONDS))
.build();
try (TestManagedProcess testProcess = new TestManagedProcess()) {
underTest.start(() -> testProcess);
underTest.hardStop();
testProcess.waitFor();
assertThat(testProcess.askedForHardStop).isTrue();
assertThat(testProcess.destroyedForcibly).isTrue();
assertThat(testProcess.isAlive()).isFalse();
assertThat(underTest.getState()).isEqualTo(ManagedProcessLifecycle.State.STOPPED);
verify(listener).onProcessState(A_PROCESS_ID, ManagedProcessLifecycle.State.STOPPED);
}
}
@Test
public void process_requests_are_listened_on_regular_basis() {
ManagedProcessEventListener listener = mock(ManagedProcessEventListener.class);
ManagedProcessHandler underTest = newHanderBuilder(A_PROCESS_ID)
.addEventListener(listener)
.setWatcherDelayMs(1L)
.build();
try (TestManagedProcess testProcess = new TestManagedProcess()) {
underTest.start(() -> testProcess);
testProcess.operational = true;
verify(listener, timeout(1_000L)).onManagedProcessEvent(A_PROCESS_ID, ManagedProcessEventListener.Type.OPERATIONAL);
}
}
@Test
public void test_toString() {
ManagedProcessHandler underTest = newHanderBuilder(A_PROCESS_ID).build();
assertThat(underTest).hasToString("Process[" + A_PROCESS_ID.getHumanReadableName() + "]");
}
private static class TestManagedProcess implements ManagedProcess, AutoCloseable {
private final CountDownLatch alive = new CountDownLatch(1);
private final InputStream inputStream = mock(InputStream.class, Mockito.RETURNS_MOCKS);
private final InputStream errorStream = mock(InputStream.class, Mockito.RETURNS_MOCKS);
private boolean streamsClosed = false;
private boolean operational = false;
private boolean askedForRestart = false;
private boolean askedForStop = false;
private boolean askedForHardStop = false;
private boolean destroyedForcibly = false;
@Override
public InputStream getInputStream() {
return inputStream;
}
@Override
public InputStream getErrorStream() {
return errorStream;
}
@Override
public void closeStreams() {
streamsClosed = true;
}
@Override
public boolean isAlive() {
return alive.getCount() == 1;
}
@Override
public void askForStop() {
askedForStop = true;
// do not stop, just asking
}
@Override
public void askForHardStop() {
askedForHardStop = true;
// do not stop, just asking
}
@Override
public void destroyForcibly() {
destroyedForcibly = true;
alive.countDown();
}
@Override
public void waitFor() throws InterruptedException {
alive.await();
}
@Override
public void waitFor(long timeout, TimeUnit timeoutUnit) throws InterruptedException {
alive.await(timeout, timeoutUnit);
}
@Override
public boolean isOperational() {
return operational;
}
@Override
public boolean askedForRestart() {
return askedForRestart;
}
@Override
public void acknowledgeAskForRestart() {
this.askedForRestart = false;
}
@Override
public void close() {
alive.countDown();
}
}
}
|