-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Unit tests for the RateLimitGate
- Loading branch information
1 parent
17f021b
commit 6022391
Showing
2 changed files
with
77 additions
and
2 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
75 changes: 75 additions & 0 deletions
75
src/test/java/com/penguineering/hareairis/rmq/RateLimitGateTest.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,75 @@ | ||
package com.penguineering.hareairis.rmq; | ||
|
||
import com.penguineering.hareairis.ai.RateLimitException; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.concurrent.Callable; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class RateLimitGateTest { | ||
private RateLimitGate rateLimitGate; | ||
|
||
@Mock | ||
private Callable<String> protectedCall; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
rateLimitGate = new RateLimitGate(); | ||
} | ||
|
||
@Test | ||
void testCallWithRateLimit_Success() throws Exception { | ||
when(protectedCall.call()).thenReturn("Success"); | ||
|
||
String result = rateLimitGate.callWithRateLimit(protectedCall); | ||
|
||
assertEquals("Success", result); | ||
verify(protectedCall, times(1)).call(); | ||
} | ||
|
||
@Test | ||
void testCallWithRateLimit_RateLimitException() throws Exception { | ||
RateLimitException rateLimitException = new RateLimitException("Rate limit exceeded", Duration.ofSeconds(1)); | ||
when(protectedCall.call()).thenThrow(rateLimitException).thenReturn("Success"); | ||
|
||
String result = rateLimitGate.callWithRateLimit(protectedCall); | ||
|
||
assertEquals("Success", result); | ||
verify(protectedCall, times(2)).call(); | ||
} | ||
|
||
@Test | ||
void testCallWithRateLimit_InterruptedException() throws Exception { | ||
when(protectedCall.call()).thenThrow(new InterruptedException("Interrupted")); | ||
|
||
assertThrows(InterruptedException.class, () -> rateLimitGate.callWithRateLimit(protectedCall)); | ||
verify(protectedCall, times(1)).call(); | ||
} | ||
|
||
@Test | ||
void testRegisterRateLimitException() { | ||
RateLimitException rateLimitException = new RateLimitException("Rate limit exceeded", Duration.ofSeconds(1)); | ||
rateLimitGate.registerRateLimitException(rateLimitException); | ||
|
||
assertTrue(rateLimitGate.nextAvailableTime.get().isAfter(Instant.now())); | ||
} | ||
|
||
@Test | ||
void testInterruptWaitingThread() { | ||
Thread mockThread = mock(Thread.class); | ||
rateLimitGate.waitingThread.set(mockThread); | ||
|
||
rateLimitGate.interruptWaitingThread(); | ||
|
||
verify(mockThread, times(1)).interrupt(); | ||
} | ||
} |