#28869 - example for setting policies in docs (persistence testkit)
This commit is contained in:
parent
b4677f58d8
commit
b78125267f
3 changed files with 120 additions and 2 deletions
|
|
@ -110,7 +110,7 @@ Scala
|
||||||
: @@snip [TestKitExamples.scala](/akka-docs/src/test/scala/docs/persistence/testkit/TestKitExamples.scala) { #test }
|
: @@snip [TestKitExamples.scala](/akka-docs/src/test/scala/docs/persistence/testkit/TestKitExamples.scala) { #test }
|
||||||
|
|
||||||
Java
|
Java
|
||||||
: @@snip [TestKitExamples.java](/akka-docs/src/test/java/jdocs/persistence/testkit/PersistenceTestKitSampleTest.java) { #test }
|
: @@snip [PersistenceTestKitSampleTest.java](/akka-docs/src/test/java/jdocs/persistence/testkit/PersistenceTestKitSampleTest.java) { #test }
|
||||||
|
|
||||||
You can safely use persistence testkit in combination with main akka testkit.
|
You can safely use persistence testkit in combination with main akka testkit.
|
||||||
|
|
||||||
|
|
@ -134,6 +134,12 @@ Implement the @apidoc[ProcessingPolicy[EventStorage.JournalOperation]] @scala[tr
|
||||||
or @apidoc[ProcessingPolicy[SnapshotStorage.SnapshotOperation]] @scala[trait]@java[interface] for snapshot storage,
|
or @apidoc[ProcessingPolicy[SnapshotStorage.SnapshotOperation]] @scala[trait]@java[interface] for snapshot storage,
|
||||||
and set it with `withPolicy()` method.
|
and set it with `withPolicy()` method.
|
||||||
|
|
||||||
|
Scala
|
||||||
|
: @@snip [TestKitExamples.scala](/akka-docs/src/test/scala/docs/persistence/testkit/TestKitExamples.scala) { #policy-test }
|
||||||
|
|
||||||
|
Java
|
||||||
|
: @@snip [PersistenceTestKitPolicySampleTest.java](/akka-docs/src/test/java/jdocs/persistence/testkit/PersistenceTestKitPolicySampleTest.java) { #policy-test }
|
||||||
|
|
||||||
`tryProcess()` method of the @apidoc[ProcessingPolicy] has two arguments: persistence id and the storage operation.
|
`tryProcess()` method of the @apidoc[ProcessingPolicy] has two arguments: persistence id and the storage operation.
|
||||||
|
|
||||||
Event storage has the following operations:
|
Event storage has the following operations:
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,68 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2020 Lightbend Inc. <https://www.lightbend.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
package jdocs.persistence.testkit;
|
||||||
|
|
||||||
|
import akka.actor.testkit.typed.javadsl.TestKitJunitResource;
|
||||||
|
import akka.actor.typed.ActorRef;
|
||||||
|
import akka.persistence.testkit.JournalOperation;
|
||||||
|
import akka.persistence.testkit.PersistenceTestKitPlugin;
|
||||||
|
import akka.persistence.testkit.ProcessingPolicy;
|
||||||
|
import akka.persistence.testkit.ProcessingResult;
|
||||||
|
import akka.persistence.testkit.ProcessingSuccess;
|
||||||
|
import akka.persistence.testkit.StorageFailure;
|
||||||
|
import akka.persistence.testkit.WriteEvents;
|
||||||
|
import akka.persistence.testkit.javadsl.PersistenceTestKit;
|
||||||
|
import akka.persistence.typed.PersistenceId;
|
||||||
|
import com.typesafe.config.ConfigFactory;
|
||||||
|
import jdocs.AbstractJavaTest;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.ClassRule;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
// #policy-test
|
||||||
|
public class PersistenceTestKitPolicySampleTest extends AbstractJavaTest {
|
||||||
|
|
||||||
|
@ClassRule
|
||||||
|
public static final TestKitJunitResource testKit =
|
||||||
|
new TestKitJunitResource(
|
||||||
|
PersistenceTestKitPlugin.getInstance()
|
||||||
|
.config()
|
||||||
|
.withFallback(ConfigFactory.defaultApplication()));
|
||||||
|
|
||||||
|
PersistenceTestKit persistenceTestKit = PersistenceTestKit.create(testKit.system());
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void beforeEach() {
|
||||||
|
persistenceTestKit.clearAll();
|
||||||
|
persistenceTestKit.returnDefaultPolicy();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
SampleEventStoragePolicy policy = new SampleEventStoragePolicy();
|
||||||
|
persistenceTestKit.withPolicy(policy);
|
||||||
|
|
||||||
|
PersistenceId persistenceId = PersistenceId.ofUniqueId("some-id");
|
||||||
|
ActorRef<YourPersistentBehavior.Cmd> ref =
|
||||||
|
testKit.spawn(YourPersistentBehavior.create(persistenceId));
|
||||||
|
|
||||||
|
YourPersistentBehavior.Cmd cmd = new YourPersistentBehavior.Cmd("data");
|
||||||
|
ref.tell(cmd);
|
||||||
|
|
||||||
|
persistenceTestKit.expectNothingPersisted(persistenceId.id());
|
||||||
|
}
|
||||||
|
|
||||||
|
static class SampleEventStoragePolicy implements ProcessingPolicy<JournalOperation> {
|
||||||
|
@Override
|
||||||
|
public ProcessingResult tryProcess(String persistenceId, JournalOperation processingUnit) {
|
||||||
|
if (processingUnit instanceof WriteEvents) {
|
||||||
|
return StorageFailure.create();
|
||||||
|
} else {
|
||||||
|
return ProcessingSuccess.getInstance();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// #policy-test
|
||||||
|
|
@ -9,7 +9,7 @@ import akka.persistence.typed.scaladsl.Effect
|
||||||
import akka.persistence.typed.scaladsl.EventSourcedBehavior
|
import akka.persistence.typed.scaladsl.EventSourcedBehavior
|
||||||
import akka.serialization.jackson.CborSerializable
|
import akka.serialization.jackson.CborSerializable
|
||||||
import com.typesafe.config.ConfigFactory
|
import com.typesafe.config.ConfigFactory
|
||||||
import docs.persistence.testkit.PersistenceTestKitSampleSpec._
|
import docs.persistence.testkit.PersistenceTestKitSampleSpec.{ Cmd, Evt, _ }
|
||||||
import org.scalatest.BeforeAndAfterEach
|
import org.scalatest.BeforeAndAfterEach
|
||||||
import org.scalatest.wordspec.AnyWordSpecLike
|
import org.scalatest.wordspec.AnyWordSpecLike
|
||||||
|
|
||||||
|
|
@ -117,3 +117,47 @@ class SampleSnapshotStoragePolicy extends SnapshotStorage.SnapshotPolicies.Polic
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//#set-snapshot-storage-policy
|
//#set-snapshot-storage-policy
|
||||||
|
|
||||||
|
//#policy-test
|
||||||
|
class PersistenceTestKitSampleSpecWithPolicy
|
||||||
|
extends ScalaTestWithActorTestKit(PersistenceTestKitPlugin.config.withFallback(ConfigFactory.defaultApplication()))
|
||||||
|
with AnyWordSpecLike
|
||||||
|
with BeforeAndAfterEach {
|
||||||
|
|
||||||
|
val persistenceTestKit = PersistenceTestKit(system)
|
||||||
|
|
||||||
|
override def beforeEach(): Unit = {
|
||||||
|
persistenceTestKit.clearAll()
|
||||||
|
persistenceTestKit.returnDefaultPolicy()
|
||||||
|
}
|
||||||
|
|
||||||
|
"Testkit policy" should {
|
||||||
|
|
||||||
|
"fail all operations with custom exception" in {
|
||||||
|
val policy = new EventStorage.JournalPolicies.PolicyType {
|
||||||
|
|
||||||
|
class CustomFailure extends RuntimeException
|
||||||
|
|
||||||
|
override def tryProcess(persistenceId: String, processingUnit: JournalOperation): ProcessingResult =
|
||||||
|
processingUnit match {
|
||||||
|
case WriteEvents(_) => StorageFailure(new CustomFailure)
|
||||||
|
case _ => ProcessingSuccess
|
||||||
|
}
|
||||||
|
}
|
||||||
|
persistenceTestKit.withPolicy(policy)
|
||||||
|
|
||||||
|
val persistenceId = PersistenceId.ofUniqueId("your-persistence-id")
|
||||||
|
val persistentActor = spawn(
|
||||||
|
EventSourcedBehavior[Cmd, Evt, State](
|
||||||
|
persistenceId,
|
||||||
|
emptyState = State.empty,
|
||||||
|
commandHandler = (_, cmd) => Effect.persist(Evt(cmd.data)),
|
||||||
|
eventHandler = (state, evt) => state.updated(evt)))
|
||||||
|
|
||||||
|
persistentActor ! Cmd("data")
|
||||||
|
persistenceTestKit.expectNothingPersisted(persistenceId.id)
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//#policy-test
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue