|
| 1 | +/* |
| 2 | + * Copyright (C) 2017-2018 Lightbend Inc. <http://www.lightbend.com> |
| 3 | + */ |
| 4 | + |
| 5 | +package akka.management.cluster.bootstrap.internal |
| 6 | + |
| 7 | +import java.util.concurrent.atomic.AtomicReference |
| 8 | + |
| 9 | +import akka.actor.{ ActorRef, ActorSystem, Props } |
| 10 | +import akka.discovery.ServiceDiscovery.{ Resolved, ResolvedTarget } |
| 11 | +import akka.discovery.{ Lookup, MockDiscovery } |
| 12 | +import akka.management.cluster.bootstrap.internal.BootstrapCoordinator.Protocol.InitiateBootstrapping |
| 13 | +import akka.management.cluster.bootstrap.{ ClusterBootstrapSettings, LowestAddressJoinDecider } |
| 14 | +import com.typesafe.config.ConfigFactory |
| 15 | +import org.scalatest.concurrent.Eventually |
| 16 | +import org.scalatest.{ BeforeAndAfterAll, Matchers, WordSpec } |
| 17 | + |
| 18 | +import scala.concurrent.{ Await, Future } |
| 19 | +import scala.concurrent.duration._ |
| 20 | + |
| 21 | +class BootstrapCoordinatorSpec extends WordSpec with Matchers with BeforeAndAfterAll with Eventually { |
| 22 | + val serviceName = "bootstrap-coordinator-test-service" |
| 23 | + val system = ActorSystem("test", ConfigFactory.parseString(s""" |
| 24 | + |akka.management.cluster.bootstrap { |
| 25 | + | contact-point-discovery.service-name = $serviceName |
| 26 | + |} |
| 27 | + """.stripMargin).withFallback(ConfigFactory.load())) |
| 28 | + val settings = ClusterBootstrapSettings(system.settings.config, system.log) |
| 29 | + val joinDecider = new LowestAddressJoinDecider(system, settings) |
| 30 | + |
| 31 | + val discovery = new MockDiscovery(system) |
| 32 | + |
| 33 | + MockDiscovery.set( |
| 34 | + Lookup(serviceName, portName = None, protocol = Some("tcp")), |
| 35 | + () => |
| 36 | + Future.successful(Resolved(serviceName, |
| 37 | + List( |
| 38 | + ResolvedTarget("host1", Some(2552), None), |
| 39 | + ResolvedTarget("host1", Some(8558), None), |
| 40 | + ResolvedTarget("host2", Some(2552), None), |
| 41 | + ResolvedTarget("host2", Some(8558), None) |
| 42 | + ))) |
| 43 | + ) |
| 44 | + |
| 45 | + "The bootstrap coordinator, when avoiding named port lookups" should { |
| 46 | + |
| 47 | + "probe only on the Akka Management port" in { |
| 48 | + val targets = new AtomicReference[List[ResolvedTarget]](Nil) |
| 49 | + val coordinator = system.actorOf(Props(new BootstrapCoordinator(discovery, joinDecider, settings) { |
| 50 | + override def ensureProbing(contactPoint: ResolvedTarget): Option[ActorRef] = { |
| 51 | + println(s"Resolving $contactPoint") |
| 52 | + val targetsSoFar = targets.get |
| 53 | + targets.compareAndSet(targetsSoFar, contactPoint +: targetsSoFar) |
| 54 | + None |
| 55 | + } |
| 56 | + })) |
| 57 | + coordinator ! InitiateBootstrapping |
| 58 | + eventually { |
| 59 | + val targetsToCheck = targets.get |
| 60 | + targetsToCheck.length should be >= (2) |
| 61 | + targetsToCheck.map(_.host) should contain("host1") |
| 62 | + targetsToCheck.map(_.host) should contain("host2") |
| 63 | + targetsToCheck.flatMap(_.port).toSet should be(Set(8558)) |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + override def afterAll(): Unit = { |
| 69 | + Await.result(system.terminate(), 10.seconds) |
| 70 | + super.afterAll() |
| 71 | + } |
| 72 | +} |
0 commit comments