/testkit/src/main/scala/io/cloudstate/testkit/InterceptService.scala

https://github.com/cloudstateio/cloudstate · Scala · 88 lines · 55 code · 18 blank · 15 comment · 0 complexity · beacac432bfe528d390cdfa86e151e8b MD5 · raw file

  1. /*
  2. * Copyright 2019 Lightbend Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package io.cloudstate.testkit
  17. import akka.actor.ActorSystem
  18. import akka.grpc.GrpcClientSettings
  19. import akka.http.scaladsl.Http
  20. import akka.testkit.{TestKit, TestProbe}
  21. import com.typesafe.config.{Config, ConfigFactory}
  22. import io.cloudstate.testkit.InterceptService.InterceptorSettings
  23. import io.cloudstate.testkit.discovery.InterceptEntityDiscovery
  24. import io.cloudstate.testkit.eventsourced.InterceptEventSourcedService
  25. import scala.concurrent.Await
  26. import scala.concurrent.duration._
  27. final case class ServiceAddress(host: String, port: Int)
  28. final class InterceptService(settings: InterceptorSettings) {
  29. import InterceptService._
  30. private val context = new InterceptorContext(settings.intercept.host, settings.intercept.port)
  31. private val entityDiscovery = new InterceptEntityDiscovery(context)
  32. private val eventSourced = new InterceptEventSourcedService(context)
  33. import context.system
  34. entityDiscovery.expectOnline(60.seconds)
  35. Await.result(
  36. Http().bindAndHandleAsync(
  37. handler = entityDiscovery.handler orElse eventSourced.handler,
  38. interface = settings.bind.host,
  39. port = settings.bind.port
  40. ),
  41. 10.seconds
  42. )
  43. def expectEntityDiscovery(): InterceptEntityDiscovery.Discovery = entityDiscovery.expectDiscovery()
  44. def expectEventSourcedConnection(): InterceptEventSourcedService.Connection = eventSourced.expectConnection()
  45. def terminate(): Unit = {
  46. entityDiscovery.terminate()
  47. eventSourced.terminate()
  48. context.terminate()
  49. }
  50. }
  51. object InterceptService {
  52. final case class InterceptorSettings(bind: ServiceAddress, intercept: ServiceAddress)
  53. object InterceptorSettings {
  54. def apply(bindHost: String, bindPort: Int, interceptHost: String, interceptPort: Int): InterceptorSettings =
  55. InterceptorSettings(ServiceAddress(bindHost, bindPort), ServiceAddress(interceptHost, interceptPort))
  56. }
  57. final class InterceptorContext(val host: String, val port: Int) {
  58. val config: Config = ConfigFactory.load(ConfigFactory.parseString(s"""
  59. akka.loglevel = ERROR
  60. akka.http.server {
  61. preview.enable-http2 = on
  62. idle-timeout = infinite
  63. }
  64. """))
  65. implicit val system: ActorSystem = ActorSystem("Interceptor", config)
  66. val clientSettings: GrpcClientSettings = GrpcClientSettings.connectToServiceAt(host, port).withTls(false)
  67. val probe: TestProbe = TestProbe("InterceptorProbe")
  68. def terminate(): Unit = TestKit.shutdownActorSystem(system)
  69. }
  70. }