Skip to content

Commit 4683f9f

Browse files
committed
grpc: Add test to check if the service and client actually use the package option for the service name.
Signed-off-by: Johannes Zottele <[email protected]>
1 parent 8f3d46e commit 4683f9f

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed

grpc/grpc-core/src/commonTest/kotlin/kotlinx/rpc/grpc/test/proto/GrpcProtoTest.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ abstract class GrpcProtoTest {
1616

1717
abstract fun RpcServer.registerServices()
1818

19-
protected fun runGrpcTest(test: suspend (GrpcClient) -> Unit, ) = runTest {
19+
protected fun runGrpcTest(test: suspend (GrpcClient) -> Unit) = runTest {
2020
serverMutex.withLock {
21-
val grpcClient = GrpcClient("localhost", 8080) {
21+
val grpcClient = GrpcClient("localhost", PORT) {
2222
usePlaintext()
2323
}
2424

25-
val grpcServer = GrpcServer(8080, builder = {
25+
val grpcServer = GrpcServer(PORT, builder = {
2626
registerServices()
2727
})
2828

@@ -34,4 +34,8 @@ abstract class GrpcProtoTest {
3434
grpcClient.awaitTermination()
3535
}
3636
}
37+
38+
companion object {
39+
const val PORT = 8080
40+
}
3741
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.rpc.grpc.test.proto
6+
7+
import com.google.protobuf.kotlin.Empty
8+
import com.google.protobuf.kotlin.EmptyInternal
9+
import com.google.protobuf.kotlin.invoke
10+
import kotlinx.rpc.RpcServer
11+
import kotlinx.rpc.grpc.ManagedChannelBuilder
12+
import kotlinx.rpc.grpc.buildChannel
13+
import kotlinx.rpc.grpc.internal.MethodType
14+
import kotlinx.rpc.grpc.internal.methodDescriptor
15+
import kotlinx.rpc.grpc.internal.unaryRpc
16+
import kotlinx.rpc.grpc.test.withJavaPkg.TheService
17+
import kotlinx.rpc.registerService
18+
import kotlinx.rpc.withService
19+
import kotlin.test.Test
20+
21+
class TheServiceImpl : TheService {
22+
override suspend fun TheMethod(message: Empty): Empty {
23+
return Empty {}
24+
}
25+
}
26+
27+
/**
28+
* Tests proto service with java_package that differs from the `package` name.
29+
* While the generated Kotlin sources should be generated in the java_package,
30+
* the service name must use the `package` name.
31+
*/
32+
class JavaPackageOptionTest : GrpcProtoTest() {
33+
34+
/**
35+
* Tests that the generated service descriptor uses the `package` name.
36+
*/
37+
@Test
38+
fun testJavaPackageOptionRaw() = runGrpcTest { _ ->
39+
val channel = ManagedChannelBuilder("localhost", PORT)
40+
.usePlaintext()
41+
.buildChannel()
42+
43+
val descriptor = methodDescriptor(
44+
fullMethodName = "protopackage.TheService/TheMethod",
45+
requestCodec = EmptyInternal.CODEC,
46+
responseCodec = EmptyInternal.CODEC,
47+
type = MethodType.UNARY,
48+
schemaDescriptor = Unit,
49+
idempotent = true,
50+
safe = true,
51+
sampledToLocalTracing = true,
52+
)
53+
54+
unaryRpc(channel.platformApi, descriptor, Empty {})
55+
56+
// just reach this without an error
57+
}
58+
59+
/**
60+
* Tests that the generated client uses the `package` name to call the service.
61+
*/
62+
@Test
63+
fun testJavaPackageOptionStub() = runGrpcTest { client ->
64+
val service = client.withService<TheService>()
65+
service.TheMethod(Empty {})
66+
67+
// just reach this without an error
68+
}
69+
70+
override fun RpcServer.registerServices() {
71+
registerService<TheService> { TheServiceImpl() }
72+
}
73+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
syntax = "proto3";
2+
3+
import "google/protobuf/empty.proto";
4+
5+
// the package the service interface should be written to
6+
option java_package = "kotlinx.rpc.grpc.test.withJavaPkg";
7+
8+
// the proto package used for the service's full name
9+
package protopackage;
10+
11+
service TheService {
12+
rpc TheMethod(google.protobuf.Empty) returns (google.protobuf.Empty) {};
13+
}

0 commit comments

Comments
 (0)