Skip to content

Commit

Permalink
Configure test service retries inline
Browse files Browse the repository at this point in the history
  • Loading branch information
joroKr21 committed Jun 7, 2024
1 parent c404aa5 commit 872bde4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 38 deletions.
22 changes: 0 additions & 22 deletions e2e/src/test/resources/service_config.json

This file was deleted.

35 changes: 19 additions & 16 deletions e2e/src/test/scalajvm/scalapb/zio_grpc/TestServiceSpec.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package scalapb.zio_grpc

import com.google.gson.Gson
import com.google.gson.stream.JsonReader
import io.grpc.{ManagedChannelBuilder, ServerBuilder, Status, StatusException}
import scala.jdk.CollectionConverters._
import scalapb.zio_grpc.server.TestServiceImpl
import scalapb.zio_grpc.testservice.Request.Scenario
import scalapb.zio_grpc.testservice.ZioTestservice.TestServiceClient
Expand All @@ -18,20 +17,26 @@ object TestServiceSpec extends ZIOSpecDefault with CommonTestServiceSpec {
val serverLayer: ZLayer[TestServiceImpl, Throwable, Server] =
ServerLayer.fromEnvironment[TestServiceImpl.Service](ServerBuilder.forPort(0))

// https://github.com/grpc/proposal/blob/master/A6-client-retries.md
val serviceConfig = Map(
"methodConfig" -> Map(
"name" -> List(Map("service" -> "scalapb.zio_grpc.TestService", "method" -> "Unary").asJava).asJava,
"retryPolicy" -> Map[String, Any](
"maxAttempts" -> 5,
"initialBackoff" -> "0.1s",
"maxBackoff" -> "30s",
"backoffMultiplier" -> 1,
"retryableStatusCodes" -> List("UNAVAILABLE").asJava
).asJava
).asJava
).asJava

val clientLayer: ZLayer[Server, Nothing, TestServiceClient] =
ZLayer.scoped[Server] {
for {
ss <- ZIO.service[Server]
port <- ss.port.orDie
ch = ManagedChannelBuilder
.forAddress("localhost", port)
.defaultServiceConfig(
new Gson().fromJson(
new JsonReader(scala.io.Source.fromResource("service_config.json").reader()),
classOf[java.util.Map[String, Any]]
)
)
.usePlaintext()
ch = ManagedChannelBuilder.forAddress("localhost", port).defaultServiceConfig(serviceConfig).usePlaintext()
client <- TestServiceClient.scoped(ZManagedChannel(ch)).orDie
} yield client
}
Expand Down Expand Up @@ -60,11 +65,9 @@ object TestServiceSpec extends ZIOSpecDefault with CommonTestServiceSpec {
} yield assert(r)(fails(hasStatusCode(Status.DEADLINE_EXCEEDED))) && assert(exit.get.isInterrupted)(isTrue)
} @@ flaky(100) @@ withLiveClock,
test("let clients retry") {
assertZIO(
TestServiceClient
.unary(Request(Request.Scenario.UNAVAILABLE, in = 12))
.exit
)(fails(hasStatusCode(Status.UNAVAILABLE) && hasDescription("5")))
assertZIO(TestServiceClient.unary(Request(Request.Scenario.UNAVAILABLE, in = 12)).exit) {
fails(hasStatusCode(Status.UNAVAILABLE) && hasDescription("5"))
}
}
)

Expand Down

0 comments on commit 872bde4

Please sign in to comment.