diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2012-12-14 18:27:29 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2012-12-14 18:27:29 +0100 |
commit | af07212b764549c16cd252a9311bb68c642e8179 (patch) | |
tree | 2276df7ea25c510e83da3c70e90d93f75e07e4b2 /sonar-batch | |
parent | 189a12a09eb4fd34b632fb2582eb479e15a08125 (diff) | |
download | sonarqube-af07212b764549c16cd252a9311bb68c642e8179.tar.gz sonarqube-af07212b764549c16cd252a9311bb68c642e8179.zip |
SONAR-3887 refactor semaphore API
Diffstat (limited to 'sonar-batch')
-rw-r--r-- | sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java | 6 | ||||
-rw-r--r-- | sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectLock.java (renamed from sonar-batch/src/main/java/org/sonar/batch/bootstrap/CheckSemaphore.java) | 43 | ||||
-rw-r--r-- | sonar-batch/src/test/java/org/sonar/batch/bootstrap/ProjectLockTest.java (renamed from sonar-batch/src/test/java/org/sonar/batch/bootstrap/CheckSemaphoreTest.java) | 73 |
3 files changed, 46 insertions, 76 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 94448f6f16f..bedc8578223 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,7 +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.SemaphoresImpl; import org.sonar.core.persistence.DatabaseVersion; import org.sonar.core.persistence.MyBatis; import org.sonar.core.resource.DefaultResourcePermissions; @@ -104,8 +104,8 @@ 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); + container.addSingleton(SemaphoresImpl.class); + container.addSingleton(ProjectLock.class); } private void registerDatabaseComponents() { diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/CheckSemaphore.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectLock.java index 9bc4b65e621..33a4620a4fc 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/CheckSemaphore.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectLock.java @@ -24,44 +24,42 @@ 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.Semaphores; import org.sonar.api.utils.SonarException; import org.sonar.batch.ProjectTree; -import static org.sonar.api.utils.DatabaseSemaphore.Lock; +public class ProjectLock { -public class CheckSemaphore { + private static final Logger LOG = LoggerFactory.getLogger(ProjectLock.class); - private static final Logger LOG = LoggerFactory.getLogger(CheckSemaphore.class); - - private final DatabaseSemaphore databaseSemaphore; + private final Semaphores semaphores; private final ProjectTree projectTree; private final Settings settings; - public CheckSemaphore(DatabaseSemaphore databaseSemaphore, ProjectTree projectTree, Settings settings) { - this.databaseSemaphore = databaseSemaphore; + public ProjectLock(Semaphores semaphores, ProjectTree projectTree, Settings settings) { + this.semaphores = semaphores; this.projectTree = projectTree; this.settings = settings; } public void start() { if (!isInDryRunMode()) { - Lock lock = acquire(); - if (!lock.isAcquired()) { - LOG.error(getErrorMessage(lock)); + Semaphores.Semaphore semaphore = acquire(); + if (!semaphore.isLocked()) { + LOG.error(getErrorMessage(semaphore)); throw new SonarException("The project is already been analysing."); } } } - private String getErrorMessage(Lock lock) { - long duration = lock.getDurationSinceLocked(); + private String getErrorMessage(Semaphores.Semaphore semaphore) { + long duration = semaphore.getDurationSinceLocked(); DurationLabel durationLabel = new DurationLabel(); String durationDisplay = durationLabel.label(duration); - return "It looks like an analysis of '"+ getProject().getName() +"' is already running (started "+ durationDisplay +"). " + - "If this is not the case, it probably means that previous analysis was interrupted " + - "and you should then force a re-run by using the option '"+ CoreProperties.FORCE_ANALYSIS +"=true'."; + return "It looks like an analysis of '" + getProject().getName() + "' is already running (started " + durationDisplay + "). " + + "If this is not the case, it probably means that previous analysis was interrupted " + + "and you should then force a re-run by using the option '" + CoreProperties.FORCE_ANALYSIS + "=true'."; } public void stop() { @@ -70,19 +68,18 @@ public class CheckSemaphore { } } - private Lock acquire() { + private Semaphores.Semaphore acquire() { LOG.debug("Acquire semaphore on project : {}, with key {}", getProject(), getSemaphoreKey()); - if (!isForceAnalyseActivated()) { - return databaseSemaphore.acquire(getSemaphoreKey()); - } else { + if (shouldForce()) { // In force mode, we acquire the lock regardless there's a existing lock or not - return databaseSemaphore.acquire(getSemaphoreKey(), 0); + return semaphores.acquire(getSemaphoreKey(), 0); } + return semaphores.acquire(getSemaphoreKey()); } private void release() { LOG.debug("Release semaphore on project : {}, with key {}", getProject(), getSemaphoreKey()); - databaseSemaphore.release(getSemaphoreKey()); + semaphores.release(getSemaphoreKey()); } private String getSemaphoreKey() { @@ -97,7 +94,7 @@ public class CheckSemaphore { return settings.getBoolean(CoreProperties.DRY_RUN); } - private boolean isForceAnalyseActivated() { + private boolean shouldForce() { return settings.getBoolean(CoreProperties.FORCE_ANALYSIS); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/CheckSemaphoreTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ProjectLockTest.java index e3d09868d9a..f47061a7c0b 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/CheckSemaphoreTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ProjectLockTest.java @@ -24,7 +24,7 @@ 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.Semaphores; import org.sonar.api.utils.SonarException; import org.sonar.batch.ProjectTree; @@ -32,105 +32,78 @@ import java.util.Date; import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; import static org.mockito.Mockito.when; -import static org.sonar.api.utils.DatabaseSemaphore.Lock; -public class CheckSemaphoreTest { +public class ProjectLockTest { - private CheckSemaphore checkSemaphore; + private ProjectLock projectLock; - private DatabaseSemaphore databaseSemaphore; + private Semaphores semaphores; private ProjectTree projectTree; private Settings settings; private Project project; - private Lock lock; @Before public void setUp() { - lock = mock(Lock.class); - - databaseSemaphore = mock(DatabaseSemaphore.class); - when(databaseSemaphore.acquire(anyString())).thenReturn(lock); - when(databaseSemaphore.acquire(anyString(), anyInt())).thenReturn(lock); + semaphores = mock(Semaphores.class); projectTree = mock(ProjectTree.class); settings = new Settings(); setDryRunMode(false); setForceMode(false); - - project = new Project("key", "branch", "name"); + project = new Project("my-project-key"); when(projectTree.getRootProject()).thenReturn(project); - checkSemaphore = new CheckSemaphore(databaseSemaphore, projectTree, settings); + projectLock = new ProjectLock(semaphores, projectTree, settings); } @Test public void shouldAcquireSemaphore() { - when(lock.isAcquired()).thenReturn(true); - checkSemaphore.start(); + when(semaphores.acquire(anyString())).thenReturn(new Semaphores.Semaphore().setLocked(true)); + projectLock.start(); - verify(databaseSemaphore).acquire(anyString()); - } - - @Test - public void shouldUseProjectKeyInTheKeyOfTheSemaphore() { - project = new Project("key"); - when(projectTree.getRootProject()).thenReturn(project); - - when(lock.isAcquired()).thenReturn(true); - checkSemaphore.start(); - - verify(databaseSemaphore).acquire("batch-key"); - } - - @Test - public void shouldUseProjectKeyAndBranchIfExistingInTheKeyOfTheSemaphore() { - when(lock.isAcquired()).thenReturn(true); - checkSemaphore.start(); - - verify(databaseSemaphore).acquire("batch-key:branch"); + verify(semaphores).acquire("batch-my-project-key"); } @Test public void shouldAcquireSemaphoreIfForceAnalyseActivated() { setForceMode(true); - when(lock.isAcquired()).thenReturn(true); - checkSemaphore.start(); - verify(databaseSemaphore).acquire(anyString(), anyInt()); + when(semaphores.acquire("batch-my-project-key", 0)).thenReturn(new Semaphores.Semaphore().setLocked(true)); + + projectLock.start(); } @Test(expected = SonarException.class) public void shouldNotAcquireSemaphoreIfTheProjectIsAlreadyBeenAnalysing() { - when(lock.getLocketAt()).thenReturn(new Date()); - when(lock.isAcquired()).thenReturn(false); - checkSemaphore.start(); - verify(databaseSemaphore, never()).acquire(anyString()); + when(semaphores.acquire(anyString())).thenReturn(new Semaphores.Semaphore().setLocked(false).setDurationSinceLocked(1234L)); + projectLock.start(); } @Test public void shouldNotAcquireSemaphoreInDryRunMode() { setDryRunMode(true); settings = new Settings().setProperty(CoreProperties.DRY_RUN, true); - checkSemaphore.start(); - verify(databaseSemaphore, never()).acquire(anyString()); - verify(databaseSemaphore, never()).acquire(anyString(), anyInt()); + projectLock.start(); + verifyZeroInteractions(semaphores); } @Test public void shouldReleaseSemaphore() { - checkSemaphore.stop(); - verify(databaseSemaphore).release(anyString()); + projectLock.stop(); + verify(semaphores).release("batch-my-project-key"); } @Test public void shouldNotReleaseSemaphoreInDryRunMode() { setDryRunMode(true); - checkSemaphore.stop(); - verify(databaseSemaphore, never()).release(anyString()); + projectLock.stop(); + verifyZeroInteractions(semaphores); } private void setDryRunMode(boolean isInDryRunMode) { |