From 4c2e012e00270b3c0d1b0b1ea781d02e50a52d51 Mon Sep 17 00:00:00 2001 From: Juan Pedro Moreno Date: Mon, 18 Dec 2017 13:02:54 +0100 Subject: [PATCH] Reduces Boilerplate in Server creation (#98) * Reduces boilerplate when creating a server. Aditionally: * Unifies test utilities to DRY between @tagless and @service tests * Removes ETA Expansion warnings * Removes bootstrapFuture syntax * Removes all those imports that are not used anymore * Others * Releases new patch version, 0.4.2 --- build.sbt | 1 + rpc/src/main/scala/client/ChannelConfig.scala | 2 - .../scala/client/handlers/TaskMHandler.scala | 12 +- .../main/scala/internal/MonixAdapters.scala | 1 - .../main/scala/internal/service/service.scala | 2 +- rpc/src/main/scala/proto/annotations.scala | 1 - rpc/src/main/scala/server/ServerConfig.scala | 1 - rpc/src/main/scala/server/implicits.scala | 16 ++- rpc/src/test/scala/CommonUtils.scala | 111 +++++++++++++++ rpc/src/test/scala/RPCTests.scala | 6 +- rpc/src/test/scala/TaglessRPCTests.scala | 6 +- rpc/src/test/scala/TaglessUtils.scala | 127 +----------------- rpc/src/test/scala/Utils.scala | 127 +----------------- .../client/handlers/ChannelHandlerTests.scala | 2 +- .../client/handlers/TaskMHandlerTests.scala | 2 - rpc/src/test/scala/models.scala | 27 ++++ .../test/scala/server/GrpcServerTests.scala | 24 ++-- rpc/src/test/scala/server/HelperTests.scala | 5 +- .../scala/server/RpcServerTestSuite.scala | 26 ++-- rpc/src/test/scala/server/SyntaxTests.scala | 16 +-- .../handlers/GrpcServerHandlerTests.scala | 25 ++-- version.sbt | 2 +- 22 files changed, 225 insertions(+), 317 deletions(-) create mode 100644 rpc/src/test/scala/CommonUtils.scala create mode 100644 rpc/src/test/scala/models.scala diff --git a/build.sbt b/build.sbt index 2201144fe..bfed76c0c 100644 --- a/build.sbt +++ b/build.sbt @@ -37,6 +37,7 @@ lazy val rpc = project .settings(scalaMetaSettings: _*) .settings( Seq( + scalacOptions += "-Ywarn-unused-import", libraryDependencies ++= commonDeps ++ Seq( %%("frees-core", freesV), diff --git a/rpc/src/main/scala/client/ChannelConfig.scala b/rpc/src/main/scala/client/ChannelConfig.scala index 7a4d80fec..be520b2d5 100644 --- a/rpc/src/main/scala/client/ChannelConfig.scala +++ b/rpc/src/main/scala/client/ChannelConfig.scala @@ -22,8 +22,6 @@ import java.util.concurrent.{Executor, TimeUnit} import cats.implicits._ import freestyle._ import freestyle.config.ConfigM -import freestyle.config.implicits._ -import freestyle.rpc.client._ import io.grpc._ sealed trait ManagedChannelFor extends Product with Serializable diff --git a/rpc/src/main/scala/client/handlers/TaskMHandler.scala b/rpc/src/main/scala/client/handlers/TaskMHandler.scala index 1b3d8b47e..3da4f66ab 100644 --- a/rpc/src/main/scala/client/handlers/TaskMHandler.scala +++ b/rpc/src/main/scala/client/handlers/TaskMHandler.scala @@ -25,11 +25,13 @@ import monix.execution.Scheduler class TaskMHandler[F[_]](implicit AC: AsyncContext[F], S: Scheduler) extends FSHandler[Task, F] { override def apply[A](fa: Task[A]): F[A] = AC.runAsync { cb => - fa.runAsync(new Callback[A] { - override def onSuccess(value: A): Unit = cb(Right(value)) - - override def onError(ex: Throwable): Unit = cb(Left(ex)) - }) + fa.runAsync { + new Callback[A] { + override def onSuccess(value: A): Unit = cb(Right(value)) + override def onError(ex: Throwable): Unit = cb(Left(ex)) + } + } + (): Unit } } diff --git a/rpc/src/main/scala/internal/MonixAdapters.scala b/rpc/src/main/scala/internal/MonixAdapters.scala index 1a1c44dfa..7728e9558 100644 --- a/rpc/src/main/scala/internal/MonixAdapters.scala +++ b/rpc/src/main/scala/internal/MonixAdapters.scala @@ -21,7 +21,6 @@ package internal import cats.instances.future._ import cats.~> import io.grpc.stub.StreamObserver -import monix.eval.Callback import monix.execution.Ack.{Continue, Stop} import monix.execution.{Ack, Scheduler} import monix.reactive.Observable.Operator diff --git a/rpc/src/main/scala/internal/service/service.scala b/rpc/src/main/scala/internal/service/service.scala index dd9aead5d..77e81d867 100644 --- a/rpc/src/main/scala/internal/service/service.scala +++ b/rpc/src/main/scala/internal/service/service.scala @@ -164,7 +164,7 @@ private[internal] case class RPCRequest( val encodersImport: Import = serialization match { case Protobuf => - q"import _root_.pbdirect._, _root_.freestyle.rpc.internal.service.encoders.pbd._" + q"import _root_.freestyle.rpc.internal.service.encoders.pbd._" case Avro => q"import _root_.freestyle.rpc.internal.service.encoders.avro._" } diff --git a/rpc/src/main/scala/proto/annotations.scala b/rpc/src/main/scala/proto/annotations.scala index 2f7d075bb..d87684117 100644 --- a/rpc/src/main/scala/proto/annotations.scala +++ b/rpc/src/main/scala/proto/annotations.scala @@ -26,7 +26,6 @@ package object protocol { @compileTimeOnly("enable macro paradise to expand @free macro annotations") class service extends StaticAnnotation { - import scala.meta._ inline def apply(defn: Any): Any = meta { serviceImpl.service(defn) } } diff --git a/rpc/src/main/scala/server/ServerConfig.scala b/rpc/src/main/scala/server/ServerConfig.scala index 4b7ec28b7..8550d7268 100644 --- a/rpc/src/main/scala/server/ServerConfig.scala +++ b/rpc/src/main/scala/server/ServerConfig.scala @@ -20,7 +20,6 @@ package server import cats.implicits._ import freestyle._ import freestyle.config.ConfigM -import freestyle.config.implicits._ import io.grpc.Server case class ServerW(port: Int, configList: List[GrpcConfig]) { diff --git a/rpc/src/main/scala/server/implicits.scala b/rpc/src/main/scala/server/implicits.scala index 86d0d82e3..67b6b9fb1 100644 --- a/rpc/src/main/scala/server/implicits.scala +++ b/rpc/src/main/scala/server/implicits.scala @@ -22,8 +22,7 @@ import freestyle._ import freestyle.implicits._ import freestyle.logging._ import freestyle.loggingJVM.implicits._ - -import scala.concurrent.Future +import freestyle.rpc.server.handlers.GrpcServerHandler @module trait GrpcServerApp { @@ -31,6 +30,13 @@ trait GrpcServerApp { val log: LoggingM } +trait ServerImplicits { + + implicit def grpcServerHandler[M[_]: Capture](implicit SW: ServerW): GrpcServer.Op ~> M = + new GrpcServerHandler[M] andThen new GrpcKInterpreter[M](SW.server) + +} + trait Syntax { implicit def serverOps(server: FreeS[GrpcServerApp.Op, Unit]): ServerOps = new ServerOps(server) @@ -40,11 +46,6 @@ trait Syntax { def bootstrapM[M[_]: Monad](implicit handler: GrpcServer.Op ~> M): M[Unit] = server.interpret[M] - def bootstrapFuture( - implicit MF: Monad[Future], - handler: GrpcServer.Op ~> Future): Future[Unit] = - server.interpret[Future] - } } @@ -69,6 +70,7 @@ object implicits with RPCAsyncImplicits with Syntax with Helpers + with ServerImplicits with freestyle.Interpreters with freestyle.FreeSInstances with freestyle.loggingJVM.Implicits diff --git a/rpc/src/test/scala/CommonUtils.scala b/rpc/src/test/scala/CommonUtils.scala new file mode 100644 index 000000000..c97449f93 --- /dev/null +++ b/rpc/src/test/scala/CommonUtils.scala @@ -0,0 +1,111 @@ +/* + * Copyright 2017 47 Degrees, LLC. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package freestyle.rpc + +import cats.effect.IO +import freestyle.rpc.client.{ChannelConfig, ConfigForAddress, ManagedChannelFor} +import freestyle.rpc.server._ + +import scala.concurrent.Await +import scala.concurrent.duration.Duration +import scala.util.{Failure, Success, Try} + +trait CommonUtils { + + import cats.implicits._ + import freestyle._ + import freestyle.implicits._ + import freestyle.config.implicits._ + + type ConcurrentMonad[A] = IO[A] + + object database { + + val i: Int = 5 + val a1: A = A(1, 2) + val a2: A = A(10, 20) + val a3: A = A(100, 200) + val a4: A = A(1000, 2000) + val c1: C = C("foo1", a1) + val c2: C = C("foo2", a1) + val e1: E = E(a3, "foo3") + val e2: E = E(a4, "foo4") + + val cList = List(c1, c2) + val eList = List(e1, e2) + + val dResult: D = D(6) + } + + def createManagedChannelFor: ManagedChannelFor = + ConfigForAddress[ChannelConfig.Op]("rpc.client.host", "rpc.client.port") + .interpret[Try] match { + case Success(c) => c + case Failure(e) => + e.printStackTrace() + throw new RuntimeException("Unable to load the client configuration", e) + } + + def createServerConf(grpcConfigs: List[GrpcConfig]): ServerW = + BuildServerFromConfig[ServerConfig.Op]("rpc.server.port", grpcConfigs) + .interpret[Try] match { + case Success(c) => c + case Failure(e) => + e.printStackTrace() + throw new RuntimeException("Unable to load the server configuration", e) + } + + def serverStart[M[_]](implicit APP: GrpcServerApp[M]): FreeS[M, Unit] = { + val server = APP.server + val log = APP.log + for { + _ <- server.start() + port <- server.getPort + _ <- log.info(s"Server started, listening on $port") + } yield () + } + + def serverStop[M[_]](implicit APP: GrpcServerApp[M]): FreeS[M, Unit] = { + val server = APP.server + val log = APP.log + for { + port <- server.getPort + _ <- log.info(s"Stopping server listening on $port") + _ <- server.shutdownNow() + } yield () + } + + def debug(str: String): Unit = + println(s"\n\n$str\n\n") + + trait CommonRuntime { + + import freestyle._ + import freestyle.rpc.server.implicits._ + + implicit val S: monix.execution.Scheduler = monix.execution.Scheduler.Implicits.global + + implicit class InterpreterOps[F[_], A](fs: FreeS[F, A])( + implicit H: FSHandler[F, ConcurrentMonad]) { + + def runF: A = Await.result(fs.interpret[ConcurrentMonad].unsafeToFuture(), Duration.Inf) + + } + + } + +} diff --git a/rpc/src/test/scala/RPCTests.scala b/rpc/src/test/scala/RPCTests.scala index b145387b6..8b279dedf 100644 --- a/rpc/src/test/scala/RPCTests.scala +++ b/rpc/src/test/scala/RPCTests.scala @@ -24,10 +24,8 @@ import freestyle.rpc.protocol.Empty class RPCTests extends RpcBaseTestSuite with BeforeAndAfterAll { - import cats.implicits._ - import freestyle.rpc.Utils.service._ + import freestyle.rpc.Utils._ import freestyle.rpc.Utils.database._ - import freestyle.rpc.Utils.helpers._ import freestyle.rpc.Utils.implicits._ override protected def beforeAll(): Unit = { @@ -180,4 +178,4 @@ class RPCTests extends RpcBaseTestSuite with BeforeAndAfterAll { } -} \ No newline at end of file +} diff --git a/rpc/src/test/scala/TaglessRPCTests.scala b/rpc/src/test/scala/TaglessRPCTests.scala index 861dec2d6..9f6698689 100644 --- a/rpc/src/test/scala/TaglessRPCTests.scala +++ b/rpc/src/test/scala/TaglessRPCTests.scala @@ -24,10 +24,8 @@ import freestyle.rpc.protocol.Empty class FreesRPCTests extends RpcBaseTestSuite with BeforeAndAfterAll { - import cats.implicits._ - import freestyle.rpc.TaglessUtils.service._ + import freestyle.rpc.Utils._ import freestyle.rpc.TaglessUtils.database._ - import freestyle.rpc.TaglessUtils.helpers._ import freestyle.rpc.TaglessUtils.implicits._ override protected def beforeAll(): Unit = { @@ -180,4 +178,4 @@ class FreesRPCTests extends RpcBaseTestSuite with BeforeAndAfterAll { } -} \ No newline at end of file +} diff --git a/rpc/src/test/scala/TaglessUtils.scala b/rpc/src/test/scala/TaglessUtils.scala index c1bb849ca..83776588e 100644 --- a/rpc/src/test/scala/TaglessUtils.scala +++ b/rpc/src/test/scala/TaglessUtils.scala @@ -16,42 +16,17 @@ package freestyle.rpc -import cats.effect.IO import cats.{~>, Monad, MonadError} import freestyle._ import freestyle.asyncCatsEffect.implicits._ -import freestyle.rpc.client._ import freestyle.rpc.protocol._ -import freestyle.rpc.server._ -import io.grpc.ManagedChannel import monix.eval.Task import monix.reactive.Observable -import scala.concurrent.duration._ -import scala.concurrent.Await -import scala.util.{Failure, Success, Try} - -object TaglessUtils { - - type ConcurrentMonad[A] = IO[A] +object TaglessUtils extends CommonUtils { object service { - @message - case class A(x: Int, y: Int) - - @message - case class B(a1: A, a2: A) - - @message - case class C(foo: String, a: A) - - @message - case class D(bar: Int) - - @message - case class E(a: A, foo: String) - @freestyle.tagless.tagless @service trait TaglessRPCService { @@ -87,26 +62,6 @@ object TaglessUtils { } - object database { - - import service._ - - val i: Int = 5 - val a1: A = A(1, 2) - val a2: A = A(10, 20) - val a3: A = A(100, 200) - val a4: A = A(1000, 2000) - val c1: C = C("foo1", a1) - val c2: C = C("foo2", a1) - val e1: E = E(a3, "foo3") - val e2: E = E(a4, "foo4") - - val cList = List(c1, c2) - val eList = List(e1, e2) - - val dResult: D = D(6) - } - object handlers { object server { @@ -136,7 +91,7 @@ object TaglessUtils { C.capture(c1) def serverStreaming(b: B): F[Observable[C]] = { - helpers.debug(s"[SERVER] b -> $b") + debug(s"[SERVER] b -> $b") val obs = Observable.fromIterable(cList) C.capture(obs) } @@ -145,7 +100,7 @@ object TaglessUtils { T2F( oa.foldLeftL(D(0)) { case (current, a) => - helpers.debug(s"[SERVER] Current -> $current / a -> $a") + debug(s"[SERVER] Current -> $current / a -> $a") D(current.bar + a.x + a.y) } ) @@ -168,7 +123,6 @@ object TaglessUtils { import freestyle.rpc.TaglessUtils.clientProgram.MyRPCClient import service._ - import cats.implicits._ import freestyle.rpc.protocol._ class TaglessRPCServiceClientHandler[F[_]: Monad]( @@ -207,7 +161,7 @@ object TaglessUtils { .zipWithIndex .map { case (c, i) => - helpers.debug(s"[CLIENT] Result #$i: $c") + debug(s"[CLIENT] Result #$i: $c") c } .toListL @@ -230,8 +184,6 @@ object TaglessUtils { object clientProgram { - import service._ - @free trait MyRPCClient { def notAllowed(b: Boolean): FS[C] @@ -248,67 +200,13 @@ object TaglessUtils { } } - object helpers { - - import cats.implicits._ - import freestyle.implicits._ - import freestyle.config.implicits._ - - def createManagedChannelFor: ManagedChannelFor = - ConfigForAddress[ChannelConfig.Op]("rpc.client.host", "rpc.client.port") - .interpret[Try] match { - case Success(c) => c - case Failure(e) => - e.printStackTrace() - throw new RuntimeException("Unable to load the client configuration", e) - } - - def createServerConf(grpcConfigs: List[GrpcConfig]): ServerW = - BuildServerFromConfig[ServerConfig.Op]("rpc.server.port", grpcConfigs) - .interpret[Try] match { - case Success(c) => c - case Failure(e) => - e.printStackTrace() - throw new RuntimeException("Unable to load the server configuration", e) - } - - def serverStart[M[_]](implicit APP: GrpcServerApp[M]): FreeS[M, Unit] = { - val server = APP.server - val log = APP.log - for { - _ <- server.start() - port <- server.getPort - _ <- log.info(s"Server started, listening on $port") - } yield () - } - - def serverStop[M[_]](implicit APP: GrpcServerApp[M]): FreeS[M, Unit] = { - val server = APP.server - val log = APP.log - for { - port <- server.getPort - _ <- log.info(s"Stopping server listening on $port") - _ <- server.shutdownNow() - } yield () - } - - def debug(str: String): Unit = - println(s"\n\n$str\n\n") - - } - - trait TaglessRuntime { + trait TaglessRuntime extends CommonRuntime { import service._ - import helpers._ import handlers.server._ import handlers.client._ - import cats.implicits._ import freestyle.rpc.server._ import freestyle.rpc.server.implicits._ - import freestyle.rpc.server.handlers._ - - implicit val S: monix.execution.Scheduler = monix.execution.Scheduler.Implicits.global ////////////////////////////////// // Server Runtime Configuration // @@ -321,9 +219,7 @@ object TaglessUtils { AddService(TaglessRPCService.bindService[ConcurrentMonad]) ) - implicit val grpcServerHandler: GrpcServer.Op ~> ConcurrentMonad = - new GrpcServerHandler[ConcurrentMonad] andThen - new GrpcKInterpreter[ConcurrentMonad](createServerConf(grpcConfigs).server) + implicit val serverW: ServerW = createServerConf(grpcConfigs) ////////////////////////////////// // Client Runtime Configuration // @@ -335,17 +231,6 @@ object TaglessUtils { implicit val taglessRPCServiceClientHandler: TaglessRPCServiceClientHandler[ConcurrentMonad] = new TaglessRPCServiceClientHandler[ConcurrentMonad] - //////////// - // Syntax // - //////////// - - implicit class InterpreterOps[F[_], A](fs: FreeS[F, A])( - implicit H: FSHandler[F, ConcurrentMonad]) { - - def runF: A = Await.result(fs.interpret[ConcurrentMonad].unsafeToFuture(), Duration.Inf) - - } - } object implicits extends TaglessRuntime diff --git a/rpc/src/test/scala/Utils.scala b/rpc/src/test/scala/Utils.scala index 787655217..173e9dee1 100644 --- a/rpc/src/test/scala/Utils.scala +++ b/rpc/src/test/scala/Utils.scala @@ -16,43 +16,18 @@ package freestyle.rpc -import cats.effect.IO import cats.{~>, Monad, MonadError} import freestyle._ import freestyle.rpc.Utils.database.a4 import freestyle.asyncCatsEffect.implicits._ -import freestyle.rpc.client._ import freestyle.rpc.protocol._ -import freestyle.rpc.server._ -import io.grpc.ManagedChannel import monix.eval.Task import monix.reactive.Observable -import scala.concurrent.duration._ -import scala.concurrent.Await -import scala.util.{Failure, Success, Try} - -object Utils { - - type ConcurrentMonad[A] = IO[A] +object Utils extends CommonUtils { object service { - @message - case class A(x: Int, y: Int) - - @message - case class B(a1: A, a2: A) - - @message - case class C(foo: String, a: A) - - @message - case class D(bar: Int) - - @message - case class E(a: A, foo: String) - @service trait RPCService[F[_]] { @@ -87,26 +62,6 @@ object Utils { } - object database { - - import service._ - - val i: Int = 5 - val a1: A = A(1, 2) - val a2: A = A(10, 20) - val a3: A = A(100, 200) - val a4: A = A(1000, 2000) - val c1: C = C("foo1", a1) - val c2: C = C("foo2", a1) - val e1: E = E(a3, "foo3") - val e2: E = E(a4, "foo4") - - val cList = List(c1, c2) - val eList = List(e1, e2) - - val dResult: D = D(6) - } - object handlers { object server { @@ -135,7 +90,7 @@ object Utils { C.capture(c1) def serverStreaming(b: B): F[Observable[C]] = { - helpers.debug(s"[SERVER] b -> $b") + debug(s"[SERVER] b -> $b") val obs = Observable.fromIterable(cList) C.capture(obs) } @@ -144,7 +99,7 @@ object Utils { T2F( oa.foldLeftL(D(0)) { case (current, a) => - helpers.debug(s"[SERVER] Current -> $current / a -> $a") + debug(s"[SERVER] Current -> $current / a -> $a") D(current.bar + a.x + a.y) } ) @@ -167,7 +122,6 @@ object Utils { import freestyle.rpc.Utils.clientProgram.MyRPCClient import service._ - import cats.implicits._ import freestyle.rpc.protocol._ class FreesRPCServiceClientHandler[F[_]: Monad]( @@ -206,7 +160,7 @@ object Utils { .zipWithIndex .map { case (c, i) => - helpers.debug(s"[CLIENT] Result #$i: $c") + debug(s"[CLIENT] Result #$i: $c") c } .toListL @@ -229,8 +183,6 @@ object Utils { object clientProgram { - import service._ - @free trait MyRPCClient { def notAllowed(b: Boolean): FS[C] @@ -247,67 +199,13 @@ object Utils { } } - object helpers { - - import cats.implicits._ - import freestyle.implicits._ - import freestyle.config.implicits._ - - def createManagedChannelFor: ManagedChannelFor = - ConfigForAddress[ChannelConfig.Op]("rpc.client.host", "rpc.client.port") - .interpret[Try] match { - case Success(c) => c - case Failure(e) => - e.printStackTrace() - throw new RuntimeException("Unable to load the client configuration", e) - } - - def createServerConf(grpcConfigs: List[GrpcConfig]): ServerW = - BuildServerFromConfig[ServerConfig.Op]("rpc.server.port", grpcConfigs) - .interpret[Try] match { - case Success(c) => c - case Failure(e) => - e.printStackTrace() - throw new RuntimeException("Unable to load the server configuration", e) - } - - def serverStart[M[_]](implicit APP: GrpcServerApp[M]): FreeS[M, Unit] = { - val server = APP.server - val log = APP.log - for { - _ <- server.start() - port <- server.getPort - _ <- log.info(s"Server started, listening on $port") - } yield () - } - - def serverStop[M[_]](implicit APP: GrpcServerApp[M]): FreeS[M, Unit] = { - val server = APP.server - val log = APP.log - for { - port <- server.getPort - _ <- log.info(s"Stopping server listening on $port") - _ <- server.shutdownNow() - } yield () - } - - def debug(str: String): Unit = - println(s"\n\n$str\n\n") - - } - - trait FreesRuntime { + trait FreesRuntime extends CommonRuntime { import service._ - import helpers._ import handlers.server._ import handlers.client._ - import cats.implicits._ import freestyle.rpc.server._ import freestyle.rpc.server.implicits._ - import freestyle.rpc.server.handlers._ - - implicit val S: monix.execution.Scheduler = monix.execution.Scheduler.Implicits.global ////////////////////////////////// // Server Runtime Configuration // @@ -320,9 +218,7 @@ object Utils { AddService(RPCService.bindService[ConcurrentMonad]) ) - implicit val grpcServerHandler: GrpcServer.Op ~> ConcurrentMonad = - new GrpcServerHandler[ConcurrentMonad] andThen - new GrpcKInterpreter[ConcurrentMonad](createServerConf(grpcConfigs).server) + implicit val serverW: ServerW = createServerConf(grpcConfigs) ////////////////////////////////// // Client Runtime Configuration // @@ -334,17 +230,6 @@ object Utils { implicit val freesRPCServiceClientHandler: FreesRPCServiceClientHandler[ConcurrentMonad] = new FreesRPCServiceClientHandler[ConcurrentMonad] - //////////// - // Syntax // - //////////// - - implicit class InterpreterOps[F[_], A](fs: FreeS[F, A])( - implicit H: FSHandler[F, ConcurrentMonad]) { - - def runF: A = Await.result(fs.interpret[ConcurrentMonad].unsafeToFuture(), Duration.Inf) - - } - } object implicits extends FreesRuntime diff --git a/rpc/src/test/scala/client/handlers/ChannelHandlerTests.scala b/rpc/src/test/scala/client/handlers/ChannelHandlerTests.scala index 29afcaecb..87735cb48 100644 --- a/rpc/src/test/scala/client/handlers/ChannelHandlerTests.scala +++ b/rpc/src/test/scala/client/handlers/ChannelHandlerTests.scala @@ -44,7 +44,7 @@ class ChannelHandlerTests extends RpcClientTestSuite { "allow to fetch the authority of the destination the channel connects to" in { - (managedChannelMock.authority _).expects().returns(authority) + (managedChannelMock.authority _: () => String).expects().returns(authority) runKFuture(handler.authority, managedChannelMock) shouldBe authority } diff --git a/rpc/src/test/scala/client/handlers/TaskMHandlerTests.scala b/rpc/src/test/scala/client/handlers/TaskMHandlerTests.scala index d5cb8c4cf..4edfd7a5f 100644 --- a/rpc/src/test/scala/client/handlers/TaskMHandlerTests.scala +++ b/rpc/src/test/scala/client/handlers/TaskMHandlerTests.scala @@ -35,7 +35,6 @@ class TaskMHandlerTests extends RpcClientTestSuite { "freestyle.async.AsyncContext[F] is provided" in { implicit val S: monix.execution.Scheduler = monix.execution.Scheduler.Implicits.global - import freestyle.async.implicits.futureAsyncContext val handler: Task ~> Future = task2Future val fooTask: Task[String] = Task.now(foo) @@ -46,7 +45,6 @@ class TaskMHandlerTests extends RpcClientTestSuite { "recover from a failed monix.eval.Task wrapping them into scala.concurrent.Future" in { implicit val S: monix.execution.Scheduler = monix.execution.Scheduler.Implicits.global - import freestyle.async.implicits.futureAsyncContext val handler: Task ~> Future = task2Future val taskFailed: Task[String] = Task.raiseError(new RuntimeException(failureMessage)) diff --git a/rpc/src/test/scala/models.scala b/rpc/src/test/scala/models.scala new file mode 100644 index 000000000..d9b86e855 --- /dev/null +++ b/rpc/src/test/scala/models.scala @@ -0,0 +1,27 @@ +/* + * Copyright 2017 47 Degrees, LLC. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package freestyle.rpc + +case class A(x: Int, y: Int) + +case class B(a1: A, a2: A) + +case class C(foo: String, a: A) + +case class D(bar: Int) + +case class E(a: A, foo: String) diff --git a/rpc/src/test/scala/server/GrpcServerTests.scala b/rpc/src/test/scala/server/GrpcServerTests.scala index 8fef80a96..84764fed2 100644 --- a/rpc/src/test/scala/server/GrpcServerTests.scala +++ b/rpc/src/test/scala/server/GrpcServerTests.scala @@ -80,17 +80,21 @@ class GrpcServerTests extends RpcServerTestSuite { b, unit): Result) - (serverMock.start _).verify().once() - (serverMock.getPort _).verify().once() - (serverMock.getServices _).verify().once() - (serverMock.getImmutableServices _).verify().once() - (serverMock.getMutableServices _).verify().once() - (serverMock.shutdown _).verify().once() - (serverMock.shutdownNow _).verify().once() - (serverMock.isShutdown _).verify().twice() - (serverMock.isTerminated _).verify().once() + (serverMock.start _: () => Server).verify().once() + (serverMock.getPort _: () => Int).verify().once() + (serverMock.getServices _: () => java.util.List[ServerServiceDefinition]).verify().once() + (serverMock.getImmutableServices _: () => java.util.List[ServerServiceDefinition]) + .verify() + .once() + (serverMock.getMutableServices _: () => java.util.List[ServerServiceDefinition]) + .verify() + .once() + (serverMock.shutdown _: () => Server).verify().once() + (serverMock.shutdownNow _: () => Server).verify().once() + (serverMock.isShutdown _: () => Boolean).verify().twice() + (serverMock.isTerminated _: () => Boolean).verify().once() (serverMock.awaitTermination(_: Long, _: TimeUnit)).verify(timeout, timeoutUnit).once() - (serverMock.awaitTermination _).verify().once() + (serverMock.awaitTermination _: () => Unit).verify().once() } } diff --git a/rpc/src/test/scala/server/HelperTests.scala b/rpc/src/test/scala/server/HelperTests.scala index 952db03c8..cec65f434 100644 --- a/rpc/src/test/scala/server/HelperTests.scala +++ b/rpc/src/test/scala/server/HelperTests.scala @@ -19,6 +19,7 @@ package server import cats.Id import freestyle.rpc.server.implicits._ +import io.grpc.Server class HelperTests extends RpcServerTestSuite { @@ -30,8 +31,8 @@ class HelperTests extends RpcServerTestSuite { server[GrpcServerApp.Op].bootstrapM[Id] shouldBe ((): Unit) - (serverMock.start _).verify() - (serverMock.awaitTermination _).verify() + (serverMock.start _: () => Server).verify() + (serverMock.awaitTermination _: () => Unit).verify() } } diff --git a/rpc/src/test/scala/server/RpcServerTestSuite.scala b/rpc/src/test/scala/server/RpcServerTestSuite.scala index 234967806..0a5f9a359 100644 --- a/rpc/src/test/scala/server/RpcServerTestSuite.scala +++ b/rpc/src/test/scala/server/RpcServerTestSuite.scala @@ -42,17 +42,23 @@ trait RpcServerTestSuite extends RpcBaseTestSuite { val immutableServiceList: List[ServerServiceDefinition] = List(sd1) val mutableServiceList: List[ServerServiceDefinition] = List(sd2) - (serverMock.start _).when().returns(serverCopyMock) - (serverMock.getPort _).when().returns(port) - (serverMock.getServices _).when().returns(serviceList.asJava) - (serverMock.getImmutableServices _).when().returns(immutableServiceList.asJava) - (serverMock.getMutableServices _).when().returns(mutableServiceList.asJava) - (serverMock.shutdown _).when().returns(serverCopyMock) - (serverMock.shutdownNow _).when().returns(serverCopyMock) - (serverMock.isShutdown _).when().returns(b) - (serverMock.isTerminated _).when().returns(b) + (serverMock.start _: () => Server).when().returns(serverCopyMock) + (serverMock.getPort _: () => Int).when().returns(port) + (serverMock.getServices _: () => java.util.List[ServerServiceDefinition]) + .when() + .returns(serviceList.asJava) + (serverMock.getImmutableServices _: () => java.util.List[ServerServiceDefinition]) + .when() + .returns(immutableServiceList.asJava) + (serverMock.getMutableServices _: () => java.util.List[ServerServiceDefinition]) + .when() + .returns(mutableServiceList.asJava) + (serverMock.shutdown _: () => Server).when().returns(serverCopyMock) + (serverMock.shutdownNow _: () => Server).when().returns(serverCopyMock) + (serverMock.isShutdown _: () => Boolean).when().returns(b) + (serverMock.isTerminated _: () => Boolean).when().returns(b) (serverMock.awaitTermination(_: Long, _: TimeUnit)).when(timeout, timeoutUnit).returns(b) - (serverMock.awaitTermination _).when().returns(unit) + (serverMock.awaitTermination _: () => Unit).when().returns(unit) } object implicits extends Helpers with DummyData { diff --git a/rpc/src/test/scala/server/SyntaxTests.scala b/rpc/src/test/scala/server/SyntaxTests.scala index bababdb78..b3632014d 100644 --- a/rpc/src/test/scala/server/SyntaxTests.scala +++ b/rpc/src/test/scala/server/SyntaxTests.scala @@ -18,8 +18,8 @@ package freestyle.rpc package server import cats.Id -import cats.implicits._ import freestyle.rpc.server.implicits._ +import io.grpc.Server import scala.concurrent.ExecutionContext @@ -35,18 +35,8 @@ class SyntaxTests extends RpcServerTestSuite { server[GrpcServerApp.Op].bootstrapM[Id] shouldBe ((): Unit) - (serverMock.start _).verify().once() - (serverMock.awaitTermination _).verify().once() - } - - "allow using bootstrapFuture" in { - - val f = server[GrpcServerApp.Op].bootstrapFuture.await - - f shouldBe ((): Unit) - - (serverMock.start _).verify().once() - (serverMock.awaitTermination _).verify().once() + (serverMock.start _: () => Server).verify().once() + (serverMock.awaitTermination _: () => Unit).verify().once() } } diff --git a/rpc/src/test/scala/server/handlers/GrpcServerHandlerTests.scala b/rpc/src/test/scala/server/handlers/GrpcServerHandlerTests.scala index 41fac7465..998b4e4af 100644 --- a/rpc/src/test/scala/server/handlers/GrpcServerHandlerTests.scala +++ b/rpc/src/test/scala/server/handlers/GrpcServerHandlerTests.scala @@ -18,6 +18,7 @@ package freestyle.rpc package server.handlers import freestyle.rpc.server.RpcServerTestSuite +import io.grpc.{Server, ServerServiceDefinition} import scala.concurrent.duration.TimeUnit import scala.concurrent.{ExecutionContext, Future} @@ -35,63 +36,67 @@ class GrpcServerHandlerTests extends RpcServerTestSuite { "allow to start a GrpcServer" in { runKFuture(handler.start, serverMock) shouldBe serverCopyMock - (serverMock.start _).verify().once() + (serverMock.start _: () => Server).verify().once() } "allow to get the port where server is running" in { runKFuture(handler.getPort, serverMock) shouldBe port - (serverMock.getPort _).verify().once() + (serverMock.getPort _: () => Int).verify().once() } "allow to get the services running under the Server instance" in { runKFuture(handler.getServices, serverMock) shouldBe serviceList - (serverMock.getServices _).verify().once() + (serverMock.getServices _: () => java.util.List[ServerServiceDefinition]).verify().once() } "allow to get the immutable services running under the Server instance" in { runKFuture(handler.getImmutableServices, serverMock) shouldBe immutableServiceList - (serverMock.getImmutableServices _).verify().once() + (serverMock.getImmutableServices _: () => java.util.List[ServerServiceDefinition]) + .verify() + .once() } "allow to get the mutable services running under the Server instance" in { runKFuture(handler.getMutableServices, serverMock) shouldBe mutableServiceList - (serverMock.getMutableServices _).verify().once() + (serverMock.getMutableServices _: () => java.util.List[ServerServiceDefinition]) + .verify() + .once() } "allow to stop a started GrpcServer" in { runKFuture(handler.shutdown, serverMock) shouldBe serverCopyMock - (serverMock.shutdown _).verify().once() + (serverMock.shutdown _: () => Server).verify().once() } "allow to stop immediately a started GrpcServer" in { runKFuture(handler.shutdownNow, serverMock) shouldBe serverCopyMock - (serverMock.shutdownNow _).verify().once() + (serverMock.shutdownNow _: () => Server).verify().once() } "allow to ask whether a Server is shutdown" in { runKFuture(handler.isShutdown, serverMock) shouldBe b - (serverMock.isShutdown _).verify().once() + (serverMock.isShutdown _: () => Boolean).verify().once() } "allow to ask whether a Server instance has been terminated" in { runKFuture(handler.isTerminated, serverMock) shouldBe b - (serverMock.isTerminated _).verify().once() + (serverMock.isTerminated _: () => Boolean).verify().once() } @@ -105,7 +110,7 @@ class GrpcServerHandlerTests extends RpcServerTestSuite { "allow stopping a started GrpcServer" in { runKFuture(handler.awaitTermination, serverMock) shouldBe unit - (serverMock.awaitTermination _).verify().once() + (serverMock.awaitTermination _: () => Unit).verify().once() } diff --git a/version.sbt b/version.sbt index c4b15d25d..e6f1653ef 100644 --- a/version.sbt +++ b/version.sbt @@ -1 +1 @@ -version in ThisBuild := "0.4.2-SNAPSHOT" \ No newline at end of file +version in ThisBuild := "0.4.2" \ No newline at end of file