-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrateLimitter.java
More file actions
98 lines (84 loc) · 2.23 KB
/
rateLimitter.java
File metadata and controls
98 lines (84 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package testDD;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class rateLimitter {
static int counter = 0;
static long startingTime = System.currentTimeMillis();
static long timeInterval = 10000;
@Test
public void testSingular() {
System.out.println("comoulsory wait starting");
try {
Thread.sleep(timeInterval);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("comoulsory wait finish");
assertEquals(rateLimit(System.currentTimeMillis()),true);
}
@Test
public void testSequenceOfRequest() {
System.out.println("comoulsory wait starting");
try {
Thread.sleep(timeInterval);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("comoulsory wait ending");
for(int i = 1;i<=21;i++)
{
System.out.println("i = " + i);
if(i==21)
assertEquals(rateLimit(System.currentTimeMillis()),false);
else
assertEquals(rateLimit(System.currentTimeMillis()),true);
}
}
@Test
public void testSequenceOfRequestWithSleep() {
System.out.println("comoulsory wait starting");
try {
Thread.sleep(timeInterval);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("comoulsory wait ending");
for(int i = 1;i<=10;i++)
{
assertEquals(rateLimit(System.currentTimeMillis()),true);
}
System.out.println("start of sleep");
try {
Thread.sleep(timeInterval);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("end of sleep");
for(int i = 1;i<=11;i++)
{
assertEquals(rateLimit(System.currentTimeMillis()),true);
}
}
public static boolean rateLimit(long timeStamp)
{
if(timeStamp - startingTime >= timeInterval )
{
System.out.println("RESETTING STATIC VALUES");
counter=1;
startingTime = timeStamp-(timeStamp%timeInterval);
}
else
{
counter++;
}
System.out.println("counter: " + counter);
if(counter < 21)
return true;
else
return false;
}
}