Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests to throttler #958

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions src/test/java/com/uber/cadence/internal/worker/ThrottlerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* <p>Modifications copyright (C) 2017 Uber Technologies, Inc.
*
* <p>Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file
* except in compliance with the License. A copy of the License is located at
*
* <p>http://aws.amazon.com/apache2.0
*
* <p>or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.uber.cadence.internal.worker;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;

import org.junit.Before;
import org.junit.Test;

public class ThrottlerTest {

private Throttler throttler;

@Before
public void setUp() {
// Initialize Throttler with a name, rate of 10 messages per second, and interval of 1000ms (1
// second)
throttler = new Throttler("TestResource", 10, 1000);
}

@Test
public void testConstructorWithValidParameters() {
// Ensure no exceptions are thrown with valid constructor parameters
Throttler throttler = new Throttler("ResourceName", 5.0, 1000);
assertNotNull(throttler);
}

@Test
public void testConstructorWithNullName() {
// Ensure that constructing Throttler with a null name throws IllegalArgumentException
assertThrows(IllegalArgumentException.class, () -> new Throttler(null, 10, 1000));
}

@Test
public void testConstructorWithZeroRate() {
// Ensure that a zero max rate throws an IllegalArgumentException
assertThrows(IllegalArgumentException.class, () -> new Throttler("ResourceName", 0, 1000));
}

@Test
public void testConstructorWithNegativeRate() {
// Ensure that a negative max rate throws an IllegalArgumentException
assertThrows(IllegalArgumentException.class, () -> new Throttler("ResourceName", -5, 1000));
}

@Test
public void testConstructorWithZeroRateInterval() {
// Ensure that a zero rate interval throws an IllegalArgumentException
assertThrows(IllegalArgumentException.class, () -> new Throttler("ResourceName", 10, 0));
}

@Test
public void testConstructorWithNegativeRateInterval() {
// Ensure that a negative rate interval throws an IllegalArgumentException
assertThrows(IllegalArgumentException.class, () -> new Throttler("ResourceName", 10, -1000));
}

@Test
public void testSetMaxRatePerSecond() {
// Verify that setting a valid max rate per second does not throw exceptions and changes the
// internal rate
throttler.setMaxRatePerSecond(0);

throttler.setMaxRatePerSecond(5.0);
// No explicit output to assert; this test checks that no exceptions occur
}

@Test
public void testThrottle() throws InterruptedException {
// Test throttle by calling the method several times and verifying no exceptions
for (int i = 0; i < 10; i++) {
throttler.throttle(); // This test will run without explicit assertions
}
}

@Test
public void testThrottleAtHighRate() throws InterruptedException {
throttler.setMaxRatePerSecond(100000.0); // High rate
for (int i = 0; i < 100; i++) {
throttler.throttle();
}
}

@Test
public void testThrottleAtLowRate() throws InterruptedException {
throttler.setMaxRatePerSecond(1); // Low rate
for (int i = 0; i < 10; i++) {
throttler.throttle();
}
}
}