Skip to content

Commit b5c0680

Browse files
committed
Fixed RateLimiter test & Request constructor.
1 parent bfaad39 commit b5c0680

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

RateLimiter.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
*/
1212
class RateLimiter {
1313
private HashMap<String, LinkedList<LocalDateTime>> dtQueue;
14-
private final int SECONDS_PER_MINUTE = 60;
1514
public final int MAX_REQS_PER_MINUTE;
1615
// FOR TESTING ONLY (otherwise null)
1716
private final LocalDateTime CURRENT_DT_OVERRIDE;
@@ -38,7 +37,7 @@ public static void main(String[] args) {
3837
// False, and doesn't add it to the queue
3938
tester.isFalse(rl.newRequest(new Request(TEST_USER, RateLimiter.makeTestTime(10, 59))), testTitle);
4039
// Different user; shouldn't fail
41-
tester.isFalse(rl.newRequest(new Request(OTHER_TEST_USER, RateLimiter.makeTestTime(10, 59))), testTitle);
40+
tester.isTrue(rl.newRequest(new Request(OTHER_TEST_USER, RateLimiter.makeTestTime(10, 59))), testTitle);
4241
// True; Skipoed the 10:59 request, and 10:00 request is now > 60s old, leaving 2 within the window during check
4342
tester.isTrue(rl.newRequest(new Request(TEST_USER, RateLimiter.makeTestTime(11, 01))), testTitle);
4443
}
@@ -48,7 +47,7 @@ public boolean newRequest(Request req) {
4847
if (dtQueue.containsKey(req.user)) {
4948
if (dtQueue.get(req.user).size() == MAX_REQS_PER_MINUTE) {
5049
while (dtQueue.get(req.user).size() > 0
51-
&& Duration.between(dtQueue.get(req.user).peek(), req.dt).getSeconds() > SECONDS_PER_MINUTE) {
50+
&& Duration.between(dtQueue.get(req.user).peek(), req.dt).toMinutes() > 0) {
5251
dtQueue.get(req.user).poll();
5352
}
5453
}
@@ -76,13 +75,13 @@ static class Request {
7675
public LocalDateTime dt;
7776
public String user;
7877
public Request(String user) {
79-
8078
this.user = user;
8179
this.dt = LocalDateTime.now();
8280
}
8381

8482
// TESTING ONLY
8583
private Request(String user, LocalDateTime dt) {
84+
this.user = user;
8685
this.dt = dt;
8786
}
8887
}

0 commit comments

Comments
 (0)