2525 */
2626package com .github .games647 .fastlogin .core ;
2727
28+ import java .time .Duration ;
2829import java .util .concurrent .TimeUnit ;
2930
3031import org .junit .Test ;
@@ -43,14 +44,34 @@ public class RateLimiterTest {
4344 public void allowExpire () throws InterruptedException {
4445 int size = 3 ;
4546
47+ FakeTicker ticker = new FakeTicker (5_000_000L );
48+
49+ // run twice the size to fill it first and then test it
50+ RateLimiter rateLimiter = new RateLimiter (ticker , size , 0 );
51+ for (int i = 0 ; i < size ; i ++) {
52+ assertTrue ("Filling up" , rateLimiter .tryAcquire ());
53+ }
54+
55+ for (int i = 0 ; i < size ; i ++) {
56+ ticker .add (Duration .ofSeconds (1 ));
57+ assertTrue ("Should be expired" , rateLimiter .tryAcquire ());
58+ }
59+ }
60+
61+ @ Test
62+ public void allowExpireNegative () throws InterruptedException {
63+ int size = 3 ;
64+
65+ FakeTicker ticker = new FakeTicker (-5_000_000L );
66+
4667 // run twice the size to fill it first and then test it
47- RateLimiter rateLimiter = new RateLimiter (size , 0 );
68+ RateLimiter rateLimiter = new RateLimiter (ticker , size , 0 );
4869 for (int i = 0 ; i < size ; i ++) {
4970 assertTrue ("Filling up" , rateLimiter .tryAcquire ());
5071 }
5172
5273 for (int i = 0 ; i < size ; i ++) {
53- Thread . sleep ( 1 );
74+ ticker . add ( Duration . ofSeconds ( 1 ) );
5475 assertTrue ("Should be expired" , rateLimiter .tryAcquire ());
5576 }
5677 }
@@ -62,8 +83,28 @@ public void allowExpire() throws InterruptedException {
6283 public void shouldBlock () {
6384 int size = 3 ;
6485
86+ FakeTicker ticker = new FakeTicker (5_000_000L );
87+
88+ // fill the size
89+ RateLimiter rateLimiter = new RateLimiter (ticker , size , TimeUnit .SECONDS .toMillis (30 ));
90+ for (int i = 0 ; i < size ; i ++) {
91+ assertTrue ("Filling up" , rateLimiter .tryAcquire ());
92+ }
93+
94+ assertFalse ("Should be full and no entry should be expired" , rateLimiter .tryAcquire ());
95+ }
96+
97+ /**
98+ * Too many requests
99+ */
100+ @ Test
101+ public void shouldBlockNegative () {
102+ int size = 3 ;
103+
104+ FakeTicker ticker = new FakeTicker (-5_000_000L );
105+
65106 // fill the size
66- RateLimiter rateLimiter = new RateLimiter (size , TimeUnit .SECONDS .toMillis (30 ));
107+ RateLimiter rateLimiter = new RateLimiter (ticker , size , TimeUnit .SECONDS .toMillis (30 ));
67108 for (int i = 0 ; i < size ; i ++) {
68109 assertTrue ("Filling up" , rateLimiter .tryAcquire ());
69110 }
@@ -76,17 +117,40 @@ public void shouldBlock() {
76117 */
77118 @ Test
78119 public void blockedNotAdded () throws InterruptedException {
120+ FakeTicker ticker = new FakeTicker (5_000_000L );
121+
122+ // fill the size - 100ms should be reasonable high
123+ RateLimiter rateLimiter = new RateLimiter (ticker , 1 , 100 );
124+ assertTrue ("Filling up" , rateLimiter .tryAcquire ());
125+
126+ ticker .add (Duration .ofMillis (50 ));
127+
128+ // still is full - should fail
129+ assertFalse ("Expired too early" , rateLimiter .tryAcquire ());
130+
131+ // wait the remaining time and add a threshold, because
132+ ticker .add (Duration .ofMillis (50 ));
133+ assertTrue ("Request not released" , rateLimiter .tryAcquire ());
134+ }
135+
136+ /**
137+ * Blocked attempts shouldn't replace existing ones.
138+ */
139+ @ Test
140+ public void blockedNotAddedNegative () throws InterruptedException {
141+ FakeTicker ticker = new FakeTicker (-5_000_000L );
142+
79143 // fill the size - 100ms should be reasonable high
80- RateLimiter rateLimiter = new RateLimiter (1 , 100 );
144+ RateLimiter rateLimiter = new RateLimiter (ticker , 1 , 100 );
81145 assertTrue ("Filling up" , rateLimiter .tryAcquire ());
82146
83- Thread . sleep ( 50 );
147+ ticker . add ( Duration . ofMillis ( 50 ) );
84148
85149 // still is full - should fail
86150 assertFalse ("Expired too early" , rateLimiter .tryAcquire ());
87151
88152 // wait the remaining time and add a threshold, because
89- Thread . sleep ( 50 + THRESHOLD_MILLI );
153+ ticker . add ( Duration . ofMillis ( 50 ) );
90154 assertTrue ("Request not released" , rateLimiter .tryAcquire ());
91155 }
92156}
0 commit comments