|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.dromara.dynamictp.test.core.support; |
| 19 | + |
| 20 | +import org.dromara.dynamictp.core.executor.DtpExecutor; |
| 21 | +import org.dromara.dynamictp.core.support.ExecutorWrapper; |
| 22 | +import org.dromara.dynamictp.core.support.ThreadPoolStatProvider; |
| 23 | +import org.junit.jupiter.api.AfterEach; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import java.util.concurrent.LinkedBlockingQueue; |
| 27 | +import java.util.concurrent.TimeUnit; |
| 28 | + |
| 29 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 30 | + |
| 31 | +/** |
| 32 | + * ThreadPoolStatProvider test |
| 33 | + * |
| 34 | + * @author yanhom |
| 35 | + * @since 1.2.2 |
| 36 | + */ |
| 37 | +class ThreadPoolStatProviderTest { |
| 38 | + |
| 39 | + private DtpExecutor dtpExecutor; |
| 40 | + |
| 41 | + @AfterEach |
| 42 | + void tearDown() { |
| 43 | + if (dtpExecutor != null) { |
| 44 | + dtpExecutor.shutdownNow(); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void testNullRunnableOperationsDoNotThrow() { |
| 50 | + ThreadPoolStatProvider provider = createProvider(); |
| 51 | + |
| 52 | + assertDoesNotThrow(() -> provider.startTask(null)); |
| 53 | + assertDoesNotThrow(() -> provider.completeTask(null)); |
| 54 | + assertDoesNotThrow(() -> provider.cancelRunTimeoutTask(null)); |
| 55 | + assertDoesNotThrow(() -> provider.cancelQueueTimeoutTask(null)); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void testStartAndCompleteTaskWorkNormally() { |
| 60 | + ThreadPoolStatProvider provider = createProvider(); |
| 61 | + Runnable task = () -> { |
| 62 | + }; |
| 63 | + |
| 64 | + assertDoesNotThrow(() -> { |
| 65 | + provider.startTask(task); |
| 66 | + TimeUnit.MILLISECONDS.sleep(5); |
| 67 | + provider.completeTask(task); |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + private ThreadPoolStatProvider createProvider() { |
| 72 | + dtpExecutor = new DtpExecutor(1, 1, 30, TimeUnit.SECONDS, new LinkedBlockingQueue<>()); |
| 73 | + dtpExecutor.setThreadPoolName("stat-provider-test"); |
| 74 | + ExecutorWrapper wrapper = new ExecutorWrapper(dtpExecutor); |
| 75 | + return wrapper.getThreadPoolStatProvider(); |
| 76 | + } |
| 77 | +} |
0 commit comments