diff options
author | Julien Lancelot <julien.lancelot@gmail.com> | 2012-12-03 09:16:04 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@gmail.com> | 2012-12-03 12:44:01 +0100 |
commit | 534b76c9e499860eca7003a6496f0e21484c976f (patch) | |
tree | 049ba785d53fd988d47dcc54b8bea82788e6cb3b /sonar-core/src/test/java | |
parent | 852abadb6d99037d951f025e20b7a376c2f4a83e (diff) | |
download | sonarqube-534b76c9e499860eca7003a6496f0e21484c976f.tar.gz sonarqube-534b76c9e499860eca7003a6496f0e21484c976f.zip |
SONAR-3306 Use a semaphore to prevent launching several analysis of the same project at the same time
Diffstat (limited to 'sonar-core/src/test/java')
-rw-r--r-- | sonar-core/src/test/java/org/sonar/core/persistence/SemaphoreDaoTest.java | 85 |
1 files changed, 81 insertions, 4 deletions
diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/SemaphoreDaoTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/SemaphoreDaoTest.java index 5efd621aa30..d0a97e07655 100644 --- a/sonar-core/src/test/java/org/sonar/core/persistence/SemaphoreDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/persistence/SemaphoreDaoTest.java @@ -70,7 +70,45 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase { @Test public void create_and_acquire_semaphore() throws Exception { SemaphoreDao dao = new SemaphoreDao(getMyBatis()); - assertThat(dao.acquire("foo", 60)).isTrue(); + Lock lock = dao.acquire("foo", 60); + assertThat(lock.isAcquired()).isTrue(); + assertThat(lock.getDurationSinceLocked()).isNull(); + + Semaphore semaphore = selectSemaphore("foo"); + assertThat(semaphore).isNotNull(); + assertThat(semaphore.name).isEqualTo("foo"); + assertThat(isRecent(semaphore.createdAt, 60)).isTrue(); + assertThat(isRecent(semaphore.updatedAt, 60)).isTrue(); + assertThat(isRecent(semaphore.lockedAt, 60)).isTrue(); + + dao.release("foo"); + assertThat(selectSemaphore("foo")).isNull(); + } + + @Test + public void create_and_acquire_semaphore_when_timeout_is_zeo() throws Exception { + SemaphoreDao dao = new SemaphoreDao(getMyBatis()); + Lock lock = dao.acquire("foo", 0); + assertThat(lock.isAcquired()).isTrue(); + assertThat(lock.getDurationSinceLocked()).isNull(); + + Semaphore semaphore = selectSemaphore("foo"); + assertThat(semaphore).isNotNull(); + assertThat(semaphore.name).isEqualTo("foo"); + assertThat(isRecent(semaphore.createdAt, 60)).isTrue(); + assertThat(isRecent(semaphore.updatedAt, 60)).isTrue(); + assertThat(isRecent(semaphore.lockedAt, 60)).isTrue(); + + dao.release("foo"); + assertThat(selectSemaphore("foo")).isNull(); + } + + @Test + public void create_and_acquire_semaphore_when_no_timeout() throws Exception { + SemaphoreDao dao = new SemaphoreDao(getMyBatis()); + Lock lock = dao.acquire("foo"); + assertThat(lock.isAcquired()).isTrue(); + assertThat(lock.getDurationSinceLocked()).isNull(); Semaphore semaphore = selectSemaphore("foo"); assertThat(semaphore).isNotNull(); @@ -87,7 +125,9 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase { public void fail_to_acquire_locked_semaphore() throws Exception { setupData("old_semaphore"); SemaphoreDao dao = new SemaphoreDao(getMyBatis()); - assertThat(dao.acquire("foo", Integer.MAX_VALUE)).isFalse(); + Lock lock = dao.acquire("foo", Integer.MAX_VALUE); + assertThat(lock.isAcquired()).isFalse(); + assertThat(lock.getDurationSinceLocked()).isNotNull(); Semaphore semaphore = selectSemaphore("foo"); assertThat(semaphore).isNotNull(); @@ -101,7 +141,9 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase { public void acquire_long_locked_semaphore() throws Exception { setupData("old_semaphore"); SemaphoreDao dao = new SemaphoreDao(getMyBatis()); - assertThat(dao.acquire("foo", 60)).isTrue(); + Lock lock = dao.acquire("foo", 60); + assertThat(lock.isAcquired()).isTrue(); + assertThat(lock.getDurationSinceLocked()).isNull(); Semaphore semaphore = selectSemaphore("foo"); assertThat(semaphore).isNotNull(); @@ -112,6 +154,41 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase { } @Test + public void acquire_locked_semaphore_when_timeout_is_zeo() throws Exception { + setupData("old_semaphore"); + SemaphoreDao dao = new SemaphoreDao(getMyBatis()); + Lock lock = dao.acquire("foo", 0); + assertThat(lock.isAcquired()).isTrue(); + assertThat(lock.getDurationSinceLocked()).isNull(); + + Semaphore semaphore = selectSemaphore("foo"); + assertThat(semaphore).isNotNull(); + assertThat(semaphore.name).isEqualTo("foo"); + assertThat(isRecent(semaphore.createdAt, 60)).isFalse(); + assertThat(isRecent(semaphore.updatedAt, 60)).isTrue(); + assertThat(isRecent(semaphore.lockedAt, 60)).isTrue(); + + dao.release("foo"); + assertThat(selectSemaphore("foo")).isNull(); + } + + @Test + public void fail_to_acquire_locked_semaphore_when_no_timeout() throws Exception { + setupData("old_semaphore"); + SemaphoreDao dao = new SemaphoreDao(getMyBatis()); + Lock lock = dao.acquire("foo"); + assertThat(lock.isAcquired()).isFalse(); + assertThat(lock.getDurationSinceLocked()).isNotNull(); + + Semaphore semaphore = selectSemaphore("foo"); + assertThat(semaphore).isNotNull(); + assertThat(semaphore.name).isEqualTo("foo"); + assertThat(isRecent(semaphore.createdAt, 60)).isFalse(); + assertThat(isRecent(semaphore.updatedAt, 60)).isFalse(); + assertThat(isRecent(semaphore.lockedAt, 60)).isFalse(); + } + + @Test public void test_concurrent_locks() throws Exception { SemaphoreDao dao = new SemaphoreDao(getMyBatis()); @@ -184,7 +261,7 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase { try { barrier.await(); for (int i = 0; i < 100; i++) { - if (dao.acquire("my-lock", 60 * 5)) { + if (dao.acquire("my-lock", 60 * 5).isAcquired()) { locks.incrementAndGet(); } } |