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

Adds some GRPC testing utilities #143

Merged
merged 1 commit into from
Jan 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions modules/testing/src/main/scala/methods.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2017-2018 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 testing

import io.grpc.MethodDescriptor
import io.grpc.MethodDescriptor.MethodType
import io.grpc.testing.TestMethodDescriptors

object methods {

def voidMethod(methodName: Option[String] = None): MethodDescriptor[Void, Void] =
MethodDescriptor
.newBuilder[Void, Void]()
.setType(MethodType.UNARY)
.setFullMethodName(
methodName.getOrElse(MethodDescriptor.generateFullMethodName("service_foo", "method_bar")))
.setRequestMarshaller(TestMethodDescriptors.voidMarshaller())
.setResponseMarshaller(TestMethodDescriptors.voidMarshaller())
.build()

}
41 changes: 41 additions & 0 deletions modules/testing/src/main/scala/servers.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2017-2018 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 testing

import io.grpc.internal.NoopServerCall.NoopServerCallListener
import io.grpc.{Metadata, ServerCall, ServerCallHandler, ServerServiceDefinition}

object servers {

def serverCallHandler[Req, Res]: ServerCallHandler[Req, Res] = new ServerCallHandler[Req, Res] {
override def startCall(
call: ServerCall[Req, Res],
headers: Metadata): ServerCall.Listener[Req] = new NoopServerCallListener[Req]
}

def serverServiceDefinition(
serviceName: String,
methodList: List[String]): ServerServiceDefinition = {
val ssdBuilder = ServerServiceDefinition.builder(serviceName)
methodList.foreach { methodName =>
ssdBuilder.addMethod(methods.voidMethod(Some(methodName)), serverCallHandler[Void, Void])
}
ssdBuilder.build()
}

}
43 changes: 43 additions & 0 deletions modules/testing/src/test/scala/MethodsTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2017-2018 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 testing

import org.scalacheck.Gen
import org.scalatest._
import org.scalatest.prop.Checkers
import org.scalacheck.Prop._

class MethodsTests extends WordSpec with Matchers with Checkers {

"methods.voidMethod" should {

"generate the method with the provided name" in {
check {
forAll(Gen.identifier) { methodName =>
methods.voidMethod(Some(methodName)).getFullMethodName == methodName
}
}
}

"generate the method with an auto-generated name" in {
methods.voidMethod().getFullMethodName.isEmpty shouldBe false
}

}

}
60 changes: 60 additions & 0 deletions modules/testing/src/test/scala/ServersTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2017-2018 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 testing

import io.grpc.{ServerCall, ServerCallHandler, ServerServiceDefinition}
import org.scalatest._
import org.scalatest.prop.Checkers
import org.scalacheck.Prop._

import scala.collection.JavaConverters._

class ServersTests extends WordSpec with Matchers with Checkers {

import TestingUtils._

"servers.serverCallHandler" should {

"create a noop call handler" in {
type StringCallListener = ServerCall.Listener[String]
val handler: ServerCallHandler[String, Int] = servers.serverCallHandler[String, Int]
handler.startCall(null, null) shouldBe a[StringCallListener]
}

}

"servers.serverServiceDefinition" should {

"create a server service definition with the provided name and methods" in {

check {
forAllNoShrink(serverDefGen) {
case (serverName, methodNameList) =>
val serverDef: ServerServiceDefinition =
servers.serverServiceDefinition(serverName, methodNameList)
(serverDef.getServiceDescriptor.getName == serverName) && (serverDef.getMethods.asScala
.map(_.getMethodDescriptor.getFullMethodName)
.toList
.sorted == methodNameList.sorted)
}
}
}

}

}
34 changes: 34 additions & 0 deletions modules/testing/src/test/scala/TestingUtils.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2017-2018 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 testing

import org.scalacheck.Gen

trait TestingUtils {

val methodNamesGen: Gen[List[String]] = Gen.listOf(Gen.identifier).map(_.distinct)

val serverDefGen: Gen[(String, List[String])] =
for {
serverName <- Gen.identifier
methodList <- methodNamesGen.map(_.map(name => s"$serverName/$name"))
} yield (serverName, methodList)

}

object TestingUtils extends TestingUtils
3 changes: 2 additions & 1 deletion project/ProjectPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ object ProjectPlugin extends AutoPlugin {

lazy val testingSettings: Seq[Def.Setting[_]] = Seq(
libraryDependencies ++= Seq(
%("grpc-testing", V.grpc)
%("grpc-testing", V.grpc),
%%("scalacheck") % Test
)
)

Expand Down