Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server Definitions - Test Coverage #22

Merged
merged 2 commits into from
Jul 4, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ lazy val rpc = project
Seq(
%%("freestyle-async"),
%%("freestyle-config"),
"io.grpc" % "grpc-all" % "1.4.0"
"io.grpc" % "grpc-all" % "1.4.0",
%%("scalamockScalatest") % "test"
)
): _*
)
Expand All @@ -33,12 +34,13 @@ lazy val `demo-greeting` = project
.settings(noPublishSettings: _*)
.settings(commandAliases: _*)
.settings(demoCommonSettings: _*)
.settings(Seq(
libraryDependencies ++= Seq(
%%("freestyle-async"),
%%("freestyle-config")
)
): _*)
.settings(
Seq(
libraryDependencies ++= Seq(
%%("freestyle-async"),
%%("freestyle-config")
)
): _*)

lazy val googleApi = project
.in(file("third_party"))
Expand Down
6 changes: 5 additions & 1 deletion project/ProjectPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import sbt._
import sbtorgpolicies.OrgPoliciesPlugin.autoImport._
import sbtprotoc.ProtocPlugin.autoImport.PB
import com.trueaccord.scalapb.compiler.{Version => cv}
import scoverage.ScoverageKeys
import scoverage.ScoverageKeys._

object ProjectPlugin extends AutoPlugin {

Expand Down Expand Up @@ -36,6 +38,8 @@ object ProjectPlugin extends AutoPlugin {

}

override def projectSettings: Seq[Def.Setting[_]] = scalaMetaSettings
override def projectSettings: Seq[Def.Setting[_]] = scalaMetaSettings ++ Seq(
coverageExcludedPackages := "<empty>;freestyle\\.rpc\\.demo\\..*"
)

}
11 changes: 4 additions & 7 deletions rpc/src/main/scala/client/ChannelConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,12 @@ trait ChannelConfig {
val defaultPort = 50051

def loadChannelAddress(hostPath: String, portPath: String): FS.Seq[ManagedChannelForAddress] =
for {
config <- configM.load
} yield
configM.load map (config =>
ManagedChannelForAddress(
config.string(hostPath).getOrElse(defaultHost),
config.int(portPath).getOrElse(defaultPort))
config.int(portPath).getOrElse(defaultPort)))

def loadChannelTarget(targetPath: String): FS.Seq[ManagedChannelForTarget] =
for {
config <- configM.load
} yield ManagedChannelForTarget(config.string(targetPath).getOrElse("target"))
configM.load map (config =>
ManagedChannelForTarget(config.string(targetPath).getOrElse("target")))
}
4 changes: 1 addition & 3 deletions rpc/src/main/scala/server/ServerConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,5 @@ trait ServerConfig {
val defaultPort = 50051

def loadConfigPort(portPath: String): FS.Seq[Config] =
for {
config <- configM.load
} yield Config(config.int(portPath).getOrElse(defaultPort))
configM.load map (config => Config(config.int(portPath).getOrElse(defaultPort)))
}
11 changes: 7 additions & 4 deletions rpc/src/main/scala/server/implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@ trait Syntax {
}
}

