@@ -22,7 +22,6 @@ import lerna.akka.entityreplication.raft.{ ActorSpec, RaftSettings }
2222import lerna .akka .entityreplication .testkit .KryoSerializable
2323
2424import scala .concurrent .Promise
25- import scala .util .Using
2625
2726object ShardSnapshotStoreFailureSpec {
2827 final case object DummyState extends KryoSerializable
@@ -93,34 +92,42 @@ class ShardSnapshotStoreFailureSpec
9392 " ShardSnapshotStore (with time-consuming writes)" should {
9493
9594 // Emulates a time-consuming write
96- class TimeConsumingWriteSnapshotPolicy extends SnapshotStorage .SnapshotPolicies .PolicyType with AutoCloseable {
95+ // Note:
96+ // The promise (`processingResultPromise`) must be fulfilled.
97+ // The succeeding tests will fail unless the promise is fulfilled.
98+ class TimeConsumingWriteSnapshotPolicy extends SnapshotStorage .SnapshotPolicies .PolicyType {
9799 val processingResultPromise = Promise [ProcessingResult ]()
98100 override def tryProcess (persistenceId : String , processingUnit : SnapshotOperation ): ProcessingResult = {
99101 processingUnit match {
100102 case _ : WriteSnapshot => processingResultPromise.future.await
101103 case _ => ProcessingSuccess
102104 }
103105 }
104- override def close (): Unit = {
106+ def trySuccess (): Unit = {
105107 processingResultPromise.trySuccess(ProcessingSuccess )
106108 }
107109 }
108110
109- " reply with `SnapshotNotFound` to `FetchSnapshot` if it has no EntitySnapshot and is saving an EntitySnapshot" ignore {
111+ " reply with `SnapshotNotFound` to `FetchSnapshot` if it has no EntitySnapshot and is saving an EntitySnapshot" in {
110112 // TODO Change SnapshotStore.savingSnapshot such that this test passes.
111113 val entityId = generateUniqueEntityId()
112114 val shardSnapshotStore = createShardSnapshotStore()
113115 val metadata = EntitySnapshotMetadata (entityId, LogEntryIndex (1 ))
114116 val snapshot = EntitySnapshot (metadata, EntityState (DummyState ))
115117
116- Using (new TimeConsumingWriteSnapshotPolicy ()) { timeConsumingWriteSnapshotPolicy =>
118+ val timeConsumingWriteSnapshotPolicy = new TimeConsumingWriteSnapshotPolicy ()
119+ try {
117120 // Prepare: SnapshotStore is saving the snapshot
118121 snapshotTestKit.withPolicy(timeConsumingWriteSnapshotPolicy)
119122 shardSnapshotStore ! SaveSnapshot (snapshot, replyTo = testActor)
120123
121124 // Test:
122125 shardSnapshotStore ! FetchSnapshot (entityId, replyTo = testActor)
123126 expectMsg(SnapshotNotFound )
127+ } finally {
128+ // Cleanup:
129+ // The succeeding tests will fail unless the promise is fulfilled.
130+ timeConsumingWriteSnapshotPolicy.trySuccess()
124131 }
125132 }
126133
@@ -134,7 +141,8 @@ class ShardSnapshotStoreFailureSpec
134141 shardSnapshotStore ! SaveSnapshot (firstSnapshot, replyTo = testActor)
135142 expectMsg(SaveSnapshotSuccess (firstSnapshotMetadata))
136143
137- Using (new TimeConsumingWriteSnapshotPolicy ()) { timeConsumingWriteSnapshotPolicy =>
144+ val timeConsumingWriteSnapshotPolicy = new TimeConsumingWriteSnapshotPolicy ()
145+ try {
138146 // Prepare: SnapshotStore is saving the second snapshot
139147 snapshotTestKit.withPolicy(timeConsumingWriteSnapshotPolicy)
140148 val secondSnapshot =
@@ -144,6 +152,10 @@ class ShardSnapshotStoreFailureSpec
144152 // Test:
145153 shardSnapshotStore ! FetchSnapshot (entityId, replyTo = testActor)
146154 expectMsg(SnapshotFound (firstSnapshot))
155+ } finally {
156+ // Cleanup:
157+ // The succeeding tests will fail unless the promise is fulfilled.
158+ timeConsumingWriteSnapshotPolicy.trySuccess()
147159 }
148160 }
149161
@@ -155,7 +167,8 @@ class ShardSnapshotStoreFailureSpec
155167 val metadata = EntitySnapshotMetadata (entityId, LogEntryIndex (1 ))
156168 val snapshot = EntitySnapshot (metadata, EntityState (DummyState ))
157169
158- Using (new TimeConsumingWriteSnapshotPolicy ()) { timeConsumingWriteSnapshotPolicy =>
170+ val timeConsumingWriteSnapshotPolicy = new TimeConsumingWriteSnapshotPolicy ()
171+ try {
159172 // Prepare: SnapshotStore is saving the snapshot
160173 snapshotTestKit.withPolicy(timeConsumingWriteSnapshotPolicy)
161174 shardSnapshotStore ! SaveSnapshot (snapshot, replyTo = testActor)
@@ -168,6 +181,10 @@ class ShardSnapshotStoreFailureSpec
168181 shardSnapshotStore ! SaveSnapshot (snapshot, replyTo = testActor)
169182 }
170183 expectNoMessage()
184+ } finally {
185+ // Cleanup:
186+ // The succeeding tests will fail unless the promise is fulfilled.
187+ timeConsumingWriteSnapshotPolicy.trySuccess()
171188 }
172189 }
173190
0 commit comments