]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4898 fix quality flaws
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 12 Sep 2014 15:46:05 +0000 (17:46 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 12 Sep 2014 15:46:05 +0000 (17:46 +0200)
server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/RmiJmxConnector.java
server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/StreamGobbler.java
server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/Timeouts.java
server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/JavaCommandTest.java
server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/MonitorTest.java
server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/RmiJmxConnectorTest.java
server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/StreamGobblerTest.java [new file with mode: 0644]
server/sonar-process/src/main/java/org/sonar/process/StopperThread.java
server/sonar-process/src/test/java/org/sonar/process/JmxUtilsTest.java
server/sonar-search/src/test/java/org/sonar/search/SearchServerTest.java [deleted file]

index 876ccc2b22f6fc108cb33f1eb9f522a20c26b042..68dfc34182b3eaaedbe6dc129e3450ca16c5a76f 100644 (file)
@@ -68,8 +68,6 @@ class RmiJmxConnector implements JmxConnector {
     ProcessMXBean mxBean = execute(callable, timeoutMs);
     if (mxBean != null) {
       register(processRef, mxBean);
-    } else if (!processRef.isTerminated()) {
-      throw new IllegalStateException("Fail to connect to JMX RMI server of " + processRef);
     }
   }
 
index 55d95c8a4675cfe4847909e28c00a6d70dcc8031..cb412965f5534fcf85c223daff4c88b46eabdedd 100644 (file)
@@ -38,9 +38,13 @@ class StreamGobbler extends Thread {
   private final Logger logger;
 
   StreamGobbler(InputStream is, String processKey) {
+    this(is, processKey, LoggerFactory.getLogger(processKey));
+  }
+
+  StreamGobbler(InputStream is, String processKey, Logger logger) {
     super(String.format("Gobbler[%s]", processKey));
     this.is = is;
-    this.logger = LoggerFactory.getLogger(processKey);
+    this.logger = logger;
   }
 
   @Override
@@ -51,8 +55,8 @@ class StreamGobbler extends Thread {
       while ((line = br.readLine()) != null) {
         logger.info(line);
       }
-    } catch (Exception ignored) {
-
+    } catch (Exception e) {
+      logger.error("Fail to read process logs", e);
     } finally {
       IOUtils.closeQuietly(br);
     }
@@ -63,6 +67,7 @@ class StreamGobbler extends Thread {
       try {
         gobbler.join();
       } catch (InterruptedException ignored) {
+        // consider as finished
       }
     }
   }
index 68934f21066c05fb85691dfb6555921d6b70f34e..a3f7210f37fd650c7f4dc07b8928c0ff27bc9ff1 100644 (file)
@@ -25,7 +25,7 @@ package org.sonar.process.monitor;
 class Timeouts {
 
   private long terminationTimeout = 120000L;
-  private long jmxConnectionTimeout = 30000L;
+  private long jmxConnectionTimeout = 15000L;
   private long monitorPingInterval = 3000L;
   private long monitorIsReadyTimeout = 10000L;
   private long autokillPingTimeout = 60000L;
index 766f24c86fcbc195a91f8df31665d2a0b9255d2a..8744fb73ff32640c95a7200056b1bafb0122145b 100644 (file)
@@ -76,4 +76,11 @@ public class JavaCommandTest {
     command.addJavaOption("-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005");
     assertThat(command.isDebugMode()).isTrue();
   }
+
+  @Test
+  public void split_java_options() throws Exception {
+    JavaCommand command = new JavaCommand("foo");
+    command.addJavaOptions("-Xmx512m -Xms256m -Dfoo");
+    assertThat(command.getJavaOptions()).containsOnly("-Xmx512m", "-Xms256m", "-Dfoo");
+  }
 }
index 6f0be0eb5be4c1cd5915e3aab8d4461e49375fb8..043054c40eeb8bdc44f346adff3d64d28ad2efb8 100644 (file)
@@ -33,7 +33,6 @@ import org.sonar.process.SystemExit;
 
 import java.io.File;
 import java.io.IOException;
-import java.net.ConnectException;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
@@ -359,10 +358,7 @@ public class MonitorTest {
           .readTimeout(500).connectTimeout(500);
         return httpRequest.ok() && httpRequest.body().equals("ping");
       } catch (HttpRequest.HttpRequestException e) {
-        if (e.getCause() instanceof ConnectException) {
-          return false;
-        }
-        throw new IllegalStateException("Fail to know the process status", e);
+        return false;
       }
     }
 
