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
|
/*
* 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 com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.application.NodeLifecycle.State.FINALIZE_STOPPING;
import static org.sonar.application.NodeLifecycle.State.HARD_STOPPING;
import static org.sonar.application.NodeLifecycle.State.INIT;
import static org.sonar.application.NodeLifecycle.State.OPERATIONAL;
import static org.sonar.application.NodeLifecycle.State.RESTARTING;
import static org.sonar.application.NodeLifecycle.State.STARTING;
import static org.sonar.application.NodeLifecycle.State.STOPPED;
import static org.sonar.application.NodeLifecycle.State.STOPPING;
@RunWith(DataProviderRunner.class)
public class NodeLifecycleTest {
private NodeLifecycle underTest = new NodeLifecycle();
@Test
public void verify_regular_start_and_graceful_stop_cycle() {
assertThat(underTest.getState()).isEqualTo(INIT);
verifyMoveTo(STARTING);
verifyMoveTo(OPERATIONAL);
verifyMoveTo(STOPPING);
verifyMoveTo(FINALIZE_STOPPING);
verifyMoveTo(STOPPED);
}
@Test
public void verify_regular_start_and_early_graceful_stop_cycle() {
assertThat(underTest.getState()).isEqualTo(INIT);
verifyMoveTo(STARTING);
verifyMoveTo(STOPPING);
verifyMoveTo(FINALIZE_STOPPING);
verifyMoveTo(STOPPED);
}
@Test
public void verify_start_and_hard_stop_cycle() {
assertThat(underTest.getState()).isEqualTo(INIT);
verifyMoveTo(STARTING);
verifyMoveTo(OPERATIONAL);
verifyMoveTo(HARD_STOPPING);
verifyMoveTo(FINALIZE_STOPPING);
verifyMoveTo(STOPPED);
}
@Test
public void verify_start_graceful_stop_interrupted_by_hard_stop_cycle() {
assertThat(underTest.getState()).isEqualTo(INIT);
verifyMoveTo(STARTING);
verifyMoveTo(OPERATIONAL);
verifyMoveTo(STOPPING);
verifyMoveTo(HARD_STOPPING);
verifyMoveTo(FINALIZE_STOPPING);
verifyMoveTo(STOPPED);
}
@Test
public void verify_regular_start_restart_cycle() {
assertThat(underTest.getState()).isEqualTo(INIT);
verifyMoveTo(STARTING);
verifyMoveTo(OPERATIONAL);
verifyMoveTo(RESTARTING);
verifyMoveTo(STARTING);
verifyMoveTo(OPERATIONAL);
}
@Test
public void verify_failed_restart_resulting_in_hard_stop_cycle() {
assertThat(underTest.getState()).isEqualTo(INIT);
verifyMoveTo(STARTING);
verifyMoveTo(OPERATIONAL);
verifyMoveTo(RESTARTING);
verifyMoveTo(HARD_STOPPING);
verifyMoveTo(FINALIZE_STOPPING);
verifyMoveTo(STOPPED);
}
@Test
public void verify_failed_restart_even_if_FINALIZE_STOPPING_was_initiated_resulting_in_hard_stop_cycle() {
assertThat(underTest.getState()).isEqualTo(INIT);
verifyMoveTo(STARTING);
verifyMoveTo(OPERATIONAL);
verifyMoveTo(RESTARTING);
verifyMoveTo(HARD_STOPPING);
verifyMoveTo(FINALIZE_STOPPING);
verifyMoveTo(STOPPED);
}
@Test
@UseDataProvider("allStates")
public void RESTARTING_is_only_allowed_from_STARTING_and_OPERATIONAL(NodeLifecycle.State state) {
if (state == STARTING || state == OPERATIONAL) {
verifyMoveTo(newNodeLifecycle(state), RESTARTING);
} else {
assertThat(newNodeLifecycle(state).tryToMoveTo(RESTARTING)).isFalse();
}
}
/**
* Moving to stopped displays a log "SQ stopped" which must no appear when restarting
*/
@Test
public void STOPPED_is_not_allowed_from_RESTARTING() {
assertThat(newNodeLifecycle(RESTARTING).tryToMoveTo(STOPPED)).isFalse();
}
/**
* To go to STOPPED state, one must go through FINALIZE_STOPPING
*/
@Test
@UseDataProvider("allStates")
public void STOPPED_is_allowed_only_from_FINALIZE_STOPPING(NodeLifecycle.State state) {
if (state == FINALIZE_STOPPING) {
verifyMoveTo(newNodeLifecycle(state), STOPPED);
} else {
assertThat(newNodeLifecycle(state).tryToMoveTo(STOPPED)).isFalse();
}
}
@Test
@UseDataProvider("allStates")
public void FINALIZE_STOPPING_is_only_allowed_from_STOPPING_and_HARD_STOPPING(NodeLifecycle.State state) {
if (state == STOPPING || state == HARD_STOPPING) {
verifyMoveTo(newNodeLifecycle(state), FINALIZE_STOPPING);
} else {
assertThat(newNodeLifecycle(state).tryToMoveTo(FINALIZE_STOPPING)).isFalse();
}
}
@Test
@UseDataProvider("allStates")
public void cannot_move_to_any_state_from_STOPPED(NodeLifecycle.State state) {
assertThat(newNodeLifecycle(STOPPED).tryToMoveTo(state)).isFalse();
}
private void verifyMoveTo(NodeLifecycle.State newState) {
verifyMoveTo(this.underTest, newState);
}
private void verifyMoveTo(NodeLifecycle lifecycle, NodeLifecycle.State newState) {
assertThat(lifecycle.tryToMoveTo(newState)).isTrue();
assertThat(lifecycle.getState()).isEqualTo(newState);
}
/**
* Creates a Lifecycle object in the specified state emulating that the shortest path to this state has been followed
* to reach it.
*/
private static NodeLifecycle newNodeLifecycle(NodeLifecycle.State state) {
switch (state) {
case INIT:
return new NodeLifecycle();
case STARTING:
return newNodeLifecycle(INIT, state);
case OPERATIONAL:
return newNodeLifecycle(STARTING, state);
case RESTARTING:
return newNodeLifecycle(OPERATIONAL, state);
case STOPPING:
return newNodeLifecycle(OPERATIONAL, state);
case HARD_STOPPING:
return newNodeLifecycle(OPERATIONAL, state);
case FINALIZE_STOPPING:
return newNodeLifecycle(STOPPING, state);
case STOPPED:
return newNodeLifecycle(FINALIZE_STOPPING, state);
default:
throw new IllegalStateException("Missing state! " + state);
}
}
private static NodeLifecycle newNodeLifecycle(NodeLifecycle.State from, NodeLifecycle.State to) {
NodeLifecycle lifecycle = newNodeLifecycle(from);
assertThat(lifecycle.tryToMoveTo(to)).isTrue();
return lifecycle;
}
@DataProvider
public static Object[][] allStates() {
return Arrays.stream(NodeLifecycle.State.values())
.map(t -> new Object[] {t})
.toArray(Object[][]::new);
}
}
|