-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
null as empty state, in javadsl, akka#25768
- Loading branch information
Showing
9 changed files
with
568 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
akka-persistence-typed/src/test/java/akka/persistence/typed/javadsl/NullEmptyStateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* Copyright (C) 2018 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package akka.persistence.typed.javadsl; | ||
|
||
import akka.actor.testkit.typed.javadsl.TestKitJunitResource; | ||
import akka.actor.testkit.typed.javadsl.TestProbe; | ||
import akka.actor.typed.ActorRef; | ||
import akka.actor.typed.Behavior; | ||
import akka.actor.typed.javadsl.Behaviors; | ||
import akka.persistence.typed.PersistenceId; | ||
import com.typesafe.config.Config; | ||
import com.typesafe.config.ConfigFactory; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.scalatest.junit.JUnitSuite; | ||
|
||
import java.util.Objects; | ||
|
||
public class NullEmptyStateTest extends JUnitSuite { | ||
|
||
private static final Config config = ConfigFactory.parseString( | ||
"akka.persistence.journal.plugin = \"akka.persistence.journal.inmem\" \n"); | ||
|
||
@ClassRule | ||
public static final TestKitJunitResource testKit = new TestKitJunitResource(config); | ||
|
||
static class NullEmptyState extends PersistentBehavior<String, String, String> { | ||
|
||
private final ActorRef<String> probe; | ||
|
||
NullEmptyState(PersistenceId persistenceId, ActorRef<String> probe) { | ||
super(persistenceId); | ||
this.probe = probe; | ||
} | ||
|
||
@Override | ||
public String emptyState() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void onRecoveryCompleted(String s) { | ||
probe.tell("onRecoveryCompleted:" + s); | ||
} | ||
|
||
@Override | ||
public CommandHandler<String, String, String> commandHandler() { | ||
CommandHandlerBuilder<String, String, String, String> b1 = | ||
commandHandlerBuilder(Objects::isNull) | ||
.matchCommand("stop"::equals, command -> Effect().stop()) | ||
.matchCommand(String.class, this::persistCommand); | ||
|
||
CommandHandlerBuilder<String, String, String, String> b2 = | ||
commandHandlerBuilder(String.class) | ||
.matchCommand("stop"::equals, command -> Effect().stop()) | ||
.matchCommand(String.class, this::persistCommand); | ||
|
||
return b1.orElse(b2).build(); | ||
} | ||
|
||
private Effect<String, String> persistCommand(String command) { | ||
return Effect().persist(command); | ||
} | ||
|
||
@Override | ||
public EventHandler<String, String> eventHandler() { | ||
return eventHandlerBuilder() | ||
.matchEvent(String.class, this::applyEvent) | ||
.build(); | ||
} | ||
|
||
private String applyEvent(String state, String event) { | ||
probe.tell("eventHandler:" + state + ":" + event); | ||
if (state == null) | ||
return event; | ||
else | ||
return state + event; | ||
} | ||
} | ||
|
||
@Test | ||
public void handleNullState() throws Exception { | ||
TestProbe<String> probe = testKit.createTestProbe(); | ||
Behavior<String> b = Behaviors.setup(ctx -> new NullEmptyState(new PersistenceId("a"), probe.ref())); | ||
|
||
ActorRef<String> ref1 = testKit.spawn(b); | ||
probe.expectMessage("onRecoveryCompleted:null"); | ||
ref1.tell("stop"); | ||
|
||
ActorRef<String> ref2 = testKit.spawn(b); | ||
probe.expectMessage("onRecoveryCompleted:null"); | ||
ref2.tell("one"); | ||
probe.expectMessage("eventHandler:null:one"); | ||
ref2.tell("two"); | ||
probe.expectMessage("eventHandler:one:two"); | ||
|
||
ref2.tell("stop"); | ||
ActorRef<String> ref3 = testKit.spawn(b); | ||
// eventHandler from reply | ||
probe.expectMessage("eventHandler:null:one"); | ||
probe.expectMessage("eventHandler:one:two"); | ||
probe.expectMessage("onRecoveryCompleted:onetwo"); | ||
ref3.tell("three"); | ||
probe.expectMessage("eventHandler:onetwo:three"); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
akka-persistence-typed/src/test/java/akka/persistence/typed/javadsl/PrimitiveStateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* Copyright (C) 2018 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package akka.persistence.typed.javadsl; | ||
|
||
import akka.actor.testkit.typed.javadsl.TestKitJunitResource; | ||
import akka.actor.testkit.typed.javadsl.TestProbe; | ||
import akka.actor.typed.ActorRef; | ||
import akka.actor.typed.Behavior; | ||
import akka.actor.typed.javadsl.Behaviors; | ||
import akka.persistence.typed.PersistenceId; | ||
import com.typesafe.config.Config; | ||
import com.typesafe.config.ConfigFactory; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.scalatest.junit.JUnitSuite; | ||
|
||
public class PrimitiveStateTest extends JUnitSuite { | ||
|
||
private static final Config config = ConfigFactory.parseString( | ||
"akka.persistence.journal.plugin = \"akka.persistence.journal.inmem\" \n"); | ||
|
||
@ClassRule | ||
public static final TestKitJunitResource testKit = new TestKitJunitResource(config); | ||
|
||
static class PrimitiveState extends PersistentBehavior<Integer, Integer, Integer> { | ||
|
||
private final ActorRef<String> probe; | ||
|
||
PrimitiveState(PersistenceId persistenceId, ActorRef<String> probe) { | ||
super(persistenceId); | ||
this.probe = probe; | ||
} | ||
|
||
@Override | ||
public Integer emptyState() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public void onRecoveryCompleted(Integer n) { | ||
probe.tell("onRecoveryCompleted:" + n); | ||
} | ||
|
||
@Override | ||
public CommandHandler<Integer, Integer, Integer> commandHandler() { | ||
return (state, command) -> { | ||
if (command < 0) | ||
return Effect().stop(); | ||
else | ||
return Effect().persist(command); | ||
}; | ||
} | ||
|
||
@Override | ||
public EventHandler<Integer, Integer> eventHandler() { | ||
return (state, event) -> { | ||
probe.tell("eventHandler:" + state + ":" + event); | ||
return state + event; | ||
}; | ||
} | ||
} | ||
|
||
@Test | ||
public void handleIntegerState() throws Exception { | ||
TestProbe<String> probe = testKit.createTestProbe(); | ||
Behavior<Integer> b = Behaviors.setup(ctx -> new PrimitiveState(new PersistenceId("a"), probe.ref())); | ||
ActorRef<Integer> ref1 = testKit.spawn(b); | ||
probe.expectMessage("onRecoveryCompleted:0"); | ||
ref1.tell(1); | ||
probe.expectMessage("eventHandler:0:1"); | ||
ref1.tell(2); | ||
probe.expectMessage("eventHandler:1:2"); | ||
|
||
ref1.tell(-1); | ||
ActorRef<Integer> ref2 = testKit.spawn(b); | ||
// eventHandler from reply | ||
probe.expectMessage("eventHandler:0:1"); | ||
probe.expectMessage("eventHandler:1:2"); | ||
probe.expectMessage("onRecoveryCompleted:3"); | ||
ref2.tell(3); | ||
probe.expectMessage("eventHandler:3:3"); | ||
} | ||
} |
Oops, something went wrong.