]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7435 replace DefaultProcessCommands constructors by static methods
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 9 Mar 2016 14:08:46 +0000 (15:08 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 21 Mar 2016 15:44:03 +0000 (16:44 +0100)
server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/Monitor.java
server/sonar-process/src/main/java/org/sonar/process/DefaultProcessCommands.java
server/sonar-process/src/main/java/org/sonar/process/ProcessEntryPoint.java
server/sonar-process/src/test/java/org/sonar/process/DefaultProcessCommandsTest.java
server/sonar-server/src/main/java/org/sonar/server/app/ProcessCommandWrapperImpl.java
server/sonar-server/src/test/java/org/sonar/server/app/ProcessCommandWrapperImplTest.java

index 9a9feeab2410041ec2124d153866283e5119ac9d..6dc22a292f061a33d1e1563a9b7a47ff4e171991 100644 (file)
@@ -356,7 +356,7 @@ public class Monitor {
 
     private boolean askedForStop() {
       File tempDir = fileSystem.getTempDir();
-      try (DefaultProcessCommands processCommands = new DefaultProcessCommands(tempDir, CURRENT_PROCESS_NUMBER, false)) {
+      try (DefaultProcessCommands processCommands = DefaultProcessCommands.secondary(tempDir, CURRENT_PROCESS_NUMBER)) {
         if (processCommands.askedForStop()) {
           return true;
         }
index 6d726ee9172976bdb48e7f0383bb6b8d971ac8d5..a617ea4580b2e7b12a7b94a349339054561017bc 100644 (file)
@@ -30,15 +30,28 @@ public class DefaultProcessCommands implements ProcessCommands {
   private final AllProcessesCommands allProcessesCommands;
   private final ProcessCommands delegate;
 
-  public DefaultProcessCommands(File directory, int processNumber) {
-    this(directory, processNumber, true);
-  }
-
-  public DefaultProcessCommands(File directory, int processNumber, boolean clean) {
+  private DefaultProcessCommands(File directory, int processNumber, boolean clean) {
     this.allProcessesCommands = new AllProcessesCommands(directory);
     this.delegate = clean ? allProcessesCommands.createAfterClean(processNumber) : allProcessesCommands.create(processNumber);
   }
 
+  /**
+   * Main DefaultProcessCommands will clear the shared memory space of the specified process number when created and will
+   * then write and/or read to it.
+   * Therefor there should be only one main DefaultProcessCommands.
+   */
+  public static DefaultProcessCommands main(File directory, int processNumber) {
+    return new DefaultProcessCommands(directory, processNumber, true);
+  }
+
+  /**
+   * Secondary DefaultProcessCommands will read and write to the shared memory space but will not clear it. Therefor, there
+   * can be any number of them.
+   */
+  public static DefaultProcessCommands secondary(File directory, int processNumber) {
+    return new DefaultProcessCommands(directory, processNumber, false);
+  }
+
   @Override
   public boolean isUp() {
     return delegate.isUp();
index 8968210526dbc577052dcd0c5855472eba0b50d2..84cbb22f0937cfc646ceee61aa726059df007c5f 100644 (file)
@@ -138,8 +138,8 @@ public class ProcessEntryPoint implements Stoppable {
 
   public static ProcessEntryPoint createForArguments(String[] args) {
     Props props = ConfigurationUtils.loadPropsFromCommandLineArgs(args);
-    ProcessCommands commands = new DefaultProcessCommands(
-      props.nonNullValueAsFile(PROPERTY_SHARED_PATH), Integer.parseInt(props.nonNullValue(PROPERTY_PROCESS_INDEX)));
+    ProcessCommands commands = DefaultProcessCommands.main(
+        props.nonNullValueAsFile(PROPERTY_SHARED_PATH), Integer.parseInt(props.nonNullValue(PROPERTY_PROCESS_INDEX)));
     return new ProcessEntryPoint(props, new SystemExit(), commands);
   }
 }
index 89d07923427280a8c3d550dfe48b8e266875caa7..4bd68e86c7ccb19492cc83bb18f1002865dd204f 100644 (file)
@@ -45,7 +45,7 @@ public class DefaultProcessCommandsTest {
     FileUtils.deleteQuietly(dir);
 
     try {
-      new DefaultProcessCommands(dir, PROCESS_NUMBER);
+      DefaultProcessCommands.main(dir, PROCESS_NUMBER);
       fail();
     } catch (IllegalArgumentException e) {
       assertThat(e).hasMessage("Not a valid directory: " + dir.getAbsolutePath());
@@ -56,7 +56,7 @@ public class DefaultProcessCommandsTest {
   public void child_process_update_the_mapped_memory() throws Exception {
     File dir = temp.newFolder();
 
-    DefaultProcessCommands commands = new DefaultProcessCommands(dir, PROCESS_NUMBER);
+    DefaultProcessCommands commands = DefaultProcessCommands.main(dir, PROCESS_NUMBER);
     assertThat(commands.isUp()).isFalse();
 
     commands.setUp();
@@ -67,7 +67,7 @@ public class DefaultProcessCommandsTest {
   public void ask_for_stop() throws Exception {
     File dir = temp.newFolder();
 
-    DefaultProcessCommands commands = new DefaultProcessCommands(dir, PROCESS_NUMBER);
+    DefaultProcessCommands commands = DefaultProcessCommands.main(dir, PROCESS_NUMBER);
     assertThat(commands.askedForStop()).isFalse();
 
     commands.askForStop();
@@ -78,7 +78,7 @@ public class DefaultProcessCommandsTest {
   public void ask_for_restart() throws Exception {
     File dir = temp.newFolder();
 
-    DefaultProcessCommands commands = new DefaultProcessCommands(dir, PROCESS_NUMBER);
+    DefaultProcessCommands commands = DefaultProcessCommands.main(dir, PROCESS_NUMBER);
     assertThat(commands.askedForRestart()).isFalse();
 
     commands.askForRestart();
@@ -89,7 +89,7 @@ public class DefaultProcessCommandsTest {
   public void acknowledgeAskForRestart_has_no_effect_when_no_restart_asked() throws Exception {
     File dir = temp.newFolder();
 
-    DefaultProcessCommands commands = new DefaultProcessCommands(dir, PROCESS_NUMBER);
+    DefaultProcessCommands commands = DefaultProcessCommands.main(dir, PROCESS_NUMBER);
     assertThat(commands.askedForRestart()).isFalse();
 
     commands.acknowledgeAskForRestart();
@@ -100,7 +100,7 @@ public class DefaultProcessCommandsTest {
   public void acknowledgeAskForRestart_resets_askForRestart_has_no_effect_when_no_restart_asked() throws Exception {
     File dir = temp.newFolder();
 
-    DefaultProcessCommands commands = new DefaultProcessCommands(dir, PROCESS_NUMBER);
+    DefaultProcessCommands commands = DefaultProcessCommands.main(dir, PROCESS_NUMBER);
 
     commands.askForRestart();
     assertThat(commands.askedForRestart()).isTrue();
@@ -110,24 +110,43 @@ public class DefaultProcessCommandsTest {
   }
 
   @Test
-  public void constructor_fails_if_processNumber_is_less_than_0() throws Exception {
-    File dir = temp.newFolder();
+  public void main_fails_if_processNumber_is_less_than_0() throws Exception {
     int processNumber = -2;
 
-    expectedException.expect(IllegalArgumentException.class);
-    expectedException.expectMessage("Process number " + processNumber + " is not valid");
+    expectProcessNumberNoValidIAE(processNumber);
 
-    new DefaultProcessCommands(dir, processNumber);
+    DefaultProcessCommands.main(temp.newFolder(), processNumber);
   }
 
   @Test
-  public void getProcessCommands_fails_if_processNumber_is_higher_than_MAX_PROCESSES() throws Exception {
-    File dir = temp.newFolder();
+  public void main_fails_if_processNumber_is_higher_than_MAX_PROCESSES() throws Exception {
+    int processNumber = MAX_PROCESSES + 1;
+
+    expectProcessNumberNoValidIAE(processNumber);
+
+    DefaultProcessCommands.main(temp.newFolder(), processNumber);
+  }
+
+  @Test
+  public void secondary_fails_if_processNumber_is_less_than_0() throws Exception {
+    int processNumber = -2;
+
+    expectProcessNumberNoValidIAE(processNumber);
+
+    DefaultProcessCommands.secondary(temp.newFolder(), processNumber);
+  }
+
+  @Test
+  public void secondary_fails_if_processNumber_is_higher_than_MAX_PROCESSES() throws Exception {
     int processNumber = MAX_PROCESSES + 1;
 
+    expectProcessNumberNoValidIAE(processNumber);
+
+    DefaultProcessCommands.secondary(temp.newFolder(), processNumber);
+  }
+
+  private void expectProcessNumberNoValidIAE(int processNumber) {
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("Process number " + processNumber + " is not valid");
-
-    new DefaultProcessCommands(dir, processNumber);
   }
 }
index e24efc871f4050e926c6fd4af3340900a1d1d8be..3278c80ad5e88da9e1fe0fe53f2bfd864a3c475b 100644 (file)
@@ -52,7 +52,7 @@ public class ProcessCommandWrapperImpl implements ProcessCommandWrapper {
   private void call(VoidMethod command) {
     File shareDir = nonNullValueAsFile(PROPERTY_SHARED_PATH);
     int processNumber = nonNullAsInt(PROPERTY_PROCESS_INDEX);
-    try (ProcessCommands commands = new DefaultProcessCommands(shareDir, processNumber, false)) {
+    try (ProcessCommands commands = DefaultProcessCommands.secondary(shareDir, processNumber)) {
       command.callOn(commands);
     } catch (Exception e) {
       LOG.warn("Failed to close ProcessCommands", e);
index db49ac626321fc5b5658c098470d81241b585ab7..40d6774dd318c5839b3ed2c942efe2e2700375e8 100644 (file)
@@ -71,7 +71,7 @@ public class ProcessCommandWrapperImplTest {
     ProcessCommandWrapperImpl underTest = new ProcessCommandWrapperImpl(settings);
     underTest.requestSQRestart();
 
-    try (DefaultProcessCommands processCommands = new DefaultProcessCommands(tmpDir, PROCESS_NUMBER, false)) {
+    try (DefaultProcessCommands processCommands = DefaultProcessCommands.secondary(tmpDir, PROCESS_NUMBER)) {
       assertThat(processCommands.askedForRestart()).isTrue();
     }
   }
@@ -106,7 +106,7 @@ public class ProcessCommandWrapperImplTest {
     ProcessCommandWrapperImpl underTest = new ProcessCommandWrapperImpl(settings);
     underTest.notifyOperational();
 
-    try (DefaultProcessCommands processCommands = new DefaultProcessCommands(tmpDir, PROCESS_NUMBER, false)) {
+    try (DefaultProcessCommands processCommands = DefaultProcessCommands.secondary(tmpDir, PROCESS_NUMBER)) {
       assertThat(processCommands.isOperational()).isTrue();
     }
   }