aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephane Gamard <stephane.gamard@searchbox.com>2014-08-07 13:55:12 +0200
committerStephane Gamard <stephane.gamard@searchbox.com>2014-08-07 14:01:17 +0200
commitb4695eb44ce28f23e302b33bb95eafbdfefe6a2e (patch)
tree99c2f8049d2e87b25d6f561e037cf2ed2ac19096
parent0dc1822dc8342b18bd4063373ba43754a5e76e81 (diff)
downloadsonarqube-b4695eb44ce28f23e302b33bb95eafbdfefe6a2e.tar.gz
sonarqube-b4695eb44ce28f23e302b33bb95eafbdfefe6a2e.zip
SONAR-4898 - Created a DummyOKApplication for Unit test of ProcessWrapper
-rw-r--r--server/sonar-process-test/pom.xml87
-rw-r--r--server/sonar-process-test/src/main/java/org/sonar/DummyOKApplication.java39
-rw-r--r--server/sonar-process-test/src/main/resources/org/sonar/search/logback.xml35
-rw-r--r--server/sonar-process-test/src/test/resources/logback-test.xml38
-rw-r--r--server/sonar-process/src/main/java/org/sonar/process/MonitoredProcess.java3
-rw-r--r--server/sonar-search/src/test/java/org/sonar/search/SearchServerTest.java28
6 files changed, 230 insertions, 0 deletions
diff --git a/server/sonar-process-test/pom.xml b/server/sonar-process-test/pom.xml
new file mode 100644
index 00000000000..67b012ae4d2
--- /dev/null
+++ b/server/sonar-process-test/pom.xml
@@ -0,0 +1,87 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <parent>
+ <groupId>org.codehaus.sonar</groupId>
+ <artifactId>server</artifactId>
+ <version>4.5-SNAPSHOT</version>
+ <relativePath>../</relativePath>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+
+ <artifactId>sonar-process-test</artifactId>
+ <packaging>jar</packaging>
+ <name>SonarQube :: Search</name>
+ <description>Wrapper to start Elasticsearch</description>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.codehaus.sonar</groupId>
+ <artifactId>sonar-process</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.elasticsearch</groupId>
+ <artifactId>elasticsearch</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>com.google.code.findbugs</groupId>
+ <artifactId>jsr305</artifactId>
+ <scope>provided</scope>
+ </dependency>
+
+
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-classic</artifactId>
+ <scope>runtime</scope>
+ </dependency>
+
+ <!-- testing -->
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.easytesting</groupId>
+ <artifactId>fest-assert</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.hamcrest</groupId>
+ <artifactId>hamcrest-all</artifactId>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-shade-plugin</artifactId>
+ <version>2.3</version>
+ <executions>
+ <execution>
+ <phase>package</phase>
+ <goals>
+ <goal>shade</goal>
+ </goals>
+ <configuration>
+ <minimizeJar>true</minimizeJar>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git a/server/sonar-process-test/src/main/java/org/sonar/DummyOKApplication.java b/server/sonar-process-test/src/main/java/org/sonar/DummyOKApplication.java
new file mode 100644
index 00000000000..a862e1b3ab3
--- /dev/null
+++ b/server/sonar-process-test/src/main/java/org/sonar/DummyOKApplication.java
@@ -0,0 +1,39 @@
+package org.sonar;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonar.process.MonitoredProcess;
+import org.sonar.process.Props;
+
+import java.util.Properties;
+
+public class DummyOKApplication extends MonitoredProcess {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(DummyOKApplication.class);
+
+ protected DummyOKApplication(Props props) throws Exception {
+ super(props);
+ }
+
+ @Override
+ protected void doStart() {
+ LOGGER.info("Starting Dummy OK Process");
+ }
+
+ @Override
+ protected void doTerminate() {
+ LOGGER.info("Terminating Dummy OK Process");
+ }
+
+ @Override
+ protected boolean doIsReady() {
+ return false;
+ }
+
+ public static void main(String... args) throws Exception {
+ Props props = new Props(new Properties());
+ props.set(MonitoredProcess.NAME_PROPERTY, DummyOKApplication.class.getSimpleName());
+ new DummyOKApplication(props).start();
+ System.exit(1);
+ }
+}
diff --git a/server/sonar-process-test/src/main/resources/org/sonar/search/logback.xml b/server/sonar-process-test/src/main/resources/org/sonar/search/logback.xml
new file mode 100644
index 00000000000..648ece82e28
--- /dev/null
+++ b/server/sonar-process-test/src/main/resources/org/sonar/search/logback.xml
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<!--
+ Configuration for default logger. Only used while embedded server is starting,
+ before proper logging configuration is loaded.
+
+ See http://logback.qos.ch/manual/configuration.html
+-->
+<configuration debug="false">
+ <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator"/>
+
+ <appender name="LOGFILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
+ <File>${sonar.path.logs}/search.log</File>
+ <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
+ <param name="FileNamePattern" value="${sonar.path.logs}/search.%i.log"/>
+ <param name="MinIndex" value="1"/>
+ <param name="MaxIndex" value="3"/>
+ </rollingPolicy>
+ <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
+ <param name="MaxFileSize" value="5MB"/>
+ </triggeringPolicy>
+ <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
+ <!-- Use %d{yyyy.MM.dd HH:mm:ss.SSS} to display milliseconds -->
+ <pattern>
+ %d{yyyy.MM.dd HH:mm:ss} %-5level [%logger{20}] %X %msg%n
+ </pattern>
+ </encoder>
+ </appender>
+
+ <root>
+ <level value="INFO"/>
+ <appender-ref ref="LOGFILE"/>
+ </root>
+
+</configuration>
diff --git a/server/sonar-process-test/src/test/resources/logback-test.xml b/server/sonar-process-test/src/test/resources/logback-test.xml
new file mode 100644
index 00000000000..ff2270cc122
--- /dev/null
+++ b/server/sonar-process-test/src/test/resources/logback-test.xml
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<!--
+ Configuration for default logger. Only used while embedded server is starting,
+ before proper logging configuration is loaded.
+
+ See http://logback.qos.ch/manual/configuration.html
+-->
+<configuration debug="false">
+ <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator"/>
+
+ <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
+ <filter class="ch.qos.logback.classic.filter.LevelFilter">
+ <level>INFO</level>
+ <onMatch>ACCEPT</onMatch>
+ <onMismatch>DENY</onMismatch>
+ </filter>
+ <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
+ <pattern>
+ %d{yyyy.MM.dd HH:mm:ss} %-5level %msg%n
+ </pattern>
+ </encoder>
+ </appender>
+
+ <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
+ <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
+ <pattern>
+ %d{yyyy.MM.dd HH:mm:ss} %-5level %msg%n
+ </pattern>
+ </encoder>
+ </appender>
+
+ <root>
+ <level value="DEBUG"/>
+ <appender-ref ref="CONSOLE"/>
+ </root>
+
+</configuration>
diff --git a/server/sonar-process/src/main/java/org/sonar/process/MonitoredProcess.java b/server/sonar-process/src/main/java/org/sonar/process/MonitoredProcess.java
index 20cf4e62a07..96cea190695 100644
--- a/server/sonar-process/src/main/java/org/sonar/process/MonitoredProcess.java
+++ b/server/sonar-process/src/main/java/org/sonar/process/MonitoredProcess.java
@@ -96,6 +96,8 @@ public abstract class MonitoredProcess implements ProcessMXBean {
@Override
public final void terminate() {
if (monitor != null) {
+ Logger logger = LoggerFactory.getLogger(getClass());
+ logger.debug("Process[{}] terminating", name);
monitor.shutdownNow();
monitor = null;
if (pingTask != null) {
@@ -108,6 +110,7 @@ public abstract class MonitoredProcess implements ProcessMXBean {
LoggerFactory.getLogger(getClass()).error("Fail to terminate " + name, e);
// do not propagate exception
}
+ logger.debug("Process[{}] terminated", name);
}
}
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
index 05b073a1a51..8bc1f4bca66 100644
--- a/server/sonar-search/src/test/java/org/sonar/search/SearchServerTest.java
+++ b/server/sonar-search/src/test/java/org/sonar/search/SearchServerTest.java
@@ -80,6 +80,34 @@ public class SearchServerTest {
}
@Test
+ public void server_fail_to_start() throws Exception {
+ Properties properties = new Properties();
+ properties.setProperty(MonitoredProcess.NAME_PROPERTY, "ES");
+
+
+ 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).isEqualTo(100);
+
+ }
+
+ @Test
public void can_connect() throws Exception {
Properties properties = new Properties();
properties.setProperty(MonitoredProcess.NAME_PROPERTY, "ES");