From 853bddd5a32b6a96077cc49fd7b0df026b9732a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=97=B6=E9=97=B4=E9=82=AE=E9=80=92=E5=91=98?= <78356082+barret-yzh@users.noreply.github.com> Date: Thu, 25 May 2023 21:01:56 +0800 Subject: [PATCH] Create ThreadFactoryBuilderTest --- .../common/executor/ThreadFactoryBuilderTest | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 infra/common/src/test/java/cn/hippo4j/common/executor/ThreadFactoryBuilderTest diff --git a/infra/common/src/test/java/cn/hippo4j/common/executor/ThreadFactoryBuilderTest b/infra/common/src/test/java/cn/hippo4j/common/executor/ThreadFactoryBuilderTest new file mode 100644 index 00000000..ca83ff2f --- /dev/null +++ b/infra/common/src/test/java/cn/hippo4j/common/executor/ThreadFactoryBuilderTest @@ -0,0 +1,45 @@ +package cn.hippo4j.common.design.builder; + +import cn.hippo4j.common.design.builder.ThreadFactoryBuilder; +import org.junit.Assert; +import org.junit.Test; +import java.util.concurrent.ThreadFactory; +import java.lang.Thread.UncaughtExceptionHandler; + +public class ThreadFactoryBuilderTest { + + @Test + public void testBuild() { + ThreadFactoryBuilder builder = ThreadFactoryBuilder.builder(); + TestUncaughtExceptionHandler uncaughtExceptionHandler = new TestUncaughtExceptionHandler(); + builder.uncaughtExceptionHandler(uncaughtExceptionHandler); + builder.prefix("my-thread-factory"); + builder.daemon(true); + builder.priority(Thread.MAX_PRIORITY); + ThreadFactory threadFactory = builder.build(); + Thread thread = threadFactory.newThread(() -> { + System.out.println("创建新线程"); + }); + thread.start(); + Assert.assertEquals("my-thread-factory_0", thread.getName()); + Assert.assertTrue(thread.isDaemon()); + Assert.assertEquals(uncaughtExceptionHandler,thread.getUncaughtExceptionHandler()); + Assert.assertEquals(Thread.MAX_PRIORITY, thread.getPriority()); + } + +} + +class TestUncaughtExceptionHandler implements UncaughtExceptionHandler { + + private volatile boolean exceptionCaught = false; + + @Override + public void uncaughtException(Thread t, Throwable e) { + System.out.println("Exception caught by " + t.getName()); + exceptionCaught = true; + } + + public boolean isExceptionCaught() { + return exceptionCaught; + } +}