Skip to content

Commit 2e7dd82

Browse files
committed
Add test for Greeter server/client
1 parent 0424f35 commit 2e7dd82

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

buildSrc/src/main/kotlin/Dependencies.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@ object Libs {
357357
)
358358

359359
// https://github.com/grpc/grpc-java
360+
val grpc_api = dep(
361+
group = "io.grpc",
362+
name = "grpc-api",
363+
version = Versions.grpc
364+
)
360365
val grpc_protobuf = dep(
361366
group = "io.grpc",
362367
name = "grpc-protobuf",

jacodb-ets/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ dependencies {
2020

2121
testImplementation(kotlin("test"))
2222
testImplementation(Libs.mockk)
23+
testImplementation(Libs.grpc_api)
2324

2425
testFixturesImplementation(Libs.kotlin_logging)
2526
testFixturesImplementation(Libs.junit_jupiter_api)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2022 UnitTestBot contributors (utbot.org)
3+
* <p>
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+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
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+
17+
package org.jacodb.ets.test
18+
19+
import greeter.GreeterClient
20+
import greeter.HelloRequest
21+
import io.grpc.ServerBuilder
22+
import mu.KotlinLogging
23+
import org.jacodb.ets.service.GreeterService
24+
import org.jacodb.ets.service.grpcClient
25+
import kotlin.test.Test
26+
27+
private val logger = KotlinLogging.logger {}
28+
29+
class WireTest {
30+
companion object {
31+
private const val PORT = 7777
32+
}
33+
34+
@Test
35+
fun `test Greeter`() {
36+
val server = ServerBuilder
37+
.forPort(PORT)
38+
.addService(GreeterService())
39+
.build()
40+
server.start()
41+
logger.info { "Server listening on port ${server.port}" }
42+
43+
val client: GreeterClient = grpcClient(PORT).create()
44+
45+
val request = HelloRequest(name = "Kotlin")
46+
logger.info { "Sending $request" }
47+
val response = client.SayHello().executeBlocking(request)
48+
logger.info { "Received $response" }
49+
50+
logger.info { "Shutting down server..." }
51+
server.shutdown()
52+
server.awaitTermination()
53+
logger.info { "Server shut down" }
54+
}
55+
}

jacodb-ets/wire-server/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ dependencies {
1515
api(Libs.wire_grpc_server)
1616
implementation(Libs.grpc_protobuf)
1717
implementation(Libs.grpc_services) // for ProtoReflectionService
18+
implementation(Libs.kotlin_logging)
1819
runtimeOnly(Libs.grpc_netty_shaded)
1920
runtimeOnly(Libs.wire_runtime)
2021
}

jacodb-ets/wire-server/src/main/kotlin/org/jacodb/ets/service/Greeter.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import io.grpc.ServerBuilder
2424
import io.grpc.protobuf.services.ProtoReflectionService
2525
import io.grpc.stub.StreamObserver
2626

27+
private val logger = mu.KotlinLogging.logger {}
28+
2729
class GreeterImpl : GreeterBlockingServer {
2830
override fun SayHello(request: HelloRequest): HelloReply {
2931
return HelloReply(message = "Hello, ${request.name}!")
@@ -34,8 +36,10 @@ class GreeterService : GreeterWireGrpc.GreeterImplBase() {
3436
private val impl = GreeterImpl()
3537

3638
override fun SayHello(request: HelloRequest, response: StreamObserver<HelloReply>) {
37-
println("Received $request")
38-
response.onNext(impl.SayHello(request))
39+
logger.info { "Received $request" }
40+
val reply = impl.SayHello(request)
41+
logger.info { "Sending $reply" }
42+
response.onNext(reply)
3943
response.onCompleted()
4044
}
4145
}

0 commit comments

Comments
 (0)