You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pekko.apache.org by he...@apache.org on 2025/10/26 08:33:41 UTC

(pekko) 01/01: fix: Update statefulMap scaladoc and tests #2385

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

hepin pushed a commit to branch 1.3.x-updateStatefulMap
in repository https://gitbox.apache.org/repos/asf/pekko.git

commit ce23e3a5d239f8905a75df6da38677469c0cb73c
Author: He-Pin <he...@gmail.com>
AuthorDate: Sun Oct 26 16:33:27 2025 +0800

    fix: Update statefulMap scaladoc and tests #2385
---
 .../stream/scaladsl/FlowStatefulMapSpec.scala      | 101 ++++++++++++++++++++-
 .../org/apache/pekko/stream/impl/fusing/Ops.scala  |   6 +-
 .../org/apache/pekko/stream/javadsl/Flow.scala     |   2 +-
 .../org/apache/pekko/stream/javadsl/Source.scala   |   2 +-
 .../org/apache/pekko/stream/javadsl/SubFlow.scala  |   2 +-
 .../apache/pekko/stream/javadsl/SubSource.scala    |   2 +-
 .../org/apache/pekko/stream/scaladsl/Flow.scala    |   2 +-
 7 files changed, 109 insertions(+), 8 deletions(-)

diff --git a/stream-tests/src/test/scala/org/apache/pekko/stream/scaladsl/FlowStatefulMapSpec.scala b/stream-tests/src/test/scala/org/apache/pekko/stream/scaladsl/FlowStatefulMapSpec.scala
index 0403e62fb9..bf31e7d34b 100644
--- a/stream-tests/src/test/scala/org/apache/pekko/stream/scaladsl/FlowStatefulMapSpec.scala
+++ b/stream-tests/src/test/scala/org/apache/pekko/stream/scaladsl/FlowStatefulMapSpec.scala
@@ -337,7 +337,6 @@ class FlowStatefulMapSpec extends StreamSpec {
             Some(buffer)
           })
         .filter(_.nonEmpty)
-        .alsoTo(Sink.foreach(println))
         .runWith(sink)
         .request(4)
         .expectNext(List("A"))
@@ -409,6 +408,27 @@ class FlowStatefulMapSpec extends StreamSpec {
       closedCounter.get() should ===(1)
     }
 
+    "will not call onComplete twice on cancel when `onComplete` fails" in {
+      val closedCounter = new AtomicInteger(0)
+      val (source, sink) = TestSource()
+        .viaMat(Flow[Int].statefulMap(() => 23)((s, elem) => (s, elem),
+          _ => {
+            closedCounter.incrementAndGet()
+            throw TE("boom")
+          }))(Keep.left)
+        .toMat(TestSink[Int]())(Keep.both)
+        .run()
+
+      EventFilter[TE](occurrences = 1).intercept {
+        sink.request(1)
+        source.sendNext(1)
+        sink.expectNext(1)
+        sink.cancel()
+        source.expectCancellation()
+      }
+      closedCounter.get() should ===(1)
+    }
+
     "will not call `onComplete` twice if `onComplete` fail on upstream complete" in {
       val closedCounter = new AtomicInteger(0)
       val (pub, sub) = TestSource[Int]()
@@ -455,4 +475,83 @@ class FlowStatefulMapSpec extends StreamSpec {
       .expectComplete()
   }
 
+  "will not call onComplete twice on cancel when `onComplete` fails" in {
+    val closedCounter = new AtomicInteger(0)
+    val (source, sink) = TestSource()
+      .viaMat(Flow[Int].statefulMap(() => 23)((s, elem) => (s, elem),
+        _ => {
+          closedCounter.incrementAndGet()
+          throw TE("boom")
+        }))(Keep.left)
+      .toMat(TestSink[Int]())(Keep.both)
+      .run()
+
+    EventFilter[TE](occurrences = 1).intercept {
+      sink.request(1)
+      source.sendNext(1)
+      sink.expectNext(1)
+      sink.cancel()
+      source.expectCancellation()
+    }
+    closedCounter.get() should ===(1)
+  }
+
+  "emit onClose return value before restarting" in {
+    val stateCounter = new AtomicInteger(0)
+    val (source, sink) = TestSource[String]()
+      .viaMat(Flow[String].statefulMap(() => stateCounter.incrementAndGet())({ (s, elem) =>
+          if (elem == "boom") throw TE("boom")
+          else (s, elem + s.toString)
+        }, _ => Some("onClose")))(Keep.left)
+      .withAttributes(ActorAttributes.supervisionStrategy(Supervision.restartingDecider))
+      .toMat(TestSink())(Keep.both)
+      .run()
+
+    sink.request(1)
+    source.sendNext("one")
+    sink.expectNext("one1")
+    sink.request(1)
+    source.sendNext("boom")
+    sink.expectNext("onClose")
+    sink.request(1)
+    source.sendNext("two")
+    sink.expectNext("two2")
+    sink.cancel()
+    source.expectCancellation()
+  }
+
+  "allow null state" in {
+    EventFilter[NullPointerException](occurrences = 0).intercept {
+      Source
+        .single("one")
+        .statefulMap(() => null: String)((s, t) => (s, t), _ => None)
+        .runWith(Sink.head)
+        .futureValue shouldBe "one"
+    }
+  }
+
+  "allow null next state" in {
+    EventFilter[NullPointerException](occurrences = 0).intercept {
+      Source
+        .single("one")
+        .statefulMap(() => "state")((_, t) => (null, t), _ => None)
+        .runWith(Sink.head)
+        .futureValue shouldBe "one"
+    }
+  }
+
+  "allow null state on restart" in {
+    val counter = new AtomicInteger(0)
+    EventFilter[NullPointerException](occurrences = 0).intercept {
+      Source
+        .single("one")
+        .statefulMap[String, String](() => if (counter.incrementAndGet() == 1) "state" else null)(
+          (_, _) => throw TE("boom"),
+          _ => None)
+        .withAttributes(ActorAttributes.supervisionStrategy(Supervision.restartingDecider))
+        .runWith(Sink.seq)
+        .futureValue shouldBe Nil
+    }
+
+  }
 }
diff --git a/stream/src/main/scala/org/apache/pekko/stream/impl/fusing/Ops.scala b/stream/src/main/scala/org/apache/pekko/stream/impl/fusing/Ops.scala
index 2245aa0236..bc390db144 100644
--- a/stream/src/main/scala/org/apache/pekko/stream/impl/fusing/Ops.scala
+++ b/stream/src/main/scala/org/apache/pekko/stream/impl/fusing/Ops.scala
@@ -2299,10 +2299,12 @@ private[pekko] final class StatefulMap[S, In, Out](create: () => S, f: (S, In) =
 
       private def resetStateAndPull(): Unit = {
         needInvokeOnCompleteCallback = false
-        onComplete(state)
+        onComplete(state) match {
+          case Some(elem) => push(out, elem)
+          case None       => pull(in)
+        }
         state = create()
         needInvokeOnCompleteCallback = true;
-        pull(in)
       }
 
       private def closeStateAndComplete(): Unit = {
diff --git a/stream/src/main/scala/org/apache/pekko/stream/javadsl/Flow.scala b/stream/src/main/scala/org/apache/pekko/stream/javadsl/Flow.scala
index 2fd24a6e51..49474b642d 100755
--- a/stream/src/main/scala/org/apache/pekko/stream/javadsl/Flow.scala
+++ b/stream/src/main/scala/org/apache/pekko/stream/javadsl/Flow.scala
@@ -821,7 +821,7 @@ final class Flow[In, Out, Mat](delegate: scaladsl.Flow[In, Out, Mat]) extends Gr
    *
    * '''Cancels when''' downstream cancels
    *
-   * @tparam S the type of the state
+   * @tparam S the type of the state, `null` is allowed as a valid state value
    * @tparam T the type of the output elements
    * @param create a function that creates the initial state
    * @param f a function that transforms the upstream element and the state into a pair of next state and output element
diff --git a/stream/src/main/scala/org/apache/pekko/stream/javadsl/Source.scala b/stream/src/main/scala/org/apache/pekko/stream/javadsl/Source.scala
index fd8438601b..d2484f89a0 100755
--- a/stream/src/main/scala/org/apache/pekko/stream/javadsl/Source.scala
+++ b/stream/src/main/scala/org/apache/pekko/stream/javadsl/Source.scala
@@ -2812,7 +2812,7 @@ final class Source[Out, Mat](delegate: scaladsl.Source[Out, Mat]) extends Graph[
    *
    * '''Cancels when''' downstream cancels
    *
-   * @tparam S the type of the state
+   * @tparam S the type of the state, `null` is allowed as a valid state value
    * @tparam T the type of the output elements
    * @param create a function that creates the initial state
    * @param f a function that transforms the upstream element and the state into a pair of next state and output element
diff --git a/stream/src/main/scala/org/apache/pekko/stream/javadsl/SubFlow.scala b/stream/src/main/scala/org/apache/pekko/stream/javadsl/SubFlow.scala
index 5abb8d9abb..c5aa721850 100755
--- a/stream/src/main/scala/org/apache/pekko/stream/javadsl/SubFlow.scala
+++ b/stream/src/main/scala/org/apache/pekko/stream/javadsl/SubFlow.scala
@@ -232,7 +232,7 @@ class SubFlow[In, Out, Mat](
    *
    * '''Cancels when''' downstream cancels
    *
-   * @tparam S the type of the state
+   * @tparam S the type of the state, `null` is allowed as a valid state value
    * @tparam T the type of the output elements
    * @param create a function that creates the initial state
    * @param f a function that transforms the upstream element and the state into a pair of next state and output element
diff --git a/stream/src/main/scala/org/apache/pekko/stream/javadsl/SubSource.scala b/stream/src/main/scala/org/apache/pekko/stream/javadsl/SubSource.scala
index d03184affb..66eb0b2b04 100755
--- a/stream/src/main/scala/org/apache/pekko/stream/javadsl/SubSource.scala
+++ b/stream/src/main/scala/org/apache/pekko/stream/javadsl/SubSource.scala
@@ -223,7 +223,7 @@ class SubSource[Out, Mat](
    *
    * '''Cancels when''' downstream cancels
    *
-   * @tparam S the type of the state
+   * @tparam S the type of the state, `null` is allowed as a valid state value
    * @tparam T the type of the output elements
    * @param create a function that creates the initial state
    * @param f a function that transforms the upstream element and the state into a pair of next state and output element
diff --git a/stream/src/main/scala/org/apache/pekko/stream/scaladsl/Flow.scala b/stream/src/main/scala/org/apache/pekko/stream/scaladsl/Flow.scala
index e3c1f33b51..463ecaf411 100755
--- a/stream/src/main/scala/org/apache/pekko/stream/scaladsl/Flow.scala
+++ b/stream/src/main/scala/org/apache/pekko/stream/scaladsl/Flow.scala
@@ -1224,7 +1224,7 @@ trait FlowOps[+Out, +Mat] {
    *
    * '''Cancels when''' downstream cancels
    *
-   * @tparam S the type of the state
+   * @tparam S the type of the state, `null` is allowed as a valid state value
    * @tparam T the type of the output elements
    * @param create a function that creates the initial state
    * @param f a function that transforms the upstream element and the state into a pair of next state and output element


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