Skip to content

Commit 86a8db4

Browse files
author
Taichi Yamakawa
committed
Test SnapshotStore saves the given EntitySnapshot
1 parent a210606 commit 86a8db4

File tree

3 files changed

+56
-32
lines changed

3 files changed

+56
-32
lines changed

src/test/scala/lerna/akka/entityreplication/raft/snapshot/ShardSnapshotStoreFailureSpec.scala

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package lerna.akka.entityreplication.raft.snapshot
33
import java.util.concurrent.atomic.AtomicInteger
44
import akka.actor.{ ActorRef, ActorSystem }
55
import akka.persistence.testkit.scaladsl.SnapshotTestKit
6-
import akka.persistence.testkit.{ PersistenceTestKitPlugin, PersistenceTestKitSnapshotPlugin }
76
import akka.testkit.TestKit
8-
import com.typesafe.config.{ Config, ConfigFactory }
97
import lerna.akka.entityreplication.model.{ NormalizedEntityId, TypeName }
108
import lerna.akka.entityreplication.raft.model.LogEntryIndex
119
import lerna.akka.entityreplication.raft.routing.MemberIndex
@@ -17,29 +15,11 @@ import lerna.akka.entityreplication.testkit.KryoSerializable
1715
object ShardSnapshotStoreFailureSpec {
1816
final case object DummyState extends KryoSerializable
1917

20-
def configWithPersistenceTestKits: Config = {
21-
PersistenceTestKitPlugin.config
22-
.withFallback(PersistenceTestKitSnapshotPlugin.config)
23-
.withFallback(raftPersistenceConfigWithPersistenceTestKits)
24-
.withFallback(ConfigFactory.load())
25-
}
26-
27-
private val raftPersistenceConfigWithPersistenceTestKits: Config = ConfigFactory.parseString(
28-
s"""
29-
|lerna.akka.entityreplication.raft.persistence {
30-
| journal.plugin = ${PersistenceTestKitPlugin.PluginId}
31-
| snapshot-store.plugin = ${PersistenceTestKitSnapshotPlugin.PluginId}
32-
| # Might be possible to use PersistenceTestKitReadJournal
33-
| // query.plugin = ""
34-
|}
35-
|""".stripMargin,
36-
)
37-
3818
}
3919

4020
class ShardSnapshotStoreFailureSpec
4121
extends TestKit(
42-
ActorSystem("ShardSnapshotStoreFailureSpec", ShardSnapshotStoreFailureSpec.configWithPersistenceTestKits),
22+
ActorSystem("ShardSnapshotStoreFailureSpec", ShardSnapshotStoreSpecBase.configWithPersistenceTestKits),
4323
)
4424
with ActorSpec {
4525

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package lerna.akka.entityreplication.raft.snapshot
2+
3+
import akka.persistence.testkit.{ PersistenceTestKitPlugin, PersistenceTestKitSnapshotPlugin }
4+
import com.typesafe.config.{ Config, ConfigFactory }
5+
6+
object ShardSnapshotStoreSpecBase {
7+
8+
def configWithPersistenceTestKits: Config = {
9+
PersistenceTestKitPlugin.config
10+
.withFallback(PersistenceTestKitSnapshotPlugin.config)
11+
.withFallback(raftPersistenceConfigWithPersistenceTestKits)
12+
.withFallback(ConfigFactory.load())
13+
}
14+
15+
private val raftPersistenceConfigWithPersistenceTestKits: Config = ConfigFactory.parseString(
16+
s"""
17+
|lerna.akka.entityreplication.raft.persistence {
18+
| journal.plugin = ${PersistenceTestKitPlugin.PluginId}
19+
| snapshot-store.plugin = ${PersistenceTestKitSnapshotPlugin.PluginId}
20+
| # Might be possible to use PersistenceTestKitReadJournal
21+
| // query.plugin = ""
22+
|}
23+
|""".stripMargin,
24+
)
25+
26+
}

src/test/scala/lerna/akka/entityreplication/raft/snapshot/ShardSnapshotStoreSuccessSpec.scala

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package lerna.akka.entityreplication.raft.snapshot
22

33
import java.util.concurrent.atomic.AtomicInteger
44
import akka.actor.{ ActorRef, ActorSystem, PoisonPill }
5+
import akka.persistence.testkit.scaladsl.SnapshotTestKit
56
import akka.testkit.TestKit
67
import com.typesafe.config.{ Config, ConfigFactory }
78
import lerna.akka.entityreplication.model.{ NormalizedEntityId, TypeName }
@@ -14,31 +15,48 @@ object ShardSnapshotStoreSuccessSpec {
1415
final case object DummyState extends KryoSerializable
1516
}
1617

17-
class ShardSnapshotStoreSuccessSpec extends TestKit(ActorSystem()) with ActorSpec {
18+
class ShardSnapshotStoreSuccessSpec
19+
extends TestKit(
20+
ActorSystem("ShardSnapshotStoreSuccessSpec", ShardSnapshotStoreSpecBase.configWithPersistenceTestKits),
21+
)
22+
with ActorSpec {
1823
import ShardSnapshotStoreSuccessSpec._
1924
import lerna.akka.entityreplication.raft.snapshot.SnapshotProtocol._
2025

26+
private val snapshotTestKit = SnapshotTestKit(system)
27+
private val typeName = TypeName.from("test")
28+
private val memberIndex = MemberIndex("test-role")
2129
private[this] val dummyEntityState = EntityState(DummyState)
2230

31+
override def beforeEach(): Unit = {
32+
super.beforeEach()
33+
snapshotTestKit.clearAll()
34+
snapshotTestKit.resetPolicy()
35+
}
36+
2337
"ShardSnapshotStore(正常系)" should {
2438

2539
"SaveSnapshot に成功した場合は SaveSnapshotSuccess が返信される" in {
26-
val entityId = generateUniqueEntityId()
27-
val shardSnapshotStore = createShardSnapshotStore()
28-
val metadata = EntitySnapshotMetadata(entityId, LogEntryIndex.initial())
29-
val snapshot = EntitySnapshot(metadata, dummyEntityState)
40+
val entityId = generateUniqueEntityId()
41+
val shardSnapshotStore = createShardSnapshotStore()
42+
val snapshotStorePersistenceId = SnapshotStore.persistenceId(typeName, entityId, memberIndex)
43+
val metadata = EntitySnapshotMetadata(entityId, LogEntryIndex.initial())
44+
val snapshot = EntitySnapshot(metadata, dummyEntityState)
3045

3146
shardSnapshotStore ! SaveSnapshot(snapshot, replyTo = testActor)
47+
snapshotTestKit.expectNextPersisted(snapshotStorePersistenceId, snapshot)
3248
expectMsg(SaveSnapshotSuccess(metadata))
3349
}
3450

3551
"FetchSnapshot に成功した場合は一度停止しても SnapshotFound でスナップショットが返信される" in {
36-
val entityId = generateUniqueEntityId()
37-
val shardSnapshotStore = createShardSnapshotStore()
38-
val metadata = EntitySnapshotMetadata(entityId, LogEntryIndex.initial())
39-
val snapshot = EntitySnapshot(metadata, dummyEntityState)
52+
val entityId = generateUniqueEntityId()
53+
val shardSnapshotStore = createShardSnapshotStore()
54+
val snapshotStorePersistenceId = SnapshotStore.persistenceId(typeName, entityId, memberIndex)
55+
val metadata = EntitySnapshotMetadata(entityId, LogEntryIndex.initial())
56+
val snapshot = EntitySnapshot(metadata, dummyEntityState)
4057

4158
shardSnapshotStore ! SaveSnapshot(snapshot, replyTo = testActor)
59+
snapshotTestKit.expectNextPersisted(snapshotStorePersistenceId, snapshot)
4260
expectMsg(SaveSnapshotSuccess(metadata))
4361

4462
// terminate SnapshotStore
@@ -84,9 +102,9 @@ class ShardSnapshotStoreSuccessSpec extends TestKit(ActorSystem()) with ActorSpe
84102
planAutoKill {
85103
childActorOf(
86104
ShardSnapshotStore.props(
87-
TypeName.from("test"),
105+
typeName,
88106
RaftSettings(additionalConfig.withFallback(system.settings.config)),
89-
MemberIndex("test-role"),
107+
memberIndex,
90108
),
91109
)
92110
}

0 commit comments

Comments
 (0)