test: Add unit test about plugin manager (#892)

pull/895/head
黄成兴 2 years ago committed by GitHub
parent d2f8a5e3ab
commit f7ce392dbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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();
}
}

@ -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());
}
}

@ -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();
}
}

@ -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();
}
}
Loading…
Cancel
Save