diff --git a/akka-cluster/src/test/scala/akka/cluster/DowningProviderSpec.scala b/akka-cluster/src/test/scala/akka/cluster/DowningProviderSpec.scala index e4dac1d91d..8c559bd82a 100644 --- a/akka-cluster/src/test/scala/akka/cluster/DowningProviderSpec.scala +++ b/akka-cluster/src/test/scala/akka/cluster/DowningProviderSpec.scala @@ -5,13 +5,10 @@ package akka.cluster import java.util.concurrent.atomic.AtomicBoolean - import scala.concurrent.duration._ - import com.typesafe.config.ConfigFactory import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpec - import akka.ConfigurationException import akka.actor.ActorSystem import akka.actor.Props @@ -19,6 +16,8 @@ import akka.testkit.TestKit.awaitCond import akka.testkit.TestKit.shutdownActorSystem import akka.util.unused +import scala.util.control.NonFatal + class FailingDowningProvider(@unused system: ActorSystem) extends DowningProvider { override val downRemovalMargin: FiniteDuration = 20.seconds override def downingActorProps: Option[Props] = { @@ -76,17 +75,29 @@ class DowningProviderSpec extends AnyWordSpec with Matchers { } "stop the cluster if the downing provider throws exception in props method" in { - val system = ActorSystem( - "auto-downing", - ConfigFactory.parseString(""" + // race condition where the downing provider failure can be detected and trigger + // graceful shutdown fast enough that creating the actor system throws on constructing + // thread (or slow enough that we have time to try join the cluster before noticing) + val maybeSystem = try { + Some( + ActorSystem( + "auto-downing", + ConfigFactory.parseString(""" akka.cluster.downing-provider-class="akka.cluster.FailingDowningProvider" - """).withFallback(baseConf)) + """).withFallback(baseConf))) + } catch { + case NonFatal(_) => + // expected to sometimes happen + None + } - val cluster = Cluster(system) - cluster.join(cluster.selfAddress) + maybeSystem.foreach { system => + val cluster = Cluster(system) + cluster.join(cluster.selfAddress) - awaitCond(cluster.isTerminated, 3.seconds) - shutdownActorSystem(system) + awaitCond(cluster.isTerminated, 3.seconds) + shutdownActorSystem(system) + } } }