object implicits extends CaptureInstances with Syntax {
trait Helpers {

def server[M[_]](implicit app: GrpcServer[M]): FreeS[M, Unit] = {
def server[M[_]](implicit APP: GrpcServer[M]): FreeS[M, Unit] = {
for {
_ <- app.start()
_ <- app.awaitTermination()
_ <- APP.start()
_ <- APP.awaitTermination()
} yield ()
}

}

object implicits extends CaptureInstances with Syntax with Helpers
98 changes: 98 additions & 0 deletions rpc/src/test/scala/server/GrpcServerTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2017 47 Degrees, LLC. <http://www.47deg.com>
*
* 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 server

import java.util.concurrent.TimeUnit

import cats.Id
import cats.implicits._
import freestyle._
import io.grpc.{Server, ServerServiceDefinition}

class GrpcServerTests extends RpcTestSuite {

import implicits._

"GrpcServer.Op" should {

type Result = (
Boolean,
Server,
Int,
List[ServerServiceDefinition],
List[ServerServiceDefinition],
List[ServerServiceDefinition],
Server,
Server,
Boolean,
Boolean,
Boolean,
Unit)

"behaves as expected" in {

def program[F[_]](implicit APP: GrpcServer[F]): FreeS[F, Result] = {

import APP._

val a = isShutdown
val b = start()
val c = getPort
val d = getServices
val e = getImmutableServices
val f = getMutableServices
val g = shutdown()
val h = shutdownNow()
val i = isShutdown
val j = isTerminated
val k = awaitTerminationTimeout(timeout, timeoutUnit)
val l = awaitTermination()

(a |@| b |@| c |@| d |@| e |@| f |@| g |@| h |@| i |@| j |@| k |@| l).tupled
}

program[GrpcServer.Op].interpret[Id] shouldBe ((
b,
serverCopyMock,
port,
serviceList,
immutableServiceList,
mutableServiceList,
serverCopyMock,
serverCopyMock,
b,
b,
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.awaitTermination(_: Long, _: TimeUnit)).verify(timeout, timeoutUnit).once()
(serverMock.awaitTermination _).verify().once()
}

}

}
39 changes: 39 additions & 0 deletions rpc/src/test/scala/server/HelperTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2017 47 Degrees, LLC. <http://www.47deg.com>
*
* 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 server

import cats.Id
import freestyle.rpc.server.implicits._

class HelperTests extends RpcTestSuite {

import implicits._

"server helper" should {

"work as expected" in {

server[GrpcServer.Op].interpret[Id] shouldBe ((): Unit)

(serverMock.start _).verify()
(serverMock.awaitTermination _).verify()
}

}

}
87 changes: 87 additions & 0 deletions rpc/src/test/scala/server/RpcTestSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2017 47 Degrees, LLC. <http://www.47deg.com>
*
* 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 server

import java.util.concurrent.TimeUnit

import cats.{~>, Id}
import cats.data.Kleisli
import io.grpc.{Server, ServerServiceDefinition}
import org.scalamock.scalatest.MockFactory
import org.scalatest.{Matchers, OneInstancePerTest, WordSpec}

import scala.collection.JavaConverters._
import scala.concurrent.duration.{Duration, TimeUnit}
import scala.concurrent.{Await, Future}

trait RpcTestSuite extends WordSpec with Matchers with OneInstancePerTest with MockFactory {

def runK[F[_], A, B](kleisli: Kleisli[F, A, B], v: A): F[B] =
kleisli.run(v)

def runKFuture[A, B](kleisli: Kleisli[Future, A, B], v: A): B =
Await.result(kleisli.run(v), Duration.Inf)

trait DummyData {

val serverMock: Server = stub[Server]
val serverCopyMock: Server = stub[Server]
val port: Int = 42
val timeout: Long = 1l
val timeoutUnit: TimeUnit = TimeUnit.MINUTES
val b: Boolean = true
val unit: Unit = ()
val sd1: ServerServiceDefinition = ServerServiceDefinition.builder("s1").build()
val sd2: ServerServiceDefinition = ServerServiceDefinition.builder("s2").build()
val serviceList: List[ServerServiceDefinition] = List(sd1, sd2)
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.awaitTermination(_: Long, _: TimeUnit)).when(timeout, timeoutUnit).returns(b)
(serverMock.awaitTermination _).when().returns(unit)
}

object implicits extends DummyData {

implicit val grpcServerHandler: GrpcServer.Op ~> Id = new (GrpcServer.Op ~> Id) {
import GrpcServer._
override def apply[A](fa: GrpcServer.Op[A]): Id[A] = fa match {
case StartOP() => serverMock.start()
case GetPortOP() => serverMock.getPort
case GetServicesOP() => serverMock.getServices.asScala.toList
case GetImmutableServicesOP() => serverMock.getImmutableServices.asScala.toList
case GetMutableServicesOP() => serverMock.getMutableServices.asScala.toList
case ShutdownOP() => serverMock.shutdown()
case ShutdownNowOP() => serverMock.shutdownNow()
case IsShutdownOP() => serverMock.isShutdown
case IsTerminatedOP() => serverMock.isTerminated
case AwaitTerminationTimeoutOP(timeout, unit) => serverMock.awaitTermination(timeout, unit)
case AwaitTerminationOP() => serverMock.awaitTermination()
}
}
}
}
Loading