From a109fe9cf2f453ace715e16333a57cd600091c1a Mon Sep 17 00:00:00 2001 From: Sergey Kuznetsov Date: Tue, 22 Aug 2023 22:29:42 +0200 Subject: [PATCH] Add more tests Signed-off-by: Sergey Kuznetsov --- .../RedissonPermitExpirableSemaphoreTest.java | 58 +++++++++++++++++-- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/redisson/src/test/java/org/redisson/RedissonPermitExpirableSemaphoreTest.java b/redisson/src/test/java/org/redisson/RedissonPermitExpirableSemaphoreTest.java index 6d54f5018..239086a0a 100644 --- a/redisson/src/test/java/org/redisson/RedissonPermitExpirableSemaphoreTest.java +++ b/redisson/src/test/java/org/redisson/RedissonPermitExpirableSemaphoreTest.java @@ -175,7 +175,7 @@ public class RedissonPermitExpirableSemaphoreTest extends BaseConcurrentTest { // TODO Auto-generated catch block e.printStackTrace(); } - }; + } }; t.start(); @@ -206,7 +206,7 @@ public class RedissonPermitExpirableSemaphoreTest extends BaseConcurrentTest { // TODO Auto-generated catch block e.printStackTrace(); } - }; + } }; t.start(); @@ -465,6 +465,26 @@ public class RedissonPermitExpirableSemaphoreTest extends BaseConcurrentTest { }); } + @Test + public void testRelease() throws InterruptedException { + RPermitExpirableSemaphore semaphore = redisson.getPermitExpirableSemaphore("test"); + semaphore.trySetPermits(10); + + List permitsIds = semaphore.acquire(6); + assertThat(permitsIds).hasSize(6); + assertThat(semaphore.availablePermits()).isEqualTo(4); + + List permitsIdsFirstPart = permitsIds.subList(0, 4); + int releaseResult1 = semaphore.release(permitsIdsFirstPart); + assertThat(releaseResult1).isEqualTo(4); + assertThat(semaphore.availablePermits()).isEqualTo(8); + + List permitsIdsSecondPart = permitsIds.subList(4, 6); + int releaseResult2 = semaphore.release(permitsIdsSecondPart); + assertThat(releaseResult2).isEqualTo(2); + assertThat(semaphore.availablePermits()).isEqualTo(10); + } + @Test public void testAcquireAsyncMany() throws ExecutionException, InterruptedException { RPermitExpirableSemaphore semaphore = redisson.getPermitExpirableSemaphore("test"); @@ -485,16 +505,42 @@ public class RedissonPermitExpirableSemaphoreTest extends BaseConcurrentTest { semaphore.trySetPermits(10); List permitsIds = semaphore.acquire(6); + assertThat(permitsIds).hasSize(6); assertThat(semaphore.availablePermits()).isEqualTo(4); - RFuture releaseResult = semaphore.tryReleaseAsync(permitsIds); - + List permitsIdsFirstPart = permitsIds.subList(0, 4); + RFuture releaseResult1 = semaphore.tryReleaseAsync(permitsIdsFirstPart); Awaitility.await().atMost(Duration.ofMillis(100)).pollDelay(Duration.ofMillis(10)).untilAsserted(() -> { - assertThat(releaseResult.isDone()).isTrue(); + assertThat(releaseResult1.isDone()).isTrue(); }); - assertThat(releaseResult.get()).isEqualTo(6); + assertThat(releaseResult1.get()).isEqualTo(4); + assertThat(semaphore.availablePermits()).isEqualTo(8); + List permitsIdsSecondPart = permitsIds.subList(4, 6); + RFuture releaseResult2 = semaphore.tryReleaseAsync(permitsIdsSecondPart); + Awaitility.await().atMost(Duration.ofMillis(100)).pollDelay(Duration.ofMillis(10)).untilAsserted(() -> { + assertThat(releaseResult2.isDone()).isTrue(); + }); + assertThat(releaseResult2.get()).isEqualTo(2); assertThat(semaphore.availablePermits()).isEqualTo(10); } + @Test + public void testReleaseManyExpiredDoesNotThrow() throws InterruptedException, ExecutionException { + RPermitExpirableSemaphore semaphore = redisson.getPermitExpirableSemaphore("test"); + semaphore.trySetPermits(10); + + List permitsIds = semaphore.acquire(6, 100, TimeUnit.MILLISECONDS); + assertThat(permitsIds).hasSize(6); + assertThat(semaphore.availablePermits()).isEqualTo(4); + Thread.sleep(250); + + semaphore.acquire(100, TimeUnit.MILLISECONDS); + assertThat(semaphore.availablePermits()).isEqualTo(9); + Awaitility.await().atMost(Duration.ofMillis(250)).pollDelay(Duration.ofMillis(10)).untilAsserted(() -> { + assertThat(semaphore.availablePermits()).isEqualTo(10); + }); + + Assertions.assertDoesNotThrow(() -> semaphore.release(permitsIds)); + } }