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

[ISSUE #5095] Add some testcases for common executor #6959

Merged
merged 2 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.alibaba.nacos.common.executor;

import com.alibaba.nacos.common.JustForTest;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
Expand Down Expand Up @@ -164,7 +166,7 @@ public static ScheduledExecutorService newScheduledExecutorService(final String
* @param coreThreads core thread number
* @param maxThreads max thread number
* @param keepAliveTimeMs keep alive time milliseconds
* @param threadFactory thread facotry
* @param threadFactory thread factory
* @return new custom executor service
*/
public static ThreadPoolExecutor newCustomerThreadExecutor(final String group, final int coreThreads,
Expand All @@ -174,5 +176,10 @@ public static ThreadPoolExecutor newCustomerThreadExecutor(final String group, f
THREAD_POOL_MANAGER.register(DEFAULT_NAMESPACE, group, executor);
return executor;
}

@JustForTest
public static ThreadPoolManager getThreadPoolManager() {
return THREAD_POOL_MANAGER;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.alibaba.nacos.common.executor;

import com.alibaba.nacos.common.JustForTest;
import com.alibaba.nacos.common.utils.ThreadUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -198,4 +199,13 @@ public static void shutdown() {
}
}

@JustForTest
public Map<String, Map<String, Set<ExecutorService>>> getResourcesManager() {
return resourcesManager;
}

@JustForTest
public Map<String, Object> getLockers() {
return lockers;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.common.event;

import com.alibaba.nacos.common.notify.Event;
import org.junit.Assert;
import org.junit.Test;

public class ServerConfigChangeEventTest {

@Test
public void test() {
Event event = ServerConfigChangeEvent.newEvent();
Assert.assertTrue(event instanceof ServerConfigChangeEvent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.common.executor;

import org.junit.Assert;
import org.junit.Test;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;

public class ExecutorFactoryTest {

private final NameThreadFactory threadFactory = new NameThreadFactory("test");

@Test
public void test() {
ExecutorService executorService;
ThreadPoolExecutor threadPoolExecutor;

executorService = ExecutorFactory.newSingleExecutorService();
Assert.assertTrue(executorService instanceof ThreadPoolExecutor);
threadPoolExecutor = (ThreadPoolExecutor) executorService;
Assert.assertEquals(1, threadPoolExecutor.getCorePoolSize());
Assert.assertEquals(1, threadPoolExecutor.getMaximumPoolSize());
Assert.assertNotEquals(threadFactory, threadPoolExecutor.getThreadFactory());

executorService = ExecutorFactory.newFixedExecutorService(10);
Assert.assertTrue(executorService instanceof ThreadPoolExecutor);
threadPoolExecutor = (ThreadPoolExecutor) executorService;
Assert.assertEquals(10, threadPoolExecutor.getCorePoolSize());
Assert.assertEquals(10, threadPoolExecutor.getMaximumPoolSize());
Assert.assertNotEquals(threadFactory, threadPoolExecutor.getThreadFactory());

executorService = ExecutorFactory.newSingleExecutorService(threadFactory);
Assert.assertTrue(executorService instanceof ThreadPoolExecutor);
threadPoolExecutor = (ThreadPoolExecutor) executorService;
Assert.assertEquals(1, threadPoolExecutor.getCorePoolSize());
Assert.assertEquals(1, threadPoolExecutor.getMaximumPoolSize());
Assert.assertEquals(threadFactory, threadPoolExecutor.getThreadFactory());

executorService = ExecutorFactory.newFixedExecutorService(10, threadFactory);
Assert.assertTrue(executorService instanceof ThreadPoolExecutor);
threadPoolExecutor = (ThreadPoolExecutor) executorService;
Assert.assertEquals(10, threadPoolExecutor.getCorePoolSize());
Assert.assertEquals(10, threadPoolExecutor.getMaximumPoolSize());
Assert.assertEquals(threadFactory, threadPoolExecutor.getThreadFactory());

ScheduledThreadPoolExecutor scheduledThreadPoolExecutor;

executorService = ExecutorFactory.newSingleScheduledExecutorService(threadFactory);
Assert.assertTrue(executorService instanceof ScheduledThreadPoolExecutor);
scheduledThreadPoolExecutor = (ScheduledThreadPoolExecutor) executorService;
Assert.assertEquals(1, scheduledThreadPoolExecutor.getCorePoolSize());
Assert.assertEquals(Integer.MAX_VALUE, scheduledThreadPoolExecutor.getMaximumPoolSize());
Assert.assertEquals(threadFactory, threadPoolExecutor.getThreadFactory());

executorService = ExecutorFactory.newScheduledExecutorService(10, threadFactory);
Assert.assertTrue(executorService instanceof ScheduledThreadPoolExecutor);
scheduledThreadPoolExecutor = (ScheduledThreadPoolExecutor) executorService;
Assert.assertEquals(10, scheduledThreadPoolExecutor.getCorePoolSize());
Assert.assertEquals(Integer.MAX_VALUE, scheduledThreadPoolExecutor.getMaximumPoolSize());
Assert.assertEquals(threadFactory, threadPoolExecutor.getThreadFactory());

threadPoolExecutor = ExecutorFactory.newCustomerThreadExecutor(10, 20, 1000, threadFactory);
Assert.assertEquals(10, threadPoolExecutor.getCorePoolSize());
Assert.assertEquals(20, threadPoolExecutor.getMaximumPoolSize());
Assert.assertEquals(threadFactory, threadPoolExecutor.getThreadFactory());
}

@Test
public void testManaged() {
String testGroup = "test";
ExecutorService executorService;
ThreadPoolExecutor threadPoolExecutor;
ThreadPoolManager manager = ExecutorFactory.Managed.getThreadPoolManager();
final Map<String, Map<String, Set<ExecutorService>>> resourcesManager = manager.getResourcesManager();

executorService = ExecutorFactory.Managed.newSingleExecutorService(testGroup);
Assert.assertTrue(executorService instanceof ThreadPoolExecutor);
threadPoolExecutor = (ThreadPoolExecutor) executorService;
Assert.assertEquals(1, threadPoolExecutor.getCorePoolSize());
Assert.assertEquals(1, threadPoolExecutor.getMaximumPoolSize());
Assert.assertNotEquals(threadFactory, threadPoolExecutor.getThreadFactory());
Assert.assertEquals(1, resourcesManager.get("nacos").get(testGroup).size());

executorService = ExecutorFactory.Managed.newFixedExecutorService(testGroup, 10);
Assert.assertTrue(executorService instanceof ThreadPoolExecutor);
threadPoolExecutor = (ThreadPoolExecutor) executorService;
Assert.assertEquals(10, threadPoolExecutor.getCorePoolSize());
Assert.assertEquals(10, threadPoolExecutor.getMaximumPoolSize());
Assert.assertNotEquals(threadFactory, threadPoolExecutor.getThreadFactory());
Assert.assertEquals(2, resourcesManager.get("nacos").get(testGroup).size());

executorService = ExecutorFactory.Managed.newSingleExecutorService(testGroup, threadFactory);
Assert.assertTrue(executorService instanceof ThreadPoolExecutor);
threadPoolExecutor = (ThreadPoolExecutor) executorService;
Assert.assertEquals(1, threadPoolExecutor.getCorePoolSize());
Assert.assertEquals(1, threadPoolExecutor.getMaximumPoolSize());
Assert.assertEquals(threadFactory, threadPoolExecutor.getThreadFactory());
Assert.assertEquals(3, resourcesManager.get("nacos").get(testGroup).size());

executorService = ExecutorFactory.Managed.newFixedExecutorService(testGroup, 10, threadFactory);
Assert.assertTrue(executorService instanceof ThreadPoolExecutor);
threadPoolExecutor = (ThreadPoolExecutor) executorService;
Assert.assertEquals(10, threadPoolExecutor.getCorePoolSize());
Assert.assertEquals(10, threadPoolExecutor.getMaximumPoolSize());
Assert.assertEquals(threadFactory, threadPoolExecutor.getThreadFactory());
Assert.assertEquals(4, resourcesManager.get("nacos").get(testGroup).size());

ScheduledThreadPoolExecutor scheduledThreadPoolExecutor;

executorService = ExecutorFactory.Managed.newSingleScheduledExecutorService(testGroup, threadFactory);
Assert.assertTrue(executorService instanceof ScheduledThreadPoolExecutor);
scheduledThreadPoolExecutor = (ScheduledThreadPoolExecutor) executorService;
Assert.assertEquals(1, scheduledThreadPoolExecutor.getCorePoolSize());
Assert.assertEquals(Integer.MAX_VALUE, scheduledThreadPoolExecutor.getMaximumPoolSize());
Assert.assertEquals(threadFactory, threadPoolExecutor.getThreadFactory());
Assert.assertEquals(5, resourcesManager.get("nacos").get(testGroup).size());

executorService = ExecutorFactory.Managed.newScheduledExecutorService(testGroup, 10, threadFactory);
Assert.assertTrue(executorService instanceof ScheduledThreadPoolExecutor);
scheduledThreadPoolExecutor = (ScheduledThreadPoolExecutor) executorService;
Assert.assertEquals(10, scheduledThreadPoolExecutor.getCorePoolSize());
Assert.assertEquals(Integer.MAX_VALUE, scheduledThreadPoolExecutor.getMaximumPoolSize());
Assert.assertEquals(threadFactory, threadPoolExecutor.getThreadFactory());
Assert.assertEquals(6, resourcesManager.get("nacos").get(testGroup).size());

threadPoolExecutor = ExecutorFactory.Managed.newCustomerThreadExecutor(testGroup, 10, 20, 1000, threadFactory);
Assert.assertEquals(10, threadPoolExecutor.getCorePoolSize());
Assert.assertEquals(20, threadPoolExecutor.getMaximumPoolSize());
Assert.assertEquals(threadFactory, threadPoolExecutor.getThreadFactory());
Assert.assertEquals(7, resourcesManager.get("nacos").get(testGroup).size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.common.executor;

import org.junit.Assert;
import org.junit.Test;

public class NameThreadFactoryTest {

@Test
public void test() {
NameThreadFactory threadFactory = new NameThreadFactory("test");
Thread t1 = threadFactory.newThread(() -> {

});
Thread t2 = threadFactory.newThread(() -> {

});

Assert.assertEquals(t1.getName(), "test.0");
Assert.assertEquals(t2.getName(), "test.1");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.common.executor;

import org.junit.Assert;
import org.junit.Test;

import java.util.concurrent.ExecutorService;

public class ThreadPoolManagerTest {

@Test
public void test() {
ThreadPoolManager manager = ThreadPoolManager.getInstance();
ExecutorService executor = ExecutorFactory.newSingleExecutorService();
String namespace = "test";
String group = "test";

manager.register(namespace, group, executor);
Assert.assertTrue(manager.getResourcesManager().containsKey(namespace));
Assert.assertEquals(1, manager.getResourcesManager().get(namespace).get(group).size());
Assert.assertTrue(manager.getLockers().containsKey(namespace));

manager.register(namespace, group, ExecutorFactory.newSingleExecutorService());
Assert.assertEquals(2, manager.getResourcesManager().get(namespace).get(group).size());
Assert.assertTrue(manager.getLockers().containsKey(namespace));

manager.destroy(namespace, group);
Assert.assertFalse(manager.getResourcesManager().get(namespace).containsKey(group));
Assert.assertTrue(manager.getLockers().containsKey(namespace));

manager.register(namespace, group, executor);
manager.destroy(namespace);
Assert.assertFalse(manager.getResourcesManager().containsKey(namespace));
Assert.assertTrue(manager.getLockers().containsKey(namespace));

manager.register(namespace, group, executor);
manager.deregister(namespace, group, ExecutorFactory.newSingleExecutorService());
Assert.assertEquals(1, manager.getResourcesManager().get(namespace).get(group).size());
Assert.assertTrue(manager.getLockers().containsKey(namespace));

manager.deregister(namespace, group, executor);
Assert.assertEquals(0, manager.getResourcesManager().get(namespace).get(group).size());
Assert.assertTrue(manager.getLockers().containsKey(namespace));

manager.register(namespace, group, executor);
manager.deregister(namespace, group);
Assert.assertFalse(manager.getResourcesManager().get(namespace).containsKey(group));
Assert.assertTrue(manager.getLockers().containsKey(namespace));

manager.register(namespace, group, executor);
manager.register(namespace, group, ExecutorFactory.newSingleExecutorService());
ThreadPoolManager.shutdown();
Assert.assertFalse(manager.getResourcesManager().containsKey(namespace));
Assert.assertTrue(manager.getLockers().containsKey(namespace));

manager.destroy(namespace);
manager.destroy(namespace, group);
Assert.assertFalse(manager.getResourcesManager().containsKey(namespace));
Assert.assertTrue(manager.getLockers().containsKey(namespace));
}
}