From ed16c905da2ec183be9f99d30a8b79a583a4d9f4 Mon Sep 17 00:00:00 2001 From: Kevin Date: Thu, 16 Mar 2023 09:30:25 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Issue=201111-=E6=A0=B8=E5=BF=83=E9=98=BB?= =?UTF-8?q?=E5=A1=9E=E9=98=9F=E5=88=97=E6=9E=9A=E4=B8=BE=20BlockingQueueTy?= =?UTF-8?q?peEnum.createBlockingQueue(String=20blockingQueueName,=20Intege?= =?UTF-8?q?r=20capacity)=E6=8A=9B=E5=87=BANPE=E9=97=AE=E9=A2=98=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=20(#1117)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix the NPE about null Integer convert to int,change temCapacity's type from int to Integer * refactor BlockingQueueTypeEnum --- .../common/executor/support/BlockingQueueTypeEnum.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/executor/support/BlockingQueueTypeEnum.java b/hippo4j-common/src/main/java/cn/hippo4j/common/executor/support/BlockingQueueTypeEnum.java index a1a77720..4f2205b4 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/executor/support/BlockingQueueTypeEnum.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/executor/support/BlockingQueueTypeEnum.java @@ -234,11 +234,11 @@ public enum BlockingQueueTypeEnum { .map(each -> each.generateBlockingQueue()) .findFirst() .orElseGet(() -> { - int temCapacity = capacity; + Integer tempCapacity = capacity; if (capacity == null || capacity <= 0) { - temCapacity = DEFAULT_CAPACITY; + tempCapacity = DEFAULT_CAPACITY; } - return new LinkedBlockingQueue(temCapacity); + return new LinkedBlockingQueue(tempCapacity); }); } From 47ad7f6ad4deab735fd1a1c6bdb05b2a29a5db11 Mon Sep 17 00:00:00 2001 From: Kevin Date: Thu, 16 Mar 2023 10:19:09 +0800 Subject: [PATCH 2/2] test: add BlockingQueueTypeEnumTest (#1119) --- .../support/BlockingQueueTypeEnumTest.java | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 hippo4j-common/src/test/java/cn/hippo4j/common/executor/support/BlockingQueueTypeEnumTest.java diff --git a/hippo4j-common/src/test/java/cn/hippo4j/common/executor/support/BlockingQueueTypeEnumTest.java b/hippo4j-common/src/test/java/cn/hippo4j/common/executor/support/BlockingQueueTypeEnumTest.java new file mode 100644 index 00000000..b823b87e --- /dev/null +++ b/hippo4j-common/src/test/java/cn/hippo4j/common/executor/support/BlockingQueueTypeEnumTest.java @@ -0,0 +1,134 @@ +/* + * 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.executor.support; + +import org.junit.Assert; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.BlockingQueue; +import java.util.stream.Collectors; + +/** + * BlockingQueueTypeEnum test class + */ +public final class BlockingQueueTypeEnumTest { + + // get all blocking queue names + private static final List BLOCKING_QUEUE_NAMES = Arrays.stream(BlockingQueueTypeEnum.values()).map(BlockingQueueTypeEnum::getName).collect(Collectors.toList()); + + @Test + void assertCreateBlockingQueueNormal() { + // check legal param: name and capacity + for (String name : BLOCKING_QUEUE_NAMES) { + BlockingQueue blockingQueueByName = BlockingQueueTypeEnum.createBlockingQueue(name, 10); + Assert.assertNotNull(blockingQueueByName); + } + } + + @Test + void assertCreateBlockingQueueWithIllegalName() { + // check illegal null name + Assert.assertNotNull(BlockingQueueTypeEnum.createBlockingQueue(null, 10)); + // check unexistent name + Assert.assertNotNull(BlockingQueueTypeEnum.createBlockingQueue("ABC", 10)); + } + + @Test + void assertCreateBlockingQueueWithIllegalCapacity() { + // check illegal null capacity + for (String name : BLOCKING_QUEUE_NAMES) { + BlockingQueue blockingQueueWithNullCapacity = BlockingQueueTypeEnum.createBlockingQueue(name, null); + Assert.assertNotNull(blockingQueueWithNullCapacity); + } + // check illegal negatives capacity + final String arrayBlockingQueueName = BlockingQueueTypeEnum.ARRAY_BLOCKING_QUEUE.getName(); + Assert.assertThrows(IllegalArgumentException.class, () -> BlockingQueueTypeEnum.createBlockingQueue(arrayBlockingQueueName, -100)); + + final String linkedBlockingQueueName = BlockingQueueTypeEnum.LINKED_BLOCKING_QUEUE.getName(); + Assert.assertThrows(IllegalArgumentException.class, () -> BlockingQueueTypeEnum.createBlockingQueue(linkedBlockingQueueName, -100)); + + final String linkedBlockingDequeName = BlockingQueueTypeEnum.LINKED_BLOCKING_DEQUE.getName(); + Assert.assertThrows(IllegalArgumentException.class, () -> BlockingQueueTypeEnum.createBlockingQueue(linkedBlockingDequeName, -100)); + + final String synchronousQueueName = BlockingQueueTypeEnum.SYNCHRONOUS_QUEUE.getName(); + Assert.assertNotNull(BlockingQueueTypeEnum.createBlockingQueue(synchronousQueueName, -99)); + + final String linkedTransferQueueName = BlockingQueueTypeEnum.LINKED_TRANSFER_QUEUE.getName(); + Assert.assertNotNull(BlockingQueueTypeEnum.createBlockingQueue(linkedTransferQueueName, -0)); + + final String priorityBlockingQueueName = BlockingQueueTypeEnum.PRIORITY_BLOCKING_QUEUE.getName(); + Assert.assertThrows(IllegalArgumentException.class, () -> BlockingQueueTypeEnum.createBlockingQueue(priorityBlockingQueueName, -100)); + + final String resizableLinkedBlockingQueueName = BlockingQueueTypeEnum.RESIZABLE_LINKED_BLOCKING_QUEUE.getName(); + Assert.assertThrows(IllegalArgumentException.class, () -> BlockingQueueTypeEnum.createBlockingQueue(resizableLinkedBlockingQueueName, -100)); + } + + @Test + void assertCreateBlockingQueueWithIllegalParams() { + // check illegal name and capacity + Assert.assertNotNull(BlockingQueueTypeEnum.createBlockingQueue("HelloWorld", null)); + Assert.assertNotNull(BlockingQueueTypeEnum.createBlockingQueue(null, null)); + } + + @Test + void assertCreateBlockingQueueWithType() { + Assert.assertNotNull(BlockingQueueTypeEnum.createBlockingQueue(1, null)); + Assert.assertNotNull(BlockingQueueTypeEnum.createBlockingQueue(2, null)); + Assert.assertNotNull(BlockingQueueTypeEnum.createBlockingQueue(3, null)); + Assert.assertNotNull(BlockingQueueTypeEnum.createBlockingQueue(4, null)); + Assert.assertNotNull(BlockingQueueTypeEnum.createBlockingQueue(5, null)); + Assert.assertNotNull(BlockingQueueTypeEnum.createBlockingQueue(6, null)); + Assert.assertNotNull(BlockingQueueTypeEnum.createBlockingQueue(9, null)); + Assert.assertNotNull(BlockingQueueTypeEnum.createBlockingQueue(100, null)); + Assert.assertNotNull(BlockingQueueTypeEnum.createBlockingQueue(-1, null)); + Assert.assertNotNull(BlockingQueueTypeEnum.createBlockingQueue(0, null)); + } + + @Test + void assertGetBlockingQueueNameByType() { + // check legal range of type + Assert.assertEquals("ArrayBlockingQueue", BlockingQueueTypeEnum.getBlockingQueueNameByType(1)); + Assert.assertEquals("LinkedBlockingQueue", BlockingQueueTypeEnum.getBlockingQueueNameByType(2)); + Assert.assertEquals("LinkedBlockingDeque", BlockingQueueTypeEnum.getBlockingQueueNameByType(3)); + Assert.assertEquals("SynchronousQueue", BlockingQueueTypeEnum.getBlockingQueueNameByType(4)); + Assert.assertEquals("LinkedTransferQueue", BlockingQueueTypeEnum.getBlockingQueueNameByType(5)); + Assert.assertEquals("PriorityBlockingQueue", BlockingQueueTypeEnum.getBlockingQueueNameByType(6)); + Assert.assertEquals("ResizableCapacityLinkedBlockingQueue", BlockingQueueTypeEnum.getBlockingQueueNameByType(9)); + //check illegal range of type + Assert.assertEquals("", BlockingQueueTypeEnum.getBlockingQueueNameByType(0)); + Assert.assertEquals("", BlockingQueueTypeEnum.getBlockingQueueNameByType(-1)); + Assert.assertEquals("", BlockingQueueTypeEnum.getBlockingQueueNameByType(100)); + } + + @Test + void assertGetBlockingQueueTypeEnumByName() { + // check legal range of name + Assert.assertEquals(BlockingQueueTypeEnum.ARRAY_BLOCKING_QUEUE, BlockingQueueTypeEnum.getBlockingQueueTypeEnumByName("ArrayBlockingQueue")); + Assert.assertEquals(BlockingQueueTypeEnum.LINKED_BLOCKING_QUEUE, BlockingQueueTypeEnum.getBlockingQueueTypeEnumByName("LinkedBlockingQueue")); + Assert.assertEquals(BlockingQueueTypeEnum.LINKED_BLOCKING_DEQUE, BlockingQueueTypeEnum.getBlockingQueueTypeEnumByName("LinkedBlockingDeque")); + Assert.assertEquals(BlockingQueueTypeEnum.SYNCHRONOUS_QUEUE, BlockingQueueTypeEnum.getBlockingQueueTypeEnumByName("SynchronousQueue")); + Assert.assertEquals(BlockingQueueTypeEnum.LINKED_TRANSFER_QUEUE, BlockingQueueTypeEnum.getBlockingQueueTypeEnumByName("LinkedTransferQueue")); + Assert.assertEquals(BlockingQueueTypeEnum.PRIORITY_BLOCKING_QUEUE, BlockingQueueTypeEnum.getBlockingQueueTypeEnumByName("PriorityBlockingQueue")); + Assert.assertEquals(BlockingQueueTypeEnum.RESIZABLE_LINKED_BLOCKING_QUEUE, BlockingQueueTypeEnum.getBlockingQueueTypeEnumByName("ResizableCapacityLinkedBlockingQueue")); + //check illegal range of name + Assert.assertEquals(BlockingQueueTypeEnum.LINKED_BLOCKING_QUEUE, BlockingQueueTypeEnum.getBlockingQueueTypeEnumByName("Hello")); + Assert.assertEquals(BlockingQueueTypeEnum.LINKED_BLOCKING_QUEUE, BlockingQueueTypeEnum.getBlockingQueueTypeEnumByName(null)); + } +} \ No newline at end of file