From f7ce392dbc4f7cb818cc4470380f37ab7d635fce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=88=90=E5=85=B4?= <49221670+Createsequence@users.noreply.github.com> Date: Wed, 2 Nov 2022 12:23:59 +0800 Subject: [PATCH] test: Add unit test about plugin manager (#892) --- .../DefaultThreadPoolPluginManagerTest.java | 163 +++++++++++++++ .../DefaultThreadPoolPluginRegistrarTest.java | 57 ++++++ .../EmptyThreadPoolPluginManagerTest.java | 110 ++++++++++ .../manager/ThreadPoolPluginSupportTest.java | 189 ++++++++++++++++++ 4 files changed, 519 insertions(+) create mode 100644 hippo4j-core/src/test/java/cn/hippo4j/core/plugin/manager/DefaultThreadPoolPluginManagerTest.java create mode 100644 hippo4j-core/src/test/java/cn/hippo4j/core/plugin/manager/DefaultThreadPoolPluginRegistrarTest.java create mode 100644 hippo4j-core/src/test/java/cn/hippo4j/core/plugin/manager/EmptyThreadPoolPluginManagerTest.java create mode 100644 hippo4j-core/src/test/java/cn/hippo4j/core/plugin/manager/ThreadPoolPluginSupportTest.java diff --git a/hippo4j-core/src/test/java/cn/hippo4j/core/plugin/manager/DefaultThreadPoolPluginManagerTest.java b/hippo4j-core/src/test/java/cn/hippo4j/core/plugin/manager/DefaultThreadPoolPluginManagerTest.java new file mode 100644 index 00000000..627e65aa --- /dev/null +++ b/hippo4j-core/src/test/java/cn/hippo4j/core/plugin/manager/DefaultThreadPoolPluginManagerTest.java @@ -0,0 +1,163 @@ +/* + * 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.core.plugin.manager; + +import cn.hippo4j.core.plugin.*; +import lombok.Getter; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * test for {@link DefaultThreadPoolPluginManager} + */ +public class DefaultThreadPoolPluginManagerTest { + + private DefaultThreadPoolPluginManager manager; + + @Before + public void initRegistry() { + manager = new DefaultThreadPoolPluginManager(); + } + + @Test + public void testRegister() { + manager.register(new TestShutdownAwarePlugin()); + Assert.assertEquals(1, manager.getAllPlugins().size()); + } + + @Test + public void testGetAllPlugins() { + manager.register(new TestExecuteAwarePlugin()); + manager.register(new TestRejectedAwarePlugin()); + Assert.assertEquals(2, manager.getAllPlugins().size()); + } + + @Test + public void testClear() { + manager.register(new TestExecuteAwarePlugin()); + manager.clear(); + Assert.assertTrue(manager.getAllPlugins().isEmpty()); + } + + @Test + public void testTryRegister() { + Assert.assertTrue(manager.tryRegister(new TestExecuteAwarePlugin())); + Assert.assertFalse(manager.tryRegister(new TestExecuteAwarePlugin())); + } + + @Test + public void testIsRegistered() { + Assert.assertFalse(manager.isRegistered(TestExecuteAwarePlugin.class.getSimpleName())); + manager.register(new TestExecuteAwarePlugin()); + Assert.assertTrue(manager.isRegistered(TestExecuteAwarePlugin.class.getSimpleName())); + } + + @Test + public void testUnregister() { + manager.register(new TestExecuteAwarePlugin()); + manager.unregister(TestExecuteAwarePlugin.class.getSimpleName()); + Assert.assertFalse(manager.isRegistered(TestExecuteAwarePlugin.class.getSimpleName())); + } + + @Test + public void testGetPlugin() { + ThreadPoolPlugin plugin = new TestExecuteAwarePlugin(); + manager.register(plugin); + Assert.assertSame(plugin, manager.getPlugin(plugin.getId()).orElse(null)); + } + + @Test + public void testGetRejectedAwarePluginList() { + manager.register(new TestRejectedAwarePlugin()); + Assert.assertEquals(1, manager.getRejectedAwarePluginList().size()); + } + + @Test + public void testGetShutdownAwarePluginList() { + manager.register(new TestShutdownAwarePlugin()); + Assert.assertEquals(1, manager.getShutdownAwarePluginList().size()); + } + + @Test + public void testGetTaskAwarePluginList() { + manager.register(new TestTaskAwarePlugin()); + Assert.assertEquals(1, manager.getTaskAwarePluginList().size()); + } + + @Test + public void testGetExecuteAwarePluginList() { + manager.register(new TestExecuteAwarePlugin()); + Assert.assertEquals(1, manager.getExecuteAwarePluginList().size()); + } + + @Test + public void testGetAllPluginsOfType() { + manager.register(new TestExecuteAwarePlugin()); + manager.register(new TestRejectedAwarePlugin()); + Assert.assertEquals(1, manager.getAllPluginsOfType(TestExecuteAwarePlugin.class).size()); + Assert.assertEquals(1, manager.getAllPluginsOfType(TestRejectedAwarePlugin.class).size()); + Assert.assertEquals(2, manager.getAllPluginsOfType(ThreadPoolPlugin.class).size()); + } + + @Test + public void testGetAllPluginRuntimes() { + manager.register(new TestExecuteAwarePlugin()); + manager.register(new TestRejectedAwarePlugin()); + Assert.assertEquals(2, manager.getAllPluginRuntimes().size()); + } + + @Test + public void testGetPluginRuntime() { + manager.register(new TestExecuteAwarePlugin()); + Assert.assertTrue(manager.getRuntime(TestExecuteAwarePlugin.class.getSimpleName()).isPresent()); + } + + @Test + public void testGetPluginOfType() { + manager.register(new TestExecuteAwarePlugin()); + Assert.assertTrue(manager.getPluginOfType(TestExecuteAwarePlugin.class.getSimpleName(), TestExecuteAwarePlugin.class).isPresent()); + Assert.assertTrue(manager.getPluginOfType(TestExecuteAwarePlugin.class.getSimpleName(), ThreadPoolPlugin.class).isPresent()); + Assert.assertFalse(manager.getPluginOfType(TestExecuteAwarePlugin.class.getSimpleName(), RejectedAwarePlugin.class).isPresent()); + } + + @Getter + private final static class TestTaskAwarePlugin implements TaskAwarePlugin { + + private final String id = this.getClass().getSimpleName(); + } + + @Getter + private final static class TestExecuteAwarePlugin implements ExecuteAwarePlugin { + + private final String id = this.getClass().getSimpleName(); + } + + @Getter + private final static class TestRejectedAwarePlugin implements RejectedAwarePlugin { + + private final String id = this.getClass().getSimpleName(); + } + + @Getter + private final static class TestShutdownAwarePlugin implements ShutdownAwarePlugin { + + private final String id = this.getClass().getSimpleName(); + } + +} diff --git a/hippo4j-core/src/test/java/cn/hippo4j/core/plugin/manager/DefaultThreadPoolPluginRegistrarTest.java b/hippo4j-core/src/test/java/cn/hippo4j/core/plugin/manager/DefaultThreadPoolPluginRegistrarTest.java new file mode 100644 index 00000000..435a8c11 --- /dev/null +++ b/hippo4j-core/src/test/java/cn/hippo4j/core/plugin/manager/DefaultThreadPoolPluginRegistrarTest.java @@ -0,0 +1,57 @@ +/* + * 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.core.plugin.manager; + +import cn.hippo4j.core.executor.ExtensibleThreadPoolExecutor; +import cn.hippo4j.core.plugin.impl.*; +import org.junit.Assert; +import org.junit.Test; + +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +/** + * test for {@link DefaultThreadPoolPluginRegistrar} + */ +public class DefaultThreadPoolPluginRegistrarTest { + + @Test + public void testGetId() { + ThreadPoolPluginRegistrar registrar = new DefaultThreadPoolPluginRegistrar(); + Assert.assertEquals(registrar.getClass().getSimpleName(), registrar.getId()); + } + + @Test + public void testDoRegister() { + ThreadPoolPluginRegistrar registrar = new DefaultThreadPoolPluginRegistrar(100L, 100L); + ThreadPoolPluginManager manager = new DefaultThreadPoolPluginManager(); + ExtensibleThreadPoolExecutor executor = new ExtensibleThreadPoolExecutor( + "test", manager, + 5, 5, 1000L, TimeUnit.MILLISECONDS, + new ArrayBlockingQueue<>(1), Thread::new, new ThreadPoolExecutor.AbortPolicy()); + registrar.doRegister(executor); + + Assert.assertTrue(manager.getPlugin(TaskDecoratorPlugin.PLUGIN_NAME).isPresent()); + Assert.assertTrue(manager.getPlugin(TaskTimeoutNotifyAlarmPlugin.PLUGIN_NAME).isPresent()); + Assert.assertTrue(manager.getPlugin(TaskRejectCountRecordPlugin.PLUGIN_NAME).isPresent()); + Assert.assertTrue(manager.getPlugin(TaskRejectNotifyAlarmPlugin.PLUGIN_NAME).isPresent()); + Assert.assertTrue(manager.getPlugin(ThreadPoolExecutorShutdownPlugin.PLUGIN_NAME).isPresent()); + } + +} diff --git a/hippo4j-core/src/test/java/cn/hippo4j/core/plugin/manager/EmptyThreadPoolPluginManagerTest.java b/hippo4j-core/src/test/java/cn/hippo4j/core/plugin/manager/EmptyThreadPoolPluginManagerTest.java new file mode 100644 index 00000000..f5e0ac26 --- /dev/null +++ b/hippo4j-core/src/test/java/cn/hippo4j/core/plugin/manager/EmptyThreadPoolPluginManagerTest.java @@ -0,0 +1,110 @@ +/* + * 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.core.plugin.manager; + +import cn.hippo4j.core.plugin.ThreadPoolPlugin; +import lombok.Getter; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Collections; +import java.util.Optional; + +/** + * test for {@link cn.hippo4j.core.plugin.manager.EmptyThreadPoolPluginManager} + */ +public class EmptyThreadPoolPluginManagerTest { + + private final ThreadPoolPluginManager manager = EmptyThreadPoolPluginManager.INSTANCE; + + @Test + public void testEmpty() { + Assert.assertSame(manager, ThreadPoolPluginManager.empty()); + } + + @Test + public void testGetAllPlugins() { + Assert.assertEquals(Collections.emptyList(), manager.getAllPluginRuntimes()); + } + + @Test + public void testClear() { + manager.clear(); + Assert.assertTrue(isEmpty(manager)); + } + + @Test + public void testRegister() { + manager.register(new TestPlugin()); + Assert.assertTrue(isEmpty(manager)); + } + + @Test + public void testTryRegister() { + Assert.assertFalse(manager.tryRegister(new TestPlugin())); + } + + @Test + public void testIsRegistered() { + manager.register(new TestPlugin()); + Assert.assertFalse(manager.isRegistered(TestPlugin.class.getSimpleName())); + } + + @Test + public void testUnregister() { + manager.register(new TestPlugin()); + manager.unregister(TestPlugin.class.getSimpleName()); + Assert.assertTrue(isEmpty(manager)); + } + + @Test + public void testGetPlugin() { + Assert.assertSame(Optional.empty(), manager.getPlugin("")); + } + + @Test + public void testGetRejectedAwarePluginList() { + Assert.assertEquals(Collections.emptyList(), manager.getRejectedAwarePluginList()); + } + + @Test + public void testGetShutdownAwarePluginList() { + Assert.assertEquals(Collections.emptyList(), manager.getShutdownAwarePluginList()); + } + + @Test + public void testGetTaskAwarePluginList() { + Assert.assertEquals(Collections.emptyList(), manager.getTaskAwarePluginList()); + } + + @Test + public void testGetExecuteAwarePluginList() { + Assert.assertEquals(Collections.emptyList(), manager.getExecuteAwarePluginList()); + } + + private static boolean isEmpty(ThreadPoolPluginManager manager) { + return manager.getAllPlugins().isEmpty(); + } + + @Getter + private static class TestPlugin implements ThreadPoolPlugin { + + private final String id = TestPlugin.class.getSimpleName(); + } + +} diff --git a/hippo4j-core/src/test/java/cn/hippo4j/core/plugin/manager/ThreadPoolPluginSupportTest.java b/hippo4j-core/src/test/java/cn/hippo4j/core/plugin/manager/ThreadPoolPluginSupportTest.java new file mode 100644 index 00000000..e92ce58b --- /dev/null +++ b/hippo4j-core/src/test/java/cn/hippo4j/core/plugin/manager/ThreadPoolPluginSupportTest.java @@ -0,0 +1,189 @@ +/* + * 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.core.plugin.manager; + +import cn.hippo4j.core.executor.ExtensibleThreadPoolExecutor; +import cn.hippo4j.core.plugin.*; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.junit.Assert; +import org.junit.Test; + +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +/** + * test for default method of {@link ThreadPoolPluginSupport} + */ +public class ThreadPoolPluginSupportTest { + + private final ThreadPoolPluginManager manager = new DefaultThreadPoolPluginManager(); + private final ExtensibleThreadPoolExecutor executor = new ExtensibleThreadPoolExecutor( + "test", manager, + 5, 5, 1000L, TimeUnit.MILLISECONDS, + new ArrayBlockingQueue<>(1), Thread::new, new ThreadPoolExecutor.AbortPolicy()); + private final ThreadPoolPluginSupport support = new TestSupport(executor.getThreadPoolId(), executor, manager); + + @Test + public void testGetThreadPoolId() { + Assert.assertEquals(executor.getThreadPoolId(), support.getThreadPoolId()); + } + + @Test + public void testGetThreadPoolPluginManager() { + Assert.assertEquals(manager, support.getThreadPoolPluginManager()); + } + + @Getter + @RequiredArgsConstructor + private static class TestSupport implements ThreadPoolPluginSupport { + + private final String threadPoolId; + private final ExtensibleThreadPoolExecutor threadPoolExecutor; + private final ThreadPoolPluginManager threadPoolPluginManager; + } + + // ================ default delegate method ================ + + @Test + public void testRegister() { + support.register(new TestShutdownAwarePlugin()); + Assert.assertEquals(1, support.getAllPlugins().size()); + } + + @Test + public void testGetAllPlugins() { + support.register(new TestExecuteAwarePlugin()); + support.register(new TestRejectedAwarePlugin()); + Assert.assertEquals(2, support.getAllPlugins().size()); + } + + @Test + public void testClear() { + support.register(new TestExecuteAwarePlugin()); + support.clear(); + Assert.assertTrue(support.getAllPlugins().isEmpty()); + } + + @Test + public void testTryRegister() { + Assert.assertTrue(support.tryRegister(new TestExecuteAwarePlugin())); + Assert.assertFalse(support.tryRegister(new TestExecuteAwarePlugin())); + } + + @Test + public void testIsRegistered() { + Assert.assertFalse(support.isRegistered(TestExecuteAwarePlugin.class.getSimpleName())); + support.register(new TestExecuteAwarePlugin()); + Assert.assertTrue(support.isRegistered(TestExecuteAwarePlugin.class.getSimpleName())); + } + + @Test + public void testUnregister() { + support.register(new TestExecuteAwarePlugin()); + support.unregister(TestExecuteAwarePlugin.class.getSimpleName()); + Assert.assertFalse(support.isRegistered(TestExecuteAwarePlugin.class.getSimpleName())); + } + + @Test + public void testGetPlugin() { + ThreadPoolPlugin plugin = new TestExecuteAwarePlugin(); + support.register(plugin); + Assert.assertSame(plugin, support.getPlugin(plugin.getId()).orElse(null)); + } + + @Test + public void testGetRejectedAwarePluginList() { + support.register(new TestRejectedAwarePlugin()); + Assert.assertEquals(1, support.getRejectedAwarePluginList().size()); + } + + @Test + public void testGetShutdownAwarePluginList() { + support.register(new TestShutdownAwarePlugin()); + Assert.assertEquals(1, support.getShutdownAwarePluginList().size()); + } + + @Test + public void testGetTaskAwarePluginList() { + support.register(new TestTaskAwarePlugin()); + Assert.assertEquals(1, support.getTaskAwarePluginList().size()); + } + + @Test + public void testGetExecuteAwarePluginList() { + support.register(new TestExecuteAwarePlugin()); + Assert.assertEquals(1, support.getExecuteAwarePluginList().size()); + } + + @Test + public void testGetAllPluginsOfType() { + support.register(new TestExecuteAwarePlugin()); + support.register(new TestRejectedAwarePlugin()); + Assert.assertEquals(1, support.getAllPluginsOfType(TestExecuteAwarePlugin.class).size()); + Assert.assertEquals(1, support.getAllPluginsOfType(TestRejectedAwarePlugin.class).size()); + Assert.assertEquals(2, support.getAllPluginsOfType(ThreadPoolPlugin.class).size()); + } + + @Test + public void testGetAllPluginRuntimes() { + support.register(new TestExecuteAwarePlugin()); + support.register(new TestRejectedAwarePlugin()); + Assert.assertEquals(2, support.getAllPluginRuntimes().size()); + } + + @Test + public void testGetPluginRuntime() { + support.register(new TestExecuteAwarePlugin()); + Assert.assertTrue(support.getRuntime(TestExecuteAwarePlugin.class.getSimpleName()).isPresent()); + } + + @Test + public void testGetPluginOfType() { + support.register(new TestExecuteAwarePlugin()); + Assert.assertTrue(support.getPluginOfType(TestExecuteAwarePlugin.class.getSimpleName(), TestExecuteAwarePlugin.class).isPresent()); + Assert.assertTrue(support.getPluginOfType(TestExecuteAwarePlugin.class.getSimpleName(), ThreadPoolPlugin.class).isPresent()); + Assert.assertFalse(support.getPluginOfType(TestExecuteAwarePlugin.class.getSimpleName(), RejectedAwarePlugin.class).isPresent()); + } + + @Getter + private final static class TestTaskAwarePlugin implements TaskAwarePlugin { + + private final String id = this.getClass().getSimpleName(); + } + + @Getter + private final static class TestExecuteAwarePlugin implements ExecuteAwarePlugin { + + private final String id = this.getClass().getSimpleName(); + } + + @Getter + private final static class TestRejectedAwarePlugin implements RejectedAwarePlugin { + + private final String id = this.getClass().getSimpleName(); + } + + @Getter + private final static class TestShutdownAwarePlugin implements ShutdownAwarePlugin { + + private final String id = this.getClass().getSimpleName(); + } + +}