diff --git a/concurrency-limits-core/src/test/java/com/netflix/concurrency/limits/limiter/AbstractPartitionedLimiterTest.java b/concurrency-limits-core/src/test/java/com/netflix/concurrency/limits/limiter/AbstractPartitionedLimiterTest.java index 6133631c..b2a548ae 100644 --- a/concurrency-limits-core/src/test/java/com/netflix/concurrency/limits/limiter/AbstractPartitionedLimiterTest.java +++ b/concurrency-limits-core/src/test/java/com/netflix/concurrency/limits/limiter/AbstractPartitionedLimiterTest.java @@ -176,17 +176,23 @@ public void testBypassPartitionedLimiter() { .bypassLimitResolver(new ShouldBypassPredicate()) .build(); - for (int i = 0; i < 1; i++) { - Assert.assertTrue(limiter.acquire("batch").isPresent()); - Assert.assertEquals(i+1, limiter.getPartition("batch").getInflight()); - Assert.assertTrue(limiter.acquire("admin").isPresent()); - } + Assert.assertTrue(limiter.acquire("batch").isPresent()); + Assert.assertEquals(1, limiter.getPartition("batch").getInflight()); + Assert.assertTrue(limiter.acquire("admin").isPresent()); for (int i = 0; i < 9; i++) { Assert.assertTrue(limiter.acquire("live").isPresent()); Assert.assertEquals(i+1, limiter.getPartition("live").getInflight()); Assert.assertTrue(limiter.acquire("admin").isPresent()); } + + // Verify that bypassed requests are able to proceed even when the limiter is full + Assert.assertFalse(limiter.acquire("batch").isPresent()); + Assert.assertEquals(1, limiter.getPartition("batch").getInflight()); + Assert.assertFalse(limiter.acquire("live").isPresent()); + Assert.assertEquals(9, limiter.getPartition("live").getInflight()); + Assert.assertEquals(10, limiter.getInflight()); + Assert.assertTrue(limiter.acquire("admin").isPresent()); } @Test diff --git a/concurrency-limits-core/src/test/java/com/netflix/concurrency/limits/limiter/SimpleLimiterTest.java b/concurrency-limits-core/src/test/java/com/netflix/concurrency/limits/limiter/SimpleLimiterTest.java new file mode 100644 index 00000000..ebc80f83 --- /dev/null +++ b/concurrency-limits-core/src/test/java/com/netflix/concurrency/limits/limiter/SimpleLimiterTest.java @@ -0,0 +1,85 @@ +package com.netflix.concurrency.limits.limiter; + +import com.netflix.concurrency.limits.Limiter; +import com.netflix.concurrency.limits.limit.FixedLimit; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Optional; + +public class SimpleLimiterTest { + + @Test + public void useLimiterCapacityUntilTotalLimit() { + SimpleLimiter limiter = SimpleLimiter.newBuilder() + .limit(FixedLimit.of(10)) + .build(); + + for (int i = 0; i < 10; i++) { + Assert.assertTrue(limiter.acquire("live").isPresent()); + } + + // Rejected call after total limit is utilized + Assert.assertFalse(limiter.acquire("live").isPresent()); + Assert.assertEquals(10, limiter.getInflight()); + } + + @Test + public void testReleaseLimit() { + SimpleLimiter limiter = SimpleLimiter.newBuilder() + .limit(FixedLimit.of(10)) + .build(); + + Optional completion = limiter.acquire("live"); + for (int i = 1; i < 10; i++) { + Assert.assertTrue(limiter.acquire("live").isPresent()); + } + + Assert.assertEquals(10, limiter.getInflight()); + Assert.assertFalse(limiter.acquire("live").isPresent()); + + // Release token + completion.get().onSuccess(); + Assert.assertEquals(9, limiter.getInflight()); + + Assert.assertTrue(limiter.acquire("live").isPresent()); + Assert.assertEquals(10, limiter.getInflight()); + } + + @Test + public void testSimpleBypassLimiter() { + SimpleLimiter limiter = SimpleLimiter.newBypassLimiterBuilder() + .limit(FixedLimit.of(10)) + .bypassLimitResolver((context) -> context.equals("admin")) + .build(); + + for (int i = 0; i < 10; i++) { + Assert.assertTrue(limiter.acquire("live").isPresent()); + Assert.assertEquals(i+1, limiter.getInflight()); + } + + // Verify calls with passing bypass condition will return a token + // whereas remaining calls will be throttled since inflight count is greater than the limit + for (int i = 0; i < 10; i++) { + Assert.assertFalse(limiter.acquire("live").isPresent()); + Assert.assertTrue(limiter.acquire("admin").isPresent()); + } + } + + @Test + public void testSimpleBypassLimiterDefault() { + SimpleLimiter limiter = SimpleLimiter.newBypassLimiterBuilder() + .limit(FixedLimit.of(10)) + .build(); + + for (int i = 0; i < 10; i++) { + Assert.assertTrue(limiter.acquire("live").isPresent()); + Assert.assertEquals(i+1, limiter.getInflight()); + } + + // Verify that no calls are bypassed by default + Assert.assertFalse(limiter.acquire("live").isPresent()); + Assert.assertFalse(limiter.acquire("admin").isPresent()); + } + +}