You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@fury.apache.org by "LiangliangSui (via GitHub)" <gi...@apache.org> on 2024/03/01 03:48:32 UTC

[PR] test(java): Add test for ClassLoaderFuryPooled [incubator-fury]

LiangliangSui opened a new pull request, #1392:
URL: https://github.com/apache/incubator-fury/pull/1392

   Add test for `ClassLoaderFuryPooled`.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For additional commands, e-mail: commits-help@fury.apache.org


Re: [PR] test(java): Add test for ClassLoaderFuryPooled [incubator-fury]

Posted by "chaokunyang (via GitHub)" <gi...@apache.org>.
chaokunyang commented on code in PR #1392:
URL: https://github.com/apache/incubator-fury/pull/1392#discussion_r1510156130


##########
java/fury-core/src/test/java/org/apache/fury/pool/ClassLoaderFuryPooledTest.java:
##########
@@ -0,0 +1,131 @@
+/*
+ * 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() {
+    try {
+      Function<ClassLoader, Fury> furyFactory = getFuryFactory();
+      Fury fury = furyFactory.apply(getClass().getClassLoader());
+      ClassLoaderFuryPooled pooled = getPooled(4, 8, furyFactory);
+      pooled.returnFury(fury);
+    } catch (RuntimeException e) {
+      Assert.fail();

Review Comment:
   Seems we can just remove catch block



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For additional commands, e-mail: commits-help@fury.apache.org


Re: [PR] test(java): Add test for ClassLoaderFuryPooled [incubator-fury]

Posted by "chaokunyang (via GitHub)" <gi...@apache.org>.
chaokunyang merged PR #1392:
URL: https://github.com/apache/incubator-fury/pull/1392


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For additional commands, e-mail: commits-help@fury.apache.org


Re: [PR] test(java): Add test for ClassLoaderFuryPooled [incubator-fury]

Posted by "LiangliangSui (via GitHub)" <gi...@apache.org>.
LiangliangSui commented on code in PR #1392:
URL: https://github.com/apache/incubator-fury/pull/1392#discussion_r1510165948


##########
java/fury-core/src/test/java/org/apache/fury/pool/ClassLoaderFuryPooledTest.java:
##########
@@ -0,0 +1,131 @@
+/*
+ * 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() {
+    try {
+      Function<ClassLoader, Fury> furyFactory = getFuryFactory();
+      Fury fury = furyFactory.apply(getClass().getClassLoader());
+      ClassLoaderFuryPooled pooled = getPooled(4, 8, furyFactory);
+      pooled.returnFury(fury);
+    } catch (RuntimeException e) {
+      Assert.fail();

Review Comment:
   Updated



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For additional commands, e-mail: commits-help@fury.apache.org


Re: [PR] test(java): Add test for ClassLoaderFuryPooled [incubator-fury]

Posted by "LiangliangSui (via GitHub)" <gi...@apache.org>.
LiangliangSui commented on PR #1392:
URL: https://github.com/apache/incubator-fury/pull/1392#issuecomment-1974825415

   @chaokunyang ptal, thanks!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For additional commands, e-mail: commits-help@fury.apache.org