aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@gmail.com>2012-12-04 14:35:07 +0100
committerJulien Lancelot <julien.lancelot@gmail.com>2012-12-04 15:04:30 +0100
commitcdf6262009b0245c719ffefd746a996f2f48a289 (patch)
treeae450bf79559cc92e9ae35e403400c1140398565 /sonar-batch
parenteb8e412ce6d8c0b38fcda7ee4e077bddf79cb06c (diff)
downloadsonarqube-cdf6262009b0245c719ffefd746a996f2f48a289.tar.gz
sonarqube-cdf6262009b0245c719ffefd746a996f2f48a289.zip
SONAR-3306 Refactoring to use DatabaseSemaphore instead of SemaphoreDao
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java2
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/bootstrap/CheckSemaphore.java18
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/bootstrap/CheckSemaphoreTest.java32
3 files changed, 28 insertions, 24 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java
index 949030df4f6..3067a161cda 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java
@@ -48,6 +48,7 @@ import org.sonar.core.i18n.RuleI18nManager;
import org.sonar.core.metric.CacheMetricFinder;
import org.sonar.core.notification.DefaultNotificationManager;
import org.sonar.core.persistence.DaoUtils;
+import org.sonar.core.persistence.DatabaseSemaphoreImpl;
import org.sonar.core.persistence.DatabaseVersion;
import org.sonar.core.persistence.MyBatis;
import org.sonar.core.resource.DefaultResourcePermissions;
@@ -103,6 +104,7 @@ public class BatchModule extends Module {
container.addSingleton(DefaultUserFinder.class);
container.addSingleton(ResourceTypes.class);
container.addSingleton(MetricProvider.class);
+ container.addSingleton(DatabaseSemaphoreImpl.class);
container.addSingleton(CheckSemaphore.class);
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/CheckSemaphore.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/CheckSemaphore.java
index 7ea91681fe3..9bc4b65e621 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/CheckSemaphore.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/CheckSemaphore.java
@@ -24,21 +24,22 @@ import org.slf4j.LoggerFactory;
import org.sonar.api.CoreProperties;
import org.sonar.api.config.Settings;
import org.sonar.api.resources.Project;
+import org.sonar.api.utils.DatabaseSemaphore;
import org.sonar.api.utils.SonarException;
import org.sonar.batch.ProjectTree;
-import org.sonar.core.persistence.Lock;
-import org.sonar.core.persistence.SemaphoreDao;
+
+import static org.sonar.api.utils.DatabaseSemaphore.Lock;
public class CheckSemaphore {
private static final Logger LOG = LoggerFactory.getLogger(CheckSemaphore.class);
- private final SemaphoreDao semaphoreDao;
+ private final DatabaseSemaphore databaseSemaphore;
private final ProjectTree projectTree;
private final Settings settings;
- public CheckSemaphore(SemaphoreDao semaphoreDao, ProjectTree projectTree, Settings settings) {
- this.semaphoreDao = semaphoreDao;
+ public CheckSemaphore(DatabaseSemaphore databaseSemaphore, ProjectTree projectTree, Settings settings) {
+ this.databaseSemaphore = databaseSemaphore;
this.projectTree = projectTree;
this.settings = settings;
}
@@ -72,15 +73,16 @@ public class CheckSemaphore {
private Lock acquire() {
LOG.debug("Acquire semaphore on project : {}, with key {}", getProject(), getSemaphoreKey());
if (!isForceAnalyseActivated()) {
- return semaphoreDao.acquire(getSemaphoreKey());
+ return databaseSemaphore.acquire(getSemaphoreKey());
} else {
- return semaphoreDao.acquire(getSemaphoreKey(), 0);
+ // In force mode, we acquire the lock regardless there's a existing lock or not
+ return databaseSemaphore.acquire(getSemaphoreKey(), 0);
}
}
private void release() {
LOG.debug("Release semaphore on project : {}, with key {}", getProject(), getSemaphoreKey());
- semaphoreDao.release(getSemaphoreKey());
+ databaseSemaphore.release(getSemaphoreKey());
}
private String getSemaphoreKey() {
diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/CheckSemaphoreTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/CheckSemaphoreTest.java
index d003336713e..e3d09868d9a 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/CheckSemaphoreTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/CheckSemaphoreTest.java
@@ -24,10 +24,9 @@ import org.junit.Test;
import org.sonar.api.CoreProperties;
import org.sonar.api.config.Settings;
import org.sonar.api.resources.Project;
+import org.sonar.api.utils.DatabaseSemaphore;
import org.sonar.api.utils.SonarException;
import org.sonar.batch.ProjectTree;
-import org.sonar.core.persistence.Lock;
-import org.sonar.core.persistence.SemaphoreDao;
import java.util.Date;
@@ -37,12 +36,13 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+import static org.sonar.api.utils.DatabaseSemaphore.Lock;
public class CheckSemaphoreTest {
private CheckSemaphore checkSemaphore;
- private SemaphoreDao semaphoreDao;
+ private DatabaseSemaphore databaseSemaphore;
private ProjectTree projectTree;
private Settings settings;
@@ -53,9 +53,9 @@ public class CheckSemaphoreTest {
public void setUp() {
lock = mock(Lock.class);
- semaphoreDao = mock(SemaphoreDao.class);
- when(semaphoreDao.acquire(anyString())).thenReturn(lock);
- when(semaphoreDao.acquire(anyString(), anyInt())).thenReturn(lock);
+ databaseSemaphore = mock(DatabaseSemaphore.class);
+ when(databaseSemaphore.acquire(anyString())).thenReturn(lock);
+ when(databaseSemaphore.acquire(anyString(), anyInt())).thenReturn(lock);
projectTree = mock(ProjectTree.class);
settings = new Settings();
@@ -65,7 +65,7 @@ public class CheckSemaphoreTest {
project = new Project("key", "branch", "name");
when(projectTree.getRootProject()).thenReturn(project);
- checkSemaphore = new CheckSemaphore(semaphoreDao, projectTree, settings);
+ checkSemaphore = new CheckSemaphore(databaseSemaphore, projectTree, settings);
}
@Test
@@ -73,7 +73,7 @@ public class CheckSemaphoreTest {
when(lock.isAcquired()).thenReturn(true);
checkSemaphore.start();
- verify(semaphoreDao).acquire(anyString());
+ verify(databaseSemaphore).acquire(anyString());
}
@Test
@@ -84,7 +84,7 @@ public class CheckSemaphoreTest {
when(lock.isAcquired()).thenReturn(true);
checkSemaphore.start();
- verify(semaphoreDao).acquire("batch-key");
+ verify(databaseSemaphore).acquire("batch-key");
}
@Test
@@ -92,7 +92,7 @@ public class CheckSemaphoreTest {
when(lock.isAcquired()).thenReturn(true);
checkSemaphore.start();
- verify(semaphoreDao).acquire("batch-key:branch");
+ verify(databaseSemaphore).acquire("batch-key:branch");
}
@Test
@@ -100,7 +100,7 @@ public class CheckSemaphoreTest {
setForceMode(true);
when(lock.isAcquired()).thenReturn(true);
checkSemaphore.start();
- verify(semaphoreDao).acquire(anyString(), anyInt());
+ verify(databaseSemaphore).acquire(anyString(), anyInt());
}
@Test(expected = SonarException.class)
@@ -108,7 +108,7 @@ public class CheckSemaphoreTest {
when(lock.getLocketAt()).thenReturn(new Date());
when(lock.isAcquired()).thenReturn(false);
checkSemaphore.start();
- verify(semaphoreDao, never()).acquire(anyString());
+ verify(databaseSemaphore, never()).acquire(anyString());
}
@Test
@@ -116,21 +116,21 @@ public class CheckSemaphoreTest {
setDryRunMode(true);
settings = new Settings().setProperty(CoreProperties.DRY_RUN, true);
checkSemaphore.start();
- verify(semaphoreDao, never()).acquire(anyString());
- verify(semaphoreDao, never()).acquire(anyString(), anyInt());
+ verify(databaseSemaphore, never()).acquire(anyString());
+ verify(databaseSemaphore, never()).acquire(anyString(), anyInt());
}
@Test
public void shouldReleaseSemaphore() {
checkSemaphore.stop();
- verify(semaphoreDao).release(anyString());
+ verify(databaseSemaphore).release(anyString());
}
@Test
public void shouldNotReleaseSemaphoreInDryRunMode() {
setDryRunMode(true);
checkSemaphore.stop();
- verify(semaphoreDao, never()).release(anyString());
+ verify(databaseSemaphore, never()).release(anyString());
}
private void setDryRunMode(boolean isInDryRunMode) {