You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by xi...@apache.org on 2022/05/05 06:06:01 UTC
[incubator-shenyu] branch master updated: [ISSUE #3275]test common: add unit test cases (#3371)
This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new ea475afbd [ISSUE #3275]test common: add unit test cases (#3371)
ea475afbd is described below
commit ea475afbda329420423efcefc1c26555760dfd6f
Author: Zihao Huang <hz...@gmail.com>
AuthorDate: Thu May 5 14:05:57 2022 +0800
[ISSUE #3275]test common: add unit test cases (#3371)
* [ISSUE #3275]test common: add unit test cases for org.apache.shenyu.common.concurrent.
* [ISSUE #3275]test common: add unit test cases for org.apache.shenyu.common.concurrent.
---
.../concurrent/MemoryLimitCalculatorTest.java | 50 ++++++++++
.../MemoryLimitedLinkedBlockingQueueTest.java | 105 ++++++++++++++++++++-
...ueTest.java => MemoryLimitedTaskQueueTest.java} | 40 +++++---
...Test.java => ShenyuThreadPoolExecutorTest.java} | 40 +++++---
4 files changed, 203 insertions(+), 32 deletions(-)
diff --git a/shenyu-common/src/test/java/org/apache/shenyu/common/concurrent/MemoryLimitCalculatorTest.java b/shenyu-common/src/test/java/org/apache/shenyu/common/concurrent/MemoryLimitCalculatorTest.java
new file mode 100644
index 000000000..2dbfe509e
--- /dev/null
+++ b/shenyu-common/src/test/java/org/apache/shenyu/common/concurrent/MemoryLimitCalculatorTest.java
@@ -0,0 +1,50 @@
+/*
+ * 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.shenyu.common.concurrent;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+/**
+ * Test cases for MemoryLimitCalculator.
+ */
+public class MemoryLimitCalculatorTest {
+
+ @Test
+ public void testCalculateWhenIllegalPercentage() {
+ float largerThanOne = 2;
+ float zero = 0;
+ float lessThanZero = -1;
+ assertThrows(IllegalArgumentException.class, () -> MemoryLimitCalculator.calculate(largerThanOne));
+ assertThrows(IllegalArgumentException.class, () -> MemoryLimitCalculator.calculate(zero));
+ assertThrows(IllegalArgumentException.class, () -> MemoryLimitCalculator.calculate(lessThanZero));
+ }
+
+ @Test
+ public void testCalculate() {
+ float percentage = 0.5f;
+ assertEquals((long) (MemoryLimitCalculator.maxAvailable() * percentage), MemoryLimitCalculator.calculate(percentage));
+ }
+
+ @Test
+ public void testDefaultCalculate() {
+ assertEquals((long) (MemoryLimitCalculator.maxAvailable() * 0.8), MemoryLimitCalculator.defaultLimit());
+ }
+}
diff --git a/shenyu-common/src/test/java/org/apache/shenyu/common/concurrent/MemoryLimitedLinkedBlockingQueueTest.java b/shenyu-common/src/test/java/org/apache/shenyu/common/concurrent/MemoryLimitedLinkedBlockingQueueTest.java
index 047b98363..fc604efde 100644
--- a/shenyu-common/src/test/java/org/apache/shenyu/common/concurrent/MemoryLimitedLinkedBlockingQueueTest.java
+++ b/shenyu-common/src/test/java/org/apache/shenyu/common/concurrent/MemoryLimitedLinkedBlockingQueueTest.java
@@ -18,18 +18,38 @@
package org.apache.shenyu.common.concurrent;
import net.bytebuddy.agent.ByteBuddyAgent;
+import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.lang.instrument.Instrumentation;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+/**
+ * Test cases for MemoryLimitedLinkedBlcokingQueue.
+ */
public class MemoryLimitedLinkedBlockingQueueTest {
+
+ private static Instrumentation instrumentation;
+
+ @BeforeAll
+ public static void initialInstrumentation() {
+ ByteBuddyAgent.install();
+ instrumentation = ByteBuddyAgent.getInstrumentation();
+ }
+
@Test
public void test() throws Exception {
- ByteBuddyAgent.install();
- final Instrumentation instrumentation = ByteBuddyAgent.getInstrumentation();
MemoryLimitedLinkedBlockingQueue<Runnable> queue = new MemoryLimitedLinkedBlockingQueue<>(1, instrumentation);
//an Runnable needs more than 1 byte of space, so it will fail here
assertThat(queue.offer(() -> {
@@ -40,4 +60,85 @@ public class MemoryLimitedLinkedBlockingQueueTest {
assertThat(queue.offer(() -> {
}), is(true));
}
+
+ @Test
+ public void testGetMemoryLimit() {
+ MemoryLimitedLinkedBlockingQueue<Runnable> queue = new MemoryLimitedLinkedBlockingQueue<>(instrumentation);
+ assertEquals(queue.getMemoryLimit(), Integer.MAX_VALUE);
+ }
+
+ @Test
+ public void testOfferWhenTimeout() throws InterruptedException {
+ MemoryLimitedLinkedBlockingQueue<Runnable> queue = new MemoryLimitedLinkedBlockingQueue<>(1, instrumentation);
+ assertFalse(queue.offer(() -> {
+ }, 2, TimeUnit.SECONDS));
+ }
+
+ @Test
+ public void testPoll() {
+ MemoryLimitedLinkedBlockingQueue<Integer> queue = new MemoryLimitedLinkedBlockingQueue<>(instrumentation);
+ Integer testObject = 0;
+ queue.offer(testObject);
+ assertEquals(testObject, queue.poll());
+ assertEquals(0, queue.getCurrentMemory());
+ }
+
+ @Test
+ public void testClear() {
+ MemoryLimitedLinkedBlockingQueue<Runnable> queue = new MemoryLimitedLinkedBlockingQueue<>(instrumentation);
+ queue.offer(() -> {
+ });
+ queue.clear();
+ assertEquals(0, queue.getCurrentMemory());
+ }
+
+ @Test
+ public void testPut() throws InterruptedException, ExecutionException {
+ Integer testObject = 0;
+ long testObjectSize = instrumentation.getObjectSize(testObject);
+ MemoryLimitedLinkedBlockingQueue<Integer> queue = new MemoryLimitedLinkedBlockingQueue<>(2 * testObjectSize + 1, instrumentation);
+ queue.put(testObject);
+ queue.put(testObject);
+ assertEquals(2, queue.size());
+ ExecutorService executorService = Executors.newFixedThreadPool(1);
+ Future<Boolean> putResult = executorService.submit(() -> {
+ try {
+ queue.put(testObject);
+ return Boolean.TRUE;
+ } catch (InterruptedException e) {
+ return Boolean.FALSE;
+ }
+ });
+ Thread.sleep(2000);
+ queue.poll();
+ assertTrue(putResult.get());
+ assertEquals(2, queue.size());
+ }
+
+ @Test
+ void testTake() throws InterruptedException, ExecutionException {
+ MemoryLimitedLinkedBlockingQueue<Runnable> queue = new MemoryLimitedLinkedBlockingQueue<>(instrumentation);
+ ExecutorService executorService = Executors.newFixedThreadPool(1);
+ Future<Runnable> takeResult = executorService.submit(queue::take);
+ Thread.sleep(2000);
+ queue.put(() -> { });
+ takeResult.get();
+ assertEquals(0, queue.size());
+ }
+
+ @Test
+ void testPollWhenTimeoutWithNull() throws InterruptedException {
+ MemoryLimitedLinkedBlockingQueue<Runnable> queue = new MemoryLimitedLinkedBlockingQueue<>(instrumentation);
+ Runnable runnable = queue.poll(1, TimeUnit.SECONDS);
+ assertNull(runnable);
+ }
+
+ @Test
+ void testRemoveSuccess() throws InterruptedException {
+ MemoryLimitedLinkedBlockingQueue<Integer> queue = new MemoryLimitedLinkedBlockingQueue<>(instrumentation);
+ Integer testObject = 0;
+ queue.put(testObject);
+ assertTrue(queue.remove(testObject));
+ assertEquals(0, queue.getCurrentMemory());
+ }
}
diff --git a/shenyu-common/src/test/java/org/apache/shenyu/common/concurrent/MemoryLimitedLinkedBlockingQueueTest.java b/shenyu-common/src/test/java/org/apache/shenyu/common/concurrent/MemoryLimitedTaskQueueTest.java
similarity index 53%
copy from shenyu-common/src/test/java/org/apache/shenyu/common/concurrent/MemoryLimitedLinkedBlockingQueueTest.java
copy to shenyu-common/src/test/java/org/apache/shenyu/common/concurrent/MemoryLimitedTaskQueueTest.java
index 047b98363..f195ee879 100644
--- a/shenyu-common/src/test/java/org/apache/shenyu/common/concurrent/MemoryLimitedLinkedBlockingQueueTest.java
+++ b/shenyu-common/src/test/java/org/apache/shenyu/common/concurrent/MemoryLimitedTaskQueueTest.java
@@ -18,26 +18,36 @@
package org.apache.shenyu.common.concurrent;
import net.bytebuddy.agent.ByteBuddyAgent;
+import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.lang.instrument.Instrumentation;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
-public class MemoryLimitedLinkedBlockingQueueTest {
- @Test
- public void test() throws Exception {
+/**
+ * Test cases for MemoryLimitedTaskQueue.
+ */
+public class MemoryLimitedTaskQueueTest {
+
+ private static Instrumentation instrumentation;
+
+ @BeforeAll
+ public static void initializeInstrumentation() {
ByteBuddyAgent.install();
- final Instrumentation instrumentation = ByteBuddyAgent.getInstrumentation();
- MemoryLimitedLinkedBlockingQueue<Runnable> queue = new MemoryLimitedLinkedBlockingQueue<>(1, instrumentation);
- //an Runnable needs more than 1 byte of space, so it will fail here
- assertThat(queue.offer(() -> {
- }), is(false));
-
- //will success
- queue.setMemoryLimit(Integer.MAX_VALUE);
- assertThat(queue.offer(() -> {
- }), is(true));
+ instrumentation = ByteBuddyAgent.getInstrumentation();
+ }
+
+ @Test
+ public void testOffer() {
+ MemoryLimitedTaskQueue memoryLimitedTaskQueue = new MemoryLimitedTaskQueue<>(instrumentation);
+ assertTrue(memoryLimitedTaskQueue.doOffer(() -> { }));
+ }
+
+ @Test
+ public void testOfferWhenMemoryNotSufficient() {
+ MemoryLimitedTaskQueue memoryLimitedTaskQueue = new MemoryLimitedTaskQueue<>(1, instrumentation);
+ assertFalse(memoryLimitedTaskQueue.doOffer(() -> { }));
}
}
diff --git a/shenyu-common/src/test/java/org/apache/shenyu/common/concurrent/MemoryLimitedLinkedBlockingQueueTest.java b/shenyu-common/src/test/java/org/apache/shenyu/common/concurrent/ShenyuThreadPoolExecutorTest.java
similarity index 53%
copy from shenyu-common/src/test/java/org/apache/shenyu/common/concurrent/MemoryLimitedLinkedBlockingQueueTest.java
copy to shenyu-common/src/test/java/org/apache/shenyu/common/concurrent/ShenyuThreadPoolExecutorTest.java
index 047b98363..a8b3e71ab 100644
--- a/shenyu-common/src/test/java/org/apache/shenyu/common/concurrent/MemoryLimitedLinkedBlockingQueueTest.java
+++ b/shenyu-common/src/test/java/org/apache/shenyu/common/concurrent/ShenyuThreadPoolExecutorTest.java
@@ -18,26 +18,36 @@
package org.apache.shenyu.common.concurrent;
import net.bytebuddy.agent.ByteBuddyAgent;
+import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.lang.instrument.Instrumentation;
+import java.util.concurrent.TimeUnit;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertThrows;
-public class MemoryLimitedLinkedBlockingQueueTest {
- @Test
- public void test() throws Exception {
+/**
+ * Test cases for ShenyuThreadPoolExecutor.
+ */
+public class ShenyuThreadPoolExecutorTest {
+
+ private static Instrumentation instrumentation;
+
+ @BeforeAll
+ public static void initialExecutor() {
ByteBuddyAgent.install();
- final Instrumentation instrumentation = ByteBuddyAgent.getInstrumentation();
- MemoryLimitedLinkedBlockingQueue<Runnable> queue = new MemoryLimitedLinkedBlockingQueue<>(1, instrumentation);
- //an Runnable needs more than 1 byte of space, so it will fail here
- assertThat(queue.offer(() -> {
- }), is(false));
-
- //will success
- queue.setMemoryLimit(Integer.MAX_VALUE);
- assertThat(queue.offer(() -> {
- }), is(true));
+ instrumentation = ByteBuddyAgent.getInstrumentation();
+ }
+
+ private ShenyuThreadPoolExecutor getTestExecutor(final TaskQueue<Runnable> taskQueue) {
+ return new ShenyuThreadPoolExecutor(5, 10, 100, TimeUnit.SECONDS, taskQueue, ShenyuThreadFactory.create("Test", false), (r, e) -> {
+ });
}
+
+ @Test
+ public void testNullCommand() {
+ ShenyuThreadPoolExecutor executor = getTestExecutor(new MemoryLimitedTaskQueue<>(instrumentation));
+ assertThrows(NullPointerException.class, () -> executor.execute(null));
+ }
+
}