Skip to content

Commit

Permalink
Add test case for ThreadPoolExecutorUtil (opengoofy#1356)
Browse files Browse the repository at this point in the history
* Create ThreadFactoryBuilderTest

* Update ThreadFactoryBuilderTest

Methods are covered by multiple unit test cases .
Console printing in English

* Update ThreadFactoryBuilderTest

Add open source protocol.
use @before annotation

* Update ThreadFactoryBuilderTest

* Update ThreadFactoryBuilderTest

* Update ThreadFactoryBuilderTest

* Add test case for MessageWrapper

* Update MessageWrapperTest

* Add test case for ThreadPoolExecutorUtil

Add test case for ThreadPoolExecutorUtil

* Update ThreadPoolExecutorUtilTest
  • Loading branch information
barret-yzh authored May 30, 2023
1 parent 5d40f66 commit 9264919
Showing 1 changed file with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 cn.hippo4j.common.executor;

import cn.hippo4j.common.toolkit.ThreadPoolExecutorUtil;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolExecutorUtilTest {

private ThreadPoolExecutor executor;
private int corePoolSize;
private int maxPoolSize;

@Before
public void testSafeSetPoolSize() {
corePoolSize = 2;
maxPoolSize = 4;
executor = new ThreadPoolExecutor(
corePoolSize,
maxPoolSize,
1L,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(10)
);
}

@Test
public void testEquals(){
// Test when the new core pool size equals the original maximum pool size.
int newCorePoolSize1 = maxPoolSize;
int newMaxPoolSize1 = 6;
ThreadPoolExecutorUtil.safeSetPoolSize(executor, newCorePoolSize1, newMaxPoolSize1);
Assert.assertEquals(newCorePoolSize1, executor.getCorePoolSize());
Assert.assertEquals(newMaxPoolSize1, executor.getMaximumPoolSize());
}

@Test
public void testGreater(){
// Test when the new core pool size is greater than the original maximum pool size.
int newCorePoolSize2 = 8;
int newMaxPoolSize2 = 10;
ThreadPoolExecutorUtil.safeSetPoolSize(executor, newCorePoolSize2, newMaxPoolSize2);
Assert.assertEquals(newCorePoolSize2, executor.getCorePoolSize());
Assert.assertEquals(newMaxPoolSize2, executor.getMaximumPoolSize());
}

@Test
public void testLess(){
// Test when the new core pool size is less than the original maximum pool size.
int newCorePoolSize3 = 3;
int newMaxPoolSize3 = 5;
ThreadPoolExecutorUtil.safeSetPoolSize(executor, newCorePoolSize3, newMaxPoolSize3);
Assert.assertEquals(newCorePoolSize3, executor.getCorePoolSize());
Assert.assertEquals(newMaxPoolSize3, executor.getMaximumPoolSize());
}

@Test
public void testException(){
// Test when the new core pool size is greater than the new maximum pool size, which should throw an IllegalArgumentException.
int newCorePoolSize4 = 6;
int newMaxPoolSize4 = 4;
try {
ThreadPoolExecutorUtil.safeSetPoolSize(executor, newCorePoolSize4, newMaxPoolSize4);
} catch (IllegalArgumentException e) {
// Expected to throw an exception.
Assert.assertEquals("newCorePoolSize must be smaller than newMaximumPoolSize", e.getMessage());
}
}
}

0 comments on commit 9264919

Please sign in to comment.