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

https://github.com/cloudstateio/cloudstate · Scala · 71 lines · 41 code · 15 blank · 15 comment · 0 complexity · a736faf7fda25e7d7291907a4f14538a 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.http.scaladsl.Http
  19. import akka.testkit.{TestKit, TestProbe}
  20. import com.typesafe.config.{Config, ConfigFactory}
  21. import io.cloudstate.testkit.discovery.TestEntityDiscoveryService
  22. import io.cloudstate.testkit.eventsourced.TestEventSourcedService
  23. import scala.concurrent.Await
  24. import scala.concurrent.duration._
  25. final class TestService {
  26. import TestService._
  27. val port: Int = Sockets.temporaryLocalPort()
  28. val context = new TestServiceContext(port)
  29. val entityDiscovery = new TestEntityDiscoveryService(context)
  30. val eventSourced = new TestEventSourcedService(context)
  31. import context.system
  32. Await.result(
  33. Http().bindAndHandleAsync(
  34. handler = entityDiscovery.handler orElse eventSourced.handler,
  35. interface = "localhost",
  36. port = port
  37. ),
  38. 10.seconds
  39. )
  40. def terminate(): Unit = context.terminate()
  41. }
  42. object TestService {
  43. def apply(): TestService = new TestService
  44. final class TestServiceContext(val port: Int) {
  45. val config: Config = ConfigFactory.load(ConfigFactory.parseString(s"""
  46. akka.loglevel = ERROR
  47. akka.http.server {
  48. preview.enable-http2 = on
  49. idle-timeout = infinite
  50. }
  51. """))
  52. implicit val system: ActorSystem = ActorSystem("TestService", config)
  53. val probe: TestProbe = TestProbe("TestServiceProbe")
  54. def terminate(): Unit = TestKit.shutdownActorSystem(system)
  55. }
  56. }