From 7692ed83666860ec361e5b932f7bb50a3d1cc26a 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: Fri, 26 May 2023 14:38:08 +0800 Subject: [PATCH] Add test case for ThreadFactoryBuilder (#1341) * Create ThreadFactoryBuilderTest * Update ThreadFactoryBuilderTest Methods are covered by multiple unit test cases . Console printing in English * Update ThreadFactoryBuilderTest Add open source protocol. use @before annotation * Update ThreadFactoryBuilderTest * Update ThreadFactoryBuilderTest * Update ThreadFactoryBuilderTest --- .../common/executor/ThreadFactoryBuilderTest | 81 +++++++++++++++++++ 1 file changed, 81 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..e1344100 --- /dev/null +++ b/infra/common/src/test/java/cn/hippo4j/common/executor/ThreadFactoryBuilderTest @@ -0,0 +1,81 @@ +/* + * 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.common.design.builder; + +import cn.hippo4j.common.design.builder.ThreadFactoryBuilder; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import java.util.concurrent.ThreadFactory; +import java.lang.Thread.UncaughtExceptionHandler; + +public class ThreadFactoryBuilderTest { + + ThreadFactoryBuilder builder; + TestUncaughtExceptionHandler uncaughtExceptionHandler; + Thread thread; + + @Before + public void buildThread() { + builder = ThreadFactoryBuilder.builder(); + uncaughtExceptionHandler = new TestUncaughtExceptionHandler(); + builder.uncaughtExceptionHandler(uncaughtExceptionHandler); + builder.prefix("my-thread-factory"); + builder.daemon(true); + builder.priority(Thread.MAX_PRIORITY); + ThreadFactory threadFactory = builder.build(); + thread = threadFactory.newThread(() -> { + System.out.println("Create a new thread."); + }); + thread.start(); + } + + @Test + public void testName() { + Assert.assertEquals("my-thread-factory_0", thread.getName()); + } + + @Test + public void testIsDaemon() { + Assert.assertTrue(thread.isDaemon()); + } + + @Test + public void testExceptionHandler() { + Assert.assertEquals(uncaughtExceptionHandler, thread.getUncaughtExceptionHandler()); + } + + @Test + public void testPriority() { + 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; + } +}