Skip to content

Commit 54d7396

Browse files
committed
added test coverage for app.java and fixed random to be thread safe #2973
1 parent 4e886f8 commit 54d7396

File tree

4 files changed

+24
-16
lines changed

4 files changed

+24
-16
lines changed

rate-limiting-pattern/src/main/java/com/iluwatar/rate/limiting/pattern/App.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.iluwatar.rate.limiting.pattern;
22

3-
import org.slf4j.Logger;
4-
import org.slf4j.LoggerFactory;
53
import java.util.concurrent.*;
64
import java.util.concurrent.atomic.AtomicBoolean;
75
import java.util.concurrent.atomic.AtomicInteger;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
88

99
/**
1010
* The <em>Rate Limiter</em> pattern is a key defensive strategy used to prevent system overload and
@@ -99,10 +99,13 @@ private static void shutdownExecutor(ExecutorService service, String name) {
9999
}
100100
}
101101

102-
static Runnable createClientTask(int clientId, RateLimiter s3Limiter, RateLimiter dynamoDbLimiter, RateLimiter lambdaLimiter) {
102+
static Runnable createClientTask(
103+
int clientId, RateLimiter s3Limiter, RateLimiter dynamoDbLimiter, RateLimiter lambdaLimiter) {
103104
return () -> {
104105
String[] services = {"s3", "dynamodb", "lambda"};
105-
String[] operations = {"GetObject", "PutObject", "Query", "Scan", "PutItem", "Invoke", "ListFunctions"};
106+
String[] operations = {
107+
"GetObject", "PutObject", "Query", "Scan", "PutItem", "Invoke", "ListFunctions"
108+
};
106109
ThreadLocalRandom random = ThreadLocalRandom.current();
107110

108111
while (running.get() && !Thread.currentThread().isInterrupted()) {
@@ -132,7 +135,12 @@ static void makeRequest(int clientId, RateLimiter limiter, String service, Strin
132135
LOGGER.info("Client {}: {}.{} - ALLOWED", clientId, service, operation);
133136
} catch (ThrottlingException e) {
134137
throttledRequests.incrementAndGet();
135-
LOGGER.warn("Client {}: {}.{} - THROTTLED (Retry in {}ms)", clientId, service, operation, e.getRetryAfterMillis());
138+
LOGGER.warn(
139+
"Client {}: {}.{} - THROTTLED (Retry in {}ms)",
140+
clientId,
141+
service,
142+
operation,
143+
e.getRetryAfterMillis());
136144
} catch (ServiceUnavailableException e) {
137145
failedRequests.incrementAndGet();
138146
LOGGER.warn("Client {}: {}.{} - SERVICE UNAVAILABLE", clientId, service, operation);

rate-limiting-pattern/src/test/java/com/iluwatar/rate/limiting/pattern/AppTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
import org.junit.jupiter.api.BeforeEach;
77
import org.junit.jupiter.api.Test;
88

9-
/**
10-
* Unit tests for {@link App}.
11-
*/
9+
/** Unit tests for {@link App}. */
1210
class AppTest {
1311

1412
private RateLimiter mockLimiter;

rate-limiting-pattern/src/test/java/com/iluwatar/rate/limiting/pattern/AppTestUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
public class AppTestUtils {
66

7-
public static void invokeMakeRequest(int clientId, RateLimiter limiter, String service, String operation) {
7+
public static void invokeMakeRequest(
8+
int clientId, RateLimiter limiter, String service, String operation) {
89
App.makeRequest(clientId, limiter, service, operation);
910
}
1011

rate-limiting-pattern/src/test/java/com/iluwatar/rate/limiting/pattern/FindCustomerRequestTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,14 @@ void shouldExecuteUsingDefaultHelper() throws Exception {
4848
void shouldThrowServiceUnavailableOnInterruptedException() {
4949
RateLimiter noOpLimiter = (service, operation) -> {}; // no throttling
5050

51-
FindCustomerRequest request = new FindCustomerRequest("999", noOpLimiter) {
52-
@Override
53-
public String execute() throws RateLimitException {
54-
Thread.currentThread().interrupt(); // Simulate thread interruption
55-
return super.execute(); // Should throw ServiceUnavailableException
56-
}
57-
};
51+
FindCustomerRequest request =
52+
new FindCustomerRequest("999", noOpLimiter) {
53+
@Override
54+
public String execute() throws RateLimitException {
55+
Thread.currentThread().interrupt(); // Simulate thread interruption
56+
return super.execute(); // Should throw ServiceUnavailableException
57+
}
58+
};
5859

5960
assertThrows(ServiceUnavailableException.class, request::execute);
6061
}

0 commit comments

Comments
 (0)