From eec1b7ce73338d60e91a7fb86c0e1b2aae71d594 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjo=CC=88rn=20Antonsson?= Date: Wed, 28 May 2014 11:54:28 +0200 Subject: [PATCH] =per #15131 Make LocalSnapshotStore fail if it can't create target directory --- .../snapshot/local/LocalSnapshotStore.scala | 6 ++- .../SnapshotDirectoryFailureSpec.scala | 53 +++++++++++++++++++ .../SnapshotSerializationSpec.scala | 1 - 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 akka-persistence/src/test/scala/akka/persistence/SnapshotDirectoryFailureSpec.scala diff --git a/akka-persistence/src/main/scala/akka/persistence/snapshot/local/LocalSnapshotStore.scala b/akka-persistence/src/main/scala/akka/persistence/snapshot/local/LocalSnapshotStore.scala index 369fc856ac..a23c454607 100644 --- a/akka-persistence/src/main/scala/akka/persistence/snapshot/local/LocalSnapshotStore.scala +++ b/akka-persistence/src/main/scala/akka/persistence/snapshot/local/LocalSnapshotStore.scala @@ -105,7 +105,11 @@ private[persistence] class LocalSnapshotStore extends SnapshotStore with ActorLo }.filter(md ⇒ criteria.matches(md) && !saving.contains(md)).toVector override def preStart() { - if (!snapshotDir.exists) snapshotDir.mkdirs() + if (!snapshotDir.isDirectory) { + if (!snapshotDir.mkdirs()) { + throw new IOException(s"Failed to create snapshot directory [${snapshotDir.getCanonicalPath}]") + } + } super.preStart() } diff --git a/akka-persistence/src/test/scala/akka/persistence/SnapshotDirectoryFailureSpec.scala b/akka-persistence/src/test/scala/akka/persistence/SnapshotDirectoryFailureSpec.scala new file mode 100644 index 0000000000..de60fc8dfa --- /dev/null +++ b/akka-persistence/src/test/scala/akka/persistence/SnapshotDirectoryFailureSpec.scala @@ -0,0 +1,53 @@ +/** + * Copyright (C) 2009-2014 Typesafe Inc. + */ + +package akka.persistence + +import akka.testkit.{ ImplicitSender, EventFilter, TestEvent, AkkaSpec } +import java.io.{ IOException, File } +import akka.actor.{ ActorInitializationException, Props, ActorRef } + +object SnapshotDirectoryFailureSpec { + val inUseSnapshotPath = "target/inUseSnapshotPath" + + class TestProcessor(name: String, probe: ActorRef) extends Processor { + + override def processorId: String = name + + override def preStart(): Unit = () + + def receive = { + case s: String ⇒ saveSnapshot(s) + case SaveSnapshotSuccess(md) ⇒ probe ! md.sequenceNr + case other ⇒ probe ! other + } + } +} + +class SnapshotDirectoryFailureSpec extends AkkaSpec(PersistenceSpec.config("leveldb", "SnapshotDirectoryFailureSpec", extraConfig = Some( + s""" + |akka.persistence.snapshot-store.local.dir = "${SnapshotDirectoryFailureSpec.inUseSnapshotPath}" + """.stripMargin))) with ImplicitSender { + + import SnapshotDirectoryFailureSpec._ + + val file = new File(inUseSnapshotPath) + + override protected def atStartup() { + if (!file.createNewFile()) throw new IOException(s"Failed to create test file [${file.getCanonicalFile}]") + } + + override protected def afterTermination() { + if (!file.delete()) throw new IOException(s"Failed to delete test file [${file.getCanonicalFile}]") + } + + "A local snapshot store configured with an failing directory name " must { + "throw an exception at startup" in { + EventFilter[ActorInitializationException](occurrences = 1).intercept { + val processor = system.actorOf(Props(classOf[TestProcessor], "SnapshotDirectoryFailureSpec-1", testActor)) + processor ! "blahonga" + } + } + } +} diff --git a/akka-persistence/src/test/scala/akka/persistence/SnapshotSerializationSpec.scala b/akka-persistence/src/test/scala/akka/persistence/SnapshotSerializationSpec.scala index 7475f096e0..c8daa98c6a 100644 --- a/akka-persistence/src/test/scala/akka/persistence/SnapshotSerializationSpec.scala +++ b/akka-persistence/src/test/scala/akka/persistence/SnapshotSerializationSpec.scala @@ -60,7 +60,6 @@ object SnapshotSerializationSpec { class SnapshotSerializationSpec extends AkkaSpec(PersistenceSpec.config("leveldb", "SnapshotSerializationSpec", serialization = "off", extraConfig = Some( """ - |akka.logelevel = debug |akka.actor { | serializers { | my-snapshot = "akka.persistence.SnapshotSerializationSpec$MySerializer"