From d15721fa47f439d2dc41a103dbf29c1bd60bc72d Mon Sep 17 00:00:00 2001 From: Juan Pedro Moreno Date: Wed, 18 Oct 2017 16:29:55 +0200 Subject: [PATCH 1/2] Removes ClientCallsM - covered by the macro --- rpc/src/main/scala/client/ClientCallsM.scala | 60 --------------- .../client/handlers/ClientCallsMHandler.scala | 74 ------------------- 2 files changed, 134 deletions(-) delete mode 100644 rpc/src/main/scala/client/ClientCallsM.scala delete mode 100644 rpc/src/main/scala/client/handlers/ClientCallsMHandler.scala diff --git a/rpc/src/main/scala/client/ClientCallsM.scala b/rpc/src/main/scala/client/ClientCallsM.scala deleted file mode 100644 index b18371482..000000000 --- a/rpc/src/main/scala/client/ClientCallsM.scala +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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 -package client - -import freestyle.free -import io.grpc._ -import io.grpc.stub.StreamObserver - -@free -trait ClientCallsM { - - def async[I, O](call: ClientCall[I, O], param: I, observer: StreamObserver[O]): FS[Unit] - - def asyncStreamServer[I, O]( - call: ClientCall[I, O], - param: I, - responseObserver: StreamObserver[O]): FS[Unit] - - def asyncStreamClient[I, O]( - call: ClientCall[I, O], - responseObserver: StreamObserver[O]): FS[StreamObserver[I]] - - def asyncStreamBidi[I, O]( - call: ClientCall[I, O], - responseObserver: StreamObserver[O]): FS[StreamObserver[I]] - - def sync[I, O](call: ClientCall[I, O], param: I): FS[O] - - def syncC[I, O]( - channel: Channel, - method: MethodDescriptor[I, O], - callOptions: CallOptions, - param: I): FS[O] - - def syncStreamServer[I, O](call: ClientCall[I, O], param: I): FS[Iterator[O]] - - def syncStreamServerC[I, O]( - channel: Channel, - method: MethodDescriptor[I, O], - callOptions: CallOptions, - param: I): FS[Iterator[O]] - - def asyncM[I, O](call: ClientCall[I, O], param: I): FS[O] - -} diff --git a/rpc/src/main/scala/client/handlers/ClientCallsMHandler.scala b/rpc/src/main/scala/client/handlers/ClientCallsMHandler.scala deleted file mode 100644 index 93105783b..000000000 --- a/rpc/src/main/scala/client/handlers/ClientCallsMHandler.scala +++ /dev/null @@ -1,74 +0,0 @@ -/* - * 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 -package client.handlers - -import freestyle.Capture -import freestyle.async.AsyncContext -import freestyle.asyncGuava.implicits._ -import freestyle.rpc.client.ClientCallsM -import io.grpc.{CallOptions, Channel, ClientCall, MethodDescriptor} -import io.grpc.stub.{ClientCalls, StreamObserver} - -import scala.collection.JavaConverters._ -import scala.concurrent.ExecutionContext - -class ClientCallsMHandler[M[_]](implicit C: Capture[M], AC: AsyncContext[M], E: ExecutionContext) - extends ClientCallsM.Handler[M] { - - def async[I, O](call: ClientCall[I, O], param: I, observer: StreamObserver[O]): M[Unit] = - C.capture(ClientCalls.asyncUnaryCall(call, param, observer)) - - def asyncStreamServer[I, O]( - call: ClientCall[I, O], - param: I, - responseObserver: StreamObserver[O]): M[Unit] = - C.capture(ClientCalls.asyncServerStreamingCall(call, param, responseObserver)) - - def asyncStreamClient[I, O]( - call: ClientCall[I, O], - responseObserver: StreamObserver[O]): M[StreamObserver[I]] = - C.capture(ClientCalls.asyncClientStreamingCall(call, responseObserver)) - - def asyncStreamBidi[I, O]( - call: ClientCall[I, O], - responseObserver: StreamObserver[O]): M[StreamObserver[I]] = - C.capture(ClientCalls.asyncBidiStreamingCall(call, responseObserver)) - - def sync[I, O](call: ClientCall[I, O], param: I): M[O] = - C.capture(ClientCalls.blockingUnaryCall(call, param)) - - def syncC[I, O]( - channel: Channel, - method: MethodDescriptor[I, O], - callOptions: CallOptions, - param: I): M[O] = - C.capture(ClientCalls.blockingUnaryCall(channel, method, callOptions, param)) - - def syncStreamServer[I, O](call: ClientCall[I, O], param: I): M[Iterator[O]] = - C.capture(ClientCalls.blockingServerStreamingCall(call, param).asScala) - - def syncStreamServerC[I, O]( - channel: Channel, - method: MethodDescriptor[I, O], - callOptions: CallOptions, - param: I): M[Iterator[O]] = - C.capture(ClientCalls.blockingServerStreamingCall(channel, method, callOptions, param).asScala) - - def asyncM[I, O](call: ClientCall[I, O], param: I): M[O] = - listenableFuture2Async.apply(ClientCalls.futureUnaryCall(call, param)) -} From 023672a992a2a153221d257f75f5f2bbf9861166 Mon Sep 17 00:00:00 2001 From: Juan Pedro Moreno Date: Wed, 18 Oct 2017 17:46:04 +0200 Subject: [PATCH 2/2] Test coverage for all client definitions --- rpc/src/main/scala/client/ChannelConfig.scala | 2 +- .../scala/client/ChannelConfigTests.scala | 47 +++++++++++ .../ManagedChannelInterpreterTests.scala | 77 +++++++++++++++++++ .../scala/client/RpcClientTestSuite.scala | 4 +- 4 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 rpc/src/test/scala/client/ChannelConfigTests.scala create mode 100644 rpc/src/test/scala/client/ManagedChannelInterpreterTests.scala diff --git a/rpc/src/main/scala/client/ChannelConfig.scala b/rpc/src/main/scala/client/ChannelConfig.scala index 650a995db..7a4d80fec 100644 --- a/rpc/src/main/scala/client/ChannelConfig.scala +++ b/rpc/src/main/scala/client/ChannelConfig.scala @@ -27,7 +27,7 @@ import freestyle.rpc.client._ import io.grpc._ sealed trait ManagedChannelFor extends Product with Serializable -case class ManagedChannelForAddress(name: String, port: Int) extends ManagedChannelFor +case class ManagedChannelForAddress(host: String, port: Int) extends ManagedChannelFor case class ManagedChannelForTarget(target: String) extends ManagedChannelFor sealed trait ManagedChannelConfig extends Product with Serializable diff --git a/rpc/src/test/scala/client/ChannelConfigTests.scala b/rpc/src/test/scala/client/ChannelConfigTests.scala new file mode 100644 index 000000000..93d3fe60b --- /dev/null +++ b/rpc/src/test/scala/client/ChannelConfigTests.scala @@ -0,0 +1,47 @@ +/* + * 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 +package client + +import cats.instances.try_._ +import freestyle._ +import freestyle.implicits._ +import freestyle.config.implicits._ + +import scala.util.{Success, Try} + +class ChannelConfigTests extends RpcClientTestSuite { + + import implicits._ + + "ChannelConfig" should { + + "for Address [host, port] work as expected" in { + + ConfigForAddress[ChannelConfig.Op](host, port.toString) + .interpret[Try] shouldBe a[Success[_]] + } + + "for Target work as expected" in { + + ConfigForTarget[ChannelConfig.Op](host) + .interpret[Try] shouldBe a[Success[_]] + } + + } + +} diff --git a/rpc/src/test/scala/client/ManagedChannelInterpreterTests.scala b/rpc/src/test/scala/client/ManagedChannelInterpreterTests.scala new file mode 100644 index 000000000..2c8d4c080 --- /dev/null +++ b/rpc/src/test/scala/client/ManagedChannelInterpreterTests.scala @@ -0,0 +1,77 @@ +/* + * 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 +package client + +import cats.data.Kleisli +import io.grpc.ManagedChannel + +import scala.concurrent.duration.Duration +import scala.concurrent.{Await, Future} + +class ManagedChannelInterpreterTests extends RpcClientTestSuite { + + import implicits._ + + "ManagedChannelInterpreter" should { + + "build a io.grpc.ManagedChannel based on the specified configuration, for an address" in { + + val channelFor: ManagedChannelFor = ManagedChannelForAddress(host, port) + + val channelConfigList: List[ManagedChannelConfig] = List(UsePlaintext(true)) + + val managedChannelInterpreter = + new ManagedChannelInterpreter[Future](channelFor, channelConfigList) + + val mc: ManagedChannel = managedChannelInterpreter.build(channelFor, channelConfigList) + + mc shouldBe an[ManagedChannel] + } + + "build a io.grpc.ManagedChannel based on the specified configuration, for an target" in { + + val channelFor: ManagedChannelFor = ManagedChannelForTarget(host) + + val channelConfigList: List[ManagedChannelConfig] = List(UsePlaintext(true)) + + val managedChannelInterpreter = + new ManagedChannelInterpreter[Future](channelFor, channelConfigList) + + val mc: ManagedChannel = managedChannelInterpreter.build(channelFor, channelConfigList) + + mc shouldBe an[ManagedChannel] + } + + "apply should work as expected" in { + + val channelFor: ManagedChannelFor = ManagedChannelForTarget(host) + + val channelConfigList: List[ManagedChannelConfig] = List(UsePlaintext(true)) + + val managedChannelInterpreter = + new ManagedChannelInterpreter[Future](channelFor, channelConfigList) + + val kleisli: ManagedChannelOps[Future, String] = + Kleisli[Future, ManagedChannel, String]((_: ManagedChannel) => Future.successful(foo)) + + Await.result(managedChannelInterpreter[String](kleisli), Duration.Inf) shouldBe foo + } + + } + +} diff --git a/rpc/src/test/scala/client/RpcClientTestSuite.scala b/rpc/src/test/scala/client/RpcClientTestSuite.scala index 07a92fa85..851e1daeb 100644 --- a/rpc/src/test/scala/client/RpcClientTestSuite.scala +++ b/rpc/src/test/scala/client/RpcClientTestSuite.scala @@ -40,7 +40,9 @@ trait RpcClientTestSuite extends RpcBaseTestSuite { new StringMarshaller() ) - val authority: String = "localhost:8696" + val host: String = "localhost" + val port: Int = 8696 + val authority: String = s"$host:$port" val foo = "Bar" val failureMessage: String = "❗"