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);
}
}
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
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);
}
try {
gobbler.join();
} catch (InterruptedException ignored) {
+ // consider as finished
}
}
}
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;
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");
+ }
}
import java.io.File;
import java.io.IOException;
-import java.net.ConnectException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
.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;
}
}
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;
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();
--- /dev/null
+/*
+ * 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);
+ }
+}
*/
package org.sonar.process;
+import org.slf4j.LoggerFactory;
+
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
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();
}
}
}
@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();
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
+++ /dev/null
-/*
- * 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");
- }
- }
-}