Skip to content

Commit c0f81c2

Browse files
committed
grpc-pb: Revert removal of grpc service tests for JVM
Signed-off-by: Johannes Zottele <[email protected]>
1 parent 0436cc3 commit c0f81c2

File tree

4 files changed

+433
-0
lines changed

4 files changed

+433
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.core.test
6+
7+
import kotlinx.coroutines.sync.Mutex
8+
import kotlinx.coroutines.sync.withLock
9+
import kotlinx.coroutines.test.runTest
10+
import kotlinx.rpc.RpcServer
11+
import kotlinx.rpc.grpc.GrpcClient
12+
import kotlinx.rpc.grpc.GrpcServer
13+
14+
abstract class GrpcServerTest {
15+
private val serverMutex = Mutex()
16+
17+
abstract fun RpcServer.registerServices()
18+
19+
protected fun runGrpcTest(test: suspend (GrpcClient) -> Unit, ) = runTest {
20+
serverMutex.withLock {
21+
val grpcClient = GrpcClient("localhost", 8080) {
22+
usePlaintext()
23+
}
24+
25+
val grpcServer = GrpcServer(8080, builder = {
26+
registerServices()
27+
})
28+
29+
grpcServer.start()
30+
test(grpcClient)
31+
grpcServer.shutdown()
32+
grpcServer.awaitTermination()
33+
grpcClient.shutdown()
34+
grpcClient.awaitTermination()
35+
}
36+
}
37+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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.core.test
6+
7+
import StreamingTestService
8+
import kotlinx.coroutines.flow.*
9+
import kotlinx.rpc.RpcServer
10+
import kotlinx.rpc.withService
11+
import kotlin.test.Test
12+
import kotlin.test.assertEquals
13+
14+
class StreamingTestServiceImpl : StreamingTestService {
15+
override fun Server(message: kotlinx.rpc.grpc.test.References): Flow<kotlinx.rpc.grpc.test.References> {
16+
return flow { emit(message); emit(message); emit(message) }
17+
}
18+
19+
override suspend fun Client(message: Flow<kotlinx.rpc.grpc.test.References>): kotlinx.rpc.grpc.test.References {
20+
return message.last()
21+
}
22+
23+
override fun Bidi(message: Flow<kotlinx.rpc.grpc.test.References>): Flow<kotlinx.rpc.grpc.test.References> {
24+
return message
25+
}
26+
}
27+
28+
class StreamingTest : GrpcServerTest() {
29+
override fun RpcServer.registerServices() {
30+
registerService<StreamingTestService> { StreamingTestServiceImpl() }
31+
}
32+
33+
@Test
34+
fun testServerStreaming() = runGrpcTest { grpcClient ->
35+
val service = grpcClient.withService<StreamingTestService>()
36+
service.Server(kotlinx.rpc.grpc.test.References {
37+
other = kotlinx.rpc.grpc.test.Other {
38+
field = 42
39+
}
40+
}).toList().run {
41+
assertEquals(3, size)
42+
43+
forEach {
44+
assertEquals(42, it.other.field)
45+
}
46+
}
47+
}
48+
49+
@Test
50+
fun testClientStreaming() = runGrpcTest { grpcClient ->
51+
val service = grpcClient.withService<StreamingTestService>()
52+
val result = service.Client(flow {
53+
repeat(3) {
54+
emit(kotlinx.rpc.grpc.test.References {
55+
other = kotlinx.rpc.grpc.test.Other {
56+
field = 42 + it
57+
}
58+
})
59+
}
60+
})
61+
62+
assertEquals(44, result.other.field)
63+
}
64+
65+
@Test
66+
fun testBidiStreaming() = runGrpcTest { grpcClient ->
67+
val service = grpcClient.withService<StreamingTestService>()
68+
service.Bidi(flow {
69+
repeat(3) {
70+
emit(kotlinx.rpc.grpc.test.References {
71+
other = kotlinx.rpc.grpc.test.Other {
72+
field = 42 + it
73+
}
74+
})
75+
}
76+
}).collectIndexed { i, it ->
77+
assertEquals(42 + i, it.other.field)
78+
}
79+
}
80+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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.core.test
6+
7+
import kotlinx.rpc.RpcServer
8+
import kotlinx.rpc.grpc.test.AllPrimitives
9+
import kotlinx.rpc.grpc.test.PrimitiveService
10+
import kotlinx.rpc.withService
11+
import kotlin.test.Test
12+
import kotlin.test.assertEquals
13+
14+
class PrimitiveServiceImpl : PrimitiveService {
15+
override suspend fun Echo(message: AllPrimitives): AllPrimitives {
16+
return message
17+
}
18+
}
19+
20+
class TestPrimitiveService : GrpcServerTest() {
21+
override fun RpcServer.registerServices() {
22+
registerService<PrimitiveService> { PrimitiveServiceImpl() }
23+
}
24+
25+
@Test
26+
fun testPrimitive(): Unit = runGrpcTest { grpcClient ->
27+
val service = grpcClient.withService<PrimitiveService>()
28+
val result = service.Echo(AllPrimitives {
29+
int32 = 42
30+
})
31+
32+
assertEquals(42, result.int32)
33+
}
34+
}

0 commit comments

Comments
 (0)