Skip to content

Commit 96785e9

Browse files
authored
Add unit tests to throttler (#958)
1 parent 704380b commit 96785e9

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* <p>Modifications copyright (C) 2017 Uber Technologies, Inc.
5+
*
6+
* <p>Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file
7+
* except in compliance with the License. A copy of the License is located at
8+
*
9+
* <p>http://aws.amazon.com/apache2.0
10+
*
11+
* <p>or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
13+
* specific language governing permissions and limitations under the License.
14+
*/
15+
package com.uber.cadence.internal.worker;
16+
17+
import static org.junit.Assert.assertNotNull;
18+
import static org.junit.Assert.assertThrows;
19+
20+
import org.junit.Before;
21+
import org.junit.Test;
22+
23+
public class ThrottlerTest {
24+
25+
private Throttler throttler;
26+
27+
@Before
28+
public void setUp() {
29+
// Initialize Throttler with a name, rate of 10 messages per second, and interval of 1000ms (1
30+
// second)
31+
throttler = new Throttler("TestResource", 10, 1000);
32+
}
33+
34+
@Test
35+
public void testConstructorWithValidParameters() {
36+
// Ensure no exceptions are thrown with valid constructor parameters
37+
Throttler throttler = new Throttler("ResourceName", 5.0, 1000);
38+
assertNotNull(throttler);
39+
}
40+
41+
@Test
42+
public void testConstructorWithNullName() {
43+
// Ensure that constructing Throttler with a null name throws IllegalArgumentException
44+
assertThrows(IllegalArgumentException.class, () -> new Throttler(null, 10, 1000));
45+
}
46+
47+
@Test
48+
public void testConstructorWithZeroRate() {
49+
// Ensure that a zero max rate throws an IllegalArgumentException
50+
assertThrows(IllegalArgumentException.class, () -> new Throttler("ResourceName", 0, 1000));
51+
}
52+
53+
@Test
54+
public void testConstructorWithNegativeRate() {
55+
// Ensure that a negative max rate throws an IllegalArgumentException
56+
assertThrows(IllegalArgumentException.class, () -> new Throttler("ResourceName", -5, 1000));
57+
}
58+
59+
@Test
60+
public void testConstructorWithZeroRateInterval() {
61+
// Ensure that a zero rate interval throws an IllegalArgumentException
62+
assertThrows(IllegalArgumentException.class, () -> new Throttler("ResourceName", 10, 0));
63+
}
64+
65+
@Test
66+
public void testConstructorWithNegativeRateInterval() {
67+
// Ensure that a negative rate interval throws an IllegalArgumentException
68+
assertThrows(IllegalArgumentException.class, () -> new Throttler("ResourceName", 10, -1000));
69+
}
70+
71+
@Test
72+
public void testSetMaxRatePerSecond() {
73+
// Verify that setting a valid max rate per second does not throw exceptions and changes the
74+
// internal rate
75+
throttler.setMaxRatePerSecond(0);
76+
77+
throttler.setMaxRatePerSecond(5.0);
78+
// No explicit output to assert; this test checks that no exceptions occur
79+
}
80+
81+
@Test
82+
public void testThrottle() throws InterruptedException {
83+
// Test throttle by calling the method several times and verifying no exceptions
84+
for (int i = 0; i < 10; i++) {
85+
throttler.throttle(); // This test will run without explicit assertions
86+
}
87+
}
88+
89+
@Test
90+
public void testThrottleAtHighRate() throws InterruptedException {
91+
throttler.setMaxRatePerSecond(100000.0); // High rate
92+
for (int i = 0; i < 100; i++) {
93+
throttler.throttle();
94+
}
95+
}
96+
97+
@Test
98+
public void testThrottleAtLowRate() throws InterruptedException {
99+
throttler.setMaxRatePerSecond(1); // Low rate
100+
for (int i = 0; i < 10; i++) {
101+
throttler.throttle();
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)