index c4a0f735d60d3712a53ceb28fa9446687c89eadf..2289c446566d89afe5c06bb34abd2c65e8681687 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.process.monitor;
 import org.junit.Test;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
+import org.sonar.process.NetworkUtils;
 import org.sonar.process.ProcessMXBean;
 
 import static org.fest.assertions.Assertions.assertThat;
@@ -31,6 +32,20 @@ import static org.mockito.Mockito.when;
 
 public class RmiJmxConnectorTest {
 
+  @Test
+  public void fail_to_connect_in_timely_fashion() throws Exception {
+    RmiJmxConnector connector = new RmiJmxConnector();
+    ProcessRef ref = mock(ProcessRef.class);
+
+    JavaCommand command = new JavaCommand("foo").setJmxPort(NetworkUtils.freePort());
+    try {
+      connector.connect(command, ref, 0L);
+      fail();
+    } catch (IllegalStateException e) {
+      // ok
+    }
+  }
+
   @Test
   public void throw_exception_on_timeout() throws Exception {
     RmiJmxConnector connector = new RmiJmxConnector();
diff --git a/server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/StreamGobblerTest.java b/server/sonar-process-monitor/src/test/java/org/sonar/process/monitor/StreamGobblerTest.java
new file mode 100644 (file)
index 0000000..16ebb28
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.monitor;
+
+import org.apache.commons.io.IOUtils;
+import org.junit.Test;
+import org.slf4j.Logger;
+
+import java.io.InputStream;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.verifyZeroInteractions;
+
+public class StreamGobblerTest {
+
+  @Test
+  public void forward_stream_to_log() throws Exception {
+    InputStream stream = IOUtils.toInputStream("one\nsecond log\nthird log\n");
+    Logger logger = mock(Logger.class);
+
+    StreamGobbler gobbler = new StreamGobbler(stream, "WEB", logger);
+    verifyZeroInteractions(logger);
+
+    gobbler.start();
+    StreamGobbler.waitUntilFinish(gobbler);
+
+    verify(logger).info("one");
+    verify(logger).info("second log");
+    verify(logger).info("third log");
+    verifyNoMoreInteractions(logger);
+  }
+}
index 2d0c6734b3089a7e2c060c86cba97ae910893ed9..97e2183e985ea79c772f3957c62ad69a944a03c0 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.sonar.process;
 
+import org.slf4j.LoggerFactory;
+
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
@@ -50,7 +52,8 @@ class StopperThread extends Thread {
     try {
       future.get(terminationTimeout, TimeUnit.MILLISECONDS);
     } catch (Exception e) {
-      future.cancel(true);
+      LoggerFactory.getLogger(getClass()).error("Can not terminate in " + terminationTimeout + "ms", e);
+    } finally {
       executor.shutdownNow();
     }
   }
index 599ea5d7a3010ba703dc8e3373a25456fb921c21..0e9d320cd74801d9679f1d71526747d1c931500c 100644 (file)
@@ -77,7 +77,7 @@ public class JmxUtilsTest {
   }
 
   @Test
-  public void testRegisterMBean() throws Exception {
+  public void register_mbean() throws Exception {
     // 0 Get mbServer and create out test MXBean
     MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
     MyBean mxBean = new MyBean();
@@ -87,6 +87,13 @@ public class JmxUtilsTest {
     assertThat(mbeanServer.isRegistered(objectName)).isFalse();
     JmxUtils.registerMBean(mxBean, mxBean.getClass().getSimpleName());
     assertThat(mbeanServer.isRegistered(objectName)).isTrue();
+
+    try {
+      JmxUtils.registerMBean(new Object(), "");
+      fail();
+    } catch (IllegalStateException e) {
+      // ok
+    }
   }
 
   @Test
diff --git a/server/sonar-search/src/test/java/org/sonar/search/SearchServerTest.java b/server/sonar-search/src/test/java/org/sonar/search/SearchServerTest.java
deleted file mode 100644 (file)
index 54fb024..0000000
+++ /dev/null
@@ -1,155 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.search;
-
-import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
-import org.elasticsearch.client.transport.TransportClient;
-import org.elasticsearch.common.settings.ImmutableSettings;
-import org.elasticsearch.common.settings.Settings;
-import org.elasticsearch.common.transport.InetSocketTransportAddress;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Ignore;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
-import org.sonar.process.JmxUtils;
-import org.sonar.process.Props;
-
-import javax.management.InstanceNotFoundException;
-import javax.management.MBeanRegistrationException;
-import javax.management.MBeanServer;
-
-import java.io.IOException;
-import java.lang.management.ManagementFactory;
-import java.net.ServerSocket;
-import java.util.Properties;
-
-import static org.fest.assertions.Assertions.assertThat;
-import static org.junit.Assert.fail;
-
-@Ignore
-public class SearchServerTest {
-
-  @Rule
-  public TemporaryFolder temp = new TemporaryFolder();
-
-  SearchServer searchServer;
-  int freePort;
-  int freeESPort;
-
-  @Before
-  public void setup() throws IOException {
-    ServerSocket socket = new ServerSocket(0);
-    freePort = socket.getLocalPort();
-    socket.close();
-
-    socket = new ServerSocket(0);
-    freeESPort = socket.getLocalPort();
-    socket.close();
-  }
-
-  @After
-  public void tearDown() throws MBeanRegistrationException, InstanceNotFoundException {
-    resetMBeanServer();
-  }
-
-  private void resetMBeanServer() throws MBeanRegistrationException, InstanceNotFoundException {
-    try {
-      MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
-      mbeanServer.unregisterMBean(JmxUtils.objectName("ES"));
-    } catch (Exception e) {
-      e.printStackTrace();
-    }
-  }
-
-  @Test
-  public void server_fail_to_start() throws Exception {
-    Properties properties = new Properties();
-
-    searchServer = new SearchServer(new Props(properties));
-    new Thread(new Runnable() {
-      @Override
-      public void run() {
-        searchServer.start();
-      }
-    }).start();
-    assertThat(searchServer.isReady()).isFalse();
-
-    int count = 0;
-    while (!searchServer.isReady() && count < 5) {
-      try {
-        Thread.sleep(100);
-      } catch (InterruptedException e) {
-        e.printStackTrace();
-      }
-      count++;
-    }
-    assertThat(count).isEqualTo(5);
-  }
-
-  @Test
-  public void can_connect() throws Exception {
-    Properties properties = new Properties();
-    properties.setProperty(SearchServer.SONAR_PATH_DATA, temp.newFolder().getAbsolutePath());
-    properties.setProperty(SearchServer.SONAR_PATH_TEMP, temp.newFolder().getAbsolutePath());
-    properties.setProperty(SearchServer.SONAR_PATH_LOG, temp.newFolder().getAbsolutePath());
-    properties.setProperty(SearchServer.ES_PORT_PROPERTY, Integer.toString(freeESPort));
-    properties.setProperty(SearchServer.ES_CLUSTER_PROPERTY, "sonarqube");
-
-    searchServer = new SearchServer(new Props(properties));
-    new Thread(new Runnable() {
-      @Override
-      public void run() {
-        searchServer.start();
-      }
-    }).start();
-    assertThat(searchServer.isReady()).isFalse();
-
-    int count = 0;
-    while (!searchServer.isReady() && count < 100) {
-      try {
-        Thread.sleep(500);
-      } catch (InterruptedException e) {
-        e.printStackTrace();
-      }
-      count++;
-    }
-    assertThat(count).isLessThan(100);
-
-    Settings settings = ImmutableSettings.settingsBuilder()
-      .put("cluster.name", "sonarqube")
-      .build();
-    TransportClient client = new TransportClient(settings)
-      .addTransportAddress(new InetSocketTransportAddress("localhost", freeESPort));
-
-    // 0 assert that we have a OK cluster available
-    assertThat(client.admin().cluster().prepareClusterStats().get().getStatus()).isEqualTo(ClusterHealthStatus.GREEN);
-
-    // 2 assert that we can shut down ES
-    searchServer.terminate();
-    try {
-      client.admin().cluster().prepareClusterStats().get().getStatus();
-      fail();
-    } catch (Exception e) {
-      assertThat(e.getMessage()).isEqualTo("No node available");
-    }
-  }
-}