This is an automated email from the ASF dual-hosted git repository.
fanningpj pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/pekko.git
The following commit(s) were added to refs/heads/main by this push:
new 21ad8968b1 try to fix broken cluster metrics test (#1912)
21ad8968b1 is described below
commit 21ad8968b1985d5e86d2af55842ad32214201d3a
Author: PJ Fanning <[email protected]>
AuthorDate: Mon Jun 23 09:15:17 2025 +0100
try to fix broken cluster metrics test (#1912)
* try to fix broken cluster metrics test
* Update NumberInputStream.scala
* Update nightly-builds.yml
* add tests
* scalafmt
---
.../metrics/protobuf/NumberInputStream.scala | 19 +++---
.../metrics/protobuf/NumberInputStreamSpec.scala | 70 ++++++++++++++++++++++
2 files changed, 82 insertions(+), 7 deletions(-)
diff --git
a/cluster-metrics/src/main/scala/org/apache/pekko/cluster/metrics/protobuf/NumberInputStream.scala
b/cluster-metrics/src/main/scala/org/apache/pekko/cluster/metrics/protobuf/NumberInputStream.scala
index 04ca1c0cc9..9ce5438c38 100644
---
a/cluster-metrics/src/main/scala/org/apache/pekko/cluster/metrics/protobuf/NumberInputStream.scala
+++
b/cluster-metrics/src/main/scala/org/apache/pekko/cluster/metrics/protobuf/NumberInputStream.scala
@@ -17,10 +17,11 @@
package org.apache.pekko.cluster.metrics.protobuf
-import java.io.{ InputStream, ObjectInputStream, ObjectStreamClass }
+import java.io.{ InputStream, ObjectStreamClass }
import org.apache.pekko
import pekko.annotation.InternalApi
+import pekko.util.ClassLoaderObjectInputStream
/**
* A special ObjectInputStream that will only load built-in primitives or
@@ -32,24 +33,28 @@ import pekko.annotation.InternalApi
@InternalApi
private[protobuf] class NumberInputStream(
classLoader: ClassLoader,
- inputStream: InputStream) extends ObjectInputStream(inputStream) {
+ inputStream: InputStream) extends
ClassLoaderObjectInputStream(classLoader, inputStream) {
/**
* Resolve a class specified by the descriptor using the provided classloader
- * and that treats any class that is not a primitive or a subclass of
- * <code>java.lang.Number</code> as not found.
+ * and that treats any class that is not a primitive, an array of primitives
+ * or a subclass of <code>java.lang.Number</code>
+ * or <code>java.math</code> classes as not found.
*
* @param objectStreamClass descriptor of the class
* @return the Class object described by the ObjectStreamClass
* @throws ClassNotFoundException if the Class cannot be found (or is
rejected)
*/
override protected def resolveClass(objectStreamClass: ObjectStreamClass):
Class[_] = {
- val clazz = Class.forName(objectStreamClass.getName(), false, classLoader)
- if (clazz.isPrimitive() || classOf[Number].isAssignableFrom(clazz)) {
+ val clazz = super.resolveClass(objectStreamClass)
+ if (clazz.isPrimitive() || (clazz.isArray() &&
clazz.getComponentType.isPrimitive) ||
+ classOf[Number].isAssignableFrom(clazz) || clazz.getPackage.getName ==
"java.math") {
clazz
} else {
throw new ClassNotFoundException(
- s"Class rejected: ${objectStreamClass.getName()} (only primitive types
and subclasses of java.lang.Number are allowed)")
+ s"Class rejected: ${objectStreamClass.getName()} " +
+ "(only primitive types, arrays of primitive types, subclasses of
java.lang.Number " +
+ "and java.math classes are allowed)")
}
}
diff --git
a/cluster-metrics/src/test/scala/org/apache/pekko/cluster/metrics/protobuf/NumberInputStreamSpec.scala
b/cluster-metrics/src/test/scala/org/apache/pekko/cluster/metrics/protobuf/NumberInputStreamSpec.scala
index 5713433416..07014b01fa 100644
---
a/cluster-metrics/src/test/scala/org/apache/pekko/cluster/metrics/protobuf/NumberInputStreamSpec.scala
+++
b/cluster-metrics/src/test/scala/org/apache/pekko/cluster/metrics/protobuf/NumberInputStreamSpec.scala
@@ -18,6 +18,8 @@
package org.apache.pekko.cluster.metrics.protobuf
import java.io.{ ByteArrayInputStream, ByteArrayOutputStream,
ObjectOutputStream }
+import java.math.BigInteger
+import scala.math.BigInt
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
@@ -67,6 +69,74 @@ class NumberInputStreamSpec extends AnyWordSpec with
Matchers {
result.asInstanceOf[Int] shouldEqual i
}
+ "resolve java BigInteger" in {
+
+ val i = BigInteger.valueOf(Long.MaxValue)
+ val bos = new ByteArrayOutputStream()
+ val oos = new ObjectOutputStream(bos)
+ oos.writeObject(i)
+ oos.close()
+
+ val inputStream = new ByteArrayInputStream(bos.toByteArray)
+ val numberInputStream = new NumberInputStream(classLoader, inputStream)
+
+ val result = numberInputStream.readObject()
+ numberInputStream.close()
+ result shouldBe a[BigInteger]
+ result.asInstanceOf[BigInteger] shouldEqual i
+ }
+
+ "resolve scala BigInt" in {
+
+ val i = BigInt(Long.MaxValue).+(BigInt(1))
+ val bos = new ByteArrayOutputStream()
+ val oos = new ObjectOutputStream(bos)
+ oos.writeObject(i)
+ oos.close()
+
+ val inputStream = new ByteArrayInputStream(bos.toByteArray)
+ val numberInputStream = new NumberInputStream(classLoader, inputStream)
+
+ val result = numberInputStream.readObject()
+ numberInputStream.close()
+ result shouldBe a[BigInt]
+ result.asInstanceOf[BigInt] shouldEqual i
+ }
+
+ "resolve java BigDecimal" in {
+
+ val n = new
java.math.BigDecimal("123456789012345678901234567890.12345678901234567890")
+ val bos = new ByteArrayOutputStream()
+ val oos = new ObjectOutputStream(bos)
+ oos.writeObject(n)
+ oos.close()
+
+ val inputStream = new ByteArrayInputStream(bos.toByteArray)
+ val numberInputStream = new NumberInputStream(classLoader, inputStream)
+
+ val result = numberInputStream.readObject()
+ numberInputStream.close()
+ result shouldBe a[java.math.BigDecimal]
+ result.asInstanceOf[java.math.BigDecimal] shouldEqual n
+ }
+
+ "resolve scala BigDecimal" in {
+
+ val n =
scala.math.BigDecimal("123456789012345678901234567890.12345678901234567890")
+ val bos = new ByteArrayOutputStream()
+ val oos = new ObjectOutputStream(bos)
+ oos.writeObject(n)
+ oos.close()
+
+ val inputStream = new ByteArrayInputStream(bos.toByteArray)
+ val numberInputStream = new NumberInputStream(classLoader, inputStream)
+
+ val result = numberInputStream.readObject()
+ numberInputStream.close()
+ result shouldBe a[scala.math.BigDecimal]
+ result.asInstanceOf[scala.math.BigDecimal] shouldEqual n
+ }
+
"throw ClassNotFoundException for non-Number classes" in {
val bos = new ByteArrayOutputStream()
val oos = new ObjectOutputStream(bos)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]