This is an automated email from the ASF dual-hosted git repository.

He-Pin 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 3b1359aa5a fix: allow overriding testKit in ScalaTestWithActorTestKit 
(#3610)
3b1359aa5a is described below

commit 3b1359aa5a26a372031bcacd03d181a6a8c093fd
Author: PJ Fanning <[email protected]>
AuthorDate: Sat Oct 10 06:26:14 2026 +0100

    fix: allow overriding testKit in ScalaTestWithActorTestKit (#3610)
    
    * fix: allow overriding testKit in ScalaTestWithActorTestKit
    
    Motivation:
    A spec that overrides `testKit` with a `val`, e.g.
    `override val testKit = ActorTestKit()`, fails with a
    NullPointerException. `ScalaTestWithActorTestKitBase.patience` is a
    strict `val` that reads `testKit.testKitSettings` while the trait is
    initialized, before the subclass has assigned its overriding `testKit`.
    
    Modification:
    Make `patience` a `lazy val` so it is computed on first use, after
    construction. Add `ScalaTestWithActorTestKitSpec`.
    
    Result:
    Specs can override `testKit` with a `val`, and `patience` uses the
    overriding testkit's `DefaultTimeout`.
    
    Tests:
    - `actor-testkit-typed/Test/testOnly ...ScalaTestWithActorTestKitSpec`
      without the fix: failed with NullPointerException in
      `ScalaTestWithActorTestKitBase.testKit()`
    - `actor-testkit-typed/Test/testOnly 
org.apache.pekko.actor.testkit.typed.scaladsl.*`
      with the fix: 99 passed on 2.13.18 and 3.3.8
    - `actor-testkit-typed/mimaReportBinaryIssues`: passed on 2.13.18 and
      3.3.8
    - scalafmt run on changed files
    
    References:
    Fixes #1140
    
    * fix: make ScalaTestWithActorTestKitBase.patience a def
    
    Motivation:
    Making `patience` a `lazy val` broke every subclass that overrides it
    with `override implicit val patience`, e.g. `ReplicatedShardingSpec`,
    because a strict `val` cannot override a `lazy val`. User specs doing
    the same would no longer compile.
    
    Modification:
    Make `patience` a `def` instead. It is still evaluated on access rather
    than during trait initialization, and can be overridden by a `val`,
    `lazy val` or `def`.
    
    Result:
    Overriding `testKit` with a `val` works and existing `patience`
    overrides still compile.
    
    Tests:
    - `actor-testkit-typed/testOnly *ScalaTestWithActorTestKitSpec`: passed
    - `Test/compile` for cluster-sharding-typed, stream-tests, coordination,
      remote and actor-tests: passed
    - `actor-testkit-typed/mimaReportBinaryIssues`: passed
    - scalafmt run on changed files
    
    References:
    Fixes #1140
    
    * chore: add MiMa filter for ScalaTestWithActorTestKitBase $init$
    
    Motivation:
    The Check / Binary Compatibility job fails on Scala 3. Making
    `patience` a `def` leaves `ScalaTestWithActorTestKitBase` with no
    fields, so Scala 3 no longer emits the trait's synthetic `$init$`.
    
    Modification:
    Add a 2.0.x MiMa exclude for `ScalaTestWithActorTestKitBase.$init$`.
    
    Result:
    MiMa passes for actor-testkit-typed on Scala 2.13 and 3.
    
    Tests:
    - `++3.3.8 actor-testkit-typed/mimaReportBinaryIssues`: failed without
      the filter with the same error as CI, passed with it
    - `actor-testkit-typed/mimaReportBinaryIssues` (2.13): passed
    
    References:
    Refs #1140
---
 .../2.0.x.backwards.excludes/patience-def.excludes | 19 ++++++++
 .../typed/scaladsl/ScalaTestWithActorTestKit.scala |  5 +-
 .../scaladsl/ScalaTestWithActorTestKitSpec.scala   | 54 ++++++++++++++++++++++
 3 files changed, 77 insertions(+), 1 deletion(-)

diff --git 
a/actor-testkit-typed/src/main/mima-filters/2.0.x.backwards.excludes/patience-def.excludes
 
b/actor-testkit-typed/src/main/mima-filters/2.0.x.backwards.excludes/patience-def.excludes
new file mode 100644
index 0000000000..80bb0639e3
--- /dev/null
+++ 
b/actor-testkit-typed/src/main/mima-filters/2.0.x.backwards.excludes/patience-def.excludes
@@ -0,0 +1,19 @@
+# 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.
+
+# patience became a def so the trait has no fields left; Scala 3 then drops 
the trait's $init$
+ProblemFilters.exclude[DirectMissingMethodProblem]("org.apache.pekko.actor.testkit.typed.scaladsl.ScalaTestWithActorTestKitBase.$init$")
diff --git 
a/actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/scaladsl/ScalaTestWithActorTestKit.scala
 
b/actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/scaladsl/ScalaTestWithActorTestKit.scala
index a922ff7bbe..9da4698598 100644
--- 
a/actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/scaladsl/ScalaTestWithActorTestKit.scala
+++ 
b/actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/scaladsl/ScalaTestWithActorTestKit.scala
@@ -91,8 +91,11 @@ trait ScalaTestWithActorTestKitBase
    * `PatienceConfig` from 
[[pekko.actor.testkit.typed.TestKitSettings#DefaultTimeout]].
    * `DefaultTimeout` is dilated with 
[[pekko.actor.testkit.typed.TestKitSettings#TestTimeFactor]],
    * which means that the patience is also dilated.
+   *
+   * A `def` so that a spec can override `testKit` with a `val` without 
hitting a `NullPointerException`
+   * during initialization, while subclasses can still override it with a 
`val`, `lazy val` or `def`.
    */
-  implicit val patience: PatienceConfig =
+  implicit def patience: PatienceConfig =
     PatienceConfig(testKit.testKitSettings.DefaultTimeout.duration, Span(100, 
org.scalatest.time.Millis))
 
   /**
diff --git 
a/actor-testkit-typed/src/test/scala/org/apache/pekko/actor/testkit/typed/scaladsl/ScalaTestWithActorTestKitSpec.scala
 
b/actor-testkit-typed/src/test/scala/org/apache/pekko/actor/testkit/typed/scaladsl/ScalaTestWithActorTestKitSpec.scala
new file mode 100644
index 0000000000..c38be60f21
--- /dev/null
+++ 
b/actor-testkit-typed/src/test/scala/org/apache/pekko/actor/testkit/typed/scaladsl/ScalaTestWithActorTestKitSpec.scala
@@ -0,0 +1,54 @@
+/*
+ * 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.pekko.actor.testkit.typed.scaladsl
+
+import scala.concurrent.duration._
+
+import org.scalatest.matchers.should.Matchers
+import org.scalatest.wordspec.AnyWordSpec
+import org.scalatest.wordspec.AnyWordSpecLike
+
+import com.typesafe.config.ConfigFactory
+
+object ScalaTestWithActorTestKitSpec {
+  class OverridesTestKit(base: ActorTestKit) extends 
ScalaTestWithActorTestKit(base) with AnyWordSpecLike {
+    override val testKit: ActorTestKit =
+      ActorTestKit(
+        "ScalaTestWithActorTestKitSpec-overridden",
+        ConfigFactory.parseString("pekko.actor.testkit.typed.default-timeout = 
7s"))
+  }
+}
+
+class ScalaTestWithActorTestKitSpec extends AnyWordSpec with Matchers {
+  import ScalaTestWithActorTestKitSpec._
+
+  "ScalaTestWithActorTestKit" should {
+
+    "allow testKit to be overridden with a val" in {
+      val base = ActorTestKit("ScalaTestWithActorTestKitSpec-base")
+      try {
+        val suite = new OverridesTestKit(base)
+        try {
+          suite.patience.timeout.totalNanos shouldBe 
suite.testKit.testKitSettings.DefaultTimeout.duration.toNanos
+          suite.testKit.testKitSettings.DefaultTimeout.duration shouldBe
+          suite.testKit.testKitSettings.dilated(7.seconds)
+        } finally suite.testKit.shutdownTestKit()
+      } finally base.shutdownTestKit()
+    }
+  }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to