SLING-2750 moved the null check on instance inside the synchronised method to conform to rules about double null checks and synchronised blocks.
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1450003 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java b/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java
index 85a46aa..919d2b6 100644
--- a/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java
+++ b/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java
@@ -84,6 +84,8 @@
/** Means "don't care about Content-Type" in getContent(...) methods */
public static final String CONTENT_TYPE_DONTCARE = "*";
+ private static final Object startupCheckLock = new Object();
+
/** URLs stored here are deleted in tearDown */
protected final List<String> urlsToDelete = new LinkedList<String>();
@@ -196,17 +198,21 @@
*/
protected void waitForSlingStartup() throws Exception {
// Use a static flag to make sure this runs only once in our test suite
- if (slingStartupOk != null) {
- if(slingStartupOk) {
+ // we must synchronize on this if we don't 2 threads could enter the check concurrently
+ // which would leave to random results.
+ synchronized(startupCheckLock) {
+ if (slingStartupOk != null) {
+ if(slingStartupOk) {
+ return;
+ }
+ fail("Sling services not available. Already checked in earlier tests.");
+ }
+ if ( System.getProperty(PROPERTY_SKIP_STARTUP_CHECK) != null ) {
+ slingStartupOk = true;
return;
}
- fail("Sling services not available. Already checked in earlier tests.");
+ slingStartupOk = false;
}
- if ( System.getProperty(PROPERTY_SKIP_STARTUP_CHECK) != null ) {
- slingStartupOk = true;
- return;
- }
- slingStartupOk = false;
System.err.println("Checking if the required Sling services are started (timeout " + READY_TIMEOUT_SECONDS + " seconds)...");
System.err.println("(base URLs=" + HTTP_BASE_URL + " and " + WEBDAV_BASE_URL + "; servlet context="+ SERVLET_CONTEXT +")");
diff --git a/src/main/java/org/apache/sling/commons/testing/jcr/RepositoryProvider.java b/src/main/java/org/apache/sling/commons/testing/jcr/RepositoryProvider.java
index e78a4f9..1ab2742 100644
--- a/src/main/java/org/apache/sling/commons/testing/jcr/RepositoryProvider.java
+++ b/src/main/java/org/apache/sling/commons/testing/jcr/RepositoryProvider.java
@@ -44,13 +44,9 @@
private RepositoryProvider() {
}
- public static RepositoryProvider instance() {
+ public synchronized static RepositoryProvider instance() {
if(INSTANCE == null) {
- synchronized (RepositoryProvider.class) {
- if(INSTANCE == null) {
- INSTANCE = new RepositoryProvider();
- }
- }
+ INSTANCE = new RepositoryProvider();
}
return INSTANCE;
}
@@ -58,18 +54,12 @@
/** Return a SlingRepository. First call initializes it, and a JVM
* shutdown hook is registered to stop it.
**/
- public SlingRepository getRepository() throws RepositoryException {
- if(repository != null) {
- return repository;
+ public synchronized SlingRepository getRepository() throws RepositoryException {
+ if(repository == null) {
+ RepositoryUtil.startRepository();
+ repository = RepositoryUtil.getRepository();
+ Runtime.getRuntime().addShutdownHook(new ShutdownThread());
}
-
- synchronized (RepositoryTestBase.class) {
- if(repository == null) {
- RepositoryUtil.startRepository();
- repository = RepositoryUtil.getRepository();
- Runtime.getRuntime().addShutdownHook(new ShutdownThread());
- }
- return repository;
- }
+ return repository;
}
}
\ No newline at end of file