Persistence testkit implementation #15571 (#26825)

This commit is contained in:
Kirill Yankov 2020-03-20 22:18:43 +09:00 committed by GitHub
parent 630e712b9f
commit 41f20cbb81
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
43 changed files with 5726 additions and 3 deletions

View file

@ -0,0 +1,42 @@
/*
* Copyright (C) 2019-2020 Lightbend Inc. <https://www.lightbend.com>
*/
package docs.persistence.testkit
import akka.actor.typed.ActorSystem
import akka.persistence.testkit.{ PersistenceTestKitPlugin, PersistenceTestKitSnapshotPlugin }
import akka.persistence.testkit.scaladsl.{ PersistenceTestKit, SnapshotTestKit }
import com.typesafe.config.ConfigFactory
object TestKitTypedConf {
//#testkit-typed-conf
val yourConfiguration = ConfigFactory.defaultApplication()
val system =
ActorSystem(??? /*some behavior*/, "test-system", PersistenceTestKitPlugin.config.withFallback(yourConfiguration))
val testKit = PersistenceTestKit(system)
//#testkit-typed-conf
}
object SnapshotTypedConf {
//#snapshot-typed-conf
val yourConfiguration = ConfigFactory.defaultApplication()
val system = ActorSystem(
??? /*some behavior*/,
"test-system",
PersistenceTestKitSnapshotPlugin.config.withFallback(yourConfiguration))
val testKit = SnapshotTestKit(system)
//#snapshot-typed-conf
}

View file

@ -0,0 +1,107 @@
/*
* Copyright (C) 2019-2020 Lightbend Inc. <https://www.lightbend.com>
*/
package docs.persistence.testkit
import akka.actor.typed.ActorSystem
import akka.persistence.testkit._
import akka.persistence.testkit.scaladsl.PersistenceTestKit
import akka.persistence.typed.scaladsl.{ Effect, EventSourcedBehavior }
import com.typesafe.config.ConfigFactory
import org.scalatest.BeforeAndAfterAll
import org.scalatest.wordspec.AnyWordSpecLike
class TestKitExamples {
//#testkit-typed-usecase
class TypedSampleSpec extends AnyWordSpecLike with BeforeAndAfterAll {
val system: ActorSystem[Cmd] = ActorSystem(
EventSourcedBehavior[Cmd, Evt, State](
persistenceId = ???,
eventHandler = ???,
commandHandler = (_, cmd) => Effect.persist(Evt(cmd.data)),
emptyState = ???),
"name",
PersistenceTestKitPlugin.config.withFallback(ConfigFactory.defaultApplication()))
val persistenceTestKit = PersistenceTestKit(system)
override def beforeAll(): Unit =
persistenceTestKit.clearAll()
"Persistent actor" should {
"persist all events" in {
val persistentActor = system
val cmd = Cmd("data")
persistentActor ! cmd
val expectedPersistedEvent = Evt(cmd.data)
persistenceTestKit.expectNextPersisted("your-persistence-id", expectedPersistedEvent)
}
}
}
//#testkit-typed-usecase
//#set-event-storage-policy
class SampleEventStoragePolicy extends EventStorage.JournalPolicies.PolicyType {
//you can use internal state, it does not need to be thread safe
var count = 1
override def tryProcess(persistenceId: String, processingUnit: JournalOperation): ProcessingResult =
if (count < 10) {
count += 1
//check the type of operation and react with success or with reject or with failure.
//if you return ProcessingSuccess the operation will be performed, otherwise not.
processingUnit match {
case ReadEvents(batch) if batch.nonEmpty => ProcessingSuccess
case WriteEvents(batch) if batch.size > 1 =>
ProcessingSuccess
case ReadSeqNum => StorageFailure()
case DeleteEvents(_) => Reject()
case _ => StorageFailure()
}
} else {
ProcessingSuccess
}
}
//#set-event-storage-policy
//#set-snapshot-storage-policy
class SampleSnapshotStoragePolicy extends SnapshotStorage.SnapshotPolicies.PolicyType {
//you can use internal state, it does not need to be thread safe
var count = 1
override def tryProcess(persistenceId: String, processingUnit: SnapshotOperation): ProcessingResult =
if (count < 10) {
count += 1
//check the type of operation and react with success or with reject or with failure.
//if you return ProcessingSuccess the operation will be performed, otherwise not.
processingUnit match {
case ReadSnapshot(_, payload) if payload.nonEmpty =>
ProcessingSuccess
case WriteSnapshot(meta, payload) if meta.sequenceNr > 10 =>
ProcessingSuccess
case DeleteSnapshotsByCriteria(_) => StorageFailure()
case DeleteSnapshotByMeta(meta) if meta.sequenceNr < 10 =>
ProcessingSuccess
case _ => StorageFailure()
}
} else {
ProcessingSuccess
}
}
//#set-snapshot-storage-policy
}
case class Cmd(data: String)
case class Evt(data: String)
trait State