From 534b76c9e499860eca7003a6496f0e21484c976f Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 3 Dec 2012 09:16:04 +0100 Subject: SONAR-3306 Use a semaphore to prevent launching several analysis of the same project at the same time --- .../sonar/core/persistence/SemaphoreDaoTest.java | 85 +++++++++++++++++++++- 1 file changed, 81 insertions(+), 4 deletions(-) (limited to 'sonar-core/src/test/java') 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(); @@ -111,6 +153,41 @@ public class SemaphoreDaoTest extends AbstractDaoTestCase { assertThat(isRecent(semaphore.lockedAt, 60)).isTrue(); } + @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(); } } -- cgit v1.2.3