Skip to content

Commit b83754c

Browse files
committed
grpc: Fix the service name to be full-qualified
Signed-off-by: Johannes Zottele <[email protected]>
1 parent 2b7a9fa commit b83754c

File tree

5 files changed

+77
-41
lines changed

5 files changed

+77
-41
lines changed

compiler-plugin/compiler-plugin-backend/src/main/kotlin/kotlinx/rpc/codegen/extension/RpcStubGenerator.kt

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ import org.jetbrains.kotlin.ir.builders.declarations.*
1818
import org.jetbrains.kotlin.ir.declarations.*
1919
import org.jetbrains.kotlin.ir.expressions.*
2020
import org.jetbrains.kotlin.ir.expressions.impl.*
21-
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
22-
import org.jetbrains.kotlin.ir.symbols.IrPropertySymbol
23-
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
24-
import org.jetbrains.kotlin.ir.symbols.IrSymbol
25-
import org.jetbrains.kotlin.ir.symbols.IrValueSymbol
21+
import org.jetbrains.kotlin.ir.symbols.*
2622
import org.jetbrains.kotlin.ir.types.*
2723
import org.jetbrains.kotlin.ir.util.*
2824
import org.jetbrains.kotlin.name.Name
@@ -915,7 +911,8 @@ internal class RpcStubGenerator(
915911
}.apply {
916912
overriddenSymbols = listOf(ctx.properties.rpcServiceDescriptorCallables)
917913

918-
val collectionType = ctx.irBuiltIns.collectionClass.typeWith(ctx.rpcCallable.typeWith(declaration.serviceType))
914+
val collectionType =
915+
ctx.irBuiltIns.collectionClass.typeWith(ctx.rpcCallable.typeWith(declaration.serviceType))
919916

920917
addGetter {
921918
returnType = collectionType
@@ -1105,7 +1102,7 @@ internal class RpcStubGenerator(
11051102
}.apply {
11061103
arguments {
11071104
values {
1108-
+stringConst(declaration.simpleName)
1105+
+stringConst(declaration.fqName)
11091106

11101107
+irCallProperty(
11111108
receiver = IrGetValueImpl(
@@ -1130,7 +1127,7 @@ internal class RpcStubGenerator(
11301127
* // In scope: resolver: MessageCodecResolver
11311128
*
11321129
* methodDescriptor<<request-type>, <response-type>>(
1133-
* fullMethodName = "${descriptor.simpleName}/${callable.name}",
1130+
* fullMethodName = "${descriptor.fqName}/${callable.name}",
11341131
* requestCodec = <request-codec>,
11351132
* responseCodec = <response-codec>,
11361133
* type = MethodType.<method-type>,
@@ -1185,7 +1182,7 @@ internal class RpcStubGenerator(
11851182

11861183
values {
11871184
// fullMethodName
1188-
+stringConst("${declaration.simpleName}/${callable.name}")
1185+
+stringConst("${declaration.fqName}/${callable.name}")
11891186

11901187
// requestCodec
11911188
+irCodec(requestType, resolver)
@@ -1245,7 +1242,7 @@ internal class RpcStubGenerator(
12451242
val protobufMessage = owner.getAnnotation(ctx.withCodecAnnotation.owner.kotlinFqName)
12461243

12471244
return if (protobufMessage != null) {
1248-
val classReference = vsApi{ protobufMessage.argumentsVS }.single() as? IrClassReference
1245+
val classReference = vsApi { protobufMessage.argumentsVS }.single() as? IrClassReference
12491246
?: error("Expected IrClassReference for ${ctx.withCodecAnnotation.owner.kotlinFqName} parameter")
12501247

12511248
val codec = classReference.classType

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

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,26 @@
55
package kotlinx.rpc.grpc.test
66

77
import kotlinx.coroutines.delay
8+
import kotlinx.coroutines.flow.Flow
89
import kotlinx.coroutines.flow.flow
10+
import kotlinx.coroutines.flow.toList
911
import kotlinx.coroutines.test.runTest
12+
import kotlinx.rpc.grpc.GrpcServer
1013
import kotlinx.rpc.grpc.ManagedChannelBuilder
1114
import kotlinx.rpc.grpc.buildChannel
1215
import kotlinx.rpc.grpc.internal.*
16+
import kotlinx.rpc.registerService
1317
import kotlin.test.Test
1418
import kotlin.test.assertEquals
1519

20+
private const val PORT = 50051
21+
1622
/**
1723
* Tests for JVM and Native clients.
24+
*
25+
* To run the tests you must first start the server with the [EchoServiceImpl.runServer] method on JVM.
1826
*/
19-
// TODO: Start echo service server automatically
27+
// TODO: Start external service server automatically (KRPC-208)
2028
class RawClientTest {
2129

2230
@Test
@@ -86,7 +94,7 @@ class RawClientTest {
8694
.buildChannel()
8795

8896
val methodDescriptor = methodDescriptor(
89-
fullMethodName = "grpc.examples.echo.Echo/$methodName",
97+
fullMethodName = "kotlinx.rpc.grpc.test.EchoService/$methodName",
9098
requestCodec = EchoRequestInternal.CODEC,
9199
responseCodec = EchoResponseInternal.CODEC,
92100
type = type,
@@ -103,4 +111,56 @@ class RawClientTest {
103111
channel.awaitTermination()
104112
}
105113
}
106-
}
114+
}
115+
116+
117+
class EchoServiceImpl : EchoService {
118+
119+
override suspend fun UnaryEcho(message: EchoRequest): EchoResponse {
120+
delay(message.timeout.toLong())
121+
return EchoResponse { this.message = message.message }
122+
}
123+
124+
override fun ServerStreamingEcho(message: EchoRequest): Flow<EchoResponse> {
125+
val count = message.serverStreamReps ?: 5u
126+
return flow {
127+
repeat(count.toInt()) {
128+
emit(EchoResponse { this.message = message.message })
129+
}
130+
}
131+
}
132+
133+
override suspend fun ClientStreamingEcho(message: Flow<EchoRequest>): EchoResponse {
134+
val result = message.toList().joinToString(", ") { it.message }
135+
return EchoResponse { this.message = result }
136+
}
137+
138+
override fun BidirectionalStreamingEcho(message: Flow<EchoRequest>): Flow<EchoResponse> {
139+
return flow {
140+
message.collect {
141+
emit(EchoResponse { this.message = it.message })
142+
}
143+
}
144+
}
145+
146+
147+
/**
148+
* Run this on JVM before executing tests.
149+
*/
150+
@Test
151+
fun runServer() = runTest {
152+
val server = GrpcServer(
153+
port = PORT,
154+
builder = { registerService<EchoService> { EchoServiceImpl() } }
155+
)
156+
157+
try {
158+
server.start()
159+
println("Server started")
160+
server.awaitTermination()
161+
} finally {
162+
server.shutdown()
163+
server.awaitTermination()
164+
}
165+
}
166+
}

grpc/grpc-core/src/commonTest/proto/echo_grpc.proto

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
syntax = "proto3";
22

3-
package grpc.examples.echo;
3+
package kotlinx.rpc.grpc.test;
44

55
// EchoRequest is the request for echo.
66
message EchoRequest {
77
string message = 1;
8+
uint32 timeout = 2;
9+
optional uint32 serverStreamReps = 3;
810
}
911

1012
// EchoResponse is the response for echo.
@@ -13,7 +15,7 @@ message EchoResponse {
1315
}
1416

1517
// Echo is the echo service.
16-
service Echo {
18+
service EchoService {
1719
// UnaryEcho is unary echo.
1820
rpc UnaryEcho(EchoRequest) returns (EchoResponse) {}
1921
// ServerStreamingEcho is server side streaming.

grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/internal/NativeClientCall.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ internal class NativeClientCall<Request, Response>(
224224
is BatchResult.Submitted -> {
225225
callResult.future.onComplete {
226226
val details = statusDetails.toByteArray().toKString()
227+
println("details: $details")
227228
val status = Status(statusCode.value.toKotlin(), details, null)
228229
val trailers = GrpcTrailers()
229230

grpc/grpc-core/src/nativeTest/kotlin/kotlinx/rpc/grpc/internal/CoreTest.kt

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ import HelloReply
99
import HelloReplyInternal
1010
import HelloRequest
1111
import HelloRequestInternal
12-
import grpc.examples.echo.EchoRequest
13-
import grpc.examples.echo.EchoRequestInternal
14-
import grpc.examples.echo.EchoResponseInternal
15-
import grpc.examples.echo.invoke
1612
import invoke
1713
import kotlinx.cinterop.ExperimentalForeignApi
1814
import kotlinx.coroutines.CompletableDeferred
@@ -26,6 +22,8 @@ import kotlin.test.assertEquals
2622
import kotlin.test.assertFailsWith
2723
import kotlin.test.assertTrue
2824

25+
26+
// TODO: Start external service server automatically (KRPC-208)
2927
class GrpcCoreTest {
3028

3129
private fun descriptorFor(fullName: String = "helloworld.Greeter/SayHello"): MethodDescriptor<HelloRequest, HelloReply> =
@@ -293,26 +291,4 @@ class GrpcCoreTest {
293291
assertEquals("Hello world", res.message)
294292
}
295293
}
296-
297-
298-
private fun echoDescriptor(methodName: String, type: MethodType) =
299-
methodDescriptor(
300-
fullMethodName = "grpc.examples.echo.Echo/$methodName",
301-
requestCodec = EchoRequestInternal.CODEC,
302-
responseCodec = EchoResponseInternal.CODEC,
303-
type = type,
304-
schemaDescriptor = Unit,
305-
idempotent = true,
306-
safe = true,
307-
sampledToLocalTracing = true,
308-
)
309-
310-
@Test
311-
fun unaryEchoTest() = runBlocking {
312-
val ch = createChannel()
313-
val desc = echoDescriptor("UnaryEcho", MethodType.UNARY)
314-
val req = EchoRequest { message = "Echoooo" }
315-
unaryRpc(ch.platformApi, desc, req)
316-
return@runBlocking
317-
}
318294
}

0 commit comments

Comments
 (0)