Skip to content

Commit

Permalink
added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
umairk79 committed Mar 21, 2024
1 parent 1c68e5c commit 628dde9
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> limiter = SimpleLimiter.newBuilder()
.limit(FixedLimit.of(10))
.build();

Optional<Limiter.Listener> 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<String> limiter = SimpleLimiter.<String>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<String> limiter = SimpleLimiter.<String>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());
}

}

0 comments on commit 628dde9

Please sign in to comment.