|
| 1 | +/* |
| 2 | + * Copyright (C) 2017 Lightbend Inc. <http://www.lightbend.com> |
| 3 | + */ |
| 4 | +package akka.discovery.aggregate |
| 5 | + |
| 6 | +import akka.actor.{ ActorSystem, ExtendedActorSystem } |
| 7 | +import akka.discovery.SimpleServiceDiscovery.{ Resolved, ResolvedTarget } |
| 8 | +import akka.discovery.{ ServiceDiscovery, SimpleServiceDiscovery } |
| 9 | +import akka.event.Logging |
| 10 | +import akka.testkit.TestKit |
| 11 | +import com.typesafe.config.{ Config, ConfigFactory } |
| 12 | +import org.scalatest.concurrent.ScalaFutures |
| 13 | +import org.scalatest.{ BeforeAndAfterAll, Matchers, WordSpecLike } |
| 14 | + |
| 15 | +import scala.concurrent.Future |
| 16 | +import scala.concurrent.duration._ |
| 17 | +import scala.collection.immutable |
| 18 | + |
| 19 | +class StubbedSimpleServiceDiscovery(system: ExtendedActorSystem) extends SimpleServiceDiscovery { |
| 20 | + |
| 21 | + override def lookup(name: String, resolveTimeout: FiniteDuration): Future[SimpleServiceDiscovery.Resolved] = { |
| 22 | + if (name == "stubbed") { |
| 23 | + Future.successful(Resolved(name, immutable.Seq(ResolvedTarget("stubbed1", Some(1234))))) |
| 24 | + } else if (name == "fail") { |
| 25 | + Future.failed(new RuntimeException("No resolving for you!")) |
| 26 | + } else { |
| 27 | + Future.successful(Resolved(name, immutable.Seq.empty)) |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +object AggregateSimpleServiceDiscoverySpec { |
| 33 | + val config: Config = ConfigFactory.parseString(""" |
| 34 | + akka { |
| 35 | + loglevel = DEBUG |
| 36 | + discovery { |
| 37 | + method = aggregate |
| 38 | +
|
| 39 | + aggregate { |
| 40 | + discovery-mechanisms = ["stubbed1", "config"] |
| 41 | +
|
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | +
|
| 46 | + akka.discovery.stubbed1 { |
| 47 | + class = akka.discovery.aggregate.StubbedSimpleServiceDiscovery |
| 48 | + } |
| 49 | +
|
| 50 | + akka.discovery.config.services = { |
| 51 | + config1 = { |
| 52 | + endpoints = [ |
| 53 | + { |
| 54 | + host = "cat" |
| 55 | + port = 1233 |
| 56 | + }, |
| 57 | + { |
| 58 | + host = "dog" |
| 59 | + port = 1234 |
| 60 | + } |
| 61 | + ] |
| 62 | + }, |
| 63 | + fail = { |
| 64 | + endpoints = [ |
| 65 | + { |
| 66 | + host = "from-config" |
| 67 | + } |
| 68 | + ] |
| 69 | + } |
| 70 | + } |
| 71 | + """) |
| 72 | +} |
| 73 | + |
| 74 | +class AggregateSimpleServiceDiscoverySpec |
| 75 | + extends TestKit(ActorSystem("AggregateSimpleDiscoverySpec", AggregateSimpleServiceDiscoverySpec.config)) |
| 76 | + with WordSpecLike |
| 77 | + with Matchers |
| 78 | + with BeforeAndAfterAll |
| 79 | + with ScalaFutures { |
| 80 | + |
| 81 | + override protected def afterAll(): Unit = { |
| 82 | + TestKit.shutdownActorSystem(system) |
| 83 | + } |
| 84 | + |
| 85 | + val discovery: SimpleServiceDiscovery = ServiceDiscovery(system).discovery |
| 86 | + |
| 87 | + "Aggregate service discovery" must { |
| 88 | + |
| 89 | + "only call first one if returns results" in { |
| 90 | + val results = discovery.lookup("stubbed", 100.millis).futureValue |
| 91 | + results shouldEqual Resolved("stubbed", immutable.Seq(ResolvedTarget("stubbed1", Some(1234)))) |
| 92 | + } |
| 93 | + |
| 94 | + "move onto the next if no resolved targets" in { |
| 95 | + val results = discovery.lookup("config1", 100.millis).futureValue |
| 96 | + results shouldEqual Resolved("config1", |
| 97 | + immutable.Seq(ResolvedTarget("cat", Some(1233)), ResolvedTarget("dog", Some(1234)))) |
| 98 | + } |
| 99 | + |
| 100 | + "move onto next if fails" in { |
| 101 | + val results = discovery.lookup("fail", 100.millis).futureValue |
| 102 | + // Stub fails then result comes from config |
| 103 | + results shouldEqual Resolved("fail", immutable.Seq(ResolvedTarget("from-config", None))) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments