-
Notifications
You must be signed in to change notification settings - Fork 124
/
AkkaHttpClientConnectionFailSpec.scala
66 lines (58 loc) · 2.15 KB
/
AkkaHttpClientConnectionFailSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Copyright (C) 2023 Lightbend Inc. <https://www.lightbend.com>
*/
package akka.grpc.scaladsl
import akka.actor.ActorSystem
import akka.grpc.{ GrpcClientSettings, GrpcServiceException }
import akka.http.scaladsl.model.HttpResponse
import akka.testkit.TestKit
import com.typesafe.config.ConfigFactory
import example.myapp.helloworld.grpc.helloworld.{ GreeterServiceClient, HelloRequest }
import io.grpc.Status
import org.scalatest.Inspectors.forAll
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike
import scala.concurrent.Future
import scala.concurrent.duration.DurationInt
import scala.util.{ Failure, Success }
class AkkaHttpClientConnectionFailSpec
extends TestKit(
ActorSystem(
"GrpcExceptionHandlerSpec",
ConfigFactory
.parseString("""
akka.grpc.client."*".backend = "akka-http"
akka.http.client.http2.max-persistent-attempts = 2
""".stripMargin)
.withFallback(ConfigFactory.load())))
with AnyWordSpecLike
with Matchers
with ScalaFutures {
"The Akka HTTP client backend" should {
"fail queued requests when connection fails" in {
// Note that the Akka HTTP client does not strictly adhere to the gRPC backoff protocol but has its own
// backoff algorithm
val client = GreeterServiceClient(GrpcClientSettings.connectToServiceAt("127.0.0.1", 5).withTls(false))
val futures = (1 to 10).map { _ =>
client.sayHello(HelloRequest())
}
// all should be failed
import system.dispatcher
val lifted = Future.sequence(futures.map(_.map(Success(_)).recover {
case th: Throwable => Failure[HttpResponse](th)
}))
val results = lifted.futureValue(timeout(5.seconds))
forAll(results) { it =>
it.isFailure should be(true)
it.failed.get match {
case ex: GrpcServiceException =>
ex.status.getCode shouldBe (Status.Code.UNAVAILABLE)
case unexpected =>
unexpected.printStackTrace()
fail(s"Exception ${unexpected} was not a GrpcServiceException")
}
}
}
}
}