2013-10-08 11:46:02 +02:00
|
|
|
/**
|
2014-02-02 19:05:45 -06:00
|
|
|
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
|
2013-10-08 11:46:02 +02:00
|
|
|
*/
|
|
|
|
|
|
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 {
|
2014-05-26 13:55:13 +02:00
|
|
|
def config(plugin: String, test: String, serialization: String = "on", extraConfig: Option[String] = None) =
|
|
|
|
|
extraConfig.map(ConfigFactory.parseString(_)).getOrElse(ConfigFactory.empty()).withFallback(
|
|
|
|
|
ConfigFactory.parseString(
|
|
|
|
|
s"""
|
2013-11-25 12:02:29 +01:00
|
|
|
akka.actor.serialize-creators = ${serialization}
|
|
|
|
|
akka.actor.serialize-messages = ${serialization}
|
2014-01-17 06:58:25 +01:00
|
|
|
akka.persistence.publish-confirmations = on
|
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}/"
|
2014-01-20 09:32:53 +01:00
|
|
|
akka.test.single-expect-default = 10s
|
2014-05-26 13:55:13 +02: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-23 13:05:18 +01:00
|
|
|
storageLocations.foreach(FileUtils.deleteDirectory)
|
2013-11-20 13:47:42 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-18 11:55:29 +02:00
|
|
|
abstract class NamedProcessor(name: String) extends Processor {
|
2014-06-23 14:33:35 +02:00
|
|
|
override def persistenceId: String = name
|
2013-09-18 11:55:29 +02:00
|
|
|
}
|
|
|
|
|
|
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
|