]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-12043 process lifecycle: can hard stop when stopping
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 2 May 2019 10:02:53 +0000 (12:02 +0200)
committerSonarTech <sonartech@sonarsource.com>
Mon, 3 Jun 2019 18:21:19 +0000 (20:21 +0200)
server/sonar-process/src/main/java/org/sonar/process/Lifecycle.java
server/sonar-process/src/test/java/org/sonar/process/LifecycleTest.java

index 33b4888e3d331e510da8b7997fa9ac5c6b58a343..506c63e92f8c923c73a32ccd5ad77f2961080d24 100644 (file)
@@ -56,7 +56,7 @@ public class Lifecycle {
     res.put(STARTED, toSet(OPERATIONAL, RESTARTING, STOPPING, HARD_STOPPING));
     res.put(OPERATIONAL, toSet(RESTARTING, STOPPING, HARD_STOPPING));
     res.put(RESTARTING, toSet(STARTING, HARD_STOPPING));
-    res.put(STOPPING, toSet(STOPPED));
+    res.put(STOPPING, toSet(HARD_STOPPING, STOPPED));
     res.put(HARD_STOPPING, toSet(STOPPED));
     res.put(STOPPED, toSet());
     return res;
index f58a27f1c4090f57dd40086b273ca93591a0dd11..3ff651464e47d454ee8ebd9d7557a5b2d8a07036 100644 (file)
  */
 package org.sonar.process;
 
-import java.util.Objects;
 import org.junit.Test;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.sonar.process.Lifecycle.State;
+import static org.sonar.process.Lifecycle.State.HARD_STOPPING;
 import static org.sonar.process.Lifecycle.State.INIT;
 import static org.sonar.process.Lifecycle.State.OPERATIONAL;
 import static org.sonar.process.Lifecycle.State.RESTARTING;
 import static org.sonar.process.Lifecycle.State.STARTED;
 import static org.sonar.process.Lifecycle.State.STARTING;
+import static org.sonar.process.Lifecycle.State.STOPPED;
 import static org.sonar.process.Lifecycle.State.STOPPING;
 import static org.sonar.process.Lifecycle.State.values;
 
@@ -97,6 +98,34 @@ public class LifecycleTest {
     assertThat(newLifeCycle(RESTARTING).tryToMoveTo(STARTING)).isTrue();
   }
 
+  @Test
+  public void can_move_to_STOPPING_only_from_STARTING_STARTED_and_OPERATIONAL() {
+    for (State state : values()) {
+      boolean tryToMoveTo = newLifeCycle(state).tryToMoveTo(STOPPING);
+      if (state == STARTING || state == STARTED || state == OPERATIONAL) {
+        assertThat(tryToMoveTo).describedAs("from state " + state).isTrue();
+      } else {
+        assertThat(tryToMoveTo).describedAs("from state " + state).isFalse();
+      }
+    }
+  }
+
+  @Test
+  public void can_move_to_HARD_STOPPING_from_any_step_but_from_INIT_HARD_STOPPING_and_STOPPED() {
+    for (State state : values()) {
+      boolean tryToMoveTo = newLifeCycle(state).tryToMoveTo(HARD_STOPPING);
+      if (state == INIT || state == STOPPED || state == HARD_STOPPING) {
+        assertThat(tryToMoveTo).describedAs("from state " + state).isFalse();
+      } else {
+        assertThat(tryToMoveTo).describedAs("from state " + state).isTrue();
+      }
+    }
+  }
+
+  /**
+   * Creates a Lifecycle object in the specified state emulating that the shortest path to this state has been followed
+   * to reach it.
+   */
   private static Lifecycle newLifeCycle(State state) {
     switch (state) {
       case INIT:
@@ -127,35 +156,4 @@ public class LifecycleTest {
     return lifecycle;
   }
 
-  private static final class Transition {
-    private final State from;
-    private final State to;
-
-    private Transition(State from, State to) {
-      this.from = from;
-      this.to = to;
-    }
-
-    @Override
-    public boolean equals(Object o) {
-      if (this == o) {
-        return true;
-      }
-      if (o == null || getClass() != o.getClass()) {
-        return false;
-      }
-      Transition that = (Transition) o;
-      return from == that.from && to == that.to;
-    }
-
-    @Override
-    public int hashCode() {
-      return Objects.hash(from, to);
-    }
-
-    @Override
-    public String toString() {
-      return "Transition{" + from + " => " + to + '}';
-    }
-  }
 }