From cdc0fd3e3dd8897905464db596ba0e2829646e0e Mon Sep 17 00:00:00 2001 From: Filip Date: Fri, 23 Feb 2018 10:50:32 +0100 Subject: [PATCH] Avoid infinite recursion in AbstractPersistentFSM, #24448 --- .../persistence/fsm/PersistentFSMBase.scala | 2 +- .../fsm/AbstractPersistentFSMTest.java | 55 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/akka-persistence/src/main/scala/akka/persistence/fsm/PersistentFSMBase.scala b/akka-persistence/src/main/scala/akka/persistence/fsm/PersistentFSMBase.scala index b19c92dcfa..6cf043dcd1 100644 --- a/akka-persistence/src/main/scala/akka/persistence/fsm/PersistentFSMBase.scala +++ b/akka-persistence/src/main/scala/akka/persistence/fsm/PersistentFSMBase.scala @@ -745,7 +745,7 @@ abstract class AbstractPersistentFSMBase[S, D, E] extends PersistentFSMBase[S, D * called, not only the first one matching. */ final def onTransition(transitionHandler: UnitApply2[S, S]): Unit = - onTransition(transitionHandler) + super.onTransition(transitionHandler(_: S, _: S)) /** * Set handler which is called upon reception of unhandled messages. Calling diff --git a/akka-persistence/src/test/java/akka/persistence/fsm/AbstractPersistentFSMTest.java b/akka-persistence/src/test/java/akka/persistence/fsm/AbstractPersistentFSMTest.java index a0d8ffdf73..e6b99bf812 100644 --- a/akka-persistence/src/test/java/akka/persistence/fsm/AbstractPersistentFSMTest.java +++ b/akka-persistence/src/test/java/akka/persistence/fsm/AbstractPersistentFSMTest.java @@ -24,6 +24,8 @@ import org.junit.Test; import org.scalatest.junit.JUnitSuite; import scala.concurrent.duration.Duration; +import static akka.persistence.fsm.PersistentFSM.FSMState; + import static akka.persistence.fsm.AbstractPersistentFSMTest.WebStoreCustomerFSM.UserState; import static akka.persistence.fsm.AbstractPersistentFSMTest.WebStoreCustomerFSM.ShoppingCart; import static akka.persistence.fsm.AbstractPersistentFSMTest.WebStoreCustomerFSM.Item; @@ -589,4 +591,57 @@ public class AbstractPersistentFSMTest extends JUnitSuite { } //#customer-apply-event } + + enum PFSMState implements FSMState { + + STARTED; + + @Override + public String identifier() { + return this.name(); + } + } + + public static class PFSMwithLog extends AbstractPersistentFSM { + { + startWith(PFSMState.STARTED, 0); + + when(PFSMState.STARTED, + matchEvent(String.class, (command, currentData) -> { + sender().tell("started", getSelf()); + return stay(); + }) + ); + + onTransition(this::transitionLogger); // this is tested command + } + + private void transitionLogger(PFSMState from, PFSMState to) { + System.out.println("transition from " + from + " to " + to); + } + + @Override + public Class domainEventClass() { + return Integer.class; + } + + @Override + public Integer applyEvent(final Integer domainEvent, final Integer currentData) { + return 0; + } + + @Override + public String persistenceId() { + return "id"; + } + } + + @Test + public void testCreationOfActorCallingOnTransitionWithVoidFunction() { + new TestKit(system) {{ + ActorRef persistentActor = system.actorOf(Props.create(PFSMwithLog.class)); + persistentActor.tell("check", getRef()); + expectMsg(duration("1000 millis"), "started"); + }}; + } }