Skip to content

Commit

Permalink
Add Unit tests for the RateLimitGate
Browse files Browse the repository at this point in the history
  • Loading branch information
penguineer committed Nov 5, 2024
1 parent 17f021b commit 6022391
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
public class RateLimitGate {
private static final Logger logger = LoggerFactory.getLogger(RateLimitGate.class);

private final AtomicReference<Instant> nextAvailableTime = new AtomicReference<>(Instant.now());
private final AtomicReference<Thread> waitingThread = new AtomicReference<>(null);
final AtomicReference<Instant> nextAvailableTime = new AtomicReference<>(Instant.now());
final AtomicReference<Thread> waitingThread = new AtomicReference<>(null);
private final Lock threadLock = new ReentrantLock();
private final Condition threadCondition = threadLock.newCondition();

Expand Down
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();
}
}

0 comments on commit 6022391

Please sign in to comment.