From 8e0fb35366c08e5488a11e8ea5054da7f73c7cbc Mon Sep 17 00:00:00 2001 From: dailidong Date: Thu, 8 Oct 2020 23:27:36 +0800 Subject: [PATCH 1/4] Update dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/threadutils/ThreadUtilsTest.java Co-authored-by: Yichao Yang <1048262223@qq.com> --- .../dolphinscheduler/common/threadutils/ThreadUtilsTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/threadutils/ThreadUtilsTest.java b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/threadutils/ThreadUtilsTest.java index 53441f0463..821399ed6c 100644 --- a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/threadutils/ThreadUtilsTest.java +++ b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/threadutils/ThreadUtilsTest.java @@ -37,7 +37,7 @@ public class ThreadUtilsTest { @Test public void testNewDaemonFixedThreadExecutor() { // create core size and max size are all 3 - ExecutorService testExec = ThreadUtils.newDaemonFixedThreadExecutor("test-exec-thread",3); + ExecutorService testExec = ThreadUtils.newDaemonFixedThreadExecutor("test-exec-thread", 3); for (int i = 0; i < 2; i++) { final int index = i; From 29a083ca8f9a6def8a3a3dc405086190e5261587 Mon Sep 17 00:00:00 2001 From: dailidong Date: Fri, 16 Oct 2020 10:43:46 +0800 Subject: [PATCH 2/4] Update ThreadUtilsTest.java --- .../dolphinscheduler/common/threadutils/ThreadUtilsTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/threadutils/ThreadUtilsTest.java b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/threadutils/ThreadUtilsTest.java index 821399ed6c..53441f0463 100644 --- a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/threadutils/ThreadUtilsTest.java +++ b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/threadutils/ThreadUtilsTest.java @@ -37,7 +37,7 @@ public class ThreadUtilsTest { @Test public void testNewDaemonFixedThreadExecutor() { // create core size and max size are all 3 - ExecutorService testExec = ThreadUtils.newDaemonFixedThreadExecutor("test-exec-thread", 3); + ExecutorService testExec = ThreadUtils.newDaemonFixedThreadExecutor("test-exec-thread",3); for (int i = 0; i < 2; i++) { final int index = i; From ed3f08916f782f1451455914288ebbb2824f46e1 Mon Sep 17 00:00:00 2001 From: dailidong Date: Fri, 16 Oct 2020 13:16:03 +0800 Subject: [PATCH 3/4] Update ThreadUtilsTest.java --- .../common/threadutils/ThreadUtilsTest.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/threadutils/ThreadUtilsTest.java b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/threadutils/ThreadUtilsTest.java index 53441f0463..3d44c553d3 100644 --- a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/threadutils/ThreadUtilsTest.java +++ b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/threadutils/ThreadUtilsTest.java @@ -19,6 +19,7 @@ package org.apache.dolphinscheduler.common.threadutils; import org.apache.dolphinscheduler.common.thread.Stopper; import org.apache.dolphinscheduler.common.thread.ThreadPoolExecutors; import org.apache.dolphinscheduler.common.thread.ThreadUtils; + import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,13 +32,14 @@ import static org.junit.Assert.*; public class ThreadUtilsTest { private static final Logger logger = LoggerFactory.getLogger(ThreadUtilsTest.class); + /** * create a naming thread */ @Test public void testNewDaemonFixedThreadExecutor() { // create core size and max size are all 3 - ExecutorService testExec = ThreadUtils.newDaemonFixedThreadExecutor("test-exec-thread",3); + ExecutorService testExec = ThreadUtils.newDaemonFixedThreadExecutor("test-exec-thread", 3); for (int i = 0; i < 2; i++) { final int index = i; @@ -73,8 +75,8 @@ public class ThreadUtilsTest { // send heart beat work while (start.getTime().getTime() <= end.getTime().getTime()) { System.out.println("worker here"); - System.out.println(index ++); - start.add(Calendar.SECOND, 1); + System.out.println(index++); + start.add(Calendar.SECOND, 1); globalTimer.add(Calendar.SECOND, 1); } } @@ -102,19 +104,17 @@ public class ThreadUtilsTest { /** * test threadPoolExecutors with 3 workers and current each 5 tasks - * @throws InterruptedException */ @Test public void testThreadInfo() throws InterruptedException { ThreadPoolExecutors workers = ThreadPoolExecutors.getInstance("worker", 1); - for (int i = 0; i < 2; ++i ) { + for (int i = 0; i < 2; ++i) { int index = i; workers.execute(() -> { for (int j = 0; j < 1; ++j) { try { Thread.sleep(100); logger.info("worker %s is doing the task", index); -// workers.printStatus(); } catch (InterruptedException e) { logger.error("InterruptedException", e); } @@ -129,7 +129,7 @@ public class ThreadUtilsTest { * test a single daemon thread pool */ @Test - public void testNewDaemonSingleThreadExecutor() { + public void testNewDaemonSingleThreadExecutor() { ExecutorService threadTest = ThreadUtils.newDaemonSingleThreadExecutor("thread_test"); threadTest.execute(() -> { for (int i = 0; i < 2; ++i) { @@ -148,7 +148,7 @@ public class ThreadUtilsTest { ThreadPoolExecutor threadPoolExecutor = ThreadUtils.newDaemonCachedThreadPool("threadTest-"); Thread thread1 = threadPoolExecutor.getThreadFactory().newThread(() -> { for (int i = 0; i < 2; ++i) { - logger.info("this task is with index " + i ); + logger.info("this task is with index " + i); } }); assertTrue(thread1.getName().startsWith("threadTest-")); @@ -160,7 +160,7 @@ public class ThreadUtilsTest { @Test public void testNewDaemonCachedThreadPoolWithThreadNumber() { ThreadPoolExecutor threadPoolExecutor = ThreadUtils.newDaemonCachedThreadPool("threadTest--", 3, 10); - for (int i = 0; i < 2; ++ i) { + for (int i = 0; i < 2; ++i) { threadPoolExecutor.getThreadFactory().newThread(() -> { assertEquals(3, threadPoolExecutor.getActiveCount()); logger.info("this task is first work to do"); @@ -172,5 +172,4 @@ public class ThreadUtilsTest { } - } From 15436cfcdd7612011e3d02bc3c84da8958de16dc Mon Sep 17 00:00:00 2001 From: dailidong Date: Fri, 16 Oct 2020 13:49:57 +0800 Subject: [PATCH 4/4] Delete ThreadUtilsTest.java not useful test --- .../common/threadutils/ThreadUtilsTest.java | 175 ------------------ 1 file changed, 175 deletions(-) delete mode 100644 dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/threadutils/ThreadUtilsTest.java diff --git a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/threadutils/ThreadUtilsTest.java b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/threadutils/ThreadUtilsTest.java deleted file mode 100644 index 3d44c553d3..0000000000 --- a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/threadutils/ThreadUtilsTest.java +++ /dev/null @@ -1,175 +0,0 @@ -/* - * 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 org.apache.dolphinscheduler.common.threadutils; - -import org.apache.dolphinscheduler.common.thread.Stopper; -import org.apache.dolphinscheduler.common.thread.ThreadPoolExecutors; -import org.apache.dolphinscheduler.common.thread.ThreadUtils; - -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Calendar; -import java.util.concurrent.*; - -import static org.junit.Assert.*; - - -public class ThreadUtilsTest { - private static final Logger logger = LoggerFactory.getLogger(ThreadUtilsTest.class); - - /** - * create a naming thread - */ - @Test - public void testNewDaemonFixedThreadExecutor() { - // create core size and max size are all 3 - ExecutorService testExec = ThreadUtils.newDaemonFixedThreadExecutor("test-exec-thread", 3); - - for (int i = 0; i < 2; i++) { - final int index = i; - testExec.submit(() -> { - logger.info("do some work index " + index); - }); - } - assertFalse(testExec.isShutdown()); - testExec.shutdownNow(); - assertTrue(testExec.isShutdown()); - - } - - /** - * test schedulerThreadExecutor as for print time in scheduler - * default check thread is 1 - */ - @Test - public void testNewDaemonScheduleThreadExecutor() { - - ScheduledExecutorService scheduleService = ThreadUtils.newDaemonThreadScheduledExecutor("scheduler-thread", 1); - Calendar start = Calendar.getInstance(); - Calendar globalTimer = Calendar.getInstance(); - globalTimer.set(2019, Calendar.DECEMBER, 1, 0, 0, 0); - // current - Calendar end = Calendar.getInstance(); - end.set(2019, Calendar.DECEMBER, 1, 0, 0, 3); - Runnable schedulerTask = new Runnable() { - @Override - public void run() { - start.set(2019, Calendar.DECEMBER, 1, 0, 0, 0); - int index = 0; - // send heart beat work - while (start.getTime().getTime() <= end.getTime().getTime()) { - System.out.println("worker here"); - System.out.println(index++); - start.add(Calendar.SECOND, 1); - globalTimer.add(Calendar.SECOND, 1); - } - } - }; - scheduleService.scheduleAtFixedRate(schedulerTask, 2, 10, TimeUnit.SECONDS); - assertFalse(scheduleService.isShutdown()); - try { - Thread.sleep(2000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - scheduleService.shutdownNow(); - assertTrue(scheduleService.isShutdown()); - } - - /** - * test stopper is working normal - */ - @Test - public void testStopper() { - assertTrue(Stopper.isRunning()); - Stopper.stop(); - assertTrue(Stopper.isStopped()); - } - - /** - * test threadPoolExecutors with 3 workers and current each 5 tasks - */ - @Test - public void testThreadInfo() throws InterruptedException { - ThreadPoolExecutors workers = ThreadPoolExecutors.getInstance("worker", 1); - for (int i = 0; i < 2; ++i) { - int index = i; - workers.execute(() -> { - for (int j = 0; j < 1; ++j) { - try { - Thread.sleep(100); - logger.info("worker %s is doing the task", index); - } catch (InterruptedException e) { - logger.error("InterruptedException", e); - } - } - }); - } - Thread.sleep(1001); - workers.shutdown(); - } - - /** - * test a single daemon thread pool - */ - @Test - public void testNewDaemonSingleThreadExecutor() { - ExecutorService threadTest = ThreadUtils.newDaemonSingleThreadExecutor("thread_test"); - threadTest.execute(() -> { - for (int i = 0; i < 2; ++i) { - logger.info("daemon working "); - } - - }); - assertFalse(threadTest.isShutdown()); - threadTest.shutdownNow(); - assertTrue(threadTest.isShutdown()); - } - - @Test - public void testNewDaemonCachedThreadPool() { - - ThreadPoolExecutor threadPoolExecutor = ThreadUtils.newDaemonCachedThreadPool("threadTest-"); - Thread thread1 = threadPoolExecutor.getThreadFactory().newThread(() -> { - for (int i = 0; i < 2; ++i) { - logger.info("this task is with index " + i); - } - }); - assertTrue(thread1.getName().startsWith("threadTest-")); - assertFalse(threadPoolExecutor.isShutdown()); - threadPoolExecutor.shutdown(); - assertTrue(threadPoolExecutor.isShutdown()); - } - - @Test - public void testNewDaemonCachedThreadPoolWithThreadNumber() { - ThreadPoolExecutor threadPoolExecutor = ThreadUtils.newDaemonCachedThreadPool("threadTest--", 3, 10); - for (int i = 0; i < 2; ++i) { - threadPoolExecutor.getThreadFactory().newThread(() -> { - assertEquals(3, threadPoolExecutor.getActiveCount()); - logger.info("this task is first work to do"); - }); - } - assertFalse(threadPoolExecutor.isShutdown()); - threadPoolExecutor.shutdown(); - assertTrue(threadPoolExecutor.isShutdown()); - } - - -}