You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@fury.apache.org by ch...@apache.org on 2024/03/03 07:02:12 UTC
(incubator-fury) branch main updated: test(java): Add test for ClassLoaderFuryPooled (#1392)
This is an automated email from the ASF dual-hosted git repository.
chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-fury.git
The following commit(s) were added to refs/heads/main by this push:
new ed6fa439 test(java): Add test for ClassLoaderFuryPooled (#1392)
ed6fa439 is described below
commit ed6fa4398fa447e6b78b0b5d24c9459724be3825
Author: LiangliangSui <11...@users.noreply.github.com>
AuthorDate: Sun Mar 3 15:02:08 2024 +0800
test(java): Add test for ClassLoaderFuryPooled (#1392)
Add test for `ClassLoaderFuryPooled`.
Signed-off-by: LiangliangSui <co...@gmail.com>
---
.../apache/fury/pool/ClassLoaderFuryPooled.java | 3 +
.../fury/pool/ClassLoaderFuryPooledTest.java | 127 +++++++++++++++++++++
2 files changed, 130 insertions(+)
diff --git a/java/fury-core/src/main/java/org/apache/fury/pool/ClassLoaderFuryPooled.java b/java/fury-core/src/main/java/org/apache/fury/pool/ClassLoaderFuryPooled.java
index 909ee75c..66430974 100644
--- a/java/fury-core/src/main/java/org/apache/fury/pool/ClassLoaderFuryPooled.java
+++ b/java/fury-core/src/main/java/org/apache/fury/pool/ClassLoaderFuryPooled.java
@@ -19,6 +19,7 @@
package org.apache.fury.pool;
+import java.util.Objects;
import java.util.Queue;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
@@ -67,6 +68,7 @@ public class ClassLoaderFuryPooled {
Function<ClassLoader, Fury> furyFactory,
int minPoolSize,
int maxPoolSize) {
+ Objects.requireNonNull(furyFactory);
this.maxPoolSize = maxPoolSize;
this.furyFactory = furyFactory;
this.classLoader = classLoader;
@@ -99,6 +101,7 @@ public class ClassLoaderFuryPooled {
}
public void returnFury(Fury fury) {
+ Objects.requireNonNull(fury);
try {
lock.lock();
idleCacheQueue.add(fury);
diff --git a/java/fury-core/src/test/java/org/apache/fury/pool/ClassLoaderFuryPooledTest.java b/java/fury-core/src/test/java/org/apache/fury/pool/ClassLoaderFuryPooledTest.java
new file mode 100644
index 00000000..9e148e78
--- /dev/null
+++ b/java/fury-core/src/test/java/org/apache/fury/pool/ClassLoaderFuryPooledTest.java
@@ -0,0 +1,127 @@
+/*
+ * 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.fury.pool;
+
+import java.util.function.Function;
+import org.apache.fury.Fury;
+import org.apache.fury.config.Language;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class ClassLoaderFuryPooledTest {
+
+ private Function<ClassLoader, Fury> getFuryFactory() {
+ return classLoader ->
+ Fury.builder()
+ .withLanguage(Language.JAVA)
+ .withClassLoader(classLoader)
+ .requireClassRegistration(false)
+ .build();
+ }
+
+ private ClassLoaderFuryPooled getPooled(int minPoolSize, int maxPoolSize) {
+ return getPooled(minPoolSize, maxPoolSize, getFuryFactory());
+ }
+
+ private ClassLoaderFuryPooled getPooled(
+ int minPoolSize, int maxPoolSize, Function<ClassLoader, Fury> factory) {
+ return new ClassLoaderFuryPooled(
+ getClass().getClassLoader(), factory, minPoolSize, maxPoolSize);
+ }
+
+ @Test
+ public void testInitialIllegalPool() {
+ Assert.assertThrows(
+ NullPointerException.class,
+ () -> {
+ getPooled(1, 5, null);
+ });
+ }
+
+ @Test
+ public void testGetFuryNormal() {
+ ClassLoaderFuryPooled pooled = getPooled(3, 5);
+ Fury fury = pooled.getFury();
+ Assert.assertNotNull(fury);
+ }
+
+ @Test
+ public void testGetFuryWithIncreaseCapacity() {
+ int minPoolSize = 4;
+ ClassLoaderFuryPooled pooled = getPooled(minPoolSize, 6);
+ for (int i = 0; i < minPoolSize; i++) {
+ Fury fury = pooled.getFury();
+ Assert.assertNotNull(fury);
+ }
+
+ try {
+ pooled.setFactoryCallback(
+ fury -> {
+ throw new RuntimeException();
+ });
+ pooled.getFury();
+ Assert.fail();
+ } catch (RuntimeException e) {
+ // Success
+ }
+ }
+
+ @Test
+ public void testGetFuryAwait() throws InterruptedException {
+ int minPoolSize = 3;
+ ClassLoaderFuryPooled pooled = getPooled(minPoolSize, 3);
+ for (int i = 0; i < minPoolSize; i++) {
+ Fury fury = pooled.getFury();
+ Assert.assertNotNull(fury);
+ }
+
+ Thread thread = new Thread(pooled::getFury);
+ thread.start();
+
+ int timeoutMs = 3000;
+ int loopCount = 10;
+ int waitMs = timeoutMs / loopCount;
+ while (thread.getState() != Thread.State.WAITING && loopCount > 0) {
+ Thread.sleep(waitMs);
+ loopCount--;
+ }
+
+ Assert.assertNotEquals(loopCount, 0);
+
+ // loopCount != 0
+ Fury fury = getFuryFactory().apply(getClass().getClassLoader());
+ pooled.returnFury(fury);
+ thread.join();
+ }
+
+ @Test
+ public void testReturnFury() {
+ Function<ClassLoader, Fury> furyFactory = getFuryFactory();
+ Fury fury = furyFactory.apply(getClass().getClassLoader());
+ ClassLoaderFuryPooled pooled = getPooled(4, 8, furyFactory);
+ pooled.returnFury(fury);
+ }
+
+ @Test
+ public void testReturnFuryForbidden() {
+ ClassLoaderFuryPooled pooled = getPooled(4, 9);
+ Assert.assertThrows(NullPointerException.class, () -> pooled.returnFury(null));
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For additional commands, e-mail: commits-help@fury.apache.org