From af36f335155b2ed4d3a3579938b766263b7d7c3a Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Fri, 18 Mar 2011 12:08:53 +0100 Subject: [PATCH] SONAR-2289 Increase timeouts of embedded database startup/shutdown --- .../server/database/EmbeddedDatabase.java | 47 +++++++++++-------- .../server/database/EmbeddedDatabaseTest.java | 25 ++++++---- 2 files changed, 44 insertions(+), 28 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/database/EmbeddedDatabase.java b/sonar-server/src/main/java/org/sonar/server/database/EmbeddedDatabase.java index 16b907b3592..0543528de6b 100644 --- a/sonar-server/src/main/java/org/sonar/server/database/EmbeddedDatabase.java +++ b/sonar-server/src/main/java/org/sonar/server/database/EmbeddedDatabase.java @@ -64,7 +64,7 @@ public class EmbeddedDatabase { String dirName = configuration.getString(DatabaseProperties.PROP_EMBEDDED_DATA_DIR); if (dirName == null) { File sonarHome = new File(configuration.getString(CoreProperties.SONAR_HOME)); - if ( !sonarHome.isDirectory() || !sonarHome.exists()) { + if (!sonarHome.isDirectory() || !sonarHome.exists()) { throw new ServerStartException("Sonar home directory does not exist"); } return new File(sonarHome, "data"); @@ -80,7 +80,7 @@ public class EmbeddedDatabase { if (dbHome.exists() && !dbHome.isDirectory()) { throw new SonarException("Database home " + dbHome.getPath() + " is not a directory"); } - if ( !dbHome.exists()) { + if (!dbHome.exists()) { dbHome.mkdirs(); } System.setProperty("derby.system.home", dbHome.getPath()); @@ -95,7 +95,7 @@ public class EmbeddedDatabase { int port = Integer.parseInt(dbProps.getProperty("derby.drda.portNumber")); String host = dbProps.getProperty("derby.drda.host"); serverControl = new NetworkServerControl(InetAddress.getByName(host), port, DEFAULT_USER, DEFAULT_PWD); - Logs.INFO.info("Embedded database: " + serverControl); + Logs.INFO.info("Starting embedded database on port " + port); serverControl.start(dbLog); ensureServerIsUp(); } catch (Exception e) { @@ -122,44 +122,53 @@ public class EmbeddedDatabase { if (serverControl != null) { try { serverControl.shutdown(); + ensureServerIsDown(); + serverControl = null; + Logs.INFO.info("Embedded database stopped"); + } catch (Exception e) { throw new SonarException(e); } - ensureServerIsDown(); - serverControl = null; - Logs.INFO.info("Embedded database stopped."); } } private void ensureServerIsUp() { - for (int retry = 0; retry < 16; retry++) { + for (int retry = 0; retry < 100; retry++) { try { serverControl.ping(); return; + } catch (Exception ex) { - sleep(250); + sleep(300); } } throw new SonarException("Embedded database does not respond to ping requests"); } - private void sleep(long time) { - try { - Thread.sleep(time); - } catch (InterruptedException e) { - } - } - private void ensureServerIsDown() { - for (int retry = 0; retry < 16; retry++) { + for (int retry = 0; retry < 100; retry++) { try { serverControl.ping(); - sleep(250); - } catch (Exception ex) { + sleep(300); + + } catch (SonarException se) { + throw se; + + } catch (Exception e) { + // normal case: the database does not respond to ping return; } } - throw new SonarException("Embedded database is not stopped"); + throw new SonarException("Fail to stop embedded database"); + } + + + private void sleep(long time) { + try { + Thread.sleep(time); + } catch (InterruptedException e) { + throw new SonarException("Fail to ping embedded database", e); + } } public static Properties getDefaultProperties(Configuration configuration) { diff --git a/sonar-server/src/test/java/org/sonar/server/database/EmbeddedDatabaseTest.java b/sonar-server/src/test/java/org/sonar/server/database/EmbeddedDatabaseTest.java index b58d1b6c1cc..a75dd1fca40 100644 --- a/sonar-server/src/test/java/org/sonar/server/database/EmbeddedDatabaseTest.java +++ b/sonar-server/src/test/java/org/sonar/server/database/EmbeddedDatabaseTest.java @@ -19,10 +19,12 @@ */ package org.sonar.server.database; -import junit.framework.TestCase; import org.apache.commons.configuration.CompositeConfiguration; import org.apache.commons.io.FileUtils; import org.apache.derby.jdbc.ClientDriver; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import java.io.File; import java.io.IOException; @@ -31,7 +33,10 @@ import java.sql.Connection; import java.sql.DriverManager; import java.util.Properties; -public class EmbeddedDatabaseTest extends TestCase { +import static junit.framework.Assert.fail; +import static org.junit.Assert.assertTrue; + +public class EmbeddedDatabaseTest { private final static String TEST_ROOT_DIR = "./target/"; private final static String TEST_DB_DIR_PREFIX = "testDB"; @@ -41,8 +46,8 @@ public class EmbeddedDatabaseTest extends TestCase { private Properties defaultProps; private static String testPort; - @Override - protected void setUp() throws Exception { + @Before + public void setUp() throws Exception { windowsCleanup(); if (testPort == null) { testPort = Integer.toString(findFreeServerPort()); @@ -77,11 +82,12 @@ public class EmbeddedDatabaseTest extends TestCase { return port; } - public void testStart() throws Exception { + @Test + public void shouldStart() throws Exception { database = new EmbeddedDatabase(new File(TEST_ROOT_DIR + TEST_DB_DIR_PREFIX + "Start" + testPort), defaultProps); database.start(); ClientDriver.class.newInstance(); - Connection conn = null; + Connection conn; try { conn = DriverManager.getConnection(driverUrl); conn.close(); @@ -102,11 +108,12 @@ public class EmbeddedDatabaseTest extends TestCase { database.stop(); } - public void testStop() throws Exception { + @Test + public void shouldStop() throws Exception { database = new EmbeddedDatabase(new File(TEST_ROOT_DIR + TEST_DB_DIR_PREFIX + "Stop" + testPort), defaultProps); database.start(); ClientDriver.class.newInstance(); - Connection conn = null; + Connection conn; try { conn = DriverManager.getConnection(driverUrl); conn.close(); @@ -123,7 +130,7 @@ public class EmbeddedDatabaseTest extends TestCase { } } - @Override + @After public void tearDown() throws IOException { if (database.getDataDir().exists()) { String os = System.getProperty("os.name"); -- 2.39.5