2013-10-08 11:46:02 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
|
2013-09-14 14:19:18 +02:00
|
|
|
package akka.persistence
|
|
|
|
|
|
|
|
|
|
import java.io.File
|
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger
|
|
|
|
|
|
2013-09-18 11:55:29 +02:00
|
|
|
import scala.reflect.ClassTag
|
2013-10-27 08:01:14 +01:00
|
|
|
import scala.util.control.NoStackTrace
|
2013-09-18 11:55:29 +02:00
|
|
|
|
2013-10-08 11:46:02 +02:00
|
|
|
import com.typesafe.config.ConfigFactory
|
|
|
|
|
|
2013-09-14 14:19:18 +02:00
|
|
|
import org.apache.commons.io.FileUtils
|
|
|
|
|
import org.scalatest.BeforeAndAfterEach
|
|
|
|
|
|
2013-09-18 11:55:29 +02:00
|
|
|
import akka.actor.Props
|
2013-09-14 14:19:18 +02:00
|
|
|
import akka.testkit.AkkaSpec
|
|
|
|
|
|
2013-11-20 13:47:42 +01:00
|
|
|
trait PersistenceSpec extends BeforeAndAfterEach with Cleanup { this: AkkaSpec ⇒
|
2013-09-14 14:19:18 +02:00
|
|
|
private var _name: String = _
|
|
|
|
|
|
2013-11-20 13:47:42 +01:00
|
|
|
lazy val extension = Persistence(system)
|
2013-09-14 14:19:18 +02:00
|
|
|
val counter = new AtomicInteger(0)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Unique name per test.
|
|
|
|
|
*/
|
|
|
|
|
def name = _name
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Prefix for generating a unique name per test.
|
|
|
|
|
*/
|
2013-12-20 12:40:54 +01:00
|
|
|
def namePrefix: String = system.name
|
2013-09-14 14:19:18 +02:00
|
|
|
|
2013-09-18 11:55:29 +02:00
|
|
|
/**
|
|
|
|
|
* Creates a processor with current name as constructor argument.
|
|
|
|
|
*/
|
|
|
|
|
def namedProcessor[T <: NamedProcessor: ClassTag] =
|
|
|
|
|
system.actorOf(Props(implicitly[ClassTag[T]].runtimeClass, name))
|
2013-09-14 14:19:18 +02:00
|
|
|
|
|
|
|
|
override protected def beforeEach() {
|
2013-12-06 12:48:44 +01:00
|
|
|
_name = s"${namePrefix}-${counter.incrementAndGet()}"
|
2013-09-14 14:19:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-08 11:46:02 +02:00
|
|
|
object PersistenceSpec {
|
2013-11-25 12:02:29 +01:00
|
|
|
def config(plugin: String, test: String, serialization: String = "on") = ConfigFactory.parseString(
|
2013-10-08 11:46:02 +02:00
|
|
|
s"""
|
2013-11-25 12:02:29 +01:00
|
|
|
akka.actor.serialize-creators = ${serialization}
|
|
|
|
|
akka.actor.serialize-messages = ${serialization}
|
2013-11-12 09:02:02 +01:00
|
|
|
akka.persistence.publish-plugin-commands = on
|
2013-10-27 08:01:14 +01:00
|
|
|
akka.persistence.journal.plugin = "akka.persistence.journal.${plugin}"
|
2013-12-20 12:40:54 +01:00
|
|
|
akka.persistence.journal.leveldb.dir = "target/journal-${test}"
|
|
|
|
|
akka.persistence.snapshot-store.local.dir = "target/snapshots-${test}/"
|
2013-10-27 08:01:14 +01:00
|
|
|
""")
|
2013-10-08 11:46:02 +02:00
|
|
|
}
|
|
|
|
|
|
2013-11-20 13:47:42 +01:00
|
|
|
trait Cleanup { this: AkkaSpec ⇒
|
|
|
|
|
val storageLocations = List(
|
|
|
|
|
"akka.persistence.journal.leveldb.dir",
|
2013-11-25 12:02:29 +01:00
|
|
|
"akka.persistence.journal.leveldb-shared.store.dir",
|
2013-11-20 13:47:42 +01:00
|
|
|
"akka.persistence.snapshot-store.local.dir").map(s ⇒ new File(system.settings.config.getString(s)))
|
|
|
|
|
|
|
|
|
|
override protected def atStartup() {
|
|
|
|
|
storageLocations.foreach(FileUtils.deleteDirectory)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override protected def afterTermination() {
|
2013-12-20 12:40:54 +01:00
|
|
|
storageLocations.foreach { dir ⇒
|
|
|
|
|
if (dir.exists && !FileUtils.deleteQuietly(dir)) {
|
|
|
|
|
println(s"Failed to delete [$dir], will try again on exit")
|
|
|
|
|
FileUtils.forceDeleteOnExit(dir)
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-20 13:47:42 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-18 11:55:29 +02:00
|
|
|
abstract class NamedProcessor(name: String) extends Processor {
|
|
|
|
|
override def processorId: String = name
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-14 14:19:18 +02:00
|
|
|
trait TurnOffRecoverOnStart { this: Processor ⇒
|
2013-09-15 09:04:05 +02:00
|
|
|
override def preStart(): Unit = ()
|
2013-09-14 14:19:18 +02:00
|
|
|
}
|
2013-09-26 09:14:43 +02:00
|
|
|
|
2013-10-27 08:01:14 +01:00
|
|
|
class TestException(msg: String) extends Exception(msg) with NoStackTrace
|
|
|
|
|
|
2013-09-26 09:14:43 +02:00
|
|
|
case object GetState
|