-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
96 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...y-limits-core/src/test/java/com/netflix/concurrency/limits/limiter/SimpleLimiterